0

试图在一只乌龟与另一只碰撞时改变它的颜色。我想要什么:Nidas(一只乌龟)与绿色相撞变成红色。红色|绿色碰撞>>绿色变为红色。红色|红色或绿色|绿色无动作。发生的事情是 Nidas|Green 碰撞变为红色(如预期),但 Red|Green 碰撞变为红色变为绿色。我怎么弄乱了if语句,

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, nidas):
            other_ball.color("red")
        if (
                is_collided_with(ball, other_ball) and
                other_ball.color("green") and
                ball.color("red")
            ):
            other_ball.color("red")
4

1 回答 1

0

想出了一个解决办法;

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")

            elif is_collided_with(ball, nidas):
                ball.color("red")
于 2020-03-27T14:42:51.353 回答