CS21 Lab 6: Functions

Due Saturday, October 22, before midnight

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.


Programming tips

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.


1. Stack Diagrams

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.


2. Snail Race

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


Designing the race

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.

  1. Create a graphics window.

  2. Draw text to the window.
    • At several points throughout the program, you will need to write text to the window. Create a function displayCenteredText(window,text,y-coord)
    • Your function takes in the window to draw the text on, a string containing the text to draw, and the y-coordinate for where to draw the message.
    • Your function should create a Text object given the text string. The x-coordinate should be the middle of the window.
    • Finally, the Text object should be drawn to the window and then returned so it can be undrawn later.
    • Test your function by creating the "Snail Derby" banner shown below.
    • You may also want to use this function to give instructions to the user (e.g., "click to start" and announcing the winner).

  3. Draw a starting line and a finish line in the graphics window.
    • To create and draw the starting line and the finish line, write a function called drawRacingLines(window, start, end).
    • This function has three parameters: the graphics window you are drawing to , the x-coordinate of the starting line, and the x-coordinate of the finish line.
    • The function does not have a return value. At this point, your program should resemble the following image.


  4. Create and draw your contestants of different colors on the starting line.
    • Create Donald and Hillary (and maybe a Jill and Gary if you dislike our two-snail system) using different colors of your choosing in a function called createSnails(window, start).
    • This function has two parameters: the graphics window you are drawing to and the x-coordinate of the starting line.
    • The snails should be evenly spaced on the starting line (see below).
    • The snails can be whatever size and color you'd like as long as they all fit on the screen and don't overlap.
    • Put the snails you created into a python list, which should be returned by the function.


  5. Race the snails.
    • After waiting for a user click, you will want to begin the snail race.
    • Write a function called race(snailList, end) that has two parameters, the list of snails snailList and the x-coordinate of the finish line, end.
    • In this function, you will repeatedly move the snails until one of them has crossed the finish line. Specifically, your movement entails:
      • Move each snail in the list forward by some random value. We suggested randrange(5) as it makes for an interesting race, but you can play with different options.
      • After each round of movements, check if any snail has passed the finish line. If so, return the winning snail (HINT: you may want to use the function below to accomplish this).
      • The return value from this function should be the winning snail



  6. Determine the winner (if any)
    • Write a method, winner(snailList, end), that checks to see if the race has been won.
    • For each snail in the snailList, see if it has passed the finish line. To do this, utilize the Snail class's frontPoint() method, which returns a Point object representing the right-most point of the snail. Compare the x-value of this point to the finish line coordinate (end).
    • If two snails have passed the finish line, return the snail further to the right. Break ties as you see fit.
    • The return value from this function should be the winning snail or None if there is no winner.


Here is an example full run (with some added bells and whistles).

Extra challenge 1 - Talent showcase

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).


Extra challenge 2 - Snail trail

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".




Submit

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.