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.

Remove Dependency on easygui

Diff of these changes: Removed easygui requirement and changed image & sound file paths

The first change I’m going to make is to get rid of the use of the easygui module. The game uses this just to display a message box that displays some credits. Without this call to easygui.msgbox(), the fewer additional modules the user will need to run this game (and the more likely people will play it).

I’ve also put all the image files under their own folder named “images”, so I’ll have to change the lines of code that load these to include the new path.

Add Spaces for Readability

Diff of these changes: Renamed SpellIcon class. Added spaces after +, -, and * operators.

As a general cleanup of the code to make it more readable, I prefer a single space after commas in the source code so that it is not bunched together. Using the find-and-replace feature in my text editor, I replace all commas with a comma and space. Diff of these changes: Added spaces after all the commas

Page 1 of 4 | Next page