CS 51 Test Program #2
Asteroids
Due: Wednesday, May 2 at 4 PM. Design due: Wednesday April 25, by 11AM.

This document describes what you are to do to complete your final test program. A test program is a laboratory that you complete on your own, without the help of others. It is a form of take-home exam. You may consult your text, your notes, your lab work, the lecture notes, our on-line examples, and other documentation directly available from the course web page, but use of any other source for code is forbidden. If you have problems with the assignment, please see one of the instructors.

Asteroids is a classic arcade game that was released by Atari in 1979. It was an instant hit, and remains popular today (according to wikipedia it's scheduled for release via Xbox Live Arcade for the Xbox 360 in 2007). If you'd like to play the classic asteroids, go to: http://www.80smusiclyrics.com/games.shtml and scroll down until you get to asteroids.

In case you haven't guessed, for your final test program we would like you to write a program that implements Asteroids. To be more precise, we would like you to write a program that implements "almost Asteroids". We have simplified the game considerably from the original in order to make the assignment more reasonable ...however, there is a long list of extra credit points that you can earn by making your game more closely resemble the original.


The figure above shows our Asteroids game after the player has already destroyed 2 asteroids (each worth 10 points). The game begins with a spaceship in the middle of the screen, and several asteroids travelling across the screen. The goal is for the spaceship to shoot the asteroids so that none of them hit the ship.

The asteroids are of different sizes and they move across the screen at different speeds. When an asteroid gets to the edge of the screen it wraps around and reappears at the opposite edge. Unlike the original Asteroids, the ship is fixed in the center of the screen, although it can aim its gun in any direction. If the player clicks on the right-arrow button on the keyboard, the gun rotates clockwise by 15 degrees (=Math.PI/12 radians); if the player clicks on the left-arrow button the gun rotates counterclockwise. If the player clicks on the space button, a bullet is fired from the end of the gun.

Each time a bullet hits an asteroid, the player gets 10 points. The game is over either when an asteroid hits the ship or when the player gets 200 points. In either case, a message is displayed to the user in the score area, indicating "You Won" or "Game Over". In either case, the player should no longer be able to shoot bullets.

Implementation Details

You should begin the game by setting up the sky, a score-keeping mechanism, a ship, and asteroids.

The scorekeeper should display the score at the bottom of the screen. It needs to be able to increase the score when an asteroid is hit.

The ship is blue and consists of a body and a gun protruding from it (in practice, a circle with a line). The ship appears at the center of the screen. It must respond to the player's key clicks. That is, the gun should rotate clockwise and counterclockwise, and it must be able to shoot. If the ship is hit, it should turn grey.

The bullets shot at the asteroids will be active objects. They should move in the direction that they were fired, stopping either when they reach the edge of the screen or when they hit an asteroid.

Each asteroid has its own size, speed, and starting location. You may use the gif file that we provide, or something different. An asteroid should move across the screen appropriately, wrapping around whenever it reaches the edge of the screen. When an asteroid is destroyed, another one should appear at one of the edges of the screen.

Your program will comprise several classes, corresponding to the objects just described.

AsteroidsGame
This class sets up the game by creating an array of Asteroid objects and creating a SpaceShip. It will also accept user input in the form of key clicks. In response to the different key clicks, it should invoke methods of the ship, making the gun rotate or shoot. It is your responsibility to set up the game and to fill in the lines where the ship's methods need to be invoked.

ScoreKeeper
The ScoreKeeper class displays the score on the screen. Note that the asteroids should probably know about the ScoreKeeper, as they will likely need to inform it to increase when they've been shot.

SpaceShip
The SpaceShip class draws the initial ship and provides methods that allow it to react correctly to key presses.

Asteroid
The Asteroid class will extend ActiveObject. Its image is loaded in from a gif file (you are free to use the one that we provide), but each Asteroid should have a randomly chosen speed, direction, and size. After a slight delay, an Asteroid should initially appear at the edges of the screen, then move across the screen (with wrap-around). An Asteroid can hit and destroy the ship, and can be hit and destroyed by a Bullet from the ship.

Bullet
The SpaceShip shoots bullets. A Bullet is an ActiveObject that moves across the screen in the direction that it was fired, stopping either when it reaches the edge of the screen or when it hits an asteroid. Note that to achieve this behavior, each bullet needs to know about the array of asteroids.

One slightly tricky aspect is making the gun rotate appropriately, and having the bullets appear to be fired from the gun. It is easiest to think of this in terms of an angle theta (where theta=0 is directly to the right, and theta=Math.PI/2 is straight up) and to calculate both the endpoint for the line representing the gun and the dx,dy for the bullets from theta. Note that theta is in radians (and that PI radians is equal to 180 degrees).


If the gun has length 1 and has one endpoint at (x,y), the other end should be at (x+cos(theta),y-sin(theta)). (See the figure above.) A shot fired from that gun with speed s should have dx=s*(cos(theta)), dy=s*(-sin(theta)). To calculate sin and cosine in Java, use the Math.sin(double theta) and Math.cos(double theta) methods in the Math class.

The Design

As indicated in the heading of this document, you will need to turn in a design plan for your Asteroids program well before the program itself. This design should be a clear and concise description of your planned organization of the program.

You should include in your design a sketch of each class including the types and names of all instance variables you plan to use, and the headers of all methods you expect to write. You should write a brief description of the purpose/function of each instance variable and method.

In addition, you should provide pseudo-code for any method whose implementation is at all complicated. In particular, if a method is complicated enough that it will invoke other methods you write (rather than just invoking methods provided by Java or our library), then include pseudo-code for the method so that we will see how you expect to use your own methods.

Implementation Order

We strongly encourage you to proceed as suggested below to assure that you can turn in a running program. While a partial program will not receive full credit, a program that does not run at all generally receives a lower grade. Moreover it is easier to debug a program if you know that some parts do run correctly.

  1. Write a program that draws a SpaceShip.

  2. Add code to make the SpaceShip respond to the user's right- and left-arrow key clicks.

  3. Next construct an Asteroid that can move across the screen, wrapping around appropriately, destroying the SpaceShip if it happens to collide with it. As each Asteroid moves it should be asking the ship "Have I hit you?".

  4. Now create an array of Asteroids, each of which moves across the screen at its own speed.

  5. Now allow the ship to shoot Bullet objects. The bullet shot at the asteroids should, as it's moving, be asking them "Have I hit any of you?".

  6. Last, but not least, set up the ScoreKeeper.

There is a great deal of functionality to aim for in this test program. As indicated above, do not worry if you cannot implement all of the functionality. Get as much of it working as you can. At the end of this handout we have included some basic grading guidelines for this test program. You should note the large number of points assigned to issues of style. It is always best to have full functionality, but you are better off having most of the functionality and a beautifully organized program than all of the functionality with a program that is sloppy, poorly commented, etc.

Extensions

Since we have deliberately left out many of the features of the original Asteroids game in our implementation, there are clearly many additional features you could add to your program. In general we will award 1-2 points for each extension, for a maximum of 10 points extra credit. The number of points possible if you do not do any extra credit is 95. Thus you must do some extra credit to have a chance at getting 100 points or more. The following are some suggestions, together with the maximum number of points that will be awarded for each:

Turning it in

Your design should be turned in on paper in class or e-mailed to both professors by 11AM on Wednesday April 25. Keep a copy for yourself since we won't be able to return it to you until after the program is due.

When your work is complete you should deposit in the dropoff folder (note that the deadline is 4PM on Wednesday May 2). Before turning it in, please be sure that your folder contains all of the .java files, .class files, etc. as we can't give credit for anything we don't receive.

Before turning it in, make sure the folder name includes your name and the phrase "TP2". Also make sure to double check your work for correctness, organization and style.

Grading Point Allocations

Value

Feature
Design preparation (20 pts total)
Syntax Style (15 pts total)
6 pts. Descriptive and helpful comments
2 pts. Good names
2 pts. Good use of constants
2 pts. Appropriate formatting
3 pts. Appropriate use of public/private
Code quality (25 pts total)
5 pts. conditionals and loops
5 pts. General design/efficiency issues
5 pts. Parameters, variables, and scoping
5 pts. Good correct use of arrays
5 pts. Miscellaneous
Correctness (35 pts total)
5 pts. Gun rotates correctly w/ key presses
5 pts. Ship fires projectiles
5 pts. Asteroids move correctly
5 pts. Ship projectiles destroy asteroids
5 pts. Asteroids can destroy ship
5 pts. Scoring is correct
5 pts. Asteroids generated correctly
Extra Credit (5 - 10 pts total)