Interactions between two lists in Python turtle

Viewed 51

I've got a loop that detects collisions between turtles (moving balls) in a list. I'd like to create a second list (of stationary balls) and have the same collision detection. E.g. if a red and a green ball interact, turn the green ball red. What I'm seeing is that some stationary balls will change while most do not - no idea why.

while True:
#moving balls
    wn.update()
    for ball in balls:
        ball.sety(ball.ycor() + ball.dy)
        ball.setx(ball.xcor() + ball.dx)
        for other_ball in balls:
            if (other_ball is ball):
                # We are not interested in balls colliding with themselves.
                # Skip the current iteration of the inner for-loop, and move on to the next ball
                continue

            if is_collided_with(other_ball, ball) and (ball.color()!=other_ball.color()):
                ball.color("red")     
            if is_collided_with(ball, nidas):
                ball.color("red")

#BARRIER ON
        if not_safe_top(ball):
                ball.dy *=-1
                ball.dx *=-1
        if not_safe_bottom(ball):
                ball.dy *=-1
                ball.dx *=-1
        if ball.ycor() <-400:
                ball.dy *=-1
        if ball.ycor() >+400:
                ball.dy *=-1
        if ball.xcor() >+400:
                ball.dx *=-1
        if ball.xcor() <-400:
                ball.dx *=-1

#stationary balls
    wn.update()
    for ball_s in balls_stat:
        for other_ball_s in balls_stat:
            if (other_ball_s is ball):
                # We are not interested in balls colliding with themselves.
                # Skip the current iteration of the inner for-loop, and move on to the next ball
                continue
            if is_collided_with(ball_s, ball) and (ball.color()!=ball_s.color()):
                ball_s.color("red")      
1 Answers

The code isn't clear but I think that you should use ball.update() to update the turtle ball. This draws any changes to the screen (like colors)

Related