3

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?

cdlane
  • 40,441
  • 5
  • 32
  • 81

3 Answers3

0

You could remove the green ball from the green list and append it to the list for red balls. then you got to append the red ball to the green list and remove it from the red list.

Rik Smits
  • 31
  • 7
  • I'm trying to relate you answer to the code provided by the OP, and am drawing a blank. It could be that for the writer of the original code, you comment makes sense, but for stack overflow, you should be providing answers in greater level of detail, so as to make it useful for other readers, too. Try providing actual lines of codes, and where you suggest they should be placed. – Amitai Irron May 14 '20 at 22:25
0

Below is my simplification of your program that does what I believe you're trying to do. When you say:

I also want to make sure that when the new red ball touch the other green ball, the green ball will also change color to green.

What I assume you meant was:

... will also change color to red.

Rather than create explicit lists, the following code uses the library's own internal list of active turtles and uses the color of the turtle to determine what should happen when they collide:

from turtle import Screen, Turtle
from random import randint

screen = Screen()
screen.title("Simulator")
screen.tracer(False)

for uninfected in range(10):
    ball = Turtle()
    ball.shape('circle')
    ball.shapesize(0.5)
    ball.color('green' if uninfected else 'red')
    ball.penup()
    ball.dy = randint(-2, 2)
    ball.dx = randint(-2, 2)
    x = randint(-180, 180)
    y = randint(-180, 180)
    ball.goto(x, y)

while True:
    for ball in screen.turtles():
        x, y = ball.position()

        x += ball.dx
        y += ball.dy

        ball.setposition(x, y)

        if x < -200:
            ball.dx *= -1
        elif x > 200:
            ball.dx *= -1

        if y < -200:
            ball.dy *= -1
        elif y > 200:
            ball.dy *= -1

        # change the color when distance is near

        changed = True

        while changed:
            changed = False

            for other in screen.turtles():
                if ball == other or ball.pencolor() == other.pencolor():
                    continue

                if ball.distance(other) <= 10:
                    ball.color('red')
                    other.color('red')
                    changed = True

    screen.update()

screen.mainloop()  # never reached

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81
0

Change the end part of your code to:

# 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 :
                if gball.color()[0] != 'red':
                    gball.color("red")
                else:
                    gball.color("green")
Community
  • 1
  • 1
Red
  • 26,798
  • 7
  • 36
  • 58