根据箭头键移动乌龟

时间:2016-03-15 18:41:13

标签: python turtle-graphics python-3.2

我正在尝试按照键盘上的箭头键移动乌龟。有没有人知道如何根据我的箭头键的命令在python 3.2.2中移动一只乌龟?顺便说一句,如果这对任何东西都有影响,我的乌龟会以坦克的形式出现。这是我的代码:

import turtle

unVar1 = 25
unVar2 = 100
unVar3 = 90
unVar4 = 150
unVar5 = -30
unVar6 = 75
unVar7 = 50
def polySquare(t, x, y, length):
    t.goto(x, y)
    t.setheading(270)

    t.begin_poly()

    for count in range(4):
        t.forward(length)
        t.left(90)

    t.end_poly()

    return t.get_poly()

def polyRectangle(t, x, y, length1, length2):
    t.goto(x, y)
    t.setheading(270)

    t.begin_poly()

    for count in range(2):
        t.forward(length1)
        t.left(90)
        t.forward(length2)
        t.left(90)

    t.end_poly()

    return t.get_poly()

def tankCursor():

    """
    Create the tank cursor.  An alternate solution is to toss the temporary turtle
    and use the commented out polygon assignments instead of the poly function calls
    """

    temporary = turtle.Turtle()

    screen = turtle.getscreen()

    delay = screen.delay()

    screen.delay(0)

    temporary.hideturtle()
    temporary.penup()

    tank = turtle.Shape("compound")

    # tire1 = ((10, unVar1), (10, unVar1 - unVar6), (10 + 30, unVar1 - unVar6), (10 + 30, unVar1))
    tire1 = polyRectangle(temporary, 10, unVar1, unVar6, 30)  # Tire #1
    tank.addcomponent(tire1, "gray", "black")

    # tire2 = ((110, unVar1), (110, unVar1 - unVar6), (110 + 30, unVar1 - unVar6), (110 + 30, unVar1))
    tire2 = polyRectangle(temporary, 110, unVar1, unVar6, 30)  # Tire #2
    tank.addcomponent(tire2, "gray", "black")

    # tire3 = ((110, unVar2), (110, unVar2 - unVar6), (110 + 30, unVar2 - unVar6), (110 + 30, unVar2))
    tire3 = polyRectangle(temporary, 110, unVar2, unVar6, 30)  # Tire #3
    tank.addcomponent(tire3, "gray", "black")

    # tire4 = ((10, unVar2), (10, unVar2 - unVar6), (10 + 30, unVar2 - unVar6), (10 + 30, unVar2))
    tire4 = polyRectangle(temporary, 10, unVar2, unVar6, 30)  # Tire #4
    tank.addcomponent(tire4, "gray", "black")

    # bodyTank = ((20, unVar3), (20, unVar3 - 130), (20 + 110, unVar3 - 130), (20 + 110, unVar3))
    bodyTank = polyRectangle(temporary, 20, unVar3, 130, 110)
    tank.addcomponent(bodyTank, "black", "gray")

    # gunTank = ((65, unVar4), (65, unVar4 - 100), (65 + 20, unVar4 - 100), (65 + 20, unVar4))
    gunTank = polyRectangle(temporary, 65, unVar4, 100, 20)   # Gun
    tank.addcomponent(gunTank, "black", "gray")

    # exhaustTank = ((50, unVar5), (50, unVar5 - 20), (50 + 10, unVar5 - 20), (50 + 10, unVar5))
    exhaustTank = polyRectangle(temporary, 50, unVar5, 20, 10)
    tank.addcomponent(exhaustTank, "black", "gray")

    # turretTank = ((50, unVar7), (50, unVar7 - 50), (50 + 50, unVar7 - 50), (50 + 50, unVar7))
    turretTank = polySquare(temporary, 50, unVar7, 50)  # Turret
    tank.addcomponent(turretTank, "red", "gray")

    turtle.addshape("tank", shape=tank)

    del temporary

    screen.delay(delay)

tankCursor()  # creates and registers the "tank" cursor shape

turtle.shape("tank")

turtle.up()  # get rid of the ink

# show our tank in motion
while True:
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    turtle.left(180)
    turtle.forward(200)

1 个答案:

答案 0 :(得分:1)

您可以使用turtle内置的关键事件处理程序。这是文档的链接:https://docs.python.org/3.2/library/turtle.html

使用您的代码,一切都保持不变,直至tankCursor()(第98行)。现在顺着,这就是我做的。

tankCursor()  # creates and registers the "tank" cursor shape

tank = turtle
tank.shape("tank")

turtle.up()  # get rid of the ink

screen = turtle.Screen()

我使坦克变量,所以你正在与之交互(你做了turtle.foward,turtle.backward等)而不是乌龟。这是一个很好的练习,现在你可以在屏幕上有多个坦克,或者如果你愿意,可以有多个角色。 turtle.up()来自您的原始代码。 screen = turtle.Screen()是您的代码获得关键事件的通知,即。当用户点击一个键/释放一个键时。

def moveright():
    tank.forward(40)

def moveleft():
    tank.backward(40) 

这些只是向右和向左移动水箱的功能。注意我使用了tank.forward,而不是turtle.forward。这对应于上面的tank = turtle变量。名字'坦克'可以是你想要的任何东西,只要确保你在代码中一直使用该名称。 .forward.backward函数与您在

中使用的函数相同
while True:
    turtle.forward(100)
    turtle.left(90)
    turtle.forward(100)
    turtle.left(180)
    turtle.forward(200)

所有这些内容也适用于moveright / moveleft函数。

screen.onkeypress(moveright, "Right")
screen.onkeypress(moveleft, "Left")

screen.listen()
screen.mainloop()

现在是魔术。 2 screen.onkeypress行告诉计算机侦听按下的键。它需要2个参数,第一个是函数(即moverightmoveleft部分),第二个参数是与键盘上所需键相关的字符串。例如。 ' E' - > ' E'关键,' w' - >字母w, - > '最多' - >向上箭头 - > '右' - >右箭头等等。 **键名必须在引号中,如上例所示。如果你screen.onkeypress(moveright, Right),它将抛出一个错误。

screen.listen()告诉计算机开始列出关键事件,screen.mainloop启动Turtle的主循环,以便继续查看turtle.done()screen.mainloop非常重要,因为如果没有它,你的程序就会结束,不会收到任何事件。

以下是完整的代码,您可以随意复制/粘贴并使用它。同样,代码从tankCursor()第98行继续。

tankCursor()  # creates and registers the "tank" cursor shape

tank = turtle
tank.shape("tank")

turtle.up()  # get rid of the ink

screen = turtle.Screen()

def moveright():
    tank.forward(40)

def moveleft():
    tank.backward(40)

screen.onkeypress(moveright, "Right")
screen.onkeypress(moveleft, "Left")

screen.listen()
screen.mainloop()