How can i create Turtle clones and append them into a list?

Viewed 238

I want to write a Program, that draws 5 squares in 3 rows. Every second square is a different color.

So I have prepared the squares so far

sq1 = turtle.Turtle()
sq1.shape("square")
sq1.hideturtle()
sq1.color("red")
sq1.begin_fill()

sq2 = turtle.Turtle()
sq2.shape("square")
sq2.hideturtle()
sq2.color("black")
sq2.begin_fill()

As you can imagine, typing in 15 clone.turtles and a different location for each of them will take forever. My goal is to create a simple list for this purpose. Just I don't know how to put all of the different squares into the list and how to manage to move them to different locations.

2 Answers

Create the turtles in a for loop, and append the turtle to a list in each iteration.

You can define a function:

import turtle

def draw_squares(rows, cols, x=0, y=0, size=20):
    colors = ['red', 'black']
    square = []
    for i in range(cols):
        for j in range(rows):
            t = turtle.Turtle('square')
            t.penup()
            t.shapesize(size / 20)
            t.goto(i * size + x, j * size + y)
            t.color(colors[(i + j) % 2])
            square.append(t)
    return squares

squares = draw_squares(3, 5, size=40)

Output:

enter image description here

Where x and y are the coordinates of the center of the bottom-left square, size is the size of each square in pixels, and rows and cols are the number of rows and columns in the checkerboard.

UPDATE

As requested in the comments, here is how we can change the width, height and spacing between each rectangle:

import turtle

def draw_squares(rows, cols, x=-170, y=0, w=80, h=40, space=5):
    colors = ['red', 'black']
    square = []
    for i in range(cols):
        for j in range(rows):
            t = turtle.Turtle('square')
            t.penup()
            t.shapesize(h / 20, w / 20)
            t.goto(i * space + i * w + x,j * space + j * h + y)
            t.color('white', colors[(i + j) % 2])
            square.append(t)
    return squares

squares = draw_squares(3, 5)

Output:

enter image description here

Where w is the width, h is the height and space is the spacing.

Since you mentioned cloning turtles, let's do just that using a prototype and turtle's clone() method:

from turtle import Screen, Turtle

COLORS = ['black', 'red']

CURSOR_SIZE = 20

def draw_squares(rows, columns, height=45, width=85, border=5):
    prototype = Turtle('square', visible=False)
    prototype.shapesize(height / CURSOR_SIZE, width / CURSOR_SIZE, border)
    prototype.pencolor('white')
    prototype.penup()

    x = -width / 2 * (columns // 2 * 2 + columns % 2 - 1)  # center on widow
    y = -height / 2 * (rows // 2 * 2 + rows % 2 -1)

    squares = []

    for column in range(columns):
        for row in range(rows):
            square = prototype.clone()
            square.fillcolor(COLORS[(column + row) % len(COLORS)])
            square.goto(column * width + x, row * height + y)
            square.showturtle()
            squares.append(square)

    return squares

screen = Screen()

squares = draw_squares(3, 5)

screen.exitonclick()

enter image description here

Related