在python中碰撞时更改乌龟的颜色

时间:2020-03-26 03:13:58

标签: python if-statement ninja-turtles

试图改变一只乌龟与另一只乌龟的颜色。我想要的是:奈达斯(一只乌龟)与绿色碰撞变成红色。红色|绿色碰撞>>绿色变为红色。红色|红色或绿色|绿色无效。发生的是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")

1 个答案:

答案 0 :(得分: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")