CIT 590 Assignment 12: Snipe
Spring 2010, David Matuszek
Implement a simple game based on the Bouncing Ball animation. You can use this code as a starting point for your game.
You are in control of a single ball on the screen. Your job is to manuver your ball to hit every other ball on the screen in as short a time as possible.
To simplify the description, several numbers are specified below--the size of the window, the number, sizes and speeds of the balls, etc. In all cases, those numbers should be defined constants (
finalvariables), so that they can be changed simply by changing the value of the constant in one place and recompiling. Start with these numbers, but you can adjust them to improve game play if you like.
The playing field (a
JPanel) should be about 800 by 800 pixels. Balls
should be about 10 pixels in diameter.
At the start of the game, your "ball" (which is a black square) is stationary in the center of the playing field. Seven other balls, of varying colors (but not black or white), are moving randomly about the playing field. You manuver your ball so as to collide with each of the other balls. When you collide with a ball, it stops moving and remains in place, and gradually "fades away". When all the balls (except yours) are stationary, the game is over.
While you play, the elapsed time is displayed in the top right corner of the playing field. When the game ends, the elapsed time stops advancing, so that you can read how well you did. Shorter times are better.
The animation will proceed at a rate of 20 frames/second.
Each ball (except yours)
starts in a random location on the screen, and moves with a random velocity.
The velocity is determined by two quantities:
is the amount to add to the
x position for each new frame, and
deltaY, which is the amount to add to the
for each new frame. The values
randomly chosen integers between
+8, inclusive, except that at least one of them must be
Your black ball starts in the center of the screen, with
= deltaY = 0. You control the ball by hitting keys which
increment or decrement
|Key typed||Key codes||What to do||Where to get key code|
|| Subtract 1 from
||When a key is pressed, a
|| Subtract 1 from
|| Add 1 to
|| Add 1 to
By hitting the same key repeatedly, you can increase the speed of your ball up to a maximum speed of 30 pixels/frame (or -30).
Your ball will bounce when it hits the "walls" (the borders of
the window). When the left or right wall is hit, the sign of
when the top or bottom wall is hit, the sign of
deltaY changes. Other balls will wrap--when a ball disappears off one edge, it reappears on the other edge, with
deltaY unchanged. For example, when a ball moves off the top of the window, it reappears in approximately the same x position at the bottom of the window, moving in the same direction.
ball and another ball "collide" when the distance between
their (x, y) positions is less than or equal to the ball diameter. When this
happens, your ball is unaffected, but the other ball stops moving (its
are set to zero), and it is considered to be "dead."
When the ball dies, it should "fade away"--you can do this by subtracting 5 from each of its RGB values (to a minimum of
(0, 0, 0)) each time the ball is drawn. When the ball fades away completely, you can (optionally) remove it.
Other balls do not collide with one another. They can pass through one another without any effect. Only test for collisions with your black ball.
Display the elapsed time in the top right corner of the playing field, to the nearest tenth of a second, and
update it every time you display a new frame (20 times a second). (Use
getTimeInMillis() method). Balls can
pass through the time display with no effect--but you
should display the time after displaying
all the balls, so that if a ball is stopped in that location, the time
is drawn over the ball, rather than being obscured by the ball.
Put four buttons at the bottom of the screen:
Use the MVC design pattern. Name your main class
Snipe, and have (at least) three other classes, called
Controller. (You should have at least a
Starting the game should start a
Timer to perform a
1/20 of a second. The TimerTask should call the Model to update the positions of all the "live" balls, the colors of all the "dead" balls, and the elapsed time. When the model is finished and all changes
have been made, it should notify its observers. (Don't notify observers
after every little change!) The View is an
repaint() to display the balls and the elapsed time on the screen.
Do something to indicate the end of a game, rather than just have the program stop. This doesn't have to be anything fancy, but it's discouraging when the computer doesn't appear to even notice when the player has finished. (If you want to show off your finished game to others, a display of the best scores so far, and who made them, adds a lot of appeal to your game--but that's not a requirement.)
There will be methods in your Model class that deserve JUnit testing. All methods should be properly commented.
Tuesday, April 27, before midnight. Generate (and check) the Javadoc files. Zip (not
.rar) your project and submit it
via Blackboard. As this is another pair programming assignment, only one assignment should be submitted by your team. Only assignments
submitted via Blackboard
will be accepted--do not send your program by
Grading will happen shortly after April 27, and after that grading session, no more late programs will be accepted.