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.
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 frog (not to scale) wants to cross the highway.
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.
Create a project
Frogger and a package
Simple animations can consist of two kinds of entities: backgrounds, which are the unchanging part of the picture (in the above, the highway), and sprites, which move against the background, and can show a variety of behaviors. In Frogger, the frogs and the automobiles are sprites. The commercial game also had several other kinds of sprites: snakes, turtles, flies, logs, and lily pads.
int y-- the position of this sprite on the screen. More accurately, these values usually specify the location of the sprite's bounding box--the rectangle inside which the sprite is drawn.
int dy-- describes the motion of the sprite.
dxis the amount to be added to
dythe amount to be added to
yduring each update.
abstract void update()-- makes all necessary changes in the state of this sprite before the next time it is drawn. Among other things, this usually means updating
y. In the case of a flying bird, for example, there might be different images showing the wings in different positions.
abstract void draw()
This is a fairly minimal set of features for the
Sprite class; feel free to add more.
Frog starts near the bottom of the display, and only moves in response to keystrokes. Thus, for a frog,
dx = dy = 0. The user controls the frog's motion by hitting
the arrow keys (↑, ←, ↓, →). The ↑key causes the frog to move (jump) in the direction that it is facing; the ← and → keys cause the frog to turn 90° counterclockwise or 90° clockwise, respectively, but do not move the frog. The ↓ key causes to turn 180°, again without changing position.
Frog also has an
which is used both in moving and in drawing the
Frog should also have a
boolean alive variable, initially
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 five hops to get across the road (they are very good jumpers!). The first two put it in the eastbound lane, the next two put it into the westbound lane, and the fifth 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 right place in each of these lanes. Of course, the frog can also hop back, left, or right (if it is facing in that direction).
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.
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), or faster, or both.
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 is unaffected, but the frog is dead.
This class simply maintains an
ArrayList<Sprite> of sprites. It provides methods to add sprites to the list, and remove them from the list. Each time the screen is redrawn, all the sprites should be redrawn (after the background is drawn).
This is a boring class. It has a method that the
View can call to draw the background.
Frogger object contains the
main method and controls everything
else. The (one) object of this class creates and displays the
Frogs, and tells them
when to do their thing. It should have controls to Pause and Resume the game. It should use a
Timer to control the speed of the animation.
This is the object in charge of painting. Its
paint(Graphics g) method will draw the background, then draw the sprites.
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 (ArrowKeyTests.java) that does this. It also gives an example of loading an image.
Plus, here are some images you can use.
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. Or, when a frog makes it across the highway, move it into a line of frogs in the top right corner of the display.
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.
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.
Friday, December 6, before 6am. Zip up your entire Frogger project folder and submit it via Canvas. Reminder: No email submissions will be accepted.