In this assignment, you will be building a video game called Staying Alive.
In the process, you will be gain experience using classes and objects.
The object of the game is to avoid a bunch of objects that are zooming around the screen and bouncing off the walls.
The game is over if the player is hit by any of the objects.
The player scores a point for every time step of the game in which they stay alive.
The game is played with the mouse.
The player's character moves in the direction of the mouse.
The further the mouse cursor is away from the character's current location, the faster it moves towards the mouse location.
You should start by downloading the file alive.zip.
This file contains some example control files and the associated images.
These files will need to be in the same directory where you run your program.
The zip file also contains stub versions for the three classes you will be developing.
You will need to add instances variables, the bodies of instance methods, and the body of the main() method.
You should also comment the top of every method (every method needs a comment describing what it does,
what parameters it takes, and what it returns).
Here is a description of the three classes:
StayingAlive.java - Client program containing the main() method that runs the game.
Handles reading in the game control file using StdIn.
It will create the Player object and an array of Enemy objects.
When the program is first started, it prints out a text representation of the Player and Enemy objects.
Player.java - Player class.
This class represents the player.
It knows things like the x-location and y-location of the player.
It also knows the image filename used to display the player as well as the radius of the player.
We assume the player is a circle, using the radius to detect collision with the Enemy objects.
The Player object also has a speed update factor.
The speed factor is used to determine how fast the player moves to the mouse location (details below).
The Player object can do things such as draw itself,
update its position,
check intersection with an Enemy object,
and get a String representation of itself.
Enemy.java - Enemy class.
This class represents the various things that will be flying around trying to kill the player.
It knows things like its x-location, y-location, x-velocity, y-velocity, radius, and image.
The Enemy objects spin as then bounce around, so they also know their current rotation angle.
An Enemy object can do things such as draw itself,
update its position,
get its x-position,
get its y-position,
get its radius,
and get a String representation of itself.
Control file. Your client program StayingAlive.java shoud read in a game control file from standard input using StdIn.java.
Here is an example game control file hitchhiker.txt with comments at the end describing the order and meaning of the values:
Canvas size and coordinates.
In this assignment, we'll leave the canvas size at the default size of 512 x 512 pixels.
We will also use the default coordinate system which has the lower-left corner being (0.0, 0.0) and the upper-right corner being (1.0, 1.0).
Thus in this assignment you do NOT need to call methods such as StdDraw.setCanvasSize(), StdDraw.setXscale(), or StdDraw.setYscale().
The Player object updates its x-position and y-position based on the current mouse location.
The current mouse location can be obtained by calling StdDraw.mouseX() and StdDraw.mouseY().
These methods return a double representing the mouse location in the current StdDraw coordinate system.
The mouse coordinates are sent to the Player object via the updatePos(double mouseX, double mouseY) method.
To calculate the player's new x-position, you increment the current x-position by the differece between the mouse coordinate and the player's current position divided by the player's speed factor.
For example, if the player has a speed of 100 and is currently at an x-position of 0.5 and the mouse is at the x-coordinate 0.7:
(0.7 - 0.5) / 100.0 = 0.002. After the update, the player's new x-position would be 0.5002.
Note that a higher speed factor actually causes slower responsiveness to the mouse input.
The Enemy objects update their position in the updatePos() method by adding their x-velocity and y-velocity to their x-position and y-position respectively.
In updatePos(), you should also check if the position has gone out of the box (0.0, 0.0) - (1.0, 1.0).
If so, you should invert the x-velocity and/or y-velocity as appropriate.
The Enemy objects should rotate counterclockwise 1 degree at every time step of the game.
When the object is constructed, the angle should start out at a random value between 0-359 (this way different objects using the same image file won't all look the same).
For simplicity, we assume the player and enemies are all circles.
The game is over if the circle representing the player intersects with any enemy circle.
This is similar to how I checked for the intersection of Ball objects in the lecture on classes and objects.
How do I make the drawn text bold like yours?
You need to set the font used by StdDraw.
A single call at the start of your program such as StdDraw.setFont(new Font("SansSerif", Font.BOLD, 18))
will do the job.
How long should I pause between steps in my animation loop?
We used StdDraw.show(10).
I can't print out an object's state to the console window because the instance variables are private.
How do I do this?
You should implement toString() methods in your classes.
These methods return a String containing all the details of an object's state.
toString() gets called automatically whenever you try and append an object to a String, for example: System.out.println("OBJ + " + myObj)
I want to use printf style formatting, but I don't want to print out right away but rather store the output in a String variable. How do I do that?
You should use the String.format() method.
It works exactly like System.out.printf() except that instead of printing immediately to the console window, it returns a String instead containing the output.
Extra credit. There are plenty of possibilities to improve the game. For example:
Include objects that the player tries to hit to gain points.
Once the object is hit it disappears.
Add power-up or power-down objects (e.g. making the player's speed increase or decrease).
Add sound effects.
Add visual effects for events such as when the player dies.
Make enemy objects collide with one another.
Submit your programs StayingAlive.java, Player.java, Enemy.java.
Be sure each submitted source file has the required header with your name, email address, and a description of the program.
If you did the extra-credit, submit a single zip file containing all the files required to run your creation.