1

When I run the code a blue rect is displayed in its proper location however the entire window freezes and eventually crashes. How can I fix this?

import pygame
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
run = True
while run:
    pygame.time.delay(100)
    filled_rect = pygame.Rect(100, 100, 25, 25)

    pygame.draw.rect(win, (0,0,255), filled_rect)
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

You have to add an event loop. Handle the events, by either pygame.event.pump() or pygame.event.get(). Thus the IO and internal events are handled and the window keeps responding. e.g.:

import pygame

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

run = True
while run:
    pygame.time.delay(100)

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

    # clear the disaply
    win.fill(0)

    # draw the scene
    filled_rect = pygame.Rect(100, 100, 25, 25)
    pygame.draw.rect(win, (0,0,255), filled_rect)

    # update the dispaly
    pygame.display.update()

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174