Member Avatar for Member #286182

Hi all this is my code :-

import pygame
from pygame.locals import *
from sys import exit
from random import *
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    screen.lock()
    for count in range(10):
        random_color = (randint(0,255), randint(0,255), randint(0,255))
        random_pos = (randint(0,639), randint(0,479))
        random_size = (639-randint(random_pos[0],639), 479-randint (random_pos[1],479))
        pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))
        screen.unlock()
        pygame.display.update()

What am I doing wrong to make the pygame window not quit properly?

This is from a book.

Dani AI

Generated

@jtaylor-bye The freeze you see on close is usually caused by ending the program from inside the event loop with exit()/sys.exit() while the window is still alive, or by an IDE swallowing the SystemExit exception. A safer pattern is to handle QUIT, break out of the main loop, uninitialize Pygame, then let the script end normally. Remember that pygame.quit() just releases Pygame resources; it does not terminate the interpreter, and sys.exit() simply raises SystemExit (which some interactive shells can intercept). Running from a terminal often avoids that confusion. See pygame.quit and SystemExit (Python docs). (pygame.org, docs.python.org)

Building on ’s suggestion, a few tweaks will keep the window responsive:

  • Update the display once per frame after you draw the batch of rectangles. Either call pygame.display.flip() to refresh the whole screen or call pygame.display.update(changed_rects) once with a list of rectangles; calling update() 10 times per frame is wasteful. Docs: flip, update. (pygame.org)
  • Throttle your loop with Clock.tick(60) so events are serviced regularly instead of pegging a CPU core. Docs: pygame.time.Clock.tick. (pygame.org)
  • Keep processing the event queue every frame; if you stop doing that, the OS may mark the window as not responding. Docs: pygame.event. (pygame.org)

One more detail: the explicit screen.lock()/unlock() calls are unnecessary for pygame.draw.rect. Pygame automatically locks surfaces as needed, and leaving a surface locked can prevent redraws. You can remove the manual locking entirely here. Docs: Surface.lock/unlock and mustlock. (pygame.org)

Lastly, ’s environment hunch is valid: if you were running under IDLE, its handling of SystemExit can make shutdown look hung; a plain terminal run is a good sanity check. (docs.python.org)

Recommended Answers

All 2 Replies

I can't see the problem, and it's working good.

Maybe IDLE? Maybe version of the pygame you use?

Cheers and Happy coding

Try this

import pygame
from pygame.locals import *
from sys import exit
from random import *
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
run = True
while run:
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False # Exits the loop. Not sure if 'exit()' was defined
            break
    screen.lock()
    for count in range(10):
        random_color = (randint(0,255), randint(0,255), randint(0,255))
        random_pos = (randint(0,639), randint(0,479))
        random_size = (639-randint(random_pos[0],639), 479-randint (random_pos[1],479))
        pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))
        screen.unlock()
        pygame.display.update()
pygame.quit() # Quits the window
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.