So far we have covered the fundamentals of coding but we have not gone into detail about the "best practices" surrounding programming. Understanding the best practices is critical to good programming design. Probably the best way to go about this is to follow the step-by-step coding of a program that brings together multiple classes and units. To this end, in this chapter we will design a classic 2D spaceship/alien invaders game which we will call Starfighter. Programming this game will require you to use almost all of the skills we have learned so far in this book. When you complete this case study you will be well on your way to designing your own basic games.
A project directory has already been created for Starfighter in Unit1. In this directory are the project files and a folder called Images that contain two possible images that could be used in the game (more images can be used or different ones can also be used but these are provided as optional images). These images depcit a starship (the main character) and enemy starships. Any defenses will be created using the flare graphics we have implemented in previous chapters.
Here are the basic requirements of the program. First, the main ship will only move either left or right across the bottom of the screen using the left or right arrow keys. Enemies will always fall from the top of the screen toward the bottom. Points are gained each time an enemy ship is hit. There are 4 different types of defenses (you may add more later if you like): Blaster, Dual Blaster, Detonator, and Spray. The up and down arrows toggle among defenses. Spacebar fires the currently selected defense. Keep in mind these are only the minimum requirements. After you complete this chapter you can continue to do whatever you like with the game.
We will have many units and many classes in this program. In order to keep things organized, it is strongly recommended that you group related units into folders. When we create a new folder we will have to tell the compiler where to find it by going to Project -> Compiler Options and adding to the list of "Other Unit Files". Each folder name can simply be added to the list using semicolons (;) as separators. In programming this is known as setting the path and is common across all languages.
Before we get started, the next section reviews debugging techniques. Even though this chapter provides step-by-step solutions to each problem, you should definitely force yourself to try and code a solution to each sub-problem on your own. That way, when you see the solution provided you can compare it to your own. For this, you will definitely need to hone your debugging skills so that you can identify errors in your own code.