Fall 2006, 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.
This assignment is based loosely on that game. However, since we haven't covered GUIs (Graphical User Interfaces) yet, you have no way to control the frogs yourself. Instead, you will have to teach the frogs how to save themselves.
This assignment uses all the same concepts as the previous assignment, so it should be easier to get started. The only new things are "final" variables (that is, constants), and Javadoc, which is a commenting convention we will discuss in class next week.
You will need to set up a display very similar to the following:
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.
In general you shouldn't use literal numbers (for instance,
in the body of your code. The problem is that it's not usually obvious what
these numbers mean, so they are pejoritively called "magic numbers."
It is better to give them meaningful names (for instance,
= 50;), and to use those names in your code.
(Exception: It's usually obvious why you're using the numbers
1, so you can usually use them directly in your code, without giving
them an explanatory name.)
If you declare that a number is
final, that means that the code
cannot change the number--it is a constant. Names of constants are written
in ALL_CAPS_WITH_UNDERSCORES. Also, since final numbers cannot be changed, it
is safe and often convenient to make them
public--this allows you
to declare them in a single place but use them in many classes.
Here are some numbers that I used to create the above picture:
public final int PANEL_WIDTH = 800;
public final int PANEL_HEIGHT = 300;
private final int HIGHWAY_WIDTH = 50;
final int CAR_LENGTH = 30;
final int CAR_WIDTH = 18;
If you aren't sure about what some numbers should be--for example, how fast cars should move, or how often to introduce new cars--just pick some reasonable-sounding values, give them appropriate names, and use the names throughout your code.
It seems to me that there are four basic classes that are needed in this assignment.
BouncingBallscode to find out how this is done). It creates cars and frogs, and tells them when to do their thing.
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.
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.
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
in the highway object. Hmmm, now we need a small
Frogs don't move smoothly--they hop. To have a frog hop, don't change its position
every time you draw everything. Instead, allow it to move every
time you draw things, but then move it by a large amount. (I don't yet know
a good value for
n--probably 5 or 10?)
A frog can only jump left, right, up, or down, not at an angle.
You should adjust your numbers so that it takes a frog three hops to get across the road. 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. Of course, the frog can also hop back, or left to right. Hopping as fast as it can, it can hop half the speed of a car.
Frogs are smart. When you tell a frog to move, give it (as a parameter to your
"hop" method), the
ArrayList of all the currently active
cars. It can look at all the information about cars when it decides which way
to hop. (It can also decide to stand still.)
How does a frog get to be so smart? You program it to be smart, that's how!
This class is a bit harder to describe, but basically it controls everything. It sets up the window, runs loops creating cars and telling them when to move, creates frogs and tells them when to hop, and so forth.
You should use a
ListArray<Car> to hold your cars. You can
add cars to the list as you create them, and you can
them from the list when they disappear off the edge of the screen. To move cars,
just go through the list and move each car. Since we need a collection of cars,
not just a single car, this list belongs in the
Frogger class rather
Car class. However, things that an individual car can
do, such as move and hit frogs, should be in the
Frogger should extend
JFrame, that is,
. I'll explain this later, but
it's a necessary part of making the window appear.
You should have the program quit after a certain number of frogs have risked
their lives, or when the window is closed. To quit the program when the window
is closed, add the line
somewhere early in the
Near the end of today's lab, I would like you to estimate about how
long this assignment will take you. Then, as you work on it, keep track of about
how long it actually takes you. Try to be accurate within a couple of hours.
Then put the estimated time and the actual time in the Javadoc comment for the
Frogger class. This will be a regular part of all future assignments.
Froggerclass Javadoc comments, but you will not, now or ever, be graded on how accurate your estimate was. This is entirely to help you learn to make better time estimates.
Thursday, October 5, before midnight. Zip up your entire Frogger project folder and submit it via Blackboard.