最近尝试pygame,遇到了alpha不透明度。如何调整 alpha 的范围,以便我的图像/表面每秒闪烁两次?
目前我以每秒 30 帧的速度绘制它。
def blit_image(self):
for alpha in range(0,255,50):
for i in self.image_array:
a = i.set_alpha(alpha)
i.draw_on(self.screen)
pygame.display.flip()
这里有些不对劲,它只是使我的图像边界不透明,我无法看到眨眼是否真的发生了。关于如何做到这一点的任何想法?
简而言之:(您的代码)您每秒有 30 帧 (30FPS),因此每一帧您都会降低 alpha +1/15 * 255
,15 帧后会增加 alpha -1/15 * 255
,15 帧后您再次减少等。
如何闪烁背景颜色的完整示例(这是关于SO的类似问题的答案)。这不是你所做的,但也许它可以帮助你。
import pygame
#----------------------------------------------------------------------
class Background():
def __init__(self, screen):
self.screen = screen
self.timer = 0
self.color = 0
self.up = True # up or down
#-------------------
def change(self):
if self.timer == 15: # 15 frames for UP and 15 frames for DOWN
self.timer = 0
self.up = not self.up
self.timer += 1
if self.up:
self.color += 10
else:
self.color -= 10
print self.up, self.color
#-------------------
def draw(self):
self.screen.fill( (self.color, self.color, self.color) )
#----------------------------------------------------------------------
class Game():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800,600))
self.background = Background(self.screen)
#-------------------
def run(self):
clock = pygame.time.Clock()
RUNNING = True
while RUNNING:
# ----- events -----
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
RUNNING = False
# ----- changes -----
self.background.change()
# ----- draws ------
self.background.draw()
pygame.display.update()
# ----- FPS -----
clock.tick(30)
#-------------------
pygame.quit()
#----------------------------------------------------------------------
Game().run()