Lab 8 : Starbox for Java
Lab Overview
- Building software as a group.
StarBox for Java
The StarBox intergalactic shipping company has a bit of a problem. In the middle of a delivery, one of its shipping tankers accidentally released its cargo into the vacuum of space. Now there are pieces of cargo floating in space. To make matters worse, the cargo spilled into an asteroid field. A retrieval ship now must navigate the asteroid field to collect the cargo pieces without crashing! You can check out an applet version of this scenario below. Click the screen to start a new game and press the UP
, DOWN
, LEFT
, and RIGHT
keys to move your space ship (red circle) to collect all of the cargo pieces (green squares) while avoiding the asteroids (gray squares).
The picture above shows an example of the program you're about to create for this assignment. It's a game with a single ship piloted by the user (the red circle), a few asteroids (the light gray squares), and some cargo (the green squares).
This Assignment
Writing all of the code for the Starbox game would be a big task. Luckily, you don't have to write it all. There are 16 students currently in the class and each student is going to write a portion of the code. You will each be assigned a number, 1-16, designating which section of the code you are supposed to write. Each "section" should be about 1 fairly short method, or a couple of methods that are very similar in the code you write for each. The outline of the entire program, with all involved methods, can be found here.
- (Katie) General outline (imports, class header, implement listeners, declare global var's, include requried method headers). Also, write code for the "mouseClicked()" method.
- Section 1 Test Page
- (Holley) Write code for "keyPressed()" and "actionPerformed()"
- Respond to keys pressed by the user using "event.getKeyCode()" and comparing that to values like "KeyEvent.VK_UP". In the "actionPerformed()" method, just call "repaint()" to redraw the window each time the timer goes off.
- Section 2 Test Page
- (Lili) Write code for "updateXCoordinate()" and "updateYCoordinate()" methods
- Update the given coordinate using the given speed. Make sure that the resulting coordinate is wrapped back into the 400x400 window area.
- Section 3 Test Page
- (Anne) Write code for "updateAsteroidCoordinates()" method
- Update asteroid coordinate entries base on the asteroid speed entries. Call the "updateXCoordinate()" and "updateYCoordinate()" methods to do this.
- Section 4 Test Page
- (Jordan) Write code for "getScore()" method and "drawScore()" method
- Loop through entries in the "cargoDrawn" array to determine how many entries are set to "false"... meaning that that cargo piece has been collected. The current score is the number of cargo pieces collected so far.
- In the "drawScore()" method draw the score in the upper-left corner using the "p.drawString(String text, int x, int y)" method.
- Section 5 Test Page
- (Marc) Write code for "asteroidCollision()" and "cargoCollision()" methods.
- Given the uppper-left corner coordinates for the asteroid/cargo piece and for the ship, determine if those objects have collided. You will need to remember that asteroids and the ship are 20x20 pixels and the cargo pieces are 10x10. (This is really just a problem to determine if 2 square overlap.) Note that the
Math.abs()
method could be very useful here. This method takes in an integer value and returns the absolute value of that integer, such as Math.abs(2 - 4)
returning the value 2
.
- Section 6 Test Page
- (Raphael) Write code for "gameWon()" method
- Loop through the given array entries and use the "cargoCollision()" method to see if the ship has collided with any of the cargo pieces. If so, that cargo piece's entry in the "drawn" array should be set to "false". Finally, if all entries in that array have been set to "false", return "true" that the game has been won. Otherwise, return "false" that the game has not been won because there are still cargo pieces remaining unclaimed.
- Section 7 Test Page
- (Jeremy) Write code for "gameLost()" method
- Checks for asteroid collisions with the ship and returns true/false if the game has been lost.
- Section 8 Test Page
- (Erin) Write code for "isGameOver()" and "drawShip()" methods
- Draw the ship as a red circle inside of a 20x20 pixel green square outline. In the
isGameOver()
method use the "gameWon()" and "gameLost()" methods to determine if the game has already been won or lost. If it has, use the "drawString()" method (called as "p.drawString(String text, int x, int y)") to display an appropriate message. Also, call "timer.stop()" to stop the timer and stop the game.
- Section 9 Test Page
- (Kenny) Write code for "drawCargo()" and "drawAsteroids()" methods
- Loop through all entries in the given x- and y-coordinate arrays to draw the objects at the given locations. Note that cargo pieces will only be drawn if their corresponding entry in the "drawCargo" array is set to
true
. Cargo pieces should be drawn as 10x10 pixel green squares while asteroids should be drawn as 20x20 pixel gray squares.
- Section 10 Test Page
- (Saidi) Write code for "randomX(), randomY(), randomSpeedX()", and "randomSpeedY()" methods
- Return a random value, created by the random number generator variable
gen
appropriate for each of the methods. Note that calling gen.nextInt(10)
will give you a random number in the range 0-9. The "Speed" methods should return a value in the range from -3 to 2. The "randomX()" and "randomY()" methods should return a value in the range 0 to 400.
- Section 11 Test Page
- (Kyle) Write code for "assignLocations()" method
- Use the "randomX()" and "randomY()" methods to place values in the x- and y-coordinate array entries for the asteroids and the cargo pieces.
- Section 12 Test Page
- (Ellie) Write code for "assignSpeeds()" method
- Use the "randomSpeedX()" and "randomSpeedY()" methods to place values into the x- and y-coordinate speed array entries for the asteroids.
- Section 13 Test Page
- (Christy) Write code for "paint()" method
- Draw in a black background for the 400x400 window.
- Call the "updateAsteroidCoordinates()" method to update the asteroids.
- Call the "updateXCoordinate()" method and place its returned value in the
shipX
variable.
- Call the "updateYCoordinate()" method to do the same for
shipY
- Call the "drawAsteroids()", "drawCargo()", "drawShip()", and "drawScore()" methods to draw those parts.
- Call the "isGameOver()" method to check if the game should end.
- Section 14 Test Page
- (Daniel) Write code for "init()" method
- Call the "setSize()" method to initialize the size of the applet window to 400x400 pixels.
- Initialize the
timer
variable to a 50 millisecond delay and use the key word "this" to give it a listener.
- Call the "addMouseListener()" method and give it the key word "this".
- Call the "addKeyListener()" method and give it the key word "this".
- Call the "resetWindow()" method to initialize all of the pieces in the game.
- No Section 15 Test Page
- (Ryan) Write code for "resetWindow()" method
- Call the "assignLocations()" method to give the cargo pieces and asteroids initial locations.
- Call the "assignSpeeds()" method to give the asteroids initial speeds.
- Use the "randomX()" and "randomY()" methods to create inital values to be stored in the
shipX
and shipY
variables.
- Initialize
shipSpeedX
and shipSpeedY
to 0.
- Initialize the
cargoDrawn
array so that each cargo piece will initially be drawn.
- Call the "repaint()" method to make sure the changes are reflected on the screen.
- No Section 16 Test Page