Source Code Makeover: Demon Kingdom, Part 1

I do some more cleanup by adding spaces around the +, -, and * operators in the source code, as well as capitalizing the spellIcon class. It is a common convention in programming to have class names begin with a capital letter.

Improve the Background Class

Diff of these changes: Improved the Background class.

Next, I make some improvements to the Background class. This class loads and stores all the background images as pygame.Surface objects. Previously the single Background object would be passed an image object with its changeBackground() method. In order to put all the background-loading code in the same place, I had this take place in the constructor method of the class.

The Background constructor is now passed a list of background images, which is loads and stores in its imageList member. A single list variable is always preferable to variables like back2, back3, back4, and so on. Now when changeBackground() is called, we simply pass it the index of the image it should be changed to.

Also, the rect member is updated based on the image when changeBackground() is called. Previously the code assumed that all the background images were the same size and used the rect from the first background for all the backgrounds.

Replace Magic Numbers with Constants

Diff of these changes: Improved Sidebar class. Created numGems global. Added size constants. Renamed Logo class.

Next I want to get rid of the magic numbers used in the source code. These a numbers that appear in the source code that do not have explanations, which can make it hard to figure out what they are used for. Instead we will replace them with constant variables with descriptive names. These include the MAP_WIDTH, MAP_HEIGHT, SIDEBAR_HEIGHT, WINDOW_WIDTH, and WINDOW_HEIGHT variables.

I also rename some variables to be more descriptive (such as changing gems to numGemsCache) and created a new numGems global variable. Normally I don’t advocate using global variables, but in this case the code had placed the gems member in the Sidebar class. This is odd because the Sidebar class should deal with the user interface and displaying game info, not tracking the state of the player. Also, only one Sidebar object is created, effectively making it a global variable anyway. This change also lets me get rid of the addGems() method from the Sidebar class.

I replace several calls to the SpellIcon constructor with a loop (lines 34 to 41 of the original file), and capitalize the name of the Logo class.

Get Rid of the Unneeded load() Method

Diff of these changes: Got rid of first_frame member. Renamed MySprite class. Renamed spellEffect class. Got rid of the load() methods in the AnimatedSprite classes.

Originally each monster is an object of the Monster class, which is a child class of the MySprite class, which itself is a child of Pygame’s pygame.sprite.Sprite class. This is an odd choice to make because a monster is not a sprite image, but rather several things that can include sprite images. However, I’ve found it difficult to break out this inheritance structure, so instead I just renamed MySprite to the more descriptive name AnimatedSprite.

I renamed the color-related variables (white, lightGrey, etc.) to capitalized names to show that they are constants.

Since the load() method in the AnimatedSprite class was always called directly after the constructor, I saw no reason why there should be a separate load() method and moved its code into the constructor.

Also, the first_frame member variable never has a value other than 0, so I got rid of it and simply used 0 in its place. (Though it could be argued that that is a magic number and should be replaced with a constant.)

I capitalized the name of the SpellEffect class.

Make __str__() More Debug-Friendly

Page 2 of 4 | Previous page | Next page