CS 51 Test Program #2
Space Invaders
Due: Wednesday, December 7 at 4 p.m., Design due: November 23, by 10:00 a.m.

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 me.

Space Invaders, the popular arcade game, was designed and programmed for Taito, Japan in 1978. The game was licensed from Taito by Midway for production in the US. By 1980, it had been licensed by Atari and was the first arcade game adapted for Atari's home system. It remains as popular as it was when it was first introduced. If you'd like to play the classic space invaders, go to http://www.spaceinvaders.de/.




In case you haven't guessed, for your final test program we would like you to write a program that implements Space Invaders. To be more precise, we would like you to write a program that implements "almost Space Invaders". We have simplified the game somewhat from the original in order to make the assignment more reasonable.

The starting configuration for our Space Invaders game is shown above. The game begins with several rows of aliens at the top of the screen. At the bottom is a lone space ship that will try to defend itself against the attack of the aliens.

The aliens move across the screen from left to right and then back again, occasionally shooting at the good-guy space ship. They move at a constant rate and in sync with each other. They move right until the rightmost surviving ship is close to the right edge of the screen, and then move left until the leftmost surviving ship is close to the left edge of the screen. (You can decide what "close" means - make it look good!)

The space ship also moves from left to right and back, but its movement is controlled by the player. If the player clicks on the right-arrow button on the keyboard, the ship moves to the right; if the player clicks on the left-arrow button, the ship moves the left. In addition to dodging the projectiles shot by the aliens, the ship can shoot back. This is also controlled by the user, by clicking the space bar.

Each time a defense projectile hits an alien, the player gets 10 points. The game is over either when the player gets all the aliens or when the player is hit. In either case, a message is displayed to the user, indicating "You Won" or "Game Over".

Implementation Details

You should begin the game by setting it up. This involves creating a black sky, a score-keeping mechanism, a good-guy ship, and all those nasty aliens. We have provided gifs for the aliens and the ship. They are "invader1.gif" and "rocket.gif". Feel free to use them, if you'd like.

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

The good-guy ship is an object that will appear at the bottom of the screen. It must respond to the player's key clicks. That is, it should be able to move to the right and to the left. It must also be able to shoot at aliens. If it is hit, it should disappear.

The projectile shot at an alien will be an active object. It should move up the screen and stop either when it reaches the top or when it hits an alien.

To represent the aliens you should use a two-dimensional array. The aliens, after all, do appear on the screen in a grid-like pattern. The aliens should move as a group. They move together from left to right; and then they move together from right to left. When a member of the group disappears, no one takes its place. It simply stays blank.

Each entry in the two-dimensional array will be an alien, a complex object with behaviors and capabilities all its own. It can move, it can shoot, it can disappear when shot.

The projectiles shot by the aliens must move down the screen, stopping either when they reach the bottom or when they hit the ship.

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

SpaceInvadersController
The controller will set up the game. 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 it move or shoot. We have provided the skeleton for listening to the user's key clicks. 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 aliens 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 will extend ActiveObject. This will allow it to move smoothly if the user holds down a left- or right-arrow key.

Invaders
The Invaders class will extend ActiveObject. This object will hold a two-dimensional array of aliens. The aliens move as a group from left to right across the screen and then back again. From time to time they shoot at the space ship below.

Alien
The group of Invaders is made up of individual Aliens, each with behavior all its own. An alien can move, it can launch a projectile, and it can disappear when shot.

Projectile
Aliens shoot projectiles. A Projectile is an ActiveObject that moves down the screen, stopping either when it reaches the bottom or when it hits the space ship. Note that to achieve this behavior, the projectile needs to know about the space ship. Since projectiles are created by aliens who are members of the group of Invaders, the ship must be passed as a parameter through all of these classes.

DefenseProjectile
Last, but not least, are the projectiles shot by the good guy. They move up the screen, stopping either when they reach the top or when they hit an alien.

Consider this: The aliens need to know about the ship so that they can aim for it with their projectiles; but the ship needs to know about the aliens so that it can shoot at them. If this seems circular to you, you're right. You might handle this by creating the ship and passing it as a parameter to the Invaders when you create them. Then, after you've created the Invaders, you might invoke a method of the ship class (perhaps called setTarget), that will pass the Invaders as a parameter to the ship.

The Design

As indicated in the heading of this document, you will need to turn in a design plan for your Space Invaders 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. Don't worry about shooting at this point.

  3. Next construct the aliens. If you don't feel comfortable constructing a two-dimensional array of aliens, start by constructing one row of aliens. If you construct one row of aliens, you should be using a one-dimensional array.

  4. Now make the aliens move from left to right and then right to left, etc.

  5. Make the aliens shoot at the ship. There are a number of possible ways to do this. Here are a couple of ideas.

    1. At each time step randomly select an alien to do the shooting.

    2. When you construct each alien, give it a randomly selected "shooting interval". As the time interval passes, the alien should shoot and then restart its timer.

  6. Now make the ship shoot at the aliens. The projectile shot at the aliens should, as it's moving, be asking them "Have I gotten any of you?".

  7. Last, but not least, set up the score keeper.

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 Space Invaders game, there are clearly many additional features you could add to your program. 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. Some possible extensions are:

Turning it in

Your design should be turned in on paper in class or e-mailed to us by 10 a.m. on the day before Thanksgiving. Keep a copy for yourself since we will not return it to you until after the program is due.

When your work is complete you should deposit in the dropoff folder. 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 "TP 2". Also make sure to double check your work for correctness, organization and style. In particular, the comment at the top of each class should include your name.

Grading Point Allocations

Value

Feature
Design preparation (20 pts total)
Syntax Quality (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
Semantic 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. Ship moves correctly w/ key presses
5 pts. Ship fires projectiles
5 pts. Aliens move from left to right and back
5 pts. Aliens fire projectiles
5 pts. Ship projectiles destroy aliens
5 pts. Alien projectiles destroy ship
5 pts. Scoring is correct
Extra Credit (5 - 10 pts total)