CIT 591 Assignment 11: Frogger
Fall 2009, David Matuszek

Purposes of this assignment:

General idea of the assignment:

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.)

Details:

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.

General plan of attack:

I recommend using the following kinds of objects:

Frogger (extends JFrame)
This is the class that contains the main method and controls everything else. The (one) object of this class creates and displays the window, creates Traffic and Frogs, and tells them when to do their thing.
View (extends JPanel)
Some object needs to be in charge of painting, so why not create a View to do that? Its paint(Graphics g) method can call methods in the Highway, Frog, and Traffic classes (passing in g) to draw their respective objects.
Traffic
This class is in charge of creating Cars and telling each of them to move. Also, it can tell each Car to paint itself.
Car
Cars paint themselves, and they also move. Cars should be various colors, but must be visible--that is, none of them should be the same (or nearly the same) color as the highway.
Highway
This class doesn't actually do much. However, we need to be able to paint a highway, so this is the obvious class in which to do it. It might also provide some information about itself, such as where the top edges are (that is, their y-coordinates) of the westbound and eastbound lanes.
Frog
A Frog starts near the bottom of the display. The user controls the Frog's motion by hitting the arrow keys (↑, ←, ↓, →). Frogs paint themselves.

The Frogger class

Frogger should extend JFrame, that is, public class Frogger extends JFrame {...}.

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.

View

The 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.

Traffic

The 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).

Cars

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.

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.

The Highway

The highway doesn't do much except paint itself.

The 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.

Other considerations

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.

Grading:

Due date:

Thursday, December 10, before midnight. Zip up your entire Frogger project folder and submit it via Blackboard.