Python & Pygame: Can't Get Out Of Paused State
I'm new to Python and Pygame and I programmed a little game and tried to implement a pause menu. While I can get to the pause menu I can't get out of it. I followed a tutorial by s
Solution 1:
You need to replace type with key here: if event.type == pygame.K_ESCAPE:.
You also don't need the global paused variable, just stay in the pause function until the user presses escape and then simply return from the function.
import sys
import pygame
pygame.init()
display = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
def pause():
whileTrue:
foreventin pygame.event.get():
ifevent.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
ifevent.key == pygame.K_ESCAPE:
return
pygame.draw.rect(display, (250, 0, 0), display.get_rect(), 3)
pygame.display.update()
clock.tick(60)
def game_loop():
x = 0
gameExit = Falsewhilenot gameExit:
foreventin pygame.event.get():
ifevent.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
ifevent.key == pygame.K_ESCAPE:
pause()
x += 1
display.fill((30, 30, 30))
pygame.draw.rect(display, (0, 200, 250), (x, 200, 20, 20))
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
Post a Comment for "Python & Pygame: Can't Get Out Of Paused State"