简体   繁体   English

如何找到超过特定点或线的海龟?

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

I want to write a turtle graphics program where, two turtles (I have named them 'tess' and 'alex') go forward on pressing specific keys ('t' for tess and 'a' for alex) and one turtle wins who passes a line that is set to (0, 350). 我想编写一个乌龟图形程序,其中,两只乌龟(我将它们分别命名为“ tess”和“ alex”)按特定的键前进(对于“ tess”为“ t”,对于alex为“ a”),然后一只乌龟获胜设置为(0,350)的行。 That line itself is another turtle that forwards 400 towards east. 那条线本身就是另一只向东转发400的乌龟。

I have done pretty much of this, and wondering how to win a turtle that exceeds the line. 我已经做了很多事情,并且想知道如何赢得超过界限的乌龟。

My segment of code is given below. 我的代码段如下。

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

You just need to test the Y position of the turtles against the Y position of the finish line turtle. 您只需要相对于终点线乌龟的Y位置测试乌龟的Y位置。 A starting example to the "winner" code: “优胜者”代码的起始示例:

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

When a given turtle crosses the finish line the background will change to a lighter shade of the turtle itself (if you make "tess" red instead of purple...) 当给定的乌龟越过终点线时,背景将变为乌龟本身的浅色(如果您将“ TESS”设置为红色而不是紫色...)

To finish this, you'd need to lock out the key handlers once one turtle has one and then bump the scores, etc. and restart the game. 要完成此操作,您需要在一只乌龟拥有一只乌龟之后将其锁定,然后提高分数等,然后重新开始游戏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM