Programming Quick Guide:
Pygame Cheat sheet
1.Creating the window
2. Clock speed (important)
3.Shapes and Text
4. Images
5. Keyboard
6. Mouse
7. Sounds
8. Menus and buttons
9. Layouts
10.Collisions
import pygame
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
pygame.init()
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("My Title")
running = True
while running:
#-- -- --Events should go here-- -- --
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#-- -- --Game logic should go here-- -- --
#-- -- --Drawing code should go here-- -- -- -
display.fill(white)
#Draw after you fill the screen if not your will overwrite everything
pygame.display.update()
pygame.quit()
quit()
On the left is the template code start any pygame file. It is really important you understand how each part of this code works.
The next 4 slides will break down this code and explain what each part does.
Import Pygame
Above, we've imported PyGame, which is obviously necessary to make use of the module! Then, we run pygame.init(), which is integral to every single PyGame application that you will ever write. This will initiate PyGame, and allow you to then make various commands with PyGame and our game.
4
Creating the window
Next, we define the game's display, this is basically our canvas that we will draw things to.
This include the resolution in pixels (800px by 600px in the above example) and the title of the window (“my title”)
5
Closing a window
Closing a Pygame window is not as simple as you would first think. We need to check for any event running (this will be used later for keyboard/mouse input). Once the window is closed pygame will stop. Using the running variable allows the program to be easily stopped at any point.
6
Outside the while loop at the end of your code.
Fill screen colour
At the start of our code with our variable, it can be a good idea to define the key colours you might use in your program.
To change the colour of the window, we
can use the fill function followed by a
colour we set up earlier. We then need
to update the window.
7
Clock Speed
Currently the while loop will execute code as fast as possible. Which can make you code very choppy and some commands will work faster than others.
Add the code above under your title code.
Finally add this code after updating the screen. The while loop will not only update 60 times a second.
8
3.Shapes and text
9
Shapes
Text
Moving shapes
Screen boundaries
Draw shapes
We can draw shapes on to our screen.
The following code will draw a rectangle. In the brackets we need to include the screen name (display), colour of the shape and size (this made of 4 bits of information).
Other shapes can be found in the documentation.
10
Starting coord (x)
Starting coord (y)
Shape width
Shape height
Drawing Text
11
Moving shapes
By creating variables to hold the coordinates of our shape we can adjust them each time before re-drawing the shape.
This will create a slow moving circle on the display.
12
Variables need to be created before the while loop
Screen boundaries
To stop our shape leaving the screen we need to set up some boundaries.
13
Variables need to be created before the while loop
Inside game logic
4. Images
14
Import Images
Moving Images
Importing Images
15
Moving Images
By creating variables to hold the coordinates of our image we can adjust them each time before re-drawing the shape.
This will move the shape diagonally of the screen.
To keep shape on the screen follow this guide on Screen boundaries.
16
Variables need to be created before the while loop
5.Keyboard
17
Key Pressed
Key Released
Simple movement
Screen boundaries
Key pressed
18
Key Released
19
Simple movement
1.Make sure you have the code added for keyboard presses and keyboard releases.
2.Create variables to control position and speed for both x and y coordinates.
3.Change position by current speed.
4.Apply the new x and y to either a shape or image.
20
Screen Boundaries
To make sure our shape or image does not leave the screen we can add the following code to our game logic.
It checks if the x or y coordinates is leaving the screen and resets their position.
Hint: If we store the initial screen size and shape size we can improve this code.
21
6. Mouse
22
Move with mouse (static)
Move with mouse (Fluid)
Mouse clicks
Move to mouse (static)
We can use the code below to quickly get the current mouse position. This is returned as a list so we need to extract the individual values and save them into two new variables.
This could be used to always move a shape or images to the mouse.
23
Move with mouse (smooth)
We can change the move to mouse code to make it less static. Instead of jumping straight to the mouse we can add a heading for the shape to move towards.
We can then apply the new x and y positions to our shape.
24
Mouse clicks
Mouse clicks is an example of an event. The code below adds a new event under the QUIT event.
When the mouse button is clicked the condition is met and the code below can be executed.
In this example we are teleporting the shape to the clicked space.
25
7. Sounds
26
Sound Effects
Sound Effects
Sound effects can add a lot to your game and are really easy to add.
27
8. Menus and buttons
28
29
9. Layouts
30
31
10. Collisions
32
Mouse collision
Mouse collisons
Before checking collision our image or shape needs to be saved in a variable so we can call it.
Next we need to check if that object has been clicked and decided what action should be taken.
33
When drawing the shape we swap the coordinates out with our variable created earlier.
Common Imports
34
35