简体   繁体   中英

Pygame not restoring from pause

I'm trying to pause my game by pressing the 'p' key, but after it pauses it does not unpause when p is pressed again. Here are the relevant segments of my code, I'd like to know how to fix this and/or if there are better alternative methods of implementing a game pause.

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)  

The code below does the trick.

I have added in if not pause: ... else: section also a handler for a keypress because without it there will be no chance to come out of the pause (the part if not pause won't never ever be run without this keypress check if once paused).

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)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM