Turtle Graphics

Even More on Functions

We can further improve our source code by introducing more functions:

import turtle

def draw_a_square_for_me(square_size):
    turtle.forward(square_size)
    turtle.right(90)
    turtle.forward(square_size)
    turtle.right(90)
    turtle.forward(square_size)
    turtle.right(90)
    turtle.forward(square_size)
    turtle.right(90)


def move_turtle_back():
    turtle.penup()
    turtle.backward(10)
    turtle.left(90)
    turtle.forward(10)
    turtle.right(90)
    turtle.pendown()


def draw_a_square_and_move_back(square_color, size_of_square):
    turtle.pencolor(square_color)
    draw_a_square_for_me(size_of_square)
    move_turtle_back()


turtle.penup()
turtle.goto(-50,50)
turtle.pendown()
turtle.pensize(5)
turtle.speed(7)
draw_a_square_and_move_back("Red", 100)
draw_a_square_and_move_back("Blue", 120)
draw_a_square_and_move_back("Green", 140)
draw_a_square_and_move_back("Yellow", 160)
draw_a_square_and_move_back("Orange", 180)
draw_a_square_and_move_back("Pink", 200)

turtle.done()