4.2.6 - Pygame revisited

The basic template for the pygame code follows. Click on the link below, copy the contents of the file to Thonny and save it as pygame_basic (or with whatever name you like).

Pygame basic template

So what is all of the commands? Let's review again all the commands.

import pygame # it imports the library pygame

Colors are not known to Python, so we create them using Red Green and Blue (RGB) numbers from 0 to 255. 

pygame.init() # starts the pygame 

size = (700,500) # Set the width and height of the screen

screen = pygame.display.set_mode(size) # creates a window and name it screen

pygame.display.set_caption("My Game") # This is the name on the top of the window

done = False # We create a variable that is called done and when this variable turns to True, during the game, the game stops.

clock = pygame.time.Clock() # It controls the refresh rate

The three following commands wait for user interaction (event). QUIT is when we press the X button on the top of the window. Then variable done changes to True and the script continues out of the loop

for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

screen.fill(WHITE) # background is WHITE. Try to change it to other defined colors.

pygame.display.flip() # update the screen

clock.tick(60) # refresh 60 times a second

After the script continues out of the loop we are closing pygame  (if we don't then we have a zombie !!!! windows and we have to close it using the stop button)

pygame.quit() # Close the window and quit.

In case you want to review what the commands are doing it this template you can view the following video :