How to i do a proper list with one box that is being cloned?

44 views Asked by At

I don't understand how to make 1 box that is being cloned for multiple times where every second block has a different color. I thought of making a main box and shape it and then make a loop in which i clone it multiple times, but i cant make the loop to clone it always next to the other clone. Also when it comes to the colors i defined a list which gets gives me an error for this variable not being used i wanted to use it so make every second block in the other color.

def draw_boxes(box_list):

    box = Turtle()
    box.shape("square")
    box.shapesize(1, 3)

    color_list = ["cyan", "pink"]

    box_list = []

    #Box_layer1
    for box_layer1 in range(9):
        box_layer1 = box.clone()
        box_layer1.goto() # not filled in since i dont know how to make the clone go to the other clone

    # Box_layer2

    # Box_layer3
    pass

This seems to cause me a lot of troubles if anyone knows anything it would be amazing.

1

There are 1 answers

2
Red On BEST ANSWER

I see you are defining a Turtle object whenever the draw_boxes function is called. That would result in far too many Turtles. Instead, define the boxes_list outside of the function.

To iterate through the box_list along side each box's index so that the previous box can be accessed during each iteration, use enumerate.

The below code is a demonstration, where the Turtles move in a Slinky movement:

from turtle import Turtle, Screen

wn = Screen()

box_list = []
color_list = ["cyan", "pink"]

for i in range(10):
    box = Turtle("square")
    box.color(color_list[i%2])

    box_list.append(box)

def draw_boxes(head):
    for i, box in enumerate(box_list):
        if i:
            box.goto(box_list[i-1].pos())
        else:
            box.goto(head.pos())
        box.color(color_list[i%2])

head = Turtle("square")
head.color("pink")

wn.listen()
wn.onkey(lambda: head.right(90), "Right")
wn.onkey(lambda: head.left(90), "Left")

while True:
    head.forward(40)
    draw_boxes(head)

Output:

enter image description here

If you want the standard snake game movement:

from turtle import Turtle, Screen
from time import sleep

wn = Screen()
wn.tracer(0)

box_list = []
color_list = ["cyan", "pink"]

for i in range(10):
    box = Turtle("square")
    box.color(color_list[i%2])
    box_list.append(box)

def draw_boxes(head):
    for i, box in enumerate(box_list):
        if i < len(box_list) - 1:
            box.goto(box_list[i+1].pos())
        else:
            box.goto(head.pos())

head = Turtle("square")
head.color('yellow')

wn.listen()
wn.onkey(lambda: head.right(90), "Right")
wn.onkey(lambda: head.left(90), "Left")

while True:
    draw_boxes(head)
    head.forward(20)
    wn.update()
    sleep(0.2)

Output:

enter image description here


UPDATE:

Turns out this is supposed to be a box-breaker game, not a snake game.

For the box breaker game:

from turtle import Turtle, Screen

wn = Screen()
wn.setup(600, 600)
wn.tracer(0)

def create_boxes(rows, cols, x, y, w, h):
    color_list = ["cyan", "pink"]
    boxes_list = []
    for i in range(rows):
        for j in range(cols):
            box = Turtle("square")
            box.penup()
            box.shapesize(w, h, 3) # The 3 is the outline's width. If you don't want it, simply remove that argument.
            box.color("white", color_list[(j + i) % 2]) # The first argument, "white", is the outline color. If you don't want it, simply remove that argument.
            box.goto(x + h * 20 * j, y + w * 20 * i)
            boxes_list.append(box)
    return boxes_list

boxes_list = create_boxes(3, 9, -245, 230, 1, 3)
wn.update()

enter image description here