Chapter 18. Introduction to Design Patterns and much more!

Since the second project, we have been using objects. You might have noticed that many of the objects have things in common. Things like variables for speed and direction, a RectF for handling collisions and more besides.

As our objects have more in common we should start taking advantage of OOP, inheritance, polymorphism and a concept we will now introduce design patterns.

Inheritance, polymorphism and design patterns will enable us to fashion a suitable hierarchy to try and avoid writing duplicate code and avoid sprawling classes with hundreds of lines. This type of disorganised code is hard to read, debug or extend. The bigger the game project and the more object types, the more of a problem this would become.

This project and the next will explore many ways that we can structure our Java code to make our code efficient, reusable and less buggy. When we write code to a specific, previously devised solution/structure we are using a design pattern.

Don't worry, in the remaining chapters we will also be finding out about more game development techniques to write better, more advanced games. In this chapter, we will start the Scrolling Shooter project.

To begin to achieve the Scrolling Shooter project, in this chapter we will do the following:

  • Introduce the Scrolling Shooter project
  • Discuss the structure of the Scrolling Shooter project
  • Manage the game's state with a GameState class
  • Code our first interface to communicate between different classes
  • Learn how to persist high scores even when the game is over, and the device has been turned off
  • Code a class called SoundEngine that will enable different parts of the project to trigger sound effects to play
  • Code a HUD class to control the drawing and position of text and control buttons
  • Code a Renderer class that will handle drawing to the screen

Tip

From this chapter on I will be helping the environment by not mentioning when you need to import a new class. If you are using a class from another package (any class provided by Android or Java) you need to add the appropriate import, either by copying it from the pages of this book, typing it manually or highlighting the class with an error and using the ALT | ENTER key combination.

Let's see what the Scrolling Shooter will do when we have finished it by the end of Chapter 21, Completing the Scrolling Shooter Game.

Introducing the Scrolling Shooter project

The player's objective in this game is simply to destroy as many aliens as possible. Let's go in to some more details about the features the game will have. Look at the starting screen for the Scrolling Shooter project in the next image:

Introducing the Scrolling Shooter project

You can see there is a background of silhouetted skyscrapers. This background will smoothly and speedily scroll in the direction the player is flying. The player can fly left or right, and the background will scroll accordingly. However, the player cannot stay still horizontally. You can see that when the game is over (or just been launched by the player) you can see the message Press Play.

I have numbered some items of interest in the previous image, let's run through them:

  • There is a high score feature and for the first time, we will make the high score persistent. When the player quits the game and restarts later the high score will still be there. The player can even turn their device off and come back the next day to see their high score. Below the high score is the current score and below that is the number of lives remaining before the end of the game. The player will get three lives which are exceptionally easy to lose. When they lose the third life the Press Play screen will be displayed again and if a new high score is achieved then the high score will be updated.
  • The grey rectangles in the three remaining corners of the screen are the buttons which control the game. Number 2 in the image is the play/pause button. Press it on the start screen and the game will begin, press it while the game is playing, and the pause screen will be shown (see next image).
  • The two rectangles at the corner marked 3 are for shooting and flipping. The top button shoots a laser and the bottom button changes the horizontal direction that the ship is headed in.
  • The buttons at the corner labeled 4 are for flying up and down to avoid enemy ships and lasers or to line up for taking a shot.
Introducing the Scrolling Shooter project

This next image shows a particle effect explosion. These occur when an enemy ship is hit by one of the player's lasers. I opted to not create a particle effect when the player is destroyed because refocussing on the ship after a death is quite important and a particle effect distracts from this.

Introducing the Scrolling Shooter project

This next image (which wasn't easy to capture) shows almost every game object in action. The only missing item is the enemy lasers which are the same in appearance as the player's lasers except they are red instead of green. The wide range of enemies is one of the features of this game. We will have three different enemies with different appearances, properties, and even different behaviors.

How we handle this complexity without our code turning into a maze of spaghetti-like text will be one of the key learning points. We will use some design patterns to achieve this.

Examine the next image and then look at the brief explanation for each object:

Introducing the Scrolling Shooter project
  • Label 1: This type of alien ship is a called a Diver. They spawn just out of sight at the top of the screen and dive down randomly with the objective of crashing into the player and taking one of his lives. We will create the illusion of having loads of these divers by respawning them again each time they are either destroyed (by being shot or crashing into the player) or they pass harmlessly off the bottom of the screen.
  • Label 2: The alien (actually there is two in the image) labeled as number two is a Chaser. It will constantly try and home in on the player both vertically and horizontally then take a shot with a laser. The player is slightly faster than a Chaser, so he can outrun them but at some point, he will need to flip directions and shoot them down. In addition, the player will not be able to outrun the enemy lasers. When a Chaser is destroyed they will randomly respawn off-screen to the left or right and begin chasing all over again.
  • Label 3: The object at number three is the player's ship. We already have discussed what it can do.
  • Label 4: This alien is a Patroller. It flies left to right, up and down and turns around and flies right to left when it reaches a predetermined distance from the player. These ships make no attempt to home in on the player, but they will frequently get in the way or fire a laser when in a good position with a chance to hit the player.
  • Label 5: This is the green player laser. The player will have enough lasers to create a satisfying rapid-fire effect but not so many that he can simply spam the fire button and be invincible.

Perhaps surprisingly, this project will have only a few new specifically Java lessons in it. What is going to most notably new is how we structure our code to make all this work. So, let's talk about that now.

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

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