Run update21, if you haven't already, to create the
cs21/labs/05 directory. Then cd into your cs21/labs/05
directory and create the python program for lab 5 in this directory
(handin21 looks for your lab 5 assignments in your cs21/labs/05 directory):
$ update21
$ cd ~/cs21/labs/05
$ pwd
/home/your_user_name/cs21/labs/05
Your programs are graded on both correctness and style. Please review
the comments regarding programming style on the main page.
For this lab we will continue using the
Zelle graphics library and we will introduce the Snail library.
Note that there is also a written portion to
this week's lab that is due in class, Tuesday February 21.
Snail Racing
In this week's lab, you will write a program that will simulate the
exciting, fast-paced world of snail racing! You will write your snail
racing program using functions, making use of a new Snail class.
Trying out the Snail class
Included in the lab05 directory is sample.py. This
program shows you how to use the Snail class. You use Snails
just like you used Circles from the graphics library:
- To create a Snail called gary: gary = Snail(<centerPoint>, <radius>)
- To move gary the Snail: gary.move(<x_distance>, <y_distance>)
- To set the fill color of gary's shell: gary.setFill(<color>)
- To clone gary: speedy = gary.clone()
- To draw gary in a graphics window called win: gary.draw(win)
- The getRadius() and getCenter() methods also work, returning the radius of the Snail's big shell and the center Point of the Snail's big shell.
The only method that is different for Snails is that they have a
method called front_eye() that returns the Circle representing
the front-most (and lower-down) eye of the snail. We're going to need
to know about that circle because it will tell us whether or not our
snail has crossed the finish line!
Try running the sample.py program and play around with the Snails a
bit until you are comfortable with how to use them.
Designing our snail 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. You should be sure to test out your program
and functions after each of the steps below. It is important to get in the
habit of testing small segments of code before moving on.
Here are the major steps that your program will need to accomplish:
- Create a graphics window.
- Draw a starting line and a finish line in the graphics window.
- Your snails will race from near the left side of the window to near
the right side 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 x-coordinate of the starting line start, and the x-coordinate of the finish line finish.
- The function will draw the starting and finish line on the window in the specified locations. The lines extend across the entire window vertically,
so you should define your y-coordinates to be the top and bottom of the window
- The function does not have a return value.
- Create and draw four snails of different colors.
- You will create your four snails of any four different colors you choose in a function called create_snails(window).
- This function has one parameter: the graphics window you are drawing to, window.
- This function will create and then draw four snails.
- All of the snails 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 snails are the same size, but if you want them to be different sizes, do that as an extension.
- You will put the four snails you created into a list.
- The list of snails that you created is the return value from this function.
- Place the snails at the starting line.
- The four snails you created in the create_snails function are all located at Point(0,0).
- Write a function called move_to_starting_line(snail_list,
start) that takes the list of snails snail_list and the
x-coordinate of the starting line as the only parameters.
- This function will move each snail in the list of snails to its starting location in front of the starting line. Each snail will have a different y-value
which is upto you to decide, but the snails should not overlap.
- This function has no return value.
- Ask the user which snail they think will win the race.
- Print out an introductory message to the terminal window.
- Ask the user to choose a snail they think will win. This does not have
to be done in its own function, but you may feel free to do so.
- Race the snails.
- Write a function called race(snail_list, end)
that has two parameters, the list of snails snail_list and
the x-coordinate of the finish line, end.
- In this function, you will repeatedly perform the following actions in a for-loop:
- Move each snail in your snail 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 center of the front_eye of any snail is past the finish
line, you will return from the function.
- Although you call return, this function has no return value.
- Note that the range on this for loop should be quite large, e.g. 10000, to esnure that at least one snail reaches the finish line.
- Determine the winner of the race.
- Because you returned from the race function, you know that at least one snail has crossed the finish line, but many snails may have in fact
crossed the finish line and you need to figure out which snail has
crossed the finish line by the largest amount.
- Write a function called winner(snail_list) that takes the list of snails as its only parameter.
- This function determines which snail has moved the furthest to the right in the graphics window.
- The return value from this function is the number of the snail
that won, with the snails labeled from 1 (the top-most snail) to 4
(the bottom-most snail).
- 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 snail 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 snail 2. The snails are numbered 1, 2, 3, 4, starting from the top.
Extensions
There are lots of opportunities for extending this lab.
- 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.
- Have the snails travel at different speeds so that some snails are
more likely to win than others. Of the player bets $1 on a snail
that's more likely to win, you pay them less than $4. For snails that
are more likely to lose, pay the player more than $4 if that snail
wins.
- Leave a slime trail behind the snail as it races.
- Make the snails have different sizes.
- Allow for races with an aribtrary number of snails.
- Draw horizontal lines to indicate the racing lanes and number each
lane to make it easier to identify each snail.
- Allow the user to click on the snail they think will win instead of having them type in their choice into the terminal window.
- Let snails slip backwards or take a nap on the racetrack by
letting zero and negative values be generated by your randrange
function.
- Anything else you want! Be creative - but use functions while being creative!
Submit
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.