Why Is My Pygame Application Loop Not Working Properly

Pygame window not responding after a few seconds

Call pygame.event.get() at the beginning of the while loop.

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 *

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:

  • 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

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

Why doesn't pygame continue through the loop?

Add a gameover to your application:

gameover = False:

Do different things in the application loop, dependent on the state of gameover:

while not done:
# [...]

if not gameover:
# draw game scene
# [...]

else:
# draw gamover scene (button)
# [...]

Set the gameover state if the player collides:

gameover = player.colliderect(wall1)

Reset the position of the player if the continue button is pressed:

if event.type == pygame.MOUSEBUTTONDOWN:
if gameover:
if button1.collidepoint(event.pos):
gameover = False
x, y = 100, 100

See the example:

Sample Image

import pygame
pygame.init()

clock = pygame.time.Clock()

screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Auto Maze!")

pygame.font.init()
myfont = pygame.font.SysFont("Comic Sans MS", 10)

x, y = 100, 100

gameover = False
done = False
while not done:
for event in pygame.event.get():

if event.type == pygame.QUIT:
done = True

if event.type == pygame.MOUSEBUTTONDOWN:
if gameover:
if button1.collidepoint(event.pos):
gameover = False
x, y = 100, 100

screen.fill((0, 0, 0))

if not gameover:
pressed = pygame.key.get_pressed()

if pressed[pygame.K_w]:
y -= 5
elif pressed[pygame.K_s]:
y += 5
elif pressed[pygame.K_a]:
x -= 5
elif pressed[pygame.K_d]:
x += 5

player = pygame.draw.rect(screen, (0, 255, 0), (x, y, 60, 60))
wall1 = pygame.draw.rect(screen, (255, 0, 0), (300, 0, 100, 300))
gameover = player.colliderect(wall1)

else:

button1 = pygame.draw.rect(screen, (0, 0, 255), (175, 100, 60, 30))
text = myfont.render("Try Again", False, (255, 0, 0))
screen.blit(text, (175, 100))

pygame.display.flip()
clock.tick(60)

quit()

Pygame windows isn't responding after a few seconds of running

You missed to update the display at the end of main by pygame.display.flip(). Furthermore you have to control the frames per second with pygame.time.Clock.tick in the application loop:

def main():
# [...]

while not done:
# [...]

pygame.display.flip()
clock.tick(60)

Pygame unresponsive display

A minimal, typical PyGame application

  • has a game loop

  • has to handle the events, by either pygame.event.pump() or pygame.event.get().

  • has to update the Surface whuch represents the display respectively window, by either pygame.display.flip() or pygame.display.update().

See pygame.event.get():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

See also Python Pygame Introduction

Minimal example: Sample Image repl.it/@Rabbid76/PyGame-MinimalApplicationLoop

import pygame

pygame.init()

playerX = 50
playerY = 50
player = pygame.image.load("player.png")
width, height = 64*8, 64*8
screen = pygame.display.set_mode((width, height))

# main application loop
run = True
while run:

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

# clear the display
screen.fill((255,255,255))

# draw the scene
screen.blit(player, (playerX, playerY))

# update the display
pygame.display.flip()


Related Topics



Leave a reply



Submit