Python Pygame - Intro to Pygame

Python Pygame - Intro to Pygame


Pygame is a set of Python modules designed for writing video games. Pygame is easy to understand allowing you to focus on learning the concepts of game development instead of struggling with the technicalities. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. This is important if you decide to pursue game development in the future because you will have these core concepts to take with you.

Pygame is free. For a nice introduction to pygame, examine the line-by-line chimp tutorial, and the introduction for python programmers.


The official pygame documentation recommends using pip for installation. Pip is a package management system that allows you to install and modify software packages written in python. The best way to install pygame is with the pip tool (which python uses to install packages). Note, this comes with python in recent versions.

Note: python 3.6.1 or greater is recommended. You can get the relevant python installers from their website.

pip install pygame

The above command will install pygame, as well as any dependency it might have. 

Getting Started

When you have installed both Python and Pygame, create a new Python file and import the pygame module. The next step would be to create a new game window using pygame.init() method. 


Let us see how. 

Creating our Game Window

Now, after Importing the pygame module just write the code in your script.

import pygame


game = pygame.display.set_mode((600,400))

pygame.display.set_caption(%u201Cfirst game%u201D)

Now, let us understand the above code.


Firstly, we Imported the pygame module in the first line of the code after then we called pygame.init() method that initializes all the modules required for PyGame. This will initiate PyGame, and allows you to then make various commands with PyGame.

Then after initializing pygame we initialized our game window and stored it into a variable. The set_mode function requires one parameter which is a (width, height) tuple. Note that we are not passing width and height as separate parameters. The set_caption function takes one parameter that is the title we want to give our window.


Starting Game Loop

Once initializing the pygame module, We can now get started on our game loop! This is where all of your code goes In. Let us start by creating a boolean Identifier and set its value to True, that indicates if we want our game to be running or not. After that, We will create a loop that runs forever until our boolean is set to false. We now have a game window on our screen that is ready to go.

Game_Loop = True

While Game_Loop:

# your code goes here

We now have a game window ready on our screen. Now, this will run until

Game_Loop will be set to false.we can provide our game functions inside this loop.  

Game Clock

A clock in pygame is used to track time within the game and, Is mostly used to specify the FPS or Frames Per Second.


Clock = pygame.time.Clock()

Simple enough, this is our game clock. the duration of each frame is as short as your super fancy computer can make it. The framerate needs to be throttled at a sane number such as 60 frames per second. Luckily, there is a simple class in pygame. time called Clock that does this for us. It has a method called tick which takes in the desired fps rate. run per frame, and might be a massive waste of processing. 


Adding Functionality

We are now ready to add content, so, for example, we can begin withdrawing a rectangle. It is simple and we use pygame.draw.rect for this purpose

pygame.draw.rect(game, (0, 128, 255), pygame.Rect(30, 30, 60, 60))

As you can see there are 3 arguments:

  • The first argument is the surface instance to draw the rectangle too.

  • The second argument is the (red, green, blue) tuple that represents the color to draw with.

  • The third argument is a pygame.Rect instance. The arguments for this constructor are the x and y coordinates of the top left corner, the width, and the height. 

Now, by adding all the necessary functions to our code the final template code will look like this.

import pygame


game = pygame.display.set_mode((600,400))

pygame.display.set_caption('first game')

clock = pygame.time.Clock()

Game_Loop = True

while Game_Loop:

pygame.draw.rect(game, (0, 128, 255), pygame.Rect(30, 30, 60, 60))

for event in pygame.event.get():

        if event.type == pygame.QUIT:

            Game_Loop = False







Now, run this file and you should be able to see your game window.