Quick References and Troubleshooting

Find quick references for drawing basic shapes with pygame, as well as common troubleshooting errors.

Drawing Reference Sheet

Pygame Draw Functions Summary.pdf

Troubleshooting

Here are some common mistakes that people make when programming with pygame:

My Image Isn't Loading

This is usually the result of one of two things:

1 - The image file is not in the same folder as your python program.  Browse to where your python program is and verify that the image file is actually there.

2 - You spelled the filename incorrectly when you loaded it in your program code.  Filenames are case sensitive, so make sure that the filename you are speficying matches the filename exactly.  

My Image's Background isn't Transparent

This is usually the result of one of two things:

1 - The background of the image file you are using isn't actually transparent.  Sometimes, and image appears to have that checkered background when you download it, but it really is a checkered background.  You may need to open it up in image editing software to actually give it a transparent background.

2 - You forgot to use convert_alpha() when loading the file in your program.  When you load the file into your program by specifying the filename, you need to use the convert_alpha() function to preserve transparencies:

coin_image = pygame.image.load("coin.png").convert_alpha()


Nothing is Being Drawn, My Window is Blank

This is usually the result of you accidentally deleting the line:

pygame.display.flip()

from your game loop, OR by accidentally removing its indentation, so it is no longer inside the body of your game loop.

SOMETHING is not Initialized

There are some pygame modules (such as the font module) that need to be initialized.  You may have accidentily deleted:

pygame.init()

from your program (right below where the colours are defined).

How to's?

Centering Items in the Window

In order to center an object, you need to know two things:

1 - The width/height of the window, and the width/height of the item being centered.

Center Horizontally

Let's say that we have a pygame.Rect called ghost.  Let's center it.

The first thing you need to do is determine the midpoint of your window.  Do this by dividing the width of the window by two.  For example, if your window is 700 pixels wide, the x coordinate of the middle of it is:

x_cord = 700/2                # this would be 350

In our template, we actually store the width and height in a variable called size.  We could use that to calculate the center of our window, no matter it's size:

ghost.x = size[0] / 2

If we were to draw our ghost to this location, the left side if it would be in the center, but we want the center of the ghost to be at the center of the window.  We will need to move ghost to the left by half of its width:

ghost.x = size[0] / 2 - ghost.width / 2

Center Vertically

This demonstration will cover exactly how you can center items horizontally, but the process of centering something vertically is the exact same idea.

Center Text

Centering text is the exact same as a rectangle, with an added step of actually determining how wide the text actually is.  

in order to get the width of text, you need to know the font type, size and the text you are rendering.  You can do this by using a size() function on the font you created:

text_size = font.size("the text that you will print")

text_width = text_size[0]

x_cord_center_text = size[0]/2 - text_width / 2

...

screen.blit(text, x_cord_center_text, 20)


Adding Images

To add an image, you must first put the image file into the same folder as the .py file you are working on.  When you load the image, its size will be the same as the file.

1 - Load the image into your program.  Note that filename.png, must match the filename exactly, including case and file extension:

#Use this if there is no transparency in the image

generic_image = pygame.image.load("filename.png").convert() 

#Use this if there is a transparency in the image

generic_image = pygame.image.load("filename.png").convert_alpha() 

2 - Scale the image to the size you need inside your program (this must be done after it has been loaded):

generic_image = pygame.transform.scale(generic_image, (width, height)

Adding Sounds

Load the sound:

(in progress)