Skip to content Skip to sidebar Skip to footer

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():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.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 = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.KEYDOWN:
                if event.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"