1
from turtle import *
a = Screen()
b = Turtle()
c = Turtle()

def one():
    b.forward(100)
def two():
    c.forward(-100)

I want b and c moving away from each other at the same time, tried lots of stuff but I cant figure it out. Help me out please.

icantcode
  • 11
  • 1

1 Answers1

1

Borrowing all the code from Multithreading With Python Turtle, this appears to "work" for getting both functions to run at the same time.

If so, this is likely better a duplicate of linked answer.

import queue
import threading
import turtle

a = turtle.Screen()
b = turtle.Turtle()
c = turtle.Turtle()

def one():
    b.forward(100)
def two():
    c.forward(-100)

def process_queue():
    while not graphics.empty():
        (graphics.get())(1)

    if threading.active_count() > 1:
        turtle.ontimer(process_queue, 100)

graphics = queue.Queue(1)  # size = number of hardware threads you have - 1

turtle1 = turtle.Turtle('turtle')
turtle1.speed('fastest')
thread1 = threading.Thread(target=one)
thread1.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
thread1.start()

turtle2 = turtle.Turtle('turtle')
turtle2.speed('fastest')
thread2 = threading.Thread(target=two)
thread2.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
thread2.start()

turtle.mainloop()