2

When running this code:

import pygame,time

GREEN = (30, 156, 38)
WHITE = (255,255,255)

pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
time.sleep(3)

Pygame shows a black screen for 3 seconds, but doesn't draw a rectangle. I am using Atom using script package to run code

3 Answers3

2

You have to implement an application loop. The typical PyGame application loop has to:

import pygame

GREEN = (30, 156, 38)
WHITE = (255,255,255)

pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()

# applicaition loop
run = True
while run:
    #  limit frames per second
    clock.tick(60)

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

    # clear display
    screen.fill(WHITE)

    # draw objects
    pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))

    # update display
    pygame.display.flip()

pygame.quit()
exit()

Note, you must do the event handling. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
1

You have to update the screen like that:

pygame.display.flip()

to render what you just drew.

Your code should look like this:

import pygame
import time

pygame.init()

GREEN = (30, 156, 38)
WHITE = (255,255,255)

screen = pygame.display.set_mode((640, 480))

# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))

# show that to the user
pygame.display.flip()

time.sleep(3)

Off-topic: You should also get the events to allow the user to close the window:

import pygame
from pygame.locals import QUIT
import time

pygame.init()

GREEN = (30,  156,  38)
WHITE = (255, 255, 255)

screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock() # to slow down the code to a given FPS

# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))

# show that to the user
pygame.display.flip()

start_counter = time.time()
while time.time() - start_counter < 3: # wait for 3 seconds to elapse

    for event in pygame.event.get(): # get the events
        if event.type == QUIT: # if the user clicks "X"
            exit() # quit pygame and exit the program

    clock.tick(10) # limit to 10 FPS
                   # (no animation, you don't have to have a great framerate)

Note that you must put all of this into a game loop if you want to repeat it like a classic game.

D_00
  • 1,440
  • 2
  • 13
  • 32
  • You must do the event handling. See [`pygame.event.get()`](https://www.pygame.org/docs/ref/event.html#pygame.event.get) respectively [`pygame.event.pump()`](https://www.pygame.org/docs/ref/event.html#pygame.event.pump): *" For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system."* – Rabbid76 Mar 31 '21 at 16:51
  • Thank you all, I was half asleep so I forgot to update. – DefinitelyAHuman Apr 01 '21 at 14:55
1

Update the screen with:

pygame.display.update()

at the end of your code you have posted.

SamTheProgrammer
  • 1,051
  • 1
  • 10
  • 28