C H A P T E R  9

A One-Player Strategy Game

Part 2: Coding the Game

With the framework in place, you can now write the code to create a playable game. The trick here is always to make your code as efficient as possible. When games become more complex and involve more sprites, they can begin to slow as the processor struggles to keep up. You avoid this with some clever techniques that can ease the load.

Keeping the end goal in mind as you progress is also critical because you must have a functioning game before you can add the bells and whistles that differentiate your work from others. In fact, in my experience, knowing when to stop working on a game and release it is always the trickiest part. There is a fine line between a game that is too simple and a game that is unplayable because it’s overloaded with features and additions that a normal user doesn’t have time to learn.

images Note As you progress through the code in this chapter, recall that putting Log.d statements into the code can help clarify what is happening and which functions are being called. Some of the code can be quite complex, and I still use this technique to help me step through the methods, especially if I’m not getting the desired behavior.

Here is a list of the features you must complete in this chapter in order to have a working game:

  • Enhancing the sprite objects
  • Shooting bullets out of the cannons
  • Eliminating boats after they have been hit
  • Restarting the game when boats hit the castle

Some of these—like lowering the health of a boat when a bullet hits it—are easily completed, but others require some thought and clever coding. To simplify your editing, I’ve posted the entire methods for this chapter. This way, you can ensure that your previous work is exactly what is needed for the final game. This also helps you see how each function calls the others and what information is shared among them.

The next section begins with our improvements to SpriteObject.java. You make very few modifications, but the changes that you do make will simplify your work in GameView.java.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.243.64