CPU高效的方法在Pygame中以某种颜色闪烁屏幕?

2024-04-27 18:29:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一款使用Python和Pygame的游戏,当玩家被击中时,我想让屏幕闪烁红色。我希望红色的颜色能再次淡出。我用了下面这句话。在

当程序初始化时:

screen = pygame.display.set_mode((WIDTH, HEIGHT))
red = pygame.Surface((WIDTH, HEIGHT))
red.fill((255,0,0))
bleeding = False
going_back = False

在主循环的某个地方:

^{pr2}$

当球员被击中时,我会说:

bleeding = True

然而,当我在第二次下降时,我得到了。我知道必须有一个更有效的方法来创造这种效果,但是我不知道如何去做。提前谢谢你的建议!在


Tags: 程序false游戏屏幕颜色display玩家red
1条回答
网友
1楼 · 发布于 2024-04-27 18:29:42

假设您的屏幕名为screen,只需这样做

bleedout_timer = 0


# In your loop
if bleeding:
    screen.fill((255, 0, 0)) # R, G, B
    bleedout_timer += 1

else: # Not bleeding
    screen.fill((255, 255, 255)) # White


# Reset the timer after the player has bleeded out.
if bleedout_timer > 50: # Experiment with this time length
        bleeding = False # Stop red screen
        bleedout_timer = 0 # Reset

您可以删除不影响bleeding的所有其他内容,此后它将是唯一需要的变量。在

这应该是CPU效率最高的方法,因为它使用的东西最少。在

相关问题 更多 >