How to add pause mode to Python program

Your code is quite large, but to pause the game is very general task:

  1. Add a pause state.
  2. Toggle the pause state on an certain event, e.g. when the p key is pressed.
  3. Skip the game processing if pauseis stated.

e,.g.

pause = False # pause state

while True:
    ticks = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYUP:
            if event.key == pygame.K_1 and not game_over:
                print("Next level")

            # toggle pause if "p" is pressed
            if event.key == pygame.K_p:
                pause = not pause

    # [...]

    if pause:

        # [...] draw pause screen
        pass

    elif not game_over: # <--- elif is important

        playing = True

    # [...]

    screen.blit(backbuffer, (0,0))
    pygame.display.update()
    fpsclock.tick(30)

Leave a Comment