Posts categorized “Source Code Makeover”.

Source Code Makeover: Demon Kingdom, Part 2

This is a continuation of Part 1. I make a lot of the same changes as before, mostly removing duplicate code.
More... »

Source Code Makeover: Demon Kingdom, Part 1

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. 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?" (And because someone always brings it up, I have this disclaimer: Of course, these changes are my own subjective idea of "better" and not necessarily changes that another developer would make.)

Demon Kingdom by Logi540 is a defense game where monsters walk from the left side of the screen. The player needs to attack them by clicking on them, and can pick up gems to use for spells. If any monster reaches the right side of the screen, the player loses. Download source code.

The main theme of my changes is:

  • Removing duplicate code. Duplicate code is bad because if you have to change it (to add features or fix bugs) you may forget to change it in every duplicated place.
  • Remove magic numbers. Magic numbers are hard-coded integer values which are bad because they don't describe what they represent. A better alternative is to use a constant instead (these are the ALL-CAPS variables).
  • Put sequential variables in lists and use loops. Instead of having variables named like spam1, spam2, spam3, and so on, it's better to have a single list variable, and have a loop run code on each item in the list. This usually leads to a decrease in duplicate code.
  • Get rid of unneeded variables. Removing code reaps tons of benefits: there's less code a programmer has to read and understand, less code that a programmer has to look through when debugging, and less code (usually) means less bugs and "moving parts" that could break.

Here's the inital check in of all the files. The file I'll be modifying is named demonkingdom_makeover.py. This program requires Pygame to be installed to run. I recommend downloading the game, playing it a couple times, and looking through the source code before continuing with this article. For each section, open up the diff link to see what the exact changes I made were.

More... »

Source Code Makeover: Square Shooter, Part 3

This is a continuation from Part 1 and Part 2, where I go through the source code of Square Shooter, an Asteroids clone, and try to redesign the code to be more readable.

Now that I've refactored the code, I'll try adding three new features: multiple bullets, a shotgun powerup and a UI change to show the remaining time for each powerup.

More... »

Source Code Makeover: Square Shooter, Part 2

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.

This is a continuation from Part 1, where I go through the source code of Square Shooter, an Asteroids clone, and try to redesign the code to be more readable. (Part 3 is also up.)

All of the changes I make can be viewed on github. You can also download the original source code for Square Shooter.
More... »

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.

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.

More... »