
In this week's lab, you will create a collage of "turtle tracks" that are left as multiple turtles with unique shapes and colors move from top-to-bottom across the canvas (like footprints left in the snow). The animation of the turtles drawing the collage resembles a colorful version of code rain from The Matrix.
The goals this week are to:
Create a Lab04 folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)
Open VS Code. Create a new Python source code file calledlab4.py. Import the random and turtle modules.
Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab04 folder.
Screen and Turtle objectsThis week, we will use objects to divide up the labor of creating a canvas and drawing shapes with turtle. The division of labor occurs among 2 types of objects:
Screen: You will create a Screen object that represents the canvas (pop-up window in which each turtle draws stuff). This object will allow you to store and change information relating to the canvas window ("screen") (i.e. its height and width, whether it stays up waiting for you to click, its background color, etc.)Turtle: You can now create more than one turtle that draws on the screen at once! Each turtle is a different object that you create. You will control each independently and customize each one with unique properties: each can have different pen widths, colors, shapes, and locations.In this task, you will create an object for the screen and several different turtles.
Screen objectWrite a function in lab4.py that has the following docstring:
def make_screen(width, height, title, color='black'): '''Makes a `Screen` object: the window in each turtles can draw shapes Parameters: ----------- width: int. The width of the window/screen in pixels. height: int. The height of the window/screen in pixels. title: str. The name that you'd like to call the pop-up window. Appears at the center of top of the window. color: str. Color string name (e.g. 'black', 'white', etc). This is the background color of the screen. Returns: ----------- The `Screen` object that you create. ''' # your code here
It may be helpful to have class notes and the turtle documentation open as you work. Your function should accomplish the following:
Screen object.Reminder: With objects, you call functions ON THEM, not on the turtle module (e.g. screen.setup(width, height), not:turtle.setup(width, height), assuming your Screen object is called screen).
Make a main function in lab4.py. In it, call your make_screen function and remember to assign a variable to the Screen object that make_screen returns. For example,
screen = make_screen(<plug in your 3 or 4 parameters here>). You can pass in whatever parameter values that you like when you call make_screen. Here are some suggestions:
Call the exitonclick method on your Screen object at the end of your main function.
Create the usual conditional statement for main code at the bottom of lab4.py (i.e.if __name__ == '__main__':). Call main in it.
Running lab4.py should give you a blank window like this, with your custom size and title (my background color is black and I used the Screen title "Turtle Tracks"):

Spend a moment to play with your title, window size, and background color to get a sense of how each property controls how the screen looks.
Turtle objectWrite a function that creates a single Turtle object that will appear on the canvas (represented by your Screen object) with the following docstring:
def make_turtle(shape='turtle', penColor='white'): '''Makes a `Turtle` object Parameters: ----------- shape: str. From the turtle documentation website, possible options are: 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic' penColor: str. Color string name (e.g. 'black', 'white', etc) that is to be the pen color of this turtle. Returns: ----------- The `Turtle` object that you create. ''' # your code here
Your function should do the following (again, it probably will be helpful to look at lecture notes and the turtle documentation):
Turtle object (calling your make_turtle function multiple times later on will allow you to create multiple, uniquely customized turtles).Turtle object that you created.Reminder: You should NOT be typing turtle.<stuff> except for the first line where you create the Turtle object! Instead, call methods on your turtle object.
Turtle objectsAfter creating your Screen object in main, but before you call exitonclick, call make_turtle 3 times. This will create 3 turtles that show up on your canvas. Like you did with make_screen, remember to assign the returned object from each make_turtle call to a distinct variable so that you can control each one later (e.g. turt1, turt2, turt3).
Move your first turtle to (x, y) = (-200, 0) by calling the goto method on it, the second turtle to (-50, -50), and the third turtle to (100, 100) (by calling the goto method on each of them).
Running your code should give you something that looks like:

Take a moment to have some fun playing with shapes and colors of each turtle. What do each of the built-in turtle shapes look like?
Let's start to create a colorful collage of the "tracks" left behind by each turtle as they move from top-to-bottom across the screen. First, you need to place each turtle in their starting position at the top of the screen.
reset_turtle functionWrite your function with the following signature:
def reset_turtle(turt, screen_width, screen_height):
Your function should do the following with the Turtle object passed in as a parameter (turt):
setheading).Hint: The center of the screen is position (0, 0).
The x-coordinate of the left edge of the screen is -screen_width/2 and that of the right edge of the screen is screen_width/2.
The y-coordinate of the bottom edge of the screen is -screen_height/2 and that of the top edge of the screen is screen_height/2.
main, place your turtles on the starting lineCall your reset_turtle function 3 times in main, once on each of the 3 turtles. You should get something that looks like this (your positions will be different due to randomness):
Tip: If one or more of your turtles are not showing up, try placing them 10 pixels or so below the top of the screen (e.g. screen_height/2 - 10). It is possible that they are up there, but just are not visible because they are slightly off-screen.

Write a function with the following signature that should move the turtle turt forward by distance and then stamp to leave a track.
def move_and_stamp(turt, distance):
In main, call move_and_stamp once on each turtle. Have your 3 turtles move different distances: 40, 30, 20, respectively. You should see something that looks like:

In main, repeat the process of moving all 3 turtles forward and having them leave a stamp 1000 times. If any of the turtles moves off the bottom of the screen (the y coordinate is too small), call your reset_turtle function on that turtle.
If everything is working when you run your code, you should see a collage like the one at the top of this page being created!
Once you get the collage working, have some fun and try:
speed method on your Turtle object when you make it.Turtle during each movement: randomly move each turtle between 0 and 10 units farther than the default distance.In this last task, you will make another Turtle object — but it will have the unique job of writing text on the screen (rather than leaving tracks or drawing shapes).
In main, define a counter variable that starts at 0 and gets incremented by 1 whenever any one of your turtles completes a stride from top-to-bottom of the screen and gets reset. In other words, this variable should keep track of how many times you call reset_turtle.
I suggest printing out the variable when you update it (i.e. add one to it) to help you debug / help convince yourself that your code is working.
This function will have a Turtle object write your current stride count on the screen.
Make a function with the following signature:
def writeNumStrides(turt, numStrides):
It should do three things:
clear method on the turt Turtle object. This will have the turtle erase the previous number to prepare to write the current one. For example, if numStrides was previously 1 (and 1 is shown on the screen), clear will erase that so that a new number (e.g. 2) can be written.turt.write(numStrides, font=('Arial', 30, 'normal')).hideturtle method on the Turtle object. This will make the turtle shape go invisible so that it does not block the number it just wrote!Turtle write the stride count on-screenIn main, create one new Turtle object — the shape does not matter, but I suggest using a white pen color if you are keeping the default screen background color of black (you want to be able to see the text on your background color!).
Place the new Turtle object somewhere on the screen (not the top). For example, I placed mine in the middle, 10 pixels higher than the bottom of the screen.
Whenever you call reset_turtle and increment your stride counter (i.e. assign it to one more than its current value), call the writeNumStrides function to have the latest stride count always appear on the screen.
Test out your collage with the new counter. You should see the stride count on screen and it should increase by 1 whenever a turtle reaches the bottom of the screen.
Once you are done with each of the lab tasks, you may start on the project.