Skip to content Skip to sidebar Skip to footer

How To Initialize Multiple Turtles In Python With Classes

I am a beginner at Python and I'm new to Stack Exchange. I'm trying to write a program that has 5 turtles moving within a square. I've got code that does what I want, but it's tedi

Solution 1:

You should consider storing your turtles in a list, as the turtles are already objects and you don't need to create a class just to move them to your starting positions. Lists in Python are incredibly powerful because they can store arbitrary data types. Here, I will create 5 turtles and move them so you can tell them apart:

importturtlenum_turtles=5
my_turtles = [turtle.Turtle() for i in range(num_turtles)]
for i, turt in enumerate(my_turtles):
    turt.forward(50 * i)

You want to do the same thing, just replace my turt.forward() line with whatever you want the turtles to do. In your case, go to a random position within your square.

Solution 2:

only one turtle shown on screen. Two are defined in the code below. the turtle's heading and coordinates aren't being initialized.

I believe the problem is that you defined the random position and heading once, outside the turtle creation loop so they all start in the same place, move in the same direction at the same speed. I.e. they're right on top of each other.

We don't need @BlivetWidget's explicit List to fix the problem since, as you discovered, turtles are already maintained in a list which we can get via the screen's turtles() method. Below is my rework of your code to fix various issues:

from turtle import Screen, Turtle
from random import randrange, randint

# parameters
COLORS = ['green', 'blue', 'red', 'orange', 'white']
ITERATIONS = 500
VELOCITY = 5
BOX_SIZE = 512# setting up screen
screen = Screen()
screen.setup(BOX_SIZE + 50, BOX_SIZE + 50)
screen.bgcolor('black')
screen.tracer(False)

# drawing box
turtle = Turtle()
turtle.hideturtle()
turtle.color('cyan')

turtle.penup()
turtle.goto(-BOX_SIZE/2, -BOX_SIZE/2)
turtle.pendown()

for _ inrange(4):
    turtle.forward(BOX_SIZE)
    turtle.left(90)

# turtlefor color in COLORS:
    angle = randrange(360)
    x = randint(-BOX_SIZE/2, BOX_SIZE/2)
    y = randint(-BOX_SIZE/2, BOX_SIZE/2)

    turtle = Turtle()
    turtle.color(color)
    turtle.setheading(angle)
    turtle.penup()
    turtle.setposition(x, y)
    turtle.pendown()

# turtle movementfor _ inrange(ITERATIONS):
    for turtle in screen.turtles():
        turtle.forward(VELOCITY)

        x, y = turtle.position()

        if x >= BOX_SIZE/2:
            turtle.penup()
            turtle.setx(-BOX_SIZE/2)
            turtle.pendown()
        elif x <= -BOX_SIZE/2:
            turtle.penup()
            turtle.setx(BOX_SIZE/2)
            turtle.pendown()
        elif y >= BOX_SIZE/2:
            turtle.penup()
            turtle.sety(-BOX_SIZE/2)
            turtle.pendown()
        elif y <= -BOX_SIZE/2:
            turtle.penup()
            turtle.sety(BOX_SIZE/2)
            turtle.pendown()

    screen.update()

screen.exitonclick()

I agree with @BlivetWidget that "you don't need to create a class just to move them to your starting positions". I use a simple loop above.

enter image description here

Post a Comment for "How To Initialize Multiple Turtles In Python With Classes"