CIT 591 Assignment 12: Frogger
Fall 2010, David Matuszek
Some of you may remember the old computer game Frogger!, in which your goal is to get a number of frogs (one at a time) safely across a busy highway. We will be doing a simplified version of that game--no snakes, no river, etc., just a highway.
You will need to set up a display more or less like the following (adjust to taste);

This represents a two-lane highway, with the upper cars going west (left) and the lower cars going east (right). The frog (not to scale) represents a frog.
Frogs appear one at a time at the bottom of the display, and try to cross the highway and escape off the top of the display. When a frog makes it to safety (or gets killed on the highway), the next frog appears.
The main class, Frogger, extends JFrame and acts as the Controller. It will create the FroggerModel and FroggerView objects. It will contain a JPanel on which the drawing will be done. This JPanel will have a KeyListener that sends commands to the FroggerModel.
The FroggerModel class extends Observable and responds to commands from the controller. It handles all the activity of cars and the current frog. It moves the frog, moves the cars, and checks whether the frog has been hit by a car.
The FroggerView class implements Observer. Its job is to update the display (in a public void paint(Graphics g) method). It will need to find out from the model where everything is, then display those things.
Frog, Car, and Highway should be separate classes. Each class should have a method that can be called from the paint method to draw itself on the Graphics (which can be passed in as a parameter). Then all the FroggerView needs to do is to call each of these methods.
Your Car objects should have (at least) an x-y position, a direction, and a color. Cars come off the left side of the screen going right, or they come off the
right edge of the screen going left. The best place to create a car is just
outside the visible window; as it moves into the window, it will appear in a
very natural way.
The FroggerModel class should have an ArrayList<Car> to hold the current set of cars. You can add cars to the list as you create them, and you can remove them from the list when they disappear off the edge of the screen. To move cars,
just go through the list and tell each car to move.Things that an individual car can
do, such as move and hit frogs, should be in the Car class.
Cars appear at random intervals. Make sure your cars don't overlap (create them a reasonable minimum distance apart), but since they all travel at the same speed, you do not have to worry that cars will collide.
Use a Timer to control the movement of cars, so that the cars move smoothly and constantly. Each time a car moves, it should check to see whether it hit a frog. If it
did, the car keeps on going, but the frog is dead. After all, despite appearances, a car is much bigger than a frog, and would not be affected by hitting the frog.
A highway doesn't do much. However, when a frog is killed on the highway, it
should leave a little red splotch,
. Think of it this way: The splotch becomes
part of the highway. So you could keep track of these by having a ListArray<Splotch>
in the highway object. Hmmm, you may need a small Splotch class...
Frogs don't move smoothly--they jump. A frog can only jump left, right, up, or down, not at an angle. To make the frog jump, use the arrow keys,
← ↑ → and ↓. If you prefer some other set of keys, such as a s d w, you can use these in addition to the arrow keys, but I like the arrow keys.
After a frog jumps, it should be facing the direction in which it has just jumped. Here are some images you can use: ![]()
![]()
![]()
. The length of a jump should be half the width of the highway, so that it takes three jumps to get across the road. The first jump puts it in the eastbound lane, the second puts it into the
westbound lane, and the third gets it safely off the road. Of course, the frog
can also jump back by the same amount, or it can jump left or right.
Frogs should be started in the bottom center of the display, with an initial y position such that it will take an integral number of jumps to put the frog in the middle of a lane--it shouldn't be possible for a frog to ever be partly in a lane and partly not.
Whether a frog hits a car, or a car hits a frog, it's going to be bad for the frog. So when your frog jumps, you need to check if it jumps against a car.
Basic animation using Observer, Observable, Timer, and TimerTask is covered in this set of lecture slides. Recognizing arrow keys is a bit tricky, as is drawing images, so here is some sample code you can look at to find out how it's done. The cars can be simple round rects (rectangles).
This assignment is about animation, so I don't require you to provide a certain number of frog "lives," or keep track of how many frogs have made it to safety, or anything like that. However, these features are fairly easy to add, and would make the program a lot more interesting as a game. Feel free to add them, but get the required features working first.
If you want to make sure the game has an end, all the cars could speed up just slightly each time a frog makes it across the highway. A static variable in the Car class would be perfect for this, since you want it to apply to all cars. Again, this is optional. Other (optional) features might include Start, Pause, and Stop buttons. I am not offering extra credit for extra features, however; it's just something you might like to do to make a better program.
Thursday, December 9, before midnight. Zip up your entire Frogger project folder
and submit it via Blackboard. Include an executable .jar file.