Source Code Makeover: Square Shooter, Part 1

UPDATE: Felix, the original author of Square Shooter, has made some comments in the comment section that are pretty insightful. I recommend you check them out.

In this blog post, I’m taking a game off of Pygame.org and going through it to make it more readable and extend its functionality. You’ll see an example of how to take code that works and changes to improve it’s design (but more importantly, I explain why I make those changes). This is an intermediate level tutorial and assumes some familiarity with Python. This can be pretty helpful if you know programming basics but want to know, “How can I write better code?”

(Part 2 and Part 3 are now up as well.)

This blog post covers Square Shooter, made by Felix Pleșoianu. You can download the original source or view my changes on GitHub.

I only check in working code, so you can download the code at any point in history and see how it looks. I make a lot more check ins than I normally would, but I want you to see the gradual changes that I make.

(UPDATE: A note to everyone who mentions that I break PEP-8, see this link. Also, see this link.)

Let’s play it. You’ll need to have Python (a version 2, not version 3) and Pygame installed on your computer to run the square-shooter_original.py file. It looks like a standard Asteroids clone. I notice that the circles dont wrap around the map until the center of the circle goes past the edge. Also, the bullets don’t wrap around either, they just stop once they get to the edge.

the original source code. Try to get an idea of what each of the parts do, but you don’t need to fully understand everything before continuing. (I didn’t understand all the code when I started cleaning it up.)

First we’ll do just a code clean up, and then we’ll look at changing the the game to improve play and user experience.

The computer doesn’t care what your code looks like. It will run the most unreadable code just fine. But the purpose of cleaning up code to be readable is so that it is easier to understand for humans so they can easily change code either for new features or bug fixes. This becomes especially important if multiple people are working on the same source code.

Also keep in mind that “readable” is a bit subjective. The changes I make here aren’t necessarily the changes someone else would make. Feel free to leave your own suggestives in the blog post comments.

First I’ll take a rough look through the original code. Just a note, all line numbers in this tutorial refer to the original program’s line numbers, not the line numbers of my makeover program.

There are very few comments in this script (though I’m bad about this also.) Much of this makeover will be adding comments and documentation.

We’ll start at the top of the program and just move down. Each time I make some changes, I constantly retest the program to make sure my changes don’t cause any new bugs.

Spacing and removing duplicating variables

(These changes can be seen in the github history.)

Page 1 of 8 | Next page