1 of 35

Programming Quick Guide:

Pygame Cheat sheet

2 of 35

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

3 of 35

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.

4 of 35

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

5 of 35

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

6 of 35

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.

7 of 35

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

8 of 35

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

9 of 35

3.Shapes and text

9

Shapes

Text

Moving shapes

Screen boundaries

10 of 35

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

11 of 35

Drawing Text

  1. Creates a font: includes font name, font size, bold (true/false) and italics (true/ false). Wants a font is created, it can be used for multiple texts.
  2. Creates the text: includes the text itself, anti-aliased (a technique to help reduce pixelated looking text)and colour
  3. Display the text a certain location.

11

12 of 35

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

13 of 35

Screen boundaries

To stop our shape leaving the screen we need to set up some boundaries.

  1. First we need two new variable to calculate the change we want to make to the coordinates.
  2. Move the shape
  3. Finally we need to check if it is at a wall, this is done by checking if the shape is below 0 or 30 less (as shape is 30x30) than the screen size. If so we send it in the opposite direction.

13

Variables need to be created before the while loop

Inside game logic

14 of 35

4. Images

14

Import Images

Moving Images

15 of 35

Importing Images

  1. Before adding images it is important to create an image folder inside the same folder where the pygame file is saved. We do not need to do this but this helps keeps your file structure neat and easy to use.
  2. Save the image you want to use inside the folder with a simple name.
  3. Use the first line of code to direct pygame where the file is. Make sure the correct file type is used.
  4. Display the image. The image size will be the same as the original image size.

15

16 of 35

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

17 of 35

5.Keyboard

17

Key Pressed

Key Released

Simple movement

Screen boundaries

18 of 35

Key pressed

  1. This code condition is met if any keyboard button has been pressed.
  2. In a branching if, we can check for certain keys.
  3. Finally we can choose what happens when that button is pressed. In this case we are changing a speed variable by 3 to allow movement (variable needs to be created outside the while loop).
  4. Here is a list of keycodes.

18

19 of 35

Key Released

  • This code condition is met if any keyboard button has been released.
  • In a branching if, we can check for certain keys.
  • Finally we can choose what happens when that button is released. In this case we are resetting a speed variable to 0 to stop movement. (variable needs to be created outside the while loop).
  • Here is a list of keycodes.

19

20 of 35

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

21 of 35

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

22 of 35

6. Mouse

22

Move with mouse (static)

Move with mouse (Fluid)

Mouse clicks

23 of 35

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

24 of 35

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

25 of 35

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

26 of 35

7. Sounds

26

Sound Effects

27 of 35

Sound Effects

Sound effects can add a lot to your game and are really easy to add.

  1. First create a variable that holds your sound file. If you have loads of sound effects you may want to create a folder to hold them all. Also make sure you use simple file names.

  • When you want to use your sound effect use the code below.

27

28 of 35

8. Menus and buttons

28

29 of 35

29

30 of 35

9. Layouts

30

31 of 35

31

32 of 35

10. Collisions

32

Mouse collision

33 of 35

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.

34 of 35

Common Imports

34

35 of 35

35