Why is my PyGame application not running at all?

Your application works well. However, you haven’t implemented an application loop:

import pygame
from pygame.locals import *


win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()

run = True
while run:

    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # update game objects
    # [...]

    # clear display
    win.fill((0, 0, 0))

    # draw game objects
    # [...]

    # update display

    # limit frames per second


The typical PyGame application loop has to:

  • handle the events by calling either pygame.event.pump() or pygame.event.get().
  • update the game states and positions of objects dependent on the input events and time (respectively frames)
  • clear the entire display or draw the background
  • draw the entire scene (blit all the objects)
  • update the display by calling either pygame.display.update() or pygame.display.flip()
  • limit frames per second to limit CPU usage with pygame.time.Clock.tick

repl.it/@Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop

Leave a Comment