Module 4 - Section 2 - Graphics
- identify the commands inside the Pygame basic template
- experiment with various graphics commands
- draw shapes using Python commands
- draw text using Python commands
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).
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
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
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 :