Overclock Gaming
Overclock Gaming

Reputation: 11

Rectangle not showing in PyGame window

I have this code:

import sys
import pygame

pygame.init()

size = 320, 240
black = 0, 0, 0
red = 255, 0, 0

screen = pygame.display.set_mode(size)

screen.fill(black)
pygame.draw.rect(screen, red, (10,10,50,50))
pygame.display.flip()

But it is not actually creating a rectangle in the PyGame window.

Upvotes: 1

Views: 1309

Answers (1)

Rafael
Rafael

Reputation: 7242

How you draw your rectangle is correct. You just need to constantly call the pygame.display.flip() in a while loop to render your game.

import pygame

pygame.init()

size = 320, 240
black = 0, 0, 0
red = 255, 0, 0

screen = pygame.display.set_mode(size)

screen.fill(black)
pygame.draw.rect(screen, red, (10,10,50,50))
done = False

while not done:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True

    pygame.display.flip()

pygame.quit()

enter image description here

Upvotes: 2

Related Questions