每次循环清除表面
def draw(self):
# clear screen."
self.screen.fill( self.color_bg )
# first draw background
# Then buttons
# then any extra top level text
# update
pygame.display.flip()
提示:对于颜色,您可以调用 pygame.Color() 与人类名称类似red
(gray20
并且gray80
有一个很好的对比,用于背景和文本。)
from pygame import Color
text = Color('gray20')
你的按钮,伪代码。修复:将颜色作为实例成员移动。
class Button(object):
def __init__(self, text, rect=None):
self.color_bg = Color("gray20")
self.color_text = color("gray80")
if rect is None: rect = Rect(0,0,1,1)
self.rect = rect
self._render()
def _render(self):
# draw button and .render() font, cache to surface for later.
self.surface_cached = self.surface.copy()
# render text
#if size changes, save rect.size of cached surface , mantaining location
self.rect.size = cache.get_rect.size
def draw(self):
# draw cached surface
screen.blit( self.surface_cached, self.rect)
testClick
使用http://www.pygame.org/docs/ref/rect.html#Rect.collidepoint _Rect.collidepoint