Pygame: How To’s?

This tutorial is the fifth tutorial in a series of five Pygame tutorials:

Now that you have completed your first game you may want to customise it further of or add extra functionalities. The following How-To’s tutorials may become quiet useful.

Also remember that a Pygame game will be based around the main program loop. Make sure you fully understand the structure of your main.py file:

pygame-main-program-loop-flowchart

To gain a better understanding of the pygame library yuou should familiarise yourself with its main modules and classes.
View Pygame Framework

How to Add a Background Picture to the game
This would work for background picture or any other pictures that you want to display on the screen.

Before the main program loop add the following code:

In your main program loop add the following code:

In the above code (0,0) represents the (x,y) coordinates of where you would like your picture to appear.

The background has to be saved in your “images” folder.

How to use the transparency of a PNG file
When loading a picture use the convert_alpha() method as follows:

How to display text on the screen
To display text on the screen, use the following code:
How to flip your sprite
To flip your sprite horizontally:

To flip your sprite vertically:

How to find out if a key is pressed
In your “event handling loop” of the main program loop add the following code:

How to create textboxes
The following code uses a class called TextBox and instantiates two textboxes (objects) called username and password.

How to create a popup input box
pop_up_box_pygame
In your main program, at the very top, just after the import pygame statement add the following code:

Then in your main program, whenever you want a new popup box use the following code:

Some arguments can be added:

How to drag a sprite on the screen using the mouse
Want to create a drag and drop game? Add the following code inside the main programme loop and replace playerCar with the name of your sprite.

How to check the color of a pixel at a given position
For instance you may want to check if your sprite is walking on the road or on grass. In this case you could use the get_at(x,y) method which returns the color of a pixel at position x, y.
e.g.
How to pause the game when the player presses a key (e.g. space bar)
You may want to allow the user to pause the game using a shortcut key (e.g. Space Bar). To do so add the following code in the for loop where you catch user events (for event in pygame.event.get():)
Code to add:
How to detect if the user clicks on a sprite
You can detect if the player has clicked on a Sprite by adding the following code to the for loop where you catch user events (for event in pygame.event.get():):
How to use a mask on a sprite for more accurate collision detection
The default collision detection in Pygame uses the rect attribute of the sprites to detect if two sprites are overlapping. This approach works well if your sprites are plain rectangles. However, as you start using graphics for your sprite, you will want the collision detection to use the edges of your graphics. You can do so by creating a mask on all your sprites.

To do so, go in your sprite class (e.g. Car Class) and add the following line of code in the constructor (__init__() of the class):

For instance:

In your main code, use the following code to detect collisions:

Adding Background Music
To add a background soundtrack, download a wav file or mp3 file into a folder called “Sounds”.

At the start of your code (main.py), after importing the pygame library, add the following lines of code:

Note that you can stop the music at any time using the following instruction:

You may prefer to pause the music:

… or unpause the music:

Adding Sound Effects
To add sound effects, download some wav files into a folder called “Sounds” and add the following code when you want to play the sound effect:
Tagged with: