Lecture 5 - Turtle Power!

Lecture Date: Friday, January 29

Today, we are going to move a bit quick, but mainly so you can see a quick overview of a lot of concepts.

First, you need to get everything setup to go!

  1. Open PyCharm and either make a new project or open an existing one.

  2. Make a new Python file called turtle_drawing.py.

  3. Start by making a new turtle!

Example Turtle Code
1
2
3
4
5
6
7
import turtle

tom = turtle.Turtle()

tom.forward(50)

turtle.done()

Let’s start making this a bit more complex. We’ll work up to the code below.

Final Code

Example Turtle Code
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
# Mark Sherriff (mss2x)

import turtle
import random

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(10):
    rand_x = random.randint(-100,100)
    rand_y = random.randint(-100,100)
    draw_square(tom,rand_x,rand_y)

# draw_square(tom)


turtle.done()
Example Turtle Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import turtle
def drawShape(t, sides, size):
    angle = 360.0 / sides
    for i in range(sides):
        if t.color()[0] == "red":
            t.color("black")
        else:
            t.color("red")
        t.forward(size)
        t.left(angle)
dana = turtle.Turtle()
drawShape(dana, 6, 100)
dana.right(180)
drawShape(dana, 5, 100)
turtle.done()