Making Games with Python and Pygame

Making games with Python is a great way to improve your Python programming skills and have fun along the way. It can be an end in itself, but it can also give you plenty of practice in algorithmic thinking, problem solving and general Python programming.

One of the many great things about Python is how easy to do powerful things by making use of open source libraries. One such library is pygame which makes creating games with Python fun and accessible even for beginners.

Advantages of using Pygame for Game Development

  • Large community so lots of tutorials, support and code examples easily available
  • lightweight
  • Easy to get started
  • It’s Python!

There is plenty of help and support available for making games with Pygame, in the form of books, websites, projects, forums etc. This is a big deal, as it can be very frustrating to have a great idea but then get stuck with implementing it.

Disadvantages of using Pygame for Game Development

  • Pygame programming is quite niche
  • Pygame is not really designed for 3D graphics
  • Speed

Writing games with Pygame is quite a niche activity, although it happens to be a wonderful niche. I would say though that it’s a good idea to consider your long terms goals. Having fun and learning for the sake of learning are great and I fully encourage it, but time and energy can be limited. For example, it might be that learning to make games with HTML 5/JavaScript etc. could serve you better if you have an eye on web development work. Or if you are interested in serious game development, you may want to consider a more powerful platform such as Unity. Not to discourage you from exploring Pygame – I just wanted to mention this longer-term perspective. Certainly a lot of what you learn from working with Pygame will be transferable to other types of development.

Recommend Books for learning Python game development

Here’s a few great books to help you learn game development with Python.

“Invent with Python” is mostly concerned with the basics of Python programming but has a section on working with PyGame. The others are more specifically about Pygame.

As an Amazon Associate I earn from qualifying purchases.

A taste of Python Pygame programming

Fist off, install Pygame with pip install pygame or pip3 install pygame on Mac/Linux. Or you can use your IDE’s package installation functionality.

Below is a minimalist Pygame program showing how some of the basics work, such as initialising Pygame, displaying objects and implementing some user interactivity. The code is commented to help you understand what is happening. I recommend typing the code into your Python editor rather than copy/pasting, and then running it to see what happens…

import pygame

# Define some colors and other global constants
RED = (255, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
SCREEN_SIZE = (500, 500)

# Initialize Pygame
pygame.init()

# Set the dimensions of the screen
screen = pygame.display.set_mode(SCREEN_SIZE)

# Set title
pygame.display.set_caption("Fun with Pygame")

# Create a clock to manage the speed of screen updates
clock = pygame.time.Clock()

circle_color = RED  # Initial color of the circle we're going to draw
circle_pos = (SCREEN_SIZE[0] // 2, SCREEN_SIZE[1] // 2)  # Center of screen

# Loop until user exits
done = False

# Main program loop
while not done:
    # Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # If user clicked exit
            done = True  # Allow main program loop to exit
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            print("here")
            circle_color = GREEN if circle_color == RED else RED

    # Clear the screen
    screen.fill(WHITE)

    # Draw a circle - it will be in memory after this but not sent to the screen
    pygame.draw.circle(screen, circle_color, circle_pos, 100)

    # Update the screen with the circle we've drawn
    pygame.display.flip()

    # Limit execution to 60 frames per second
    clock.tick(60)

# Exit nicely
pygame.quit()

Try pressing the space bar in rapid succession…

You can see that it really isn’t that hard to make game-like things happen with Pygame. As mentioned there are many resources online to help you. Probably the best way to approach learning game development with Pygame is to find some examples from books or websites, type them in and make sure they work, and then get stuck in and make changes. Try things out. Be creative. Once you have a bit more experience you might then want to implement a game from scratch. This is a great way to learn, but you may need to be a bit patient, and also to develop the skill of asking questions on sites like stack overflow in a way that gets you the answers you seek. This is actually a very important skill for any budding developer, and something that takes a bit of effort to get right.

Other Pygame resources

There is a website dedicated to Pygame here: https://www.pygame.org/. It’s very useful and has lots of example projects on it. Be aware though that the site had been around for many years and some of the links to code examples are broken.

You might also want to check out:

Stack Overflow
Game Development Stack Exchange


This article has explored the Pygame Python package, which is a fantastic way to make exciting games in Python and develop your programming and problem-solving skills in the process. I hope you found it interesting and helpful.

Happy Computing!

Sharing is caring!

Leave a Reply

Your email address will not be published.