Project - Asteroids
Part 4 - Make some Asteroid objects

Pretty much the whole point of this game is to be shooting at some asteroids that are flying around on the screen, so we need to figure out how to make that happen. We'll be keeping track of all the asteroids on screen using a list of some sort, an Array or ArrayList, maybe?

Before we start trying to figure that out, let's look at another collection of objects that will be slightly simpler to deal with: stars.

4.1. Stars

  1. In our game, stars are just small points of light in the background. They won't move in the course of game, but we'll need to draw them there, and redraw them every time the screen refreshes.
    First question: is a Star a Floater? or is it fundamentally different? What qualities characterize a star? What does a star need to be able to do?
  2. Show/hide conversation on the Star class

    Given that a Floater object moves and rotates, and stars in this game aren't going to do either of those, it probably doesn't make sense for a star to inherit from ("be a subclass of") the Floater class.

    Each Star does have an x-y location, however, and a size, maybe? (Or are they all just tiny pixels?) And each star has a color? or should they all just be white?
    These are all design decisions that you need to make as you build a game, and of course you may decide to make them all white pixels right for now, and change them to color shapes of different sizes later.

    Design Strategies

    Top-Down Design is a design strategy in which one examines the larger-scale challenge/problem/task before digging into the smaller details. In the context of this project, we've given thought to all the elements of the game before digging into the process of building each of those elements.

    Bottom-Up Design strategy involves giving careful consideration to the elements first, and worrying about fitting together all of the pieces later on. For an Asteroids game it would make some sense to think about how to describe the Asteroid class first, and then start thinking about the space they're in, and the ship that's trying to shoot them.

    If you're a single person developing a project, you may find yourself jumping back and forth between these two strategies as you build your project.

    If you're working as part of a larger group, you may find yourself looking at things from the top-down perspective if you're a project lead or manager, and from the bottom-up perspective if you've been assigned a specific part of the project to focus on.

  3. Write the Star class in your project so that a star, when it's constructed, has a random x-y position on the screen. (Use Processing's width and height screen attributes to identify the range of legal numbers for your star location.)
  4. Also in your Star class, write a show() method that uses Processing's ellipse() instruction to draw the star at the given location on the screen.
  5. Your completed Star class should probably look something like this:
  6. Show/hide Star class

    public class Star { // instance variables float x; float y; public Star() { x = random(width); y = random(height); } public void show() { fill(255); // star is white ellipse(x, y, 2, 2); } }
  7. Once the Star class has been written, we're ready to create a collection of stars to be shown in our game. In the AsteroidsGame tab, near the top, declare an array of Star objects.
  8. In the setup() section, initialize the array of stars, and then write a loop that stores a series of Star objects in the Array.
  9. Now that we have an array of stars, let's move into the draw() loop and display them: right after the background(0) instruction, write a loop that runs through and calls the show() method on all of the stars. Now, everytime we run through the draw() loop we'll be redrawing those stars in the background.
  10. Test your stars to make sure they work as intended.

4.2. Asteroids

As is the case with the stars, we're going to have an Asteroid class that allows us to keep track of, and draw, a number of asteroids—we'll have an ArrayList<Asteroid> to help us keep track of those asteroids.

Unlike the stars, these asteroids are going to be moving across the screen, and rotating while they do so. As a result, we'll have them inherit from the Floater class which already has almost everything we need defined for us.

Let's go!

  1. Because we're inheriting from Floater, the Asteroid class will need to initialize those nine variables: int corners, int[] xCorners, int[] yCorners, int myColor, double myCenterX, double myCenterY, double myXspeed, double myYspeed, and double myPointDirection.
  2. If you played around with the Asteroids game earlier, you probably noticed that asteroids have an additional characteristic beyond what floaters do: they rotate at some rate. Because this quality isn't described by the superclass ("parent class"), we'll have to define it in the subclass.
  3. As with the Spaceship class, we need to use graph paper to draw an asteroid shape that we'll use for describing our asteroid objects on screen. Later on in the project you may want to draw multiple asteroid shapes that can be randomly selected from, or even develop an AsteroidGenerator that generates asteroid shapes according to an algorithm you've developed. For development purposes, let's just create one asteroid shape and use that for all of our asteroids.
  4. This one is mine, you make your own

  5. With all of this in mind, let's try writing the Asteroid() class.
  6. Show/hide Asteroid class

    class Asteroid extends Floater { // instance variables private float rotationSpeed; /** * Constructs a new Asteroid */ Asteroid() { corners = 10; xCorners = new int[] {3*14, 3*10, 3*6, 3*4, 3*-10, 3*-12, 3*-6, 3*-4, 3*0, 3*6}; yCorners = new int[] {3*0, 3*10, 3*10, 3*14, 3*10, 3*0, 3*-6, 3*-10, 3*-10, 3*-12}; myColor = 240; myCenterX = random(width); myCenterY = random(height); myXspeed = random(3) - 1.5; myYspeed = random(3) - 1.5; myPointDirection = random(360); rotationSpeed = random(4) - 2; } /** * Overrides the Floater's move method */ public void move() { turn(rotationSpeed); super.move(); } }
  7. Now, as we did with the Star class, let's:
    1. in the AsteroidsGame tab, declare an ArrayList variable that will store store asteroids
    2. use a loop in the setup() method to initialize that ArrayList with a series of randomized Asteroid objects
    3. use a loop in the draw() method to move those asteroids every time the draw() method repeats
  8. Test the game and see if you have random asteroids floating through space!

We can't interact with these asteroids, though. Let's figure out how to do that!

Next...

Part 5 - Shoot the Asteroids