Week 3 - Section 1 - Data types and a bit of gaming
Week 3 - Section 1 - Data types and a bit of gaming
In this section we will learn about lists and tuples and we will scratch the surface of pygame. Be prepared for excitement.
Chapter 4.8 - Pygame
4.8 - Pygame (finally)
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?
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. So color (0, 128, 255) is no Red half(128) Green and full Blue. You can view the results here. We use CAPITAL letters for variables that don't change (we usually call them constants).
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 code gets 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
When the code gets out of the loop
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 :