Book Image

Android Game Programming by Example

By : John Horton
Book Image

Android Game Programming by Example

By: John Horton

Overview of this book

Table of Contents (18 chapters)
Android Game Programming by Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
7
Platformer – Guns, Life, Money, and the Enemy
Index

Iteration


How can we make our game better and more playable? Let's look at a number of possibilities and then go ahead and implement them.

Multiple different enemy graphics

Let's make the enemies a bit more interesting by adding a few more graphics to the game. First, we need to add the extra graphics to the project. Copy and paste enemy2.png and enemy3.png from the Chapter4/drawables folder of the download bundle into the drawables folder in Android Studio.

enemy2 and enemy3

Now, we just need to amend the EnemyShip constructor. This code generates a random number between 0 and 2, and then switches to load a different enemy bitmap accordingly. Our completed constructor now looks like this:

// Constructor
public EnemyShip(Context context, int screenX, int screenY){
    Random generator = new Random();
    int whichBitmap = generator.nextInt(3);
    switch (whichBitmap){
        case 0:
            bitmap = BitmapFactory.decodeResource
            (context.getResources(), R.drawable.enemy3);
 ...