0

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.

3
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by redyugi
0

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

Maybe IDLE? Maybe version of the pygame you use?

Cheers and Happy coding

Edited by Beat_Slayer: n/a

0

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

Edited by redyugi: Comments

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.