Commands are how we tell Tracy the Turtle to do things.
Tracy knows several built-in commands.
# The forward command makes Tracy move forward a given distance forward(10) forward(50) forward(200) # The backward command makes Tracy move backward a given distance backward(10) backward(50) backward(200)
# Tracy will turn 90 degrees to the left left(90) # Tracy will turn left 180 degrees and # face the opposite direction left(180) # Tracy will turn left a full 360 degrees # and do a complete spin left(360) # The right command does the same thing, # but Tracy turns right instead of left right(90) right(180) right(360)
# Draws a red line color("red") forward(50) # Draws a blue line color("blue") forward(50) # Some color options: color("black") color("blue") color("brown") color("cyan") color("gold") color("gray") color("green") color("indigo") color("orange") color("pink") color("purple") color("red") color("violet") color("white") color("yellow")
# To draw a filled shape, start with this call begin_fill() # Draw your shape for i in range(3): forward(150) left(120) # Stop filling once you have drawn the shape end_fill()
# Lift the pen up # Tracy will not leave a trail when she moves penup() # Put the pen down # Tracy will leave a trail when she moves pendown() # Change the size of the pen # Normal, line width of 1 pensize(1) # Bigger, line width of 10 pensize(10)
We can set Tracy's speed using the
Speed options go from 0 to 10
# Slowest speed(1) # Normal speed(6) # Fast speed(10) # Fastest (no animation at all) speed(0)
Tracy is positioned in an (x, y) coordinate plane like so:
Each point in Tracy's world can be located by an (x, y) coordinate. For example:
We can use the
setposition command to send Tracy to a specific location
# Send Tracy to the center of the world setposition(0, 0) # Send Tracy to the far right middle of the world setposition(100, 0) # Send Tracy to the top right corner of the world setposition(200, 200) # Set Tracy's x position to be 100 # The y position stays the same setx(100) # Set Tracy's y position to be 100 # The x position stays the same sety(100) # This also sends Tracy to the center of the world home()
# Make Tracy look like an arrow shape("arrow") # Make Tracy look like a turtle shape("turtle") # All shape options: shape("turtle") shape("arrow") shape("triangle") shape("circle") shape("square") shape("classic")
Use for-loops when you want to repeat something a fixed number of times.
Make sure everything inside the loop is indented one level!
for i in range(COUNT): # Code to be repeated COUNT times
# Tracy will turn left 1 degree 360 times # Resulting in one complete spin for i in range(360): left(1) # Instead of repeating code over and over forward(10) left(90) forward(10) left(90) forward(10) left(90) forward(10) left(90) # Use a for loop to repeat the code! for i in range(4): forward(10) left(90) # You can use the i variable inside the loop # i starts at 0, and goes up to COUNT-1 # This will make Tracy move forward 0, then 1, then 2 for i in range(3): forward(i)
Writing a function is like teaching Tracy the Turtle a new word.
Naming Functions: You can name your functions whatever you want, but you can't have spaces in the function name. Instead of spaces, use underscores _ like_this_for_example
Make sure that all the code inside your function is indented one level!
We define a function to teach Tracy the instructions for the new word.
def name_of_your_function(): # Code that will run when you make a call to # this function. # EXAMPLES # Teach Tracy to draw an edge def draw_edge(): forward(50) left(90) # Teach Tracy to draw a blue edge def draw_blue_edge(): color("blue") foward(50) left(90) # Teach Tracy to draw a square def draw_square(): for i in range(4): draw_edge()
We call a function to get Tracy to actually carry out the new command.
# Call the draw_edge() function 3 times # Tracy will draw 3 edges draw_edge() draw_edge() draw_edge() # Call the draw_square() function once # Tracy will draw a square draw_square() # Call the my_function() function once # Tracy will carry out all the instructions inside of my_function() my_function()
We use comments to leave notes about the code to the reader. Comments are not actually run by Python, they are just there to help us read the code.
We can make multiline comments with
""" and single line comments with
""" A multi-line comment describes your code to someone who is reading it. """ # Use single line comments to clarify code.