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

Using an if statement to match x/y coordinates in Python using Turtle

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

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

和你一样,如果陈述不正确,你可以尝试改变这个:

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

@ErickY.Carreno的回答是朝着正确的方向迈出了一步,但遗漏了一个关键点。海龟在浮点平面上游荡——当它们 return 到同一个地方时,它不一定完全是同一个地方(例如 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()