【发布时间】:2019-05-23 21:06:59
【问题描述】:
我正在尝试在 Pygame 中构建一个游戏,如果玩家移动到红色方块,玩家就会失败。发生这种情况时,我想显示一张爆炸图片,玩家在用户按下键盘上的任何键之前都迷路了。当用户按下一个键时,我会调用函数 new_game() 来开始一个新游戏。问题是我的代码似乎跳过了我点燃爆炸的那一行,而是立即开始一个新游戏。
我尝试过使用类似的东西,但我不确定在 while 循环中放入什么(我希望它等到有按键):
while event != KEYDOWN:
# Not sure what to put here
如果我将 time.sleep() 放在 while 循环中,整个程序似乎冻结并且没有图像被 blitted。
这是我将图像加载到 Pygame 中:
explosionpic = pygame.image.load('C:/Users/rohan/Desktop/explosion.png')
这就是我称之为/确定玩家是否丢失的地方(程序似乎跳过了 screen.blit 行,因为我什至根本看不到图像):
if get_color(p1.x + p1_velocity_x, p1.y + p1_velocity_y) == red: # If player lands on a red box
screen.blit(explosionpic, (p1.x, p1.y))
# Bunch of other code goes here, like changing the score, etc.
new_game()
它应该显示图像,然后当用户按键时,调用 new_game() 函数。
我将不胜感激。
【问题讨论】:
-
在“一堆其他代码”中,您应该调用
pygame.display.update(),并通过某种方式延迟调用new_game(),直到按下该键。你有吗?如果是这样,请将其添加到问题中
标签: python oop pygame game-development