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:
GameState
classinterface
to communicate between different classesSoundEngine
that will enable different parts of the project to trigger sound effects to playHUD
class to control the drawing and position of text and control buttonsRenderer
class that will handle drawing to the screenFrom 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.
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:
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:
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.
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:
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.
3.144.90.182