AP Java Assignment 12
Finishing Asteroids
Press '4' to rotate left, '5' to accelerate, '6' to rotate right, '2' for hyperspace, '8' to shoot
You may need to "click" on the applet to give it the "focus"
Finishing the game
To finish our Asteroids game, we need to write a new class that represents Bullets. We will store the Bullets in an
ArrayList much like we did with the Asteroids. Once we can shoot the bullets and destroy Asteroids, we will
have a working game. Your Asteroids game doesn't have to look like the one above. Feel free to modify it in any way you
wish.
Steps to completing this assignment
- Create a duplicate of your previous assignment by following steps #1 - 6 again, this time saving your
class as ApJava12.java
- Write a
Bullet class that extends Floater. You will need to:
- Write a constructor that takes one argument of type
Bullet(SpaceShip theShip)
- Intialize myCenterX and myCenterY of the bullet to be the same as the ship.
- Initialize myPointDirection of the bullet to be the same as myPointDirection of the ship
- convert myPointDirection to radians with the following code:
double dRadians =myPointDirection*(Math.PI/180);
- Initialize myDirectionX as
(10 * Math.cos(dRadians) + myDirectionX of the ship
- Initialize myDirectionY as
(10 * Math.sin(dRadians) + myDirectionY of the ship
- Override the
draw method of the Floater class so that you can use circular bullets
- Now, add just one bullet to your applet. First, just draw it to the screen. Make sure you can see it before continuing to
the next step.
- Now, move the bullet in the run method.
- Now create an ArrayList of Bullets. The list should be empty to start with. Everytime you press the key to "shoot",
add a new Bullet to the ArrayList. Modify paint and run with loops that draw and move all the bullets in the ArrayList
- To check for collisions between the bullets and the Asteroids, you will need a loop within a loop. Everytime you move
one asteroid you will need:
- a loop that goes through all the bullets to see if there is a collision between that bullet and the asteroid
- if there is a collision remove both the asteroid and the bullet from their
ArrayLists
You will need a web page to display your applet. Your homepage should have a link to the web page for this
assignment.
Submit the URL of your applet with the attached Java code (the .java file(s)) in an email message
to mrsimon@lycos.com.