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

使用if语句在Python中使用Turtle匹配x/y坐标,python,turtle-graphics,python-turtle,Python,Turtle Graphics,Python Turtle,在下面的代码中,我看到新的_海龟(其中6只)在屏幕上从左到右水平移动,坏的_海龟从下到上再垂直移动。我要寻找的是,当坏海龟“碰到”或与新海龟具有相同的x,y坐标时,我希望它“碰到”的新海龟变成棕色。我试着在下面的最后一个if语句中写下这一点,但它不起作用。如何做到这一点?如有任何帮助/建议,将不胜感激 from turtle import Turtle, Screen import random is_race_on = False screen = Screen() screen.setup

在下面的代码中,我看到新的_海龟(其中6只)在屏幕上从左到右水平移动,坏的_海龟从下到上再垂直移动。我要寻找的是,当坏海龟“碰到”或与新海龟具有相同的x,y坐标时,我希望它“碰到”的新海龟变成棕色。我试着在下面的最后一个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()
从海龟导入海龟,屏幕
随机输入
比赛是否开始=错误
screen=screen()
屏幕设置(宽度=500,高度=500)
user\u bet=screen.textinput(title=“下注!”,prompt=“哪只乌龟将赢得比赛?输入颜色:”)
打印(用户下注)
颜色=[“红色”、“蓝色”、“绿色”、“黄色”、“紫色”、“橙色”]
y_位置=[17510025,-50,-125,-200]
所有海龟=[]
坏海龟=海龟(shape=“海龟”)
坏乌龟
坏海龟。后藤(140,-200)
坏海龟,对(270)
对于范围(0,6)内的海龟指数:
新海龟=海龟(shape=“海龟”)
新的海龟颜色(颜色[海龟索引])
新乌龟
新海龟。转到(-230,y_位置[海龟指数])
所有海龟。追加(新海龟)
如果用户下注:
_race_on=正确吗
当比赛开始时:
对于所有海龟中的海龟:
如果turtle.xcor()大于230:
比赛是否开始=错误
获胜的颜色=乌龟
如果获胜\u color==用户\u赌注:
打印(f“你赢了,{winning_color}海龟是赢家!”)
其他:
打印(f“你输了,{winning_color}海龟是赢家!”)
rand_distance=random.randint(0,10)
乌龟向前(兰特距离)
#rand\u bad=随机选择(y\u位置\u bad)
rand\u distance\u bad=random.randint(20,40)
坏海龟。向前(兰德距离坏)
如果bad_turtle.ycor()大于200或bad_turtle.ycor()小于-200:
坏海龟。右(180)
坏海龟。向前(兰德距离坏)
如果bad_turtle.xcor()和bad_turtle.ycor()==new_turtle.xcor()和new_turtle.ycor():
新乌龟。颜色(“棕色”)
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")

@卡雷诺的回答是朝着正确的方向迈出的一步,但没有抓住关键点。海龟在浮点平面上游荡——当它们回到同一个地方时,不一定是同一个地方(例如13.99999对14.00001)。使用
==
是不可行的。因此,我们使用距离比较来测试碰撞:

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

请阅读有关调试代码的提示。谢谢,但我仍然得到相同的结果。这很有意义,非常感谢@cdlane!然而,当我使用上面的方法时,它只会将与之碰撞的第一只海龟变成棕色,但当它与其他海龟碰撞后,其余海龟的颜色保持不变。知道我做错了什么吗?@Mamdlv,看看我刚才在回答中添加的代码的修改。
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()