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

The PlayerShip object


We need to keep the model part of our code as separate as possible from the rest. We can do this by creating a class for our player's spaceship. Let's call our new class PlayerShip.

Go ahead and add a new class to the project, and call it PlayerShip. Here are a few quick steps on how to do that. Now, right-click the folder with our .java files in it and navigate to New | Java Class, then enter PlayerShip as the name and click on OK.

What do we need our PlayerShip class to be able to know about itself? As a bare minimum it needs to:

  • Know where it is on the screen

  • What it looks like

  • How fast it is flying

These requirements suggest a few member variables we can declare. Enter the code just after the class declaration that we generated:

private Bitmap bitmap;
private int x, y;
private int speed = 0;

As usual, use the Alt | Enter keyboard combination to import any missing classes. In the previous block of code, we see that we have declared an object of type Bitmap that we will use...