This lab assignment requires you to complete some written work as well as implement a Python program using objects from the Zelle graphics library. You will also use new graphics objects that we have provided. You will create a simulation in which two snails, Donald the red snail and Hillary the blue snail, race against each other.
Remember, we will only grade files submitted by handin21 in your cs21/labs/06 directory.
As your programs become larger, it is even more important to develop good habits:
The above are mandatory and you will be graded on them.
We strongly advise you to write your programs incrementally and test them as you go. No one writes interesting programs all at once. As you write, test that your program does what you expect (even if it doesn't yet do what you want). If it surprises you, make sure you understand what's happening before writing more code.
We encourage students to explore additional ideas as labs become more advanced. After you have assured yourself that you have met the lab requirements, please consider being creative with your solutions by e.g., adding extra features or applying new ideas.
The first part of the lab involves you tracing a program with functions and showing the resulting stack that the function calls generate. This is excellent practice for what will almost certainly be a quiz question! Download the PDF here, print it out, and turn it in at the start of class on either Thursday (Soni) or Friday (Knerr), depending on when your class meets.
Disclaimer: any connection between this lab and the current political environment is mere coincidence. Just because you can replace the word snail with politician doesn't mean we intended that to be the case.
Snails are known for two things - being slimy, and running races. And their races can feel like they have been going on for years and just need to end already. Hillary the blue snail and Donald the red snail are bitter rivals. Help them settle their differences in the way typical of snails: a slimy race. Use the Zelle graphics library and the Snail class we provide to simulate and animate a multi-snail race. Put your code in a file called snailrace.py.
The race should begin with your contestants lined up on a vertical starting line, facing a vertical finish line some distance away. You will repeatedly move the players towards the finish line using random choices and movements. It is okay if the snails jump forward, rather than gliding smoothly forward. When one snail reaches the finish line, some text should be displayed in the window announcing the winner, and the game should end. At this stage the user should be able to close the window by clicking anywhere in it.
To import the Snail class, add the following line of code to your snailrace.py file:
from snail import *
The Snail constructor is similar to the Circle constructor. It takes a Point and a radius and creates a Snail whose shell is a circle centered at the provided point with the provided radius. Additionally, you can pass the name of the snail as a third argument. The methods for the Snail class that you'll need are draw(window), setFill(color), and move(x, y), which behave just as they do for the shapes we've already seen, as well as getName(), which returns the name of the snail, and frontPoint(), which returns the right-most point on the snail's body. This link contains more information on the Snail class, including an example program. You should test out the Snail class in the Python interpreter before starting this assignment
Since this is the first time you've had to write a large program with many 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 will be graded on the proper use of functions. Below are our suggestions - you can use more functions as you see fit.
Here are the major steps that your program will need to accomplish. These are pretty much in order. Remember to test each function as you write them. A general strategy is that each step below describes a function. The detailed list items should be used to define the details of your function. Your main method ties it all together by calling the functions in the appropriate order with the needed arguments.
This does not affect your grade so please only attempt this after completing the rest of your lab
In the requirements above, each snail moves randomly within the same range of options. In reality, snails have varying skills which make them faster or slower. For example, Donald may like to pause and insult his competitors which slows him down. Or Hillary is storing her speeches in her shell which weighs her down. As an extension, add some variability to each contestant's range. For example, weight movements by the candidates current polling average. Or try out racing snails with different top end speeds (currently 5) as well as higher minimums (currently 0).
This does not affect your grade so please only attempt this after completing the rest of your lab
Snails, like some politicians, are slimy. Simulate their sliminess by displaying a green slime trail as the snails advance. Note that it may be difficult to combine this extra challenge with the other extra challenges. Feel free to pick the one extra challenge that most appeals to you. Also feel free to do no extra challenges at all. Below is a snapshot from a race with this extra challenge implemented. The color of the snail trail is "LimeGreen".
Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.