I have been watching this video on youtube--https://www.youtube.com/watch?v=horBQxH0M5A which creates a list of balls bouncing around.
I'm trying to alter the code to make one ball red, the rest green, and when the red ball "touches" a green ball, the green ball changes color to red. That wasn't hard, but I also want to make sure that when the new red ball touches another green ball, that green ball will also change its color to green.
What I did was create a single red ball and a list of green balls:
import turtle
import random
wn = turtle.Screen()
wn.bgcolor("white")
wn.title("simulator")
wn.tracer(1, 1)
# red ball
rball = turtle.Turtle()
rball.shape("circle")
rball.color("red")
# rball.penup()
rball.speed(1)
x = random.randint(-10, 10)
y = random.randint(-12, 12)
rball.goto(x,y)
# green ball
gballlist = []
for _ in range(5):
gballlist.append(turtle.Turtle())
for gballpeople in gballlist:
gballpeople.shape("circle")
gballpeople.color("green")
gballpeople.speed(1)
xh = random.randint(-10, 10)
yh = random.randint(-12, 12)
gballpeople.goto(xh, yh)
while True:
wn.update()
# rball.dy += acclerate
rball.dy = random.randint(-2, 2)
rball.dx = random.randint(-2, 2)
rball.setx(rball.xcor() + rball.dx)
rball.sety(rball.ycor() + rball.dy)
# list = [-1, 1]
# angel = random.choice(list)
angel = -1
if rball.xcor() < -100:
rball.dx *= angel
if rball.xcor() > 100:
rball.dx *= angel
if rball.ycor() < -100:
rball.dy *= angel
if rball.ycor() > 100:
rball.dy *= angel
for gball in gballlist:
gball.dy = random.randint(-2, 2)
gball.dx = random.randint(-2, 2)
gball.setx(gball.xcor() + gball.dx)
gball.sety(gball.ycor() + gball.dy)
# list = [-1, 1]
# angel = random.choice(list)
angel = -1
if gball.xcor() < -100:
gball.dx *= angel
if gball.xcor() > 100:
gball.dx *= angel
if gball.ycor() < -100:
gball.dy *= angel
if gball.ycor() > 100:
gball.dy *= angel
# change the color when distance is near
for gball in gballlist:
if abs(rball.xcor() - gball.xcor()) < 4 and abs(rball.ycor() - gball.ycor()) < 4 :
gball.color("red")
Any suggestions?