Lecture 6 - Turtles 2

Lecture Date: Monday, February 1

We’ll keep going with turtle today, finishing up whatever we didn’t complete on Friday.

We’ll also add some more excitement!

  • random colors
  • user input
  • how to draw a circle
  • how to draw a tree
drawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Mark Sherriff (mss2x)

import turtle
import random

def draw_shape(t, x, y, sides):
    t.penup()
    t.goto(x,y)
    t.pendown()
    rand_color = random.randint(0,len(colors)-1)
    t.color(colors[rand_color])
    t.begin_fill()
    for i in range(sides):
        t.forward(100)
        t.left(360.0/sides)
    t.end_fill()

def draw_square(t, x, y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    rand_color = random.randint(0,len(colors)-1)
    t.color(colors[rand_color])
    for i in range(4):
        t.forward(100)
        t.left(90)

tom = turtle.Turtle()
tom.speed("fastest")

colors = ["green", "red", "yellow", "orange", "pink", "cyan"]

for i in range(3):
    rand_x = random.randint(-100,100)
    rand_y = random.randint(-100,100)
    rand_sides = random.randint(3,12)
    draw_shape(tom,rand_x,rand_y, rand_sides)

draw_shape(tom,0,0,30)

# draw_square(tom)


turtle.done()
tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Mark Sherriff (mss2x)


import turtle

def draw_tree(t, depth, size):

    if depth == 0:
        return

    if depth == 1:
        t.color("green")
    else:
        t.color("black")
    t.forward(size-depth*2)
    t.left(40)
    t.forward(size-depth*2)
    draw_tree(t, depth-1, size-5)
    if depth == 1:
        t.color("green")
    else:
        t.color("black")
    t.back(size-depth*2)
    t.right(80)
    t.forward(size-depth*2)
    draw_tree(t, depth-1, size-5)
    if depth == 1:
        t.color("green")
    else:
        t.color("black")
    t.back(size-depth*2)
    t.left(40)
    t.back(size-depth*2)



tom = turtle.Turtle()
tom.left(90)
tom.penup()
tom.back(150)
tom.pendown()
tom.speed("fastest")
draw_tree(tom, 5, 20)

turtle.done()