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, 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.
- Fork this repository.
- Open the program by opening
AsteroidsGame.pde
in Processing or opening the AsteroidsGame folder in Sublime. - Uncomment the
extends floater
on Line 1 ofSpaceship.pde
- Write the
Spaceship
constructor. Make sure you initialize all 9 of the inheritedprotected
member variables. You may find slides #1 - 58 on the Asteroids and the Spaceship design worksheet helpful. You may also find this sample Spaceship program helpful in understanding how theprotected Floater
variables affect the Spaceship's movement. - At the top of
AsteroidsGame.pde
, declare a variable of typeSpaceship
- Initialize the
Spaceship
as a new instance of the class - In
draw()
inAsteroidsGame.pde
call the Spaceship'sshow()
function - When you are happy with appearance of the Spaceship, add a
public void keyPressed()
function inAsteroidsGame.pde
- Write code in
keyPressed
that 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.pde
tomove()
the Spaceship - Finish the
Star
class inStar.pde
- Finally, add code to
AsteroidsGame.pde
that declares and initializes an array of instances of theStar
class 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.html
file - 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 theshow()
function from Floater into your Spaceship class. Then add anif
statement 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.
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're collaborating! Most of the work for the
Spaceship
class has already been done in theFloater
class. Don't change it! Your job is to extend theFloater
class to "build on top of it" to make aSpaceship
class. - To create the
Spaceship
class you need to write a constructor. - When you are sketching out your ship on the 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
Spaceship
class. You are inheriting all the variables you need fromFloater
- Make sure your
Spaceship
constructor initializes all 9 of theprotected
variables it inherits fromFloater
Marvin
Timmy
David
Marc
Alexis
Alan
Jack
Erica
Wilson
Elton
Kenneth
Hannah
Joshua
Steven
Silas
Ben
Sam
Karen
Andrew
Thanawat
Jenna
Katie
Michael
Olivia
Eric
Joanna
Emily
Kirby
Dean
Ben
Maxwell
Andrea
Yev
Garvin
Aaron
Michael
Jenny
Erica
Edmund
Schuyler
Bryan
Emma
Kenny
Brandon
Nicholas
Raymond
Nathan
Steven
Brandon
Preston
Tatiana
Karen
Kyle
Michelle
Jayden
Kevin
Kyle
Lydia
Jenna
Otto
Brandon
Andrew
Darya
Felix
Elton
Robert
Skyler
Desmond
Amanda
Eric
Hannah
Kendra
Colin
Edmund
Andrew
Winfield
Jun
Steven
Conna
Hannah
Wilsom
Bryce
Eric
Maxwell
Kirby
Garvin
Aaron
Joshua
Sam
Otto
Steven
Makoi
Brandon
Kenneth
Sophie
Nicholas
Jessica
Kenny
Vivian
Conna
Janet
Emma
Katie
Nghi
Bryan
Erica
Joanna
Jonathan
Derek
Mi-Kaela
This assignment was selected as a "Nifty CS Assignment" in 2008 by Nick Parlante @ Stanford
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
.
- Create a new
Asteroid.pde
file in yourAsteroidsGame
folder. One way to do this is in Sublime is to choose New | New File and then choose File | Save as and name your fileAsteroid.pde
. - Write an
Asteroid
class thatextends Floater
in your Asteroid.pde file. You will need to write a constructor and amove()
- On line 14 of
index.html
addAsteroid.pde
to the list of files indata-processing-sources
. The canvas tag should now look like<canvas id="AsteroidsGame" data-processing-sources="Asteroid.pde AsteroidsGame.pde Floater.pde Spaceship.pde Stars.pde"> </canvas>
. Now choose File | Save. - Add a
int
member variable of theAsteroid
class. It will hold the speed of rotation for each asteroid. Make sure that this is initialized to have an equal probablility of being positive or negative. Also make sure to declare it appropriately (should it bepublic
orprivate
?) - "Override" the
move()
method of the Floater class by writing a newmove()
method in the Asteroid class that alsoturn
s (rotates) each Asteroid at its own speed - 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
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
. The ArrayList
class has a number of useful member methods:
boolean add(Object x)
void add(int index, Object element)
Object get(int index)
Object remove(int index)
Object set(int index, Object x)
int size()
- Create an
ArrayList
of typeAsteroid
. 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'sdist()
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 - Submit the same URL for your AsteroidsGame that you submitted for the previous assignment to Google Classroom.
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 any other. Feel free to modify it in any way you wish.
-
Create a new Bullet.pde file in your AsteroidsGame folder. One way to do this is in Sublime is to choose New | New File and then choose File | Save as and name your file
Bullet.pde
. -
On line 14 of
index.html
addBullet.pde
to the list of files in thedata-processing-sources
and choose File | Save. -
Write a
Bullet
class thatextends Floater
inBullet.pde
. -
Write a constructor that takes one ship argument:
Bullet(Spaceship theShip)
This constructor will use thepublic
"getter" (accessor) functions of theSpaceship
class to:- Intialize
myCenterX
andmyCenterY
of the bullet to be the same as the ship. - Initialize
myPointDirection
of the bullet to be the same as the direction the ship is pointing. - convert
myPointDirectio
n to radians with the following code:double dRadians =myPointDirection*(Math.PI/180);
- Initialize
myDirectionX
as5 * Math.cos(dRadians) +
the directionX of the ship - Initialize
myDirectionY
as5 * Math.sin(dRadians) +
the directionY of the ship
- Intialize
-
Override the
show()
method of theFloater
class so that you can use circular bullets -
Now, add just one bullet to your program. First, just draw it to the screen. Make sure you can see it before continuing to the next step.
-
Now, move the bullet.
-
Now create an
ArrayList
of Bullets. The list should be empty to start with. Everytime you press the key to "shoot", add anew Bullet
to theArrayList
. Modify the program with loops that draw and move all the bullets in the ArrayList -
One way to check for collisions between the bullets and the Asteroids is to write a loop within a loop.
- 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
then usebreak;
to stop the loop to prevent any index out of bounds execptions.
-
Submit the same URL for your AsteroidsGame that you submitted for the three previous assignments to the school loop drop box.
If you have extra time, you might add some extra features to your Asteroids game. Have fun and be creative!
- Randomly shaped Asteroids
- Different types of weapons besides bullets
- Have two Asteroids classes, one large and one small. When a large Asteroid is removed from the ArrayList add two small ones with the same x and y
- Add a second ship (UFO) that appears after a while and shoots at the space ship
- Keep track of the score and/or the health of the ship
- Save the score or health to save the player's progress
- Andrew Ong put a video up of his Asteroids Game from several years ago which has a number of amazing features.