Sale!

COP 3330 Homework 4 solved

Original price was: $35.00.Current price is: $30.00. $25.50

Category:

Description

5/5 - (1 vote)

COP 3330 Homework 4

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. Anything that goes off one side of the screen reemerges 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. JavaDoc specifications for these classes are provided, and 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.

COP 3330 Homework 4

SpaceObject has static fields for the playfield width
and height that are set up by the main class. These two fields 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 an ArrayList
containing newly created comets that are produced by their destruction, although SmallComet should return an
empty ArrayList since it doesn’t spawn additional comets.

Ship represents the player’s spaceship. The ship has a direction that it is facing. The gun turret of the ship points to
this direction. The ship’s direction determines the change in position when the ship accelerates, and 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 tip of the ship’s gun turret 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.

COP 3330 Homework 4

CometsMain is already provided to you as part of this assignment. This is where the main method resides. This
class is responsible for rendering of objects for the game, handling user input and managing space objects
interactions. The classes you write MUST be compatible with CometsMain in its original form. You must not
change the implementation of CometsMain.

An input file comets.in should be placed in the root directory of the project. This file specifies the initial layout of
the comets. Each line in the file describes one 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
coordinates), and the last two numbers are its velocity.

Game Constants

Shot radius – 3 pixels Ship radius – 10 pixels
Large Comet radius – 40 pixels Medium Comet radius – 30 pixels
Small Comet radius – 20 pixels Maximum ship speed – 10 pixels/frame
Ship acceleration – 0.1 pixels/frame2 Ship turning rate – π/32 radians/frame

Shot speed – 3 pixels/frame in the direction of the ship + velocity of the ship when it was fired.
Ship gun turret length – 15 pixels. The turret originates from center of the ship and points towards the direction ship
is facing.

Useful Formulas
Determining if two objects overlap:
Let objects one and two be at positions (x1, y1) and (x2, y2) and have radii r1 and r2, respectively. Objects one and two
overlap if:
( ) ( ) 1 2
2
1 2
2

1 2 x − x + y − y < r + r
Updating the position of an object:
Let an object at position (x0, y0) be traveling with velocity (vx, vy). Its position (x, y) after being moved is:
x = x0 + vx and y = y0 + vy

Accelerating the ship:
Let the ship have direction θ in radians. The change in x and y velocity when accelerating is given by:
Δ = 0.1⋅sin(θ ) Δ = 0.1⋅ cos(θ ) x y v v
The ship’s speed, s, is:
2 2

x y s = v + v
If this speed is greater than 10, scale the velocity down by multiplying both the x and y velocities by 10/s.
Firing a shot:
If a shot is fired from a ship traveling at velocity (vship.x, vship.y) and facing at angle θ in radians, the resulting shot
velocity should be:
( ) ( ) shot x ship x shot y ship y v v v v . . . . = 3⋅sin θ + = 3⋅ cos θ +

Deliverables

Create a folder with name CometsCode and put all seven java files (SpaceObject.java, Comet.java,
SmallComet.java, MediumComet.java, LargeComet.java, Ship.java, Shot.java) inside the folder.
Then, zip the folder into file CometsCode.zip.

Submit CometsCode.zip as an attachment using the “Add
Attachments” button. Assignments that are typed into the submission box will not be accepted. You must submit
the files in a zipped folder. If you attached each java file separately, you will get points taken off. Note that the
zipped folder must contain all seven source files and nothing else. The deadline for submission is posted on
WebCourses.

Documentation

Your code must include:

1. Header comments for each source file, with the following information: file name, your name, course
number and date.
2. Inline comments throughout the code explaining its logic and design.
3. Javadoc comments are not required for this assignment. But you are free to include them.

COP 3330 Homework 4

Restrictions

1. Your program must compile using Java 6.0 or later. The programs will be run under Eclipse environment
when grading.
2. Your implementation of the classes must conform to the specification given in the Javadoc. Even if your
program runs fine, you may still lose points if you did not implement all the methods specified in the
Javadoc.
3. Your classes must follow the inheritance hierarchy given in the Javadoc.

Execution and Grading Instructions (This is for TAs)

Points Total: 140
1. First, create a directory and put in it source file CometsMain.java.
2. For each student, download student’s source code files into the source directory.

3. Review each of the source files for comments and appropriate methods.
4. Run the program and check that the game plays properly.
Points Breakdown

Execution: 65
Code: 55
Documentation: 20
COP 3330 Homework 4