CIT 591 Assignment 11: Frogger
Fall 2009, 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. Cars appear at random, but all travel at the same constant speed. The user controls the frog. This assignment is a simplified version of that game.
This is a pair programming assignment, but I want everyone to get some experience with animation. Hence, one partner should program the cars and the other partner should program the frog. (The highway is trivial: A gray rectangle and a yellow line.)
You will need to set up a display something like the following. (This is only part of your GUI, however; you need somewhere to put other things, such as a Pause button.)

This represents a two-lane highway, with the upper cars going west (left) and the lower cars going east (right). The smaller green circle south of the highway 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.
You will need to experiment a bit to find reasonable values for things like traffic density and the speed of cars. This will be a lot easier if you give these values names, rather than using magic numbers in your code.
I recommend using the following kinds of objects:
Frogger (extends JFrame) View (extends JPanel) paint(Graphics g) method can call methods in the Highway, Frog, and Traffic classes (passing in g) to draw their respective objects. TrafficCarHighwayFrogFrogger classFrogger should extend JFrame, that is, .
The Frogger object controls everything.
It sets up the window, creates a Traffic object (which creates Cars), and creates a Frog. It should have controls to Pause and Resume the game. It should use either a Timer or the sleep(ms) method to control the speed of the animation.
You need to be able to capture keystrokes (arrow keys, in particular) that don't go into a text area. The way to do this is to attach a KeyListener to the JFrame itself. Each time an arrow key is pressed, the Frogger object should tell the Frog to jump. Since recognizing the arrow keys is a bit tricky, here's an example program (Keystrokes.java) that does this.
ViewThe View object has the update and paint methods. All it needs to do is to tell all the other objects to paint themselves on the View's Graphics object.
TrafficThe Traffic object creates and keeps track of cars, probably in an ArrayList<Car>. 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. However, things that an individual Car can
do, such as move and hit frogs, should be done by the Car objects.
Cars should arrive randomly. Initially the traffic should be light (making it easy for the first frog), but as each frog gets across, traffic should get a little bit heavier (that is, cars are created more often).
CarsCars 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.
All cars travel at the same speed. You don't have to worry about collisions between cars. Do not, however, create overlapping cars.
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.
HighwayThe highway doesn't do much except paint itself.
Frog A Frog starts at the bottom of the screen, and cannot jump off the bottom, the left end, or the right end of the display. However, if it gets across the highway, it can jump off the top, at which point you can create a "new" Frog. (Or just teleport the same Frog back to the starting point.)
A frog can only jump left, right, up, or down, not at an angle.
It takes a frog three hops to get across the road (they are very good jumpers!). The first puts it in the eastbound lane, the second puts it into the westbound lane, and the third gets it safely off the road. You should start your Frog in such a position that hopping forward ("up") a few times will put it in the center of each of these lanes. Of course, the frog can also hop back, left, or right.
If the frog jumps onto a car, that is the same as a car hitting the frog. Either way, it's bad news for the frog.
It's a game, so it should feel like a game.
Keep a count, in a visible location (upper right corner, maybe?), of how many frogs have made it across the highway.
Allow some number of "lives," and keep the user informed how many lives remain.The game ends when that many frogs have come to a bad end. Visually, it works better if you put a little red "splotch" on the highway to mark the site of each dead frog, rather than just have the frog disappear (conceptually, this is best done in the Highway class, as the splotches become part of the Highway).
Don't let the game run forever. Each time a frog gets across the highway, the traffic should get a little denser, so that the next frog will have a harder time. But start easy.
Thursday, December 10, before midnight. Zip up your entire Frogger project folder and submit it via Blackboard.