DEV Community

Sibusiso Dlamini
Sibusiso Dlamini

Posted on

Pygame Tutorial

My notes on Pygame

I want to start by saying that I believe the best way to learn a new tool/module is to actually start using and implementing it. So I will try and keep this tutorial and all future tutorials as short as possible but long enough for you to start building. For a better understanding of pygame you can always watch a Youtube video or better yet read the documentation. Without further ado. Let's pygame.

Installation

Before we even create pygame applications we need to download the module on our computer. This can be easily done by entering pip install pygame in the terminal

Hello world

It literally takes 5 lines of code to create a pygame applications and python is fairly verbose so I don't think these lines need explaining. You only need to know that the final line is not necessary

import pygame

pygame.init()
pygame.display.set_mode((500,500),)
pygame.set_caption('Hello world')
pygame.close()
Enter fullscreen mode Exit fullscreen mode

Note The method set_mode takes in the parameters as width and height as a tuple. In other words the window size of the application will be 500x500. I tend to create a variable for these as it will set the size of other objects on the screen compared to the window size.

width = height = 500
wn_size = (width,height)
wn = pygame.display.set_mode(wn_size)
Enter fullscreen mode Exit fullscreen mode

This is just so that if I ever want to draw objects on to the screen, I will make the size of the object relative to the width and height of the window.

Surfaces

Games are usually just a bunch of pictures or objects moving around on a screen. In pygame we 'draw' these pictures onto surfaces and draw the surfaces onto the window. And yes, you could draw directly onto the window wn but its just convention to use surfaces. for example...

screen = pygame.Surface((width,height))
wn.blit(screen,(0,0))
Enter fullscreen mode Exit fullscreen mode

Here screen is our surface object which we will draw or blit() onto the main window and because the dimensions are the same as our wn it will cover the entire screen

(0,0) is the position on the screen that we draw our surface onto. I would imagine that this would be used to draw multiple surfaces to separate the banner surface from the game surface. The banner would be used to keep track of the game state i.e. (your score or how many lives left etc) and you would carry out the game surface with the game logic as normal. This way you don't have to update the entire screen but only the surface that needs to be updated.

Drawing Lines

Pygame.draw.lines(surface,color,start_point, end_point,width)

Colors in pygame use rgb syntax in the from of a 3 dimensional tuple. (0,0,0) being black and (255,255,255) being white. Another Tip. Declare all your color variables at the top of your pygame files for easy use throughout your program

The width is an optional parameter and is 1 by default. The start_point and end_points are also tuples in the form (top,left). Just know that the larger the top value is the further it will be from he top and the same goes for left.

Drawing Rects

A Rect is a class in the Pygame module. It used to represent rectangles. They are created with the intention to move blocks around the screen and they even have methods to check if they collide with other Rects. A Rect is defined by its (top,left) position and its (width,height). e.g.)

w,h = width/10, height/10
my_block = pygame.Rect((0,0) (w,h)) 
Enter fullscreen mode Exit fullscreen mode

This will create a Rect that is 10% of the windows width and height. Now that we've created the Rect object we can now draw it onto our screen surface. Drawing Rects follows a similair syntax to drawing lines with the general syntax being:

pygame.draw.rect(screen, color, rect_object)

e.g.)

pygame.draw.rect(screen, black, rect_object)
Enter fullscreen mode Exit fullscreen mode

There are also circles and other polygons that you can draw using similar syntax all of which is in the documentation ๐Ÿ˜‰.

Note After drawing any object onto a surface you have to blit() the surface onto the window to see the changes.

Events

Games are event driven. This simply means that games respond to user input. The entire existance of games is centered around events. Events put the game in video game. Without it, we would just have video. This entire event tutorial would be pointless if I didn't mention how to check for events and know how to handle them.

Every time an event is fired, it is stored in a list that pygame keeps track of and we can access all these events using pygame.event.get(). We can iterate through this list like so

for event in pygame.event.get():
  if event.type == pygame.QUIT:
    #terminate program
Enter fullscreen mode Exit fullscreen mode

other event types include:

  • KEYUP
  • KEYDOWN
  • MOUSEBUTTONUP
  • MOUSEBUTTONDOWN

There are more many more event types but I think these are the most useful and common amongst games in general.

Note If you want to get the position of the mouse at anytime you can use pygame.mouse.get_pos(). This returns a tuple of in the form (x,y).

Event-loop

running = True
while running:
### 1)handle Events
### 2)Update screen  
Enter fullscreen mode Exit fullscreen mode
  • handle events
    Handling for events depends on the game that you are creating. You might want an object to follow the mouse or move according to key presses.

  • Update Screen
    You have to redraw all the objects on the screen to display all the changes.

Next Steps

If there is something that annoys me personally, it is going through a tutorial and not taking away anything from it. The best way you can get something out of this by building something. So if you want to get your time's worth from reading this tutorial then let the creative juices start flowing.

Happy coding!

Top comments (0)