free stats

Introduction to PyGame - Game Development using Python

Last Update : 08 Oct, 2022 Python, Programming

In this tutorial, You will learn the core concepts that are needed to begin developing basic applications using Python PyGame.

PyGame is a cross-platform set of Python modules that are used to create video games and other multimedia programs. This module consists of computer graphics and sound libraries that are designed to be used with the Python programming language. This is suitable for creating client-side applications that can be potentially wrapped in a standalone executable. Also, This module is popular among millions of people across the world.

 

Install PyGame on Your PC

Before installing PyGame, You need to install Python in the system.

The easiest and most common way of installing PyGame is using the below pip command which is what Python uses to install packages.

pip install pygame

If the above pip command is executed successfully, create a new Python .py file with the following code.

import pygame

If this import statement executes without giving any error, the Pygame module is successfully installed on your PC.

 

Create a PyGame Window & Game Loop

Before developing any PyGame application, First, you need to setup the game window. This game window will be used for drawing and rendering text, shapes, and other objects. 

First, you need to do the following import. Here, the sys library later helps to exit the program successfully.

import pygame
import sys

The following piece of code creates a game window that is 400 pixels wide and 400 pixels in height.

pygame.init()
screen = pygame.display.set_mode((400, 400)) 

pygame.init() function used to initialize the PyGame engine. Also, This code is a must in every PyGame application.

pygame.display.set_mode() function used to create “Screen” or “Display” object.

Now, you can setup the Game Loop.

A game loop runs continuously during the game. Also, only stops when a “QUIT” command is given. The game loop processes user inputs, update the game state, and render the game to the game window at each round in the loop.

The following format shows the basic structure of the game loop in PyGame.

while True:
    # Processing
    # Events
    # Rendering
    pygame.display.update()

You need to call the pygame.display.update() function at the very end of the game loop. This is used to update the game screen.

 

Create the Event Loop

Consider the following example PyGame code:

import pygame 
import sys
 
pygame.init()
screen = pygame.display.set_mode((400, 400))
 
while True:
    # Processing
    # Events
    # Rendering
 
    pygame.display.update()

If you try to run the above basic PyGame code, an error occurs when trying to close the window by using the exit button in the top-right corner.

Everything you do in PyGame generates an "event".

Example:

  • A Keypress generates a "Key down" event.
  •  A mouse click generates a "Mouse down" event.
  • A Click of the Exit button generates a "QUIT" event.

You can use the following code to handle the QUIT event and shout down the window properly.

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

Also, use pygame.event.get() function at the start of the Game loop to a list of every event that is currently occurring. This includes the "QUIT" event as well. You can find it by iterating through each event.

Then, you need to call the pygame.quit() and sys.exit() functions to correctly shut down the window.

Remember that need to call the pygame.quit() function first.

You will learn more about PyGame events later.

 

Drawing on the Screen

In PyGame, using the Surface Class is an easy and quick way to create Rectangular or Square objects on the Screen. It takes the values of the width and height as a tuple.

Also, use the fill() function to set the color with GRB values as a tuple. The range of each value in the tuple is from 0 to 255.

surface = pygame.Surface((100, 100))
surface.fill((255,0,0))

The following example shows the full code.

import pygame
import sys
 
pygame.init()
screen = pygame.display.set_mode((400, 400))
 
surface = pygame.Surface((100, 100))
surface.fill((255,0,0))
 
while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(surface, (150, 150))
    pygame.display.update()

This program produces the following result -:

Drawing on the Screen - PyGame - UXPython

The blit() function is used to draw the new surface to the screen. This function request the object of the surface Class and a location where it will be drawn as a tuple.

Congratulations! Now you have knowledge about the basics of PyGame. Go to the next tutorial to learn more about Python PyGame programming.

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.