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.
Now take a look at 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.