0

I'm having trouble making all of the turtles draw a circle all together because I'm using for loops to make those turtles orbit one by one,

from turtle import Turtle, Screen
import random

w = Screen()
w.bgcolor('#222')

def color_generator():
    symbols = [str(n) for n in range(5, 10)] + ['a', 'b', 'c', 'd', 'e', 'f']
    pick = [random.choice(symbols) for _ in range(3)]
    color = ''.join(pick)
    return f'#{color}'



class Particle(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('turtle')
        self.penup()
        self.create_particle()

    def create_particle(self):
        self.color(color_generator())
        self.goto(random.randint(-350, 350), random.randint(-350, 350))
        self.seth(random.randint(0, 360))

    def orbit(self):
        self.speed('fast')
        self.pendown()
        for _ in range(25):
            self.forward(10)
            self.right(15)


for _ in range(5):
    t = Particle()
    t.orbit()


w.exitonclick()

how can I make all of them orbit all together?

here's what it's doing:

turtles' image 1

turtles' image 2

python_user
  • 5,375
  • 2
  • 13
  • 32
Aicreator
  • 1
  • 1
  • 2

1 Answers1

2

We can simplify this by only simulating threading by interlacing the motion of our particles:

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

def color_generator():
    symbols = [str(n) for n in range(5, 10)] + ['a', 'b', 'c', 'd', 'e', 'f']
    pick = [choice(symbols) for _ in range(3)]
    color = ''.join(pick)
    return f"#{color}"

class Particle(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('turtle')
        self.speed('fastest')
        self.create_particle()

    def create_particle(self):
        self.color(color_generator())
        self.penup()
        self.goto(randint(-350, 350), randint(-350, 350))
        self.setheading(randrange(360))

    def arc(self):
        self.pendown()
        self.forward(10)
        self.right(15)

screen = Screen()
screen.bgcolor('#222')

particles = [Particle() for _ in range(5)]

for _ in range(24):
    for particle in particles:
        particle.arc()

screen.exitonclick()
cdlane
  • 40,441
  • 5
  • 32
  • 81