【问题标题】:Pygame not restoring from pausePygame没有从暂停中恢复
【发布时间】:2017-04-15 11:04:15
【问题描述】:

我试图通过按“p”键来暂停我的游戏,但在它暂停后再次按下 p 时它并没有取消暂停。这是我的代码的相关部分,我想知道如何解决这个问题和/或是否有更好的替代方法来实现游戏暂停。

pause = False

while True:
    if not pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        #game code
        keys = pygame.key.get_pressed()
        #if key up     
        elif keys[pygame.K_p]:
            pause = True #appears to work correctly, screen freezes and
                         #prints "PAUSED" every tick.    
        #more game code
        pygame.display.update()
        fpsClock.tick(FPS)   
    else:
        print("PAUSED")
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()


            elif event.type == pygame.KEYDOWN:
                if event.type == pygame.K_p:
                    pause = False
        pygame.display.update()
        fpsClock.tick(FPS)  

【问题讨论】:

  • event.type 在检查 pygame.K_p 时应该是 event.key

标签: python pygame


【解决方案1】:

下面的代码可以解决问题。

我在if not pause: ... else: 部分添加了一个按键处理程序,因为没有它就没有机会退出暂停(如果没有这个按键检查,if not pause 部分永远不会运行暂停)。

import pygame
pygame.init() # start PyGame (necessary because 'import pygame' doesn't start PyGame)
colorWhite = (255, 255, 255) # RGB color for later use in PyGame commands (valueRed=255, valueGreen=255, valueBlue=255)

colorWhite = (255, 255, 255) # RGB color for later use in PyGame commands (valueRed=255, valueGreen=255, valueBlue=255)
winDisplay = pygame.display.set_mode((1024, 768)) # set PyGame window size to 1024x768 pixel
pygame.display.set_caption("Minimal PyGame Test Script")
winDisplay.fill(colorWhite)

fpsClock = pygame.time.Clock()
FPS = 15
pause   = False
pauseCounter = 0

while True:
    if not pause:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN and event.key == pygame.K_p: 
                pause = not pause 

            #game code

            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        #more game code

        pygame.display.update()
        fpsClock.tick(FPS)   
    else:
        pauseCounter += 1
        print(pauseCounter, "PAUSED")
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_p: 
                pause = not pause 
        #more game code
        pygame.display.update()
        fpsClock.tick(FPS)  

【讨论】:

  • 你不应该在事件循环中使用keys = pygame.key.get_pressed()。主要是因为您为每个事件调用该函数,这是多余的,因为键状态不会改变(当您调用事件函数时它们会更新一次)。此外,在这种情况下,您正在检查按键(而不是如果它被按住),因此使用事件函数将是这里更合乎逻辑的选择if event.type == pygame.KEYDOWN and event.key == pygame.K_p: pause = not pause
  • @TedKleinBergman 感谢您的评论。我已经根据您的建议更新了提供的代码。它存在的原因是因为我尽量不更改大部分代码 - 只有暂停部分,但你是对的 - 如果提供代码,它应该是最好的知识......
猜你喜欢
  • 2020-08-24
  • 2021-02-05
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多