2

I have an object, let's call it 'ant', and I made it move, and every 10 second I make a clone of it, but the clone doesn't follow the same function that the ant had.

Is there a way to make the clones follow the same function? There will be about 10 or 20 more clones.

import turtle
import random

#make a variable moves and create an object ant
moves = 0
ant = turtle.Turtle()

#I make the direction of the ant random
ant.setheading(random.randint(0, 360))

#create A definition that makes the ant move and creates a clone
def move():
    ant.forward(10)

    if moves == 10:
        ant.clone()

    else:
        moves += 1

move()
10
  • Yes, there is a way to do this. If you want to know how, then you should share a minimal reproducible example of your code and share some examples of how you want this to work. Because you're just asking if it's possible right now. You're making us have to extrapolate too much about how things should work. Commented Feb 24, 2023 at 17:39
  • I have added the code snippet Commented Feb 24, 2023 at 17:46
  • Thanks for adding some code. I suggest you edit it to a minimal reproducible example so that people can understand and resolve your problem. Commented Feb 24, 2023 at 17:47
  • the thing is There is not really any code that needs fixing. I need to add code Commented Feb 24, 2023 at 17:51
  • 1
    Thanks for adding code. Voted to reopen, although if you could edit to fix your indentation, that'd be fantastic. You probably want a list of turtles, loop over them and run the same code for each one. Commented Feb 24, 2023 at 18:22

1 Answer 1

0

If you put your logic for one ant in a function, you can then use Screen().ontimer(run_ant, 10_000) to kick off a new ant every 10 seconds.

from random import randint
from turtle import Screen, Turtle


def run_ant():
    ant = Turtle()
    ant.setheading(randint(0, 360))

    for _ in range(10):
        ant.forward(10)


def run_ants():
    Screen().ontimer(run_ant)

    if len(Screen().turtles()) < 10:
        Screen().ontimer(run_ant, 10_000)


run_ants()
Screen().exitonclick()

Note that these turtles are never garbage collected, so the app will slow down if you keep doing this. See this post for details.

You may also see strange errors like (_tkinter.TclError: invalid command name ".!canvas") if the turtle screen closes while a turtle is in the midst of executing an operation in a different thread. I'm not sure if you even want to exit on click, so I won't dive into this--I'm using it as a convenience. Most animations use tracer(0) and update() to render each frame, which avoids the error (but the code gets a bit more complex and out of scope).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.