CS 100

Logo

Scientific Computing

Activity Turtle 🐢

In Thonny create a file named turtle-demo.py with the following content. Save it in the folder CS100/ch1.

  1. Run this program and match the output with your code. Understand each instruction is doing.

    import turtle
    
    bob = turtle.Turtle()
    print( bob )
    
    print( "Orig. pos: ", bob.position() )
    bob.forward(100)
    bob.right(90)
    bob.forward(50)
    print( "Later pos: ", bob.position() )
    print( "Direction: ", bob.heading() )
    
  2. Now, replace the existing code with the following one (or comment out the code from 1 using #) and run it.

    import turtle
    
    def drawSquare(myTurtle,sideLength):
       myTurtle.forward(sideLength)
       myTurtle.right(90)
       myTurtle.forward(sideLength)
       myTurtle.right(90)
       myTurtle.forward(sideLength)
       myTurtle.right(90)
       myTurtle.forward(sideLength)
    
    bob = turtle.Turtle()
    drawSquare(bob,150)
    
  3. Let’s add some color to your drawings! Replace the last two lines with:
    bob = turtle.Turtle()
    bob.pencolor('red')
    bob.fillcolor('yellow')
    bob.begin_fill()
    drawSquare(bob, 150)
    bob.end_fill()
    
  4. Add Python code that creates another square with side length of 300. Do you see both squares?

  5. Add another function drawTriangle that draws a triangle and try it out.

  6. Add your name as a comment at the top of your file.

  7. If you have time, experiment with changing each shape’s border color and fill color, and draw more shapes.

How to submit

Make sure you saved your work and that it runs without errors. Submit your turtle-demo.py file to Moodle.

Optional: if you have extra time

Create a new file called turtle-extra.py and save it to your cs100/ch1 folder. Copy and paste the following code into that new file and run it. Each function draws a different shape. You can comment / uncomment out by adding or removing the # character before the function call at the end. Try uncommenting out each one individually so that only one shape is drawn at a time.

Note: You are not expected to fully understand how these functions work just yet! This is to give you a preview of what interesting structures we’ll be able to make with turtles. But you can experiment a little with how the function operates. What can you change inside the function? Can you make the shapes bigger? What if you change the direction, or color? What do you think for in range(50) does?

import turtle 

def drawShape1(bob):
    for i in range(50):
        bob.forward(i * 10)
        bob.right(144)

def drawShape2(bob):
    for i in range(180):
        bob.forward(100)
        bob.right(30)
        bob.forward(20)
        bob.left(60)
        bob.forward(50)
        bob.right(30)
        
        bob.penup()
        bob.setposition(0, 0)
        bob.pendown()
        bob.right(2)

def drawShape3(bob):
    bob.pencolor("blue")
    for i in range(50):
        bob.forward(50)
        bob.left(123)
        
    bob.pencolor("red")
    for i in range(50):
        bob.forward(100)
        bob.left(123)

def drawShape4(bob):
    colors = ['orange', 'red', 'pink', 'yellow', 'blue', 'green']
    for x in range(360):
        bob.pencolor(colors[x % 6])
        bob.width(x / 5 + 1)
        bob.forward(x)
        bob.left(20)


coolBob = turtle.Turtle()
coolBob.speed(100)

# Call each function individually and comment the others out to see what it does.
drawShape1(coolBob)
#drawShape2(coolBob)
#drawShape3(coolBob)
#drawShape4(coolBob)