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 by a player. You will need to write a Spaceship class as well as a Star class for the background. Your Spaceship class will extend the Floater class, a class that represents all things that float in space. Note: To complete this assignment you will be writing two classes Spaceship and Star. Do not modify the Floater class. You may find the Asteroids Part 1 slide presentation here, or here helpful in completing this assignment.
- Start a new program in Processing called
AsteroidsGame. - Copy the code in
AsteroidsGame.pdeinto your program. - Open a new tab and name it
Spaceship.pde. Copy the class definition fromSpaceship.pdeabove. Do the same forFloater.pdeandStar.pde. - Write the
Spaceshipconstructor. Make sure you initialize all 9 of the inheritedprotectedmember variables. - You may also find the Asteroids Part 2 slide presentation here useful, or here.
- In addition, 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 typeSpaceship - Initialize the
Spaceshipas a new instance of the class - In
draw()inAsteroidsGame.pdecall the Spaceship'sshow()function - When you are happy with appearance of the Spaceship, add a
public void keyPressed()function inAsteroidsGame.pde - 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
draw()inAsteroidsGame.pdetomove()the Spaceship - Finish the
Starclass inStar.pde - Finally, add code to
AsteroidsGame.pdethat declares and initializes an array of instances of theStarclass to create a number of stars in random positions - Note that for full credit, you MUST include instructions on how to operate your Spaceship in the
index.htmlfile. For an example look at slides 32 & 33 in the Asteroids slide presentation here, or here
These steps are only a suggestion. Your Asteroids game doesn't have to work or act like any other. Have fun and be creative.
- You can smooth out the control of the ship using booleans for each key press. There is an example here
- 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 theshow()function from Floater into your Spaceship class. Then add anifstatement in your Spaceshipshow()function right afterendShape(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. Theshow()function will rotate and translate the rocket shapes to the correct position on the screen.
- You're collaborating with me (chandru) and with your Paired Programming partner! Most of the work for the
Spaceshipclass has already been done in the parentFloaterclass by me (your 3rd partner!). - In a real work environment, the
Floaterclass would have been given to you as a Library (binary) and you would not be able to change it. - In this case you will be able to change it, but don't modify it! I will test it with the original version of the
Floaterclass to verify. Your job is to extend theFloaterclass to "build on top of it" to make aSpaceshipclass. - To create the
Spaceshipclass you need to write a constructor. - Don't declare any duplicate variables in your
Spaceshipclass. You are inheriting all the variables you need fromFloater - Make sure your
Spaceshipconstructor initializes all 9 of theprotectedvariables it inherits fromFloater