For this lab, we would like you to write a program that plays the game Frogger.
In this game, you control a frog that is trying to cross a busy 4-lane highway. Each lane has cars or trucks zooming (well ok, crawling) by. The vehicles in a given lane all travel at the same speed, but vehicles in different lanes may travel at different speeds (and even in different directions if you would like). The user is in control of a frog. Clicking in front of the frog moves it forward one hop (one hop is the width of a lane of traffic), clicking behind moves it back, and similarly for clicking to the left and right of it. The goal is for the user to get the frog across the highway without it getting squished.
If the frog does get squished it displays an "OUCH!" message at the bottom of the screen. The user can restart the frog from its original starting position by clicking the mouse once in the area below the lanes of the highway.
Click this link (while holding the Shift key) to download the Frogger starter script to your home folder.
Now open a terminal window and type
source initFroggerAs a result, there will now be a CS102/lab5 folder containing the following. You can also download these files using your browser by clicking on the links below:
A demo version should appear below if your web browser supports Java. You'll notice that the animation is not very smooth and the response to the mouse click is not always immediate. This will be true for your code too, and you shouldn't worry about it. In fact, getting smooth animations in Java requires some engineering expertise that is beyond the scope of this course (you can read about it on the web if you google for "Java" and "animation").
Read all the instructions before doing anything else. We will begin by describing the classes you need to implement. After we describe the classes, we will outline a plan for proceeding with your implementation.
The key to good Java design is the choice of classes to represent the objects in your program. There are four different kinds of objects involved in the Frogger game: the frog (class Frog), the lanes of traffic (class Lane), the vehicles (class Vehicle), and the window controller (class Frogger). (There are also some graphical objects on the screen to represent the lane markings on the highway, but we won't discuss these, becuase we have provided in the starter folder the code that draws them.) Here is a general description of the purpose of each class:
There is an image file for the frog, and 8 images for vehicles. There are 4 different types of vehicles. For each type of vehicle there is an image of that vehicle facing right and another facing left. The image files are included in the files provided when you source initFrogger
It is fine if you only use a single image file so that all your vehicles look the same and move in the same direction. Using multiple pictures is a feature that we hope you add (it does make the display look better), but it is not required.
The frog is 83 pixels wide and 48 pixels tall. The widest vehicle is 139 pixels wide. The tallest vehicle is 66 pixels tall. This information should help you figure out how to place the vehicles and frog within the lanes. Remember to use constants effectively so that you would not need to change many values (if we introduce a much taller vehicle, for example).
To see details on the implementation of each class, follow the links below:
There are many ways of proceeding to a full implementation. Here is one suggested ordering:
You may notice that the sample version of Frogger we have provided on this page has more features than we have required. Vehicles move in both directions and several different images are used to represent vehicles. You are encouraged, but definitely not required, to incorporate such extensions in your program for a small amount of extra credit. Only do so, however, after completing the construction of a program that meets the basic requirements.
Hand in your final program in the usual way: make sure the files Frogger.java, Frog.java, Lane.java and Vehicle.java, as well as all the .gif files you're actually using, are in your-home-folder/CS102/lab5. If not, copy them there, and compile and run your program one last time in that folder to make sure that everything works properly.
To submit your files go to moodle in the CS102 course page and the Lab 5 section. There will be separate place to upload for each file. Three questions for you to answer are also provided below, which should be answered in the third activity provided in the moodle under lab 5.
Good luck and have fun!
The grading of this lab will be more stringent for commenting. Make sure you have comments that describe briefly the purpose of each method you write.
This lab is based on materials developed by the department of Computer Science at Williams College.