Making a Game in Python

Antriksh Misri
3 min readSep 13, 2020

Yes you read it right , python can be used to make a game , more specifically a 2d game. Although python is not an ideal language to make a game but for those who are familiar with programming and just want to get a hang of how everything is done from scratch , python can be a good start. If you are planning to start a full fledged gaming studio and plan on using python as core language then it’s not a good idea.

The particular library that will help us in making our game is pygame.

Pygame is a python library that can be used specifically to design and build games. Pygame supports only 2d games that are built using different sprites.

We’ll be creating a simple game with the following rules:-

  • The player can only move vertically.
  • Other than player block there will be two other blocks.
  • One of them will be the enemy block and one of them will be score block.
  • If the player collides with the enemy block then the game over screen pops up, if the player collides with the score block the score is incremented and it is compulsory to collect all score blocks.

We’ll be using various techniques such as the use of functions, random variables, various pygame functions etc.

If you are already familiar with pygame and just want the source code of the game skip to the bottom for source code.

Before we dive into how to code a game in python we first need to know the structure that we’ll be following to make a game. So let’s see what we’ll be doing.

  1. First and fore most we need to declare a variable that will define what will be the properties of the window that will display our game the property that we are interested in is resolution. The resolution is a tuple that holds two integer values one for width other for height. Now , how does all of this look like in code , the code below demonstrates:-
import pygame
pygame.init()
res = (720, 720)
screen = pygame.display.set_mode(res)

2. Now , as the screen is defined we can proceed to the next part i.e. game loop. In pygame ,the game loop simply means game should continue to run until and unless user interrupts the loop by pressing ESC key or by any other inputs. The game loop is the part where all the game logic goes , like how will each block look like , what will be their behavior , user controls etc. The code for this looks like this:-

# Variable to keep the main loop running
running = True

# Main loop
while running:
# Look at every event in the queue
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
running = False

# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
running = False

3. Now , that we have made the game loop , we need to create different screens like , start screen , game over screen. This is pretty simple to do. As we know , a game in pygame is basically everything written in the game loop , what if we make multiple game loops and add different code in each one of them. Well spoilers , this won’t work as pygame only expects one game loop to be running at any point of time, so what do we do ? We will overcome this problem by enclosing each game loop in functions. So if our game has three screens , start , game and game over , we will have three functions start() , game() , game_over(). Each function will call other function when user triggers an event. For example is the player is at start screen and presses the start button then the start() function will call the game() function. This will kill the start menu screen and initialize the game screen.

4. There countless possibilities in pygame let alone python. There are tons of old games written in python. The link for the source code for this particular game is down below. Feel free to copy this code , add variations to it , mix and match. Cheers!

--

--