Asteroids (Part 1)
In this assignment we will start to replicate the old video game Asteroids. You will write a program that has a space ship that can be controlled with the keyboard. You will need to write a
Spaceship class. Your
Spaceship class will extend the
Floater class, an
abstract class that represents all things that float in space.
abstract class is an "incomplete" class. It has
abstract methods--incomplete methods that have no body. When your class extends an
abstract class, you typically write the methods that are needed to "complete" the class. Note: To complete this assignment you will be writing two classes
Star. Do not modify the
Suggested steps to complete this assignment
- Fork this repository.
- Open the program by opening
AsteroidsGame.pdein Processing or opening the AsteroidsGame folder in Sublime.
- Uncomment the
extends floateron Line 1 of
- Complete the 10 abstract
getfunctions that the
Spaceshipclass inherits from
- Write the
Spaceshipconstructor. Make sure you initialize all 9 of the inherited
protectedmember variables. You may find slides #1 - 58 on the Asteroids PowerPoint and the Spaceship design worksheet helpful. You may also find this sample Spaceship program helpful in understanding how the
protected Floatervariables affect the Spaceship's movement.
- At the top of
AsteroidsGame.pde, declare a variable of type
- Initialize the
Spaceshipas a new instance of the class
AsteroidsGame.pdecall the Spaceship's
- When you are happy with appearance of the Spaceship, add a
public void keyPressed()function in
- Write code in
keyPressedthat allows you to control the spaceship with the keyboard. You must include the ability to turn left, turn right, accelerate, and enter "hyperspace." (There is no requirement for any fancy visual effects, hyperspace just needs to stop the ship, and give it a new random position and direction.)
- Add code to the
- Finish the
- Finally, add code to
AsteroidsGame.pdethat declares and initializes an instance of the
Starsclass to create a random number of stars in random positions
- Note that for full credit, you MUST include instructions on how to operate your Spaceship in the
- OPTIONAL: If you have extra time and are looking for a challenge, you might try to add an animation of "rockets" that appear from the back of the ship when you accelerate, simliar to the this sample Spaceship program. The best way to do this is to override
show()by copying the
show()function from Floater into your Spaceship class. Then add an
ifstatement in your Spaceship
show()function right after
endShape(CLOSE);. If your rockets are firing, draw additional shapes just behind your Spaceship. You can sketch out the shapes on graph paper with the ship centered at (0,0) and pointing right. The
show()function will rotate and translate the rocket shapes to the correct position on the screen.
These steps are only a suggestion. Your Asteroids game doesn't have to work or act like any other. Have fun and be creative.
Some important things to keep in mind
- Abstract classes are "unfinished"
- Abstract classes are one way that programmers can collaborate and divide up the work of a large program
- You're collaborating! Some of the work for the
Shipclass has already been done in the
Floaterclass. Don't change it! Your job is to extend the
Floaterclass to "build on top of it" to make a
- To create the
Shipclass you need to write a constructor and finish the "unfinished"
- When you are sketching out your ship on th Spaceship design worksheet make sure the ship is centered at (0,0) and pointing to the right
- Don't declare any duplicate variables in your
Shipclass. You are inheriting all the variables you need from
- Make sure your constructor initializes all 9 of the
protectedvariables it inherits from
Samples of Student Work
This assignment was selected as a "Nifty CS Assignment" in 2008 by Nick Parlante @ Stanford