python - 在python中改变海龟的颜色
问题描述
试图在一只乌龟与另一只碰撞时改变它的颜色。我想要什么: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")
解决方案
想出了一个解决办法;
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")
推荐阅读
- gitlab - 我可以限制用户访问我的 gitlab 存储库但提供读/写访问权限吗
- location - 将 FakeGPS 与 FusedLocationProviderClient 一起使用的方法
- ios - Xcode Auto Layout 不断改变我的布局
- javascript - Javascript:如何动态调用方法并为其动态设置参数
- c++ - 每次我想构建一个新的 c/c++ 项目时,是否需要在 vscode 中创建一个 tasks.json 文件?
- java - 我想从使用 Retrofit2 获得的 json 数组中获取特定对象的列表。我该怎么做呢?
- blazor - 在泛型 Blazor 子控件中调用方法
- testflight - 如何使用 Fastlane 触发 beta 审核?
- javascript - 嵌套对象上的 Join(';')
- python - 在循环中使用 ActionChains