Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Using Pygame to Build an Asteroids Game (Overview)

Do you want to create your own computer games but like Python too much to abandon it for a career as a game developer? There’s a solution for that! With the Pygame module, you can use your amazing Python skills to create games, from the basic to the very complex. Below, you’ll learn how to use Pygame by making a clone of the Asteroids game!

In this course, you’ll learn how to build a complete game, including:

  • Loading images and displaying them on the screen
  • Handling user input in order to control the game
  • Moving objects according to the game logic
  • Detecting collisions between objects
  • Displaying text on the screen
  • Playing sounds
Download

Sample Code (.zip)

6.3 MB
Download

Course Slides (.pdf)

900.2 KB

00:00 Welcome to Build an Asteroids Game in Pygame. My name is Chris and I will be your guide.

00:06 Pygame is a great Python library that allows you to quickly write your own 2D games. This course will show you how games are structured and the basics of working with graphics, animation, and sound.

00:20 Writing games is a fun way to hone your skills as a coder and create something you want to show to your friends. In this course, you will learn about writing a game with Pygame, using game loops, including image and sound assets, and controlling your games through keyboard input. Spoiler alert! This is your final goal, a clone of the olden-times arcade hit Asteroids.

00:45 Your ship flies around and shoots the rocks into smaller rocks, ultimately trying to remove all the space debris from the screen. One little hint for you: Just because you might be good at writing them, doesn’t mean you can’t suck at playing them.

00:58 This course will be full of laughs as you watch just how bad my aging reflexes are, well, aging. A quick note before I show you the code. All the examples here were tested against Python 3.9 and Pygame 2.0.1. Earlier versions of Python should be okay as long as they’re supported by Pygame, but Pygame has changed a fair amount in 2.0, so make sure you’re using the same major release.

01:25 A lot of coders I’ve met got their love of computers through a love of video games. Back in the days of the dinosaurs—they were called mainframes at the time—my friends and I would get magazines—kind of like email but on glossy paper—with code listings for games.

01:40 We would type the listings in, often not understanding what the code was doing, but the end result was a donkey that got hit by cars if you weren’t fast enough. And although the technology has changed, my young nephew is learning to code right now because he wants to create a Rogue-like. Much of the challenge of writing games is dealing with graphics and sound, both things that can be very platform-specific. Pygame is a 2D game library that abstracts away some of these problems. You won’t write the next mind-blowing awesome AAA hit with Pygame, but it is a great way to learn how to code games with a pretty acceptable learning curve. In fact, you can get simple games up and running fairly quickly. With a little practice, you can write a new game in an afternoon or a weekend.

02:25 In the next lesson, I’ll show you the basics of Pygame and how to get started blitting bits.

Become a Member to join the conversation.