Asteroids (Part 2)
Now that we have a functioning space ship, we'll add some asteroids to our game. To do that, we'll write an asteroid class that
extends Floater. You may find slides of the Asteroids Parts 2 and 3 helpful in completing this assignment.
Suggested steps to writing an Asteroid class
- Create a new tab in your
AsteroidsGameprogram. Name the new tab file
- Write an
extends Floaterin your Asteroid.pde file. You will need to write the following members and label them appropriately (
- a member variable to hold the speed of rotation for each asteroid
- a constructor to initialize all 10 variables (the speed of rotation plus the 9 inherited variables from
move()method that also
turns (rotates) each Asteroid at its own speed
- "getter" (accessor) functions for
- On line 14 of
Asteroid.pdeto the list of files in
data-processing-sources. The canvas tag should now look like
<canvas id="AsteroidsGame" data-processing-sources="Asteroid.pde AsteroidsGame.pde Floater.pde Spaceship.pde Star.pde"> </canvas>. Now choose File | Save.
- Now add just a single asteroid to your program. Start by just calling the Asteroid's
show()function. Make sure you can see it and are happy with its shape before going to the next step.
- Now add the code that moves and rotates the Asteroid. The best way to do this is to override the inherited
move()function so that asteroids rotate as they move.
An array probably isn't the best way to keep track of a bunch of asteroids. Arrays have a fixed size. You can't easily add or remove asteroids from an array as they are destroyed or created. A better choice might be an
ArrayList class has a number of useful member methods that you are expected to how how to use on the AP exam:
boolean add(Object x)
void add(int index, Object element)
Object get(int index)
Object remove(int index)
Object set(int index, Object x)
Suggested steps to adding an
ArrayList of Asteroids and finishing the project
- Create an
Asteroid. You may find the ArrayList slide presentation helpful.
- Now we'll modify the program so that when our space ship strikes an asteroid, the asteroid is removed from the
ArrayList. Everytime an asteroid moves find the distance between that asteroid and the ship. Use processing's
dist()function to find the distance between that asteroid and the ship. If the distance is less than 20 (or whatever value is appropriate for your game) remove the asteroid from the ArrayList. Otherwise, move and rotate the asteroid normally. See slide 47 of the Asteroids Parts 2 and 3 presentation for an example.
Asteroid.pdeto your GitHub website. See slides 18 & 19 of the Asteroids Parts 2 and 3 presentation for two different ways to do this.
- Submit the same URL for your AsteroidsGame that you submitted for the previous assignment to Google Classroom.