Why is my PyGame application not running at all?

Posted on

Problem :

I have a simple Pygame program:

#!/usr/bin/env python

import pygame
from pygame.locals import *

pygame.init()

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

But every time I try to run it, I get this:

pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html

And then nothing happens.
Why I can’t run this program?

Solution :

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

import pygame
from pygame.locals import *

pygame.init()

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
    pygame.display.flip()

    # limit frames per second
    clock.tick(60) 

pygame.quit()

The typical PyGame application loop has to:

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

Leave a Reply

Your email address will not be published. Required fields are marked *