我想在 2 秒内将一个矩形从 alpha = 0 淡化到 alpha = 255。使用以下代码,我可以让矩形淡入,但我不知道让持续时间准确(尽可能准确)2 秒。
pygame.init()
#frames per second setting
FPS = 30
fpsClock = pygame.time.Clock()
#Set up window
screen = pygame.display.set_mode((1280,800))
shape = screen.convert_alpha()
#Colors
alpha = 0
WHITE = (255, 255, 255,alpha)
BLACK = (0,0,0)
#Stimuli
stimulus = pygame.Rect(100,250,100,100)
def fade():
"""Fades in stimulus"""
global alpha
alpha = alpha + 5 <--- (Do I change this increment?)
#Draw on surface object
screen.fill(BLACK)
shape.fill(BLACK)
pygame.draw.rect(shape,(255, 255, 255,alpha),stimulus)
screen.blit(shape,(0,0))
pygame.display.update(stimulus)
fpsClock.tick(FPS)
while True:
fade()
此外,如果我使用全屏,我应该使用标志 HWSURFACE 和 DOUBLEBUF 吗?谢谢。