1
votes

I am new to python and made an algorithm I would like to visualize on a screen with pygame. This is my code:

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Test screen")

x = 50
y = 50
width = 40
height = 60
vel = 5

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

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

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()


pygame.quit()

But this always displays a black or kinda dark gray screen. Does anyone know what I'm doing wrong? I have tried multiple tutorials and they all give me the same screen.

I am using MacOS 10.14

1

1 Answers

0
votes

You need to call the fill function on the win object. The background is always going to be black by default. The below example shows how to turn it to white.

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Test screen")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
    pygame.time.delay(100)
    ## enter color here
    win.fill((255,255,255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()


pygame.quit()