CS21 Lab 6: Frog Races

Due 11:59pm Saturday, October 26th 2013

Run update21, to create the cs21/labs/06 directory. Then cd into your cs21/labs/06 directory.

$ update21
$ cd ~/cs21/labs/06
$ pwd 

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page. In particular, each function needs a comment.

For this lab we will continue using the Zelle graphics library and we will introduce the Frog library.

Frog Racing

In this week's lab, you will write a program that will simulate the exciting, fast-paced world of frog racing! You will write your frog racing program using functions, making use of a new Frog class.

Trying out the Frog class

Included in the lab06 directory is sample.py. This program shows you how to use the Frog class. You use Frogs just like you used Circles from the graphics library:

  1. To create a Frog called gary: gary = Frog(<centerPoint>, <radius>)
  2. To move gary the Frog: gary.move(<x_distance>, <y_distance>)
  3. To set the fill color of gary's shell: gary.setFill(<color>)
  4. To clone gary: speedy = gary.clone()
  5. To draw gary in a graphics window called win: gary.draw(win)
  6. The getRadius() and getCenter() methods also work, returning the radius of the Frog's belly and the center Point of the Frog's belly.

The only method that is different for Frogs is that they have an extra method called getLeftFoot() which returns a Point representing the lowest point the frog's left foot. We're going to need to know about that point because it will tell us whether or not our frog has crossed the finish line!

Try running the sample.py program and play around with the Frogs a bit until you are comfortable with how to use them.

Designing our frog race

Since this is the first time you've had to write your own functions, we're going to guide you closely on this lab, describing the functions you should be creating. In later labs, this part of the solution design will be up to you.

Note:You should test each function as you write them. Do not move on to writing the next function until you have tested and verified your current function is working correctly.

Here are the major steps that your program will need to accomplish:

  1. Create a graphics window.
  2. Draw a starting line and a finish line in the graphics window.
    • Your frogs will race from the top of the window to the finish line at the bottom of the window.
    • To create the starting line and the finish line, write a function called draw_racing_lines(window, start, end).
    • This function has three parameters: the graphics window you are drawing to, called window, the y-coordinate of the starting line start, and the y-coordinate of the finish line finish.
    • The function will draw the starting and finish line on the window in the specified locations.
    • The function does not have a return value.
  3. Create and draw four frogs of different colors.
    • You will create your four frogs of any four different colors you choose in a function called create_frogs(window).
    • This function has one parameter: the graphics window you are drawing to, window.
    • This function will create and then draw four frogs.
    • All the frogs will be centered at Point(0,0) and can be whatever size you'd like as long as they all fit on the screen and won't overlap, once you move them to the starting line.
    • It makes things much easier if each of the frogs are the same size, but if you want them to be different sizes, do that as an extension.
    • You will put the four frogs you created into a python list.
    • The list of frogs that you created is the return value from this function.
    • Don't worry that all the frogs are on top of each other. You'll fix this with the next function.
  4. Place the frogs at the starting line.
    • The four frogs you created in the create_frogs function are all located at Point(0,0).
    • Write a function called move_to_starting_line(frog_list, start, width) that takes the list of frogs frog_list, the y-coordinate of the starting line, and the window width as the only parameters.
    • This function will move each frog in the list of frogs to its starting location in front of the starting line. You can use the window width parameter to calibrate the spacing of your frogs.
    • This function has no return value.
  5. Ask the user which frog they think will win the race.
    • Print out an introductory message to the terminal window.
    • Ask the user to choose a frog they think will win.
    • You should test for valid input. If the user enters something that is not a number 1-4, let the user know their input was invalid and prompt them to enter a frog number again.
  6. Race the frogs.
    • Write a function called race(frog_list, end) that has two parameters, the list of frogs frog_list and the y-coordinate of the finish line, end.
    • In this function, you will repeatedly perform the following actions in a while-loop:
      • Move each frog in your frog list forward (towards the finish line) by a random value. We suggest you use something like randrange(5) because this makes for a good race, but you can choose whatever random movement you'd like.
      • If the point from getLeftFoot of any frog is past the finish line, you will return from the function after all frogs have had a chance to move.
      • Although you call return, this function has no return value.
      • Since you don't know how many steps it will take for the first frog to finish, use a while loop and exit the loop and return from the function when you notice that one frog has finished.
  7. Determine the winner of the race.
    • Because you returned from the race function, you know that at least one frog has crossed the finish line, but many frogs may have in fact crossed the finish line and you need to figure out which frog has crossed the finish line by the largest amount.
    • Write a function called winner(frog_list) that takes the list of frogs as its only parameter.
    • This function determines which frog has moved the furthest to the bottom in the graphics window.
    • The return value from this function is the number of the frog that won, with the frogs labeled from 1 (the left-most frog) to 4 (the right-most frog).
  8. Determine if the user made a correct guess.
    • If winner of the race matches the user's guess, print a congratulatory message.
    • Print an appropriate message if the user guessed incorrectly.
Sample images

You can make your frog race look however you'd like, but here's one idea of how your race may look before it starts.

And here is a sample image of what it might like after the race ended with the winner being frog 1. The frogs are numbered 1, 2, 3, 4, starting from the left.

There are lots of opportunities for extending this lab.
  1. Give the user $10 to start. On each race, let them bet $1. If they guess correctly, they win $4. Let them play 5 races. Or, let them play as long as they'd like until they go broke.
  2. Have the frogs travel at different speeds so that some frogs are more likely to win than others. Of the player bets $1 on a frog that's more likely to win, you pay them less than $4. For frogs that are more likely to lose, pay the player more than $4 if that frog wins.
  3. Make the frogs have different sizes.
  4. Allow for races with an aribtrary number of frogs.
  5. Draw horizontal lines to indicate the racing lanes and number each lane to make it easier to identify each frog.
  6. Allow the user to click on the frog they think will win instead of having them type in their choice into the terminal window.
  7. Let frogs slip backwards or take a nap on the racetrack by letting zero and negative values be generated by your randrange function.
  8. Give a frog a boost when a user clicks on the belly of a frog during the race. To do this, you may find the checkMouse method useful. It is similar to getMouse, but does not pause for a user click. It returns the latest point where the mouse was clicked or None if the window as not been clicked since the previous call to the checkMouse or getMouse methods.
  9. Anything else you want! Be creative - but use functions while being creative!
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.

You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize, after handing in some programs, that you'd like to make a few more changes to them.