Text of realpython.com › asteroids-game-python as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Build an Asteroids Game With Python and Pygame – Real Python

Build an Asteroids Game With Python and Pygame by Paweł Fertyk Updated Mar 22, 2021 Reading time estimate 1h 6m intermediate gamedev projects Table of Contents Remove ads Recommended Course Using Pygame to Build an Asteroids Game in Python (2h 9m) 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 tutorial, 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 Click the link below to download the code for this project and follow along as you build your game: Get the Source Code: Click here to get the source code you’ll use to build an Asteroids game in Python with Pygame in this tutorial.

Let’s get started! Demo: Asteroids Game in Python The game you’ll be making is a clone of the classic arcade game Asteroids . In it, you control a spaceship and shoot asteroids. If your spaceship collides with an asteroid, you lose. If you shoot down all asteroids, you win! Remove ads Project Overview Your Asteroids game in Python will feature a single spaceship. The spaceship can rotate left and right as well as accelerate forward.

When it’s not accelerating, it will continue moving with the velocity it had. The spaceship can also shoot bullets. The game will use the following key mappings: Key Action Right Rotate the spaceship right Left Rotate the spaceship left Up Accelerate the spaceship forward Space Shoot Esc Exit the game There will also be six big asteroids in the game. When a bullet hits a big asteroid, it will split into two medium ones.

When a bullet hits a medium asteroid, it will split into two small ones. A small asteroid won’t split but will be destroyed by a bullet. When an asteroid collides with the spaceship, the spaceship will be destroyed, and the game will end in a defeat. When all asteroids are gone, the game will end in a victory! The project will be broken into ten steps: Setting up Pygame for a Python project Handing input in the game Loading images and showing them on the screen Creating game objects with an image, a position, and some logic Moving the spaceship Moving the asteroids and detecting collisions with the spaceship Shooting bullets and destroying asteroids Splitting asteroids into smaller ones Playing sounds Handling the end of the game Each step will provide links to all necessary resources.

Prerequisites To build your Asteroids game, you’ll need some more advanced elements of Python. You should already be comfortable with the language itself as well as with concepts like classes, inheritance, and callbacks. If you need to refresh your knowledge on these topics, then check our Object-Oriented Programming (OOP) in Python . The game will also use vectors to represent positions and directions, as well as some vector operations to move the elements on the screen.

Pygame will take care of most of the math, and all the necessary concepts will be explained in this tutorial. However, if you want to know more, then you can check out Vector Addition . The Pygame documentation can be useful if you want to understand some concepts in depth, but you’ll find everything you need to know in this tutorial. Step 1: Pygame Setup At the end of this step, you’ll have a small Python project that uses Pygame.

It will display a window with a caption, filled with a blue color. This will be first step toward your Asteroids game. You won’t need any specific game development tools. Your favorite text editor and the command line will be enough. Python Project To organize your project, start by creating a folder for it: Language: Shell $ mkdir awesome_pygame_project $ cd awesome_pygame_project As with any Python project, you should also create a virtual environment for your Asteroids game.

You can read more about virtual environments in Python Virtual Environments: A Primer . When you’re done, create a requirements.txt file and add a Pygame dependency. For this project, it’s recommended that you use the latest version, which will make your Asteroids game work seamlessly on Linux and macOS. Your file should look like this: Language: Python Requirements pygame == 2.0.0 Next, install the dependencies: Language: Shell (venv) $ python -m pip install -r requirements.txt You can check if Pygame was installed correctly by running this command: Language: Shell (venv) $ python -m pygame.examples.aliens If everything went well, then you should see a window with the Pygame Aliens game.

Remove ads Pygame Code Now it’s time to start working on your own code! In general, the structure of a Pygame program looks like this: Language: Python 1 initialize_pygame () 2 3 while True : 4 handle_input () 5 process_game_logic () 6 draw_game_elements () Line 3 starts a loop, called the game loop . Each iteration of this loop generates a single frame of the game and usually performs the following operations: Input handling: Input like pressed buttons, mouse motion, and VR controllers position is gathered and then handled.

Depending on the game, it can cause objects to change their position, create new objects, request the end of the game, and so on. Game logic: This is where most of the game mechanics are implemented. Here, the rules of physics are applied, collisions are detected and handled, artificial intelligence does its job, and so on. This part is also responsible for checking if the player has won or lost the game. Drawing: If the game hasn’t ended yet, then this is where the frame will be drawn on screen.

It will include all the items that are currently in the game and are visible to the player. The general structure of a Pygame program isn’t complicated, and you could probably get away with putting it in a basic loop. However, considering that you might expand your Asteroids game in the future, it’s a good idea to encapsulate all these operations in a Python class . Creating a class means that you need to pick a name for your game, but “Asteroids” is already taken.

How about “Space Rocks”? Create a space_rocks directory, and inside it create a file called game.py . This is where you’ll put the main class of your Asteroids game: SpaceRocks . The file should look like this: Language: Python 1 import pygame 2 3 class SpaceRocks : 4 def __init__ ( self ): 5 self . _init_pygame () 6 self . screen = pygame . display . set_mode (( 800 , 600 )) 7 8 def main_loop ( self ): 9 while True : 10 self . _handle_input () 11 self . _process_game_logic () 12 self . _draw () 13 14 def _init_pygame ( self ): 15 pygame . init () 16 pygame . display . set_caption ( "Space Rocks" ) 17 18 def _handle_input ( self ): 19 pass 20 21 def _process_game_logic ( self ): 22 pass 23 24 def _draw ( self ): 25 self . screen . fill (( 0 , 0 , 255 )) 26 pygame . display . flip () Here’s what’s happening in the code, step by step: Line 1 imports the Pygame module to get access to all its amazing features.

Line 3 creates the SpaceRocks class. Line 4 is the constructor of the SpaceRocks class, and it’s a perfect place to put any methods required to initialize Pygame. The actual Pygame initialization happens in _init_pygame() . You’ll learn more about this method in a moment. Line 6 creates a display surface. Images in Pygame are represented by surfaces . Here are a few things to know about them: Surfaces can be drawn on one another, allowing you to create complex scenes from simple pictures.

There’s one special surface in each Pygame project. That surface represents the screen and is the one that will eventually be displayed to players. All o

The rest of this page is on the live site.