根据方向键移动乌龟

Moving turtle according to the arrow keys

我正在尝试让我的乌龟根据键盘上的箭头键移动。有谁知道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)

您可以使用 turtle 的内置键事件处理程序。这是 link 文件: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()

我创建了 tank 变量,所以你正在与之交互(你做了 turtle.foward、turtle.backward 等)而不是 turtle。只是很好的练习,现在您可以在屏幕上显示多个坦克,或者如果您愿意,可以显示多个角色。 turtle.up() 来自您的原始代码。 screen = turtle.Screen() 是为了让您的代码收到关键事件的通知,即。当用户单击一个键/释放一个键时。

def moveright():
    tank.forward(40)

def moveleft():
    tank.backward(40) 

这些只是功能,可以左右移动坦克。请注意,我使用的是 tank.forward,而不是 turtle.forward。这对应于上面的 tank = turtle 变量。名称 'tank' 可以是任何您想要的名称,只要确保您在代码中一直使用该名称即可。 .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 行告诉计算机监听按下的键。它有两个参数,第一个是函数(即 moverightmoveleft 部分),第二个参数是与键盘上所需键相关的字符串。例如。 'e' -> 'e' 键,'w' -> 字母 w, -> 'Up' -> 向上箭头 -> 'Right' -> 向右箭头,依此类推,等等。 **键名必须用引号引起来,如上例所示。如果你这样做 screen.onkeypress(moveright, Right) 它会抛出一个错误。

screen.listen() 告诉计算机开始列出关键事件,screen.mainloop 启动 Turtle 的主循环,因此它会继续查找直到 turtle.done() 被调用。 screen.mainloop 非常重要,因为如果没有它,您的程序将结束并且不会收到任何事件。

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

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