Objective
1. To reinforce your skill in writing classes.
2. To familiarize you with inheritance and abstract classes.
Problem: Comets
The software company you’re working for is developing a game named Comets that is essentially a clone of an old popular arcade game. The player controls a spaceship that floats around in two-dimensional space propelled by its engines and carried by its inertia. Comets are also floating around in the same space. Anything that goes off one side of the screen re-emerges on the opposite side. The player’s goal is to destroy all of the comets without colliding with any of them. The situation is complicated somewhat by the fact that shooting larger comets causes them to break into several smaller comets, thus making them more difficult to avoid.
A basic game interface is already written, so you don’t need to worry about drawing anything on the screen or responding to the player pressing buttons. All you need to do is write classes to represent the various objects in the game. All classes are to be located in the package “comets”. JavaDoc specifications for these classes are provided on WebCourses (along with a few other important files), but here’s a general outline:
SpaceObject is an abstract class representing all of the objects in game, including the comets, bullets fired by the player, and even the player’s ship. This should keep track of the position, velocity, and size of the object. It provides methods for updating the object’s position based on its velocity, determining whether the object is overlapping with another object, and accessors for the object’s position and size. SpaceObject has static fields for the playfield width and height that are set up by the main class and should be used to determine the size of the play area for wrap-around purposes.
Shot objects represent shots fired by the player. Shots should only stay on the screen for a certain length of time, so they have an age counter that increments every time the shot is told to move. The main class will take care of removing the shot from the game based on this age.
Comet is an abstract class representing comets. LargeComet, MediumComet, and SmallComet all extend Comet. Large comets break into two medium comets when shot, and medium comets break into three small comets. Small comets are destroyed outright by being shot. Every comet has an explode() method that returns a Vector containing newly created comets that are produced by their destruction, although SmallComet should return an empty vector since it doesn’t spawn additional comets.
Ship represents the player’s spaceship. The ship has a direction that it is facing that it uses to figure out the change in velocity when it accelerates, as well as the trajectory of the shots it fires. A ship has methods to turn left, turn right, accelerate, and fire shots. The fire() method returns a Shot object that originates from the center of the ship and travels in the direction that the ship is facing, adjusted, of course, for the ship’s own velocity. An important note about acceleration: Even though in real space objects can basically travel arbitrarily fast, it wouldn’t be much fun if the ship travels so fast that you can’t see it. Limit the maximum speed of the ship to 10 pixels per frame (i.e. per move()). That’s fast enough for the game to be exciting, but not so fast that it’s impossible to follow the ship.
CometsMain is already provided as part of this assignment via WebCourses. The classes you write MUST be compatible with CometsMain in its original form. CometsMain isn’t very good, unfortunately. Up to 25 points of extra credit are available on this assignment for making substantial improvements to CometsMain. Contact the professor with your ideas to see how much extra credit it would be worth.
A configuration file named “comets.cfg” should be placed in the root directory of the project. This gives the initial layout of the comets. Each line in the file describes a comet. The string at the start of the line specifies the size of the comet (“Large”, “Medium”, or “Small”), the two numbers after that are the position of the comet (x and y coordinate), and the last two numbers are its velocity.
Game Constants
Shot radius