Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame window not responding after a few seconds

Tags:

python

pygame

This simple piece of code crashes (the window is not responding) after a few seconds (around 5).

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 24)
#clock = pygame.time.Clock()

#font = pygame.font.Font(None, 32)

cycles = 0
while True:
    screen.fill(0)
#    text = font.render('Cycles : %d' % cycles, True, (255, 255, 255))
#    screen.blit(text, (100, 100))

    cycles += 1

    pygame.display.update()

If I uncomment the commented lines, I can clearly see the program going out of control when displaying values between 47 and 50.

I use python 2.7 and pygame 1.9.2, Windows 8 (64 bits) and Eclipse + PyDev.

like image 261
Fred Avatar asked Nov 23 '13 17:11

Fred


People also ask

What triggers pygame quit?

pygame. QUIT is sent when the user clicks the window's "X" button, or when the system 'asks' for the process to quit.

Why is pygame init () not working?

The pygame. init() error is caused by its function not being called and can be solved with the import and initialization of pygame modules.

What does Flip () do in pygame?

flip() for software displays. It allows only a portion of the screen to updated, instead of the entire area. If no argument is passed it updates the entire Surface area like pygame.


2 Answers

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

like image 55
user2746752 Avatar answered Oct 07 '22 01:10

user2746752


You need to regularly make a call to one of four functions in the pygame.event module in order for pygame to internally interact with your OS. Otherwise the OS will think your game has crashed. So make sure you call one of these:

  • pygame.event.get() returns a list of all events currently in the event queue.
  • pygame.event.poll() returns a single event from the event queue or pygame.NOEVENT if the queue is empty.
  • pygame.event.wait() returns a single event from the event queue or waits until an event can be returned.
  • pygame.event.pump() allows pygame to handle internal actions. Useful when you don't want to handle events from the event queue.
like image 45
Ted Klein Bergman Avatar answered Oct 06 '22 23:10

Ted Klein Bergman