微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Python 中使用 Turtle 使用 if 语句匹配 x/y 坐标

如何解决在 Python 中使用 Turtle 使用 if 语句匹配 x/y 坐标

在下面的代码中,我让 new_turtle(其中 6 个)在屏幕上从左到右水平移动,而 bad_turtle 从下到上垂直移动,然后再返回。我正在寻找的是当 bad_turtle “遇到”或与 new_turtle 具有相同的 x、y 坐标时,我希望它“击中”的任何 new_turtle 变成棕色。我试图在下面的最后一个 if 语句中写下这个,但它不起作用。如何才能做到这一点?任何帮助/建议将不胜感激。

from turtle import Turtle,Screen
import random

is_race_on = False
screen = Screen()
screen.setup(width=500,height=500)
user_bet = screen.textinput(title="Make your bet!",prompt="Which turtle will win the race? Enter a color: ")
print(user_bet)
colors = ["red","blue","green","yellow","purple","orange"]
y_positions = [175,100,25,-50,-125,-200]
all_turtles = []

bad_turtle = Turtle(shape="turtle")
bad_turtle.up()
bad_turtle.goto(140,-200)
bad_turtle.right(270)

for turtle_index in range(0,6):
    new_turtle = Turtle(shape="turtle")
    new_turtle.color(colors[turtle_index])
    new_turtle.up()
    new_turtle.goto(-230,y_positions[turtle_index])
    all_turtles.append(new_turtle)

if user_bet:
    is_race_on = True

while is_race_on:

    for turtle in all_turtles:
        if turtle.xcor() > 230:
            is_race_on = False
            winning_color = turtle.pencolor()
            if winning_color == user_bet:
                print(f"You've won! The {winning_color} turtle is the winner!")
            else:
                print(f"You lost! The {winning_color} turtle is the winner!")
        rand_distance = random.randint(0,10)
        turtle.forward(rand_distance)
        # rand_bad = random.choice(y_positions_bad)
    rand_distance_bad = random.randint(20,40)
    bad_turtle.forward(rand_distance_bad)
    if bad_turtle.ycor() > 200 or bad_turtle.ycor() < -200:
        bad_turtle.right(180)
        bad_turtle.forward(rand_distance_bad)

    if bad_turtle.xcor() and bad_turtle.ycor() == new_turtle.xcor() and new_turtle.ycor():
        new_turtle.color("brown")

screen.exitonclick()

解决方法

@ErickY.Carreno 的回答是朝着正确方向迈出的一步,但遗漏了一个关键点。海龟在一个浮点平面上徘徊——当它们返回同一个地方时,它不一定是完全相同的地方(例如 13.99999 与 14.00001)。使用 == 是不可行的。因此,我们使用距离比较来测试碰撞:

if bad_turtle.distance(new_turtle) < 20:
    new_turtle.color("brown")

20 可以是最适合您的最小距离。

下一个问题是,您在代码中的错误位置发生了碰撞,并且它引用了 new_turtle,在比赛的这一点上它不再是一个活动变量。修复上述所有问题并调整程序的其他方面:

from turtle import Turtle,Screen
from random import randint

RUNNERS = [('red',175),('blue',100),('green',25),('yellow',-50),('purple',-125),('orange',-200)]

screen = Screen()
screen.setup(width=500,height=500)

is_race_on = False

user_bet = screen.textinput(title="Make your bet!",prompt="Which turtle will win the race? Enter a color: ")

print(user_bet)

if user_bet:
    is_race_on = True

bad_turtle = Turtle(shape='turtle')
bad_turtle.speed('fastest')
bad_turtle.up()
bad_turtle.goto(140,-200)
bad_turtle.right(270)

all_turtles = []

for color,y_position in RUNNERS:
    new_turtle = Turtle(shape='turtle')

    new_turtle.color(color)
    new_turtle.up()
    new_turtle.goto(-230,y_position)

    all_turtles.append(new_turtle)

while is_race_on:
    for turtle in all_turtles:
        if turtle.xcor() > 230:
            is_race_on = False

            winning_color = turtle.pencolor()
            if winning_color == user_bet:
                print(f"You've won!",end=' ')
            else:
                print(f"You lost!",end=' ')
            print(f"The {winning_color} turtle is the winner!")
        elif bad_turtle.distance(turtle) < 20:
            turtle.color('brown')
            all_turtles.remove(turtle)
        else:
            turtle.forward(randint(0,10))

    if abs(bad_turtle.ycor()) > 200:
        bad_turtle.right(180)
        bad_turtle.forward(abs(bad_turtle.ycor()) - 200)

    bad_turtle.forward(randint(20,40))

screen.exitonclick()
,

如果陈述不正确,您可以尝试更改此内容:

if bad_turtle.xcor() and bad_turtle.ycor() == new_turtle.xcor() and new_turtle.ycor():
        new_turtle.color("brown")

为此:

if bad_turtle.xcor() == new_turtle.xcor() and bad_turtle.ycor() == new_turtle.ycor():
        new_turtle.color("brown")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。