如何找到超过某个点或线的海龟?

How to find a turtle that exceeds a certain point or a line?

我想编写一个海龟图形程序,其中两只海龟(我将它们命名为 'tess' 和 'alex')在按下特定键('t' for tess 和 'a' for alex) 并且一只乌龟通过设置为 (0, 350) 的线获胜。该线本身是另一只乌龟,将 400 向东转发。

我已经做了很多,想知道如何赢得超过线的海龟。

我的代码段如下。

import turtle

t = turtle.Turtle()
wn = turtle.Screen()
wn.setup(400, 500)
wn.bgcolor("lightgreen")
wn.screensize(400, 500)
wn.setworldcoordinates(0, 0, 400, 500)
t.penup()
t.goto(0, 350)
t.pendown()
t.forward(400)

# tess
tess = turtle.Turtle()
tess.color("purple")
tess.penup()
tess.forward(100)
tess.pendown()
tess.left(90)
tess.forward(10)

# alex
alex = turtle.Turtle()
alex.color("blue")
alex.penup()
alex.forward(200)
alex.pendown()
alex.left(90)
alex.forward(10)

# making key handlers
def h1():
    tess.forward(50)


def h2():
    alex.forward(50)
#----------------------------

# wiring up keypresses to the handlers
wn.onkey(h1, 't')
wn.onkey(h2, 'a')


def handler_for_tess(x, y):
    wn.title("Tess clicked at {0}, {1}".format(x, y))
    #tess.left(90)
    tess.forward(50)


def handler_for_alex(x, y):
    wn.title("Alex clicked at {0}, {1}".format(x, y))
    #alex.right(84)
    alex.forward(50)


# listening mouse clicks
tess.onclick(handler_for_tess)
alex.onclick(handler_for_alex)

# listeing key press
wn.listen()

你只需要测试乌龟的Y位置和终点线乌龟的Y位置。 "winner" 代码的起始示例:

# making key handlers
def h1():
    tess.forward(50)
    if tess.pos()[1] >= t.pos()[1]:
        wn.bgcolor("pink")

def h2():
    alex.forward(50)
    if alex.pos()[1] >= t.pos()[1]:
        wn.bgcolor("lightblue")

当给定的乌龟越过终点线时,背景将变为乌龟本身的较浅阴影(如果您将 "tess" 设为红色而不是紫色...)

要完成此操作,您需要在一只乌龟拥有一只海龟后锁定钥匙处理程序,然后增加分数等,然后重新开始游戏。