Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Using Pygame to Build an Asteroids Game (Summary)

Congratulations, you just built a clone of the Asteroids game using Python! With Pygame, your Python knowledge can be directly translated into game development projects.

In this course, you’ve learned how to:

  • Load images and display them on the screen
  • Add input handling to your game
  • Implement game logic and collision detection in Python
  • Play sounds
  • Display text on the screen

For more information on concepts covered in this lesson, you can check out:


Sample Code (.zip)

6.3 MB


Course Slides (.pdf)

900.2 KB

00:00 In the previous lesson, I showed you how to add text to the screen. In this lesson, I’ll wrap up the course.

00:07 This course has been all about creating your first game in Python using the Pygame library. By building an Asteroids clone called Space Rocks, you’ve learned about game loops and the main Pygame library methods, what sprites are and how to blit them, rotate them, and move them around.

00:25 You’ve also learned how to respond to keyboard input, how to play sounds, all about loading fonts and displaying text, and general game writing awesomeness.

00:37 Here are some ideas you can try to improve your game even further. In the lesson where ship collisions were added, I pointed out that using None for a destroyed ship meant there was a danger of crashing your code. I walked you through fixing a couple of spots but left one out. If you didn’t find it, I’ll tell you now. Fire the gun after the ship is destroyed, and the code will crash.

00:59 The simple way would be adding a None check inside the keyboard handler that fires the gun. Or, the better long-term way, that involves adding an alive and dead state to the ship class.

01:12 You would then modify the class further to behave correctly after the ship is in the dead state. This method takes a bit more work, but is probably the better solution, particularly if you’re going to… add more lives to the game!

01:27 This can be about more than just allowing the player to restart three times. You can also add a "press space to start" message with the game paused after each time the player respawns. Right now, it is possible if you accelerate in just the right way, for the ship to overtake the bullets. If you manage this, you’ll discover that these are magic bullets that only destroy rocks.

01:48 You could add some code to make them fatal to the ship.

01:52 Or, what’s a game without a score? Add something to track the score and display scoring text. You’ll need to update print_text() to do this, as right now, it only displays text as a centered message on the screen.

02:05 You can also add a play again key. When the game is done, the player should be able to play again without having to close the window and restart the program. And finally, if you want a real challenge, add an instant replay movie. Show the player what happened and how they lost.

02:22 This one could get messy. You could store all the positions of all the objects in order to create the new frames for, say, the last three seconds. Or, in a more memory-intensive version, you could store copies of the frames in buffers. When a user dies or wins, you then create a playback using the position information or the buffers.

02:40 Of course, you’d also want the player to be able to skip past this playback and get on with the game. This one could be fun to code.

02:49 Here are two great sources for more Pygame goodness. This is a link to the Pygame documentation, which is a great way to find all the ins and outs of writing Pygames.

02:59 And this is Christopher Bailey’s course on writing a 2D side-scroller in Pygame. It covers some of the same basics as this course in more detail, and it also talks about sprite groups, playing background music, and more.

03:13 That’s it for Space Rocks. I hope you enjoyed the course. Thanks for your attention. Leave a comment below bragging about any neat features you’ve added to your version of the game and challenge your fellow Pythonistas.

Become a Member to join the conversation.