【问题标题】:Pygame: how to display an image until a key is pressedPygame:如何在按下键之前显示图像
【发布时间】: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


【解决方案1】:

我想到的最简单的解决方案是编写一个小的独立函数来延迟代码的执行。比如:

def wait_for_key_press():
    wait = True
    while wait:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                wait = False
                break

此函数将暂停执行,直到事件系统捕获到KEYDOWN 信号。

所以你的代码是:

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))
    pygame.display.update() #needed to show the effect of the blit
    # Bunch of other code goes here, like changing the score, etc.
    wait_for_key_press()
    new_game()

【讨论】:

    【解决方案2】:

    为游戏添加一个状态,指示游戏是否正在运行、是否发生爆炸或是否必须开始新游戏。定义状态RUNEXPLODENEWGAME。初始化状态game_state

    RUN = 1
    EXPLODE = 2
    NEWGAME = 3
    
    game_state = RUN
    

    如果发生爆炸,设置状态EXPLODE

    if get_color(p1.x + p1_velocity_x, p1.y + p1_velocity_y) == red:  # If player lands on a red box
        game_state = EXPLODE
    

    当一个键被按下然后切换到状态NEWGAME

    if game_state == EXPLODE and event.type == pygame.KEYDOWN:
        game_state = NEWGAME
    

    newgame()被执行时,再设置game_state = RUN

    newgame()
    game_state = RUN
    

    在主循环中为游戏的每个状态实现一个单独的案例。使用此解决方案,不需要任何“睡眠”:

    例如

    ENDGAME = 0
    RUN = 1
    EXPLODE = 2
    NEWGAME = 3
    
    game_state = RUN
    while game_state != ENDGAME:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_state = ENDGAME
    
            if game_state == EXPLODE and event.type == pygame.KEYDOWN:
                game_state = NEWGAME
    
    
        if game_state == RUN:
            # [...]
    
            if get_color(p1.x + p1_velocity_x, p1.y + p1_velocity_y) == red:  # If player lands on a red box
                game_state = EXPLODE
    
            # [...]
    
        elif game_state == EXPLODE:
            screen.blit(explosionpic, (p1.x, p1.y))
    
        elif game_state == NEWGAME:
            newgame()
            game_state = RUN
    
        pygame.display.flip()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2014-05-12
      • 2021-08-25
      • 1970-01-01
      • 2016-05-17
      • 2023-03-16
      • 2013-08-29
      相关资源
      最近更新 更多