O'Reilly logo

Android Game Programming by Example by John Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Drawing and moving the asteroids

At last, we will add our cool, spinning asteroids. First, we will look at the constructor that is fairly similar to the other game object constructors, except that we set the world location randomly. However, take a little extra care not to spawn them in the center of the map, where the spaceship starts the game.

Create a new class called Asteroid and add this constructor. Note that we have not defined any vertices. We delegate this to the generatePoints method that we will see soon.

public class Asteroid extends GameObject{ PointF[] points; public Asteroid(int levelNumber, int mapWidth, int mapHeight){ super(); // set a random rotation rate in degrees per second Random r = new Random(); setRotationRate(r.nextInt(50 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required