如何找到超过某一点或一条线的乌龟?

时间:2016-04-30 17:42:12

标签: python python-3.x turtle-graphics

我想写一个乌龟图形程序,其中两只乌龟(我将它们命名为'tess'和'alex')按下特定键('t'表示tess,'a'表示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()

1 个答案:

答案 0 :(得分:0)

你只需要测试海龟的Y位置对终点线乌龟的Y位置。 "赢家"的开始示例代码:

Public Class Data
    Public Property Data1 As String
    Public Property Data2 As String
    Public Property Input As New List(Of String)
End Class

当一只特定的乌龟越过终点线时,背景将变为乌龟本身的较浅色调(如果你制作" tess"红色而不是紫色......)

要完成此操作,您需要在一只乌龟拥有一个乌龟之后锁定关键处理程序,然后敲击分数等,然后重新开始游戏。