Python 根据箭头键移动海龟

Python 根据箭头键移动海龟,python,turtle-graphics,python-3.2,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)

我试着让我的乌龟按照键盘上的箭头键移动。有人知道如何在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内置的关键事件处理程序。这是指向以下文档的链接:

使用您的代码,在
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
行告诉计算机监听按键。它有两个参数,第一个是函数(即
moveright
moveleft
部分),第二个参数是与键盘上所需键相关的字符串。例如,“e”->“e”键,“w”->字母w,->“向上”->向上箭头->“向右”->向右箭头,等等**键名必须用引号括起来,如上面的示例所示。如果执行
screen.onkeypress(moveright,Right)
操作,它将抛出一个错误

screen.listen()
告诉计算机开始列出关键事件,而
screen.mainloop
启动Turtle的mainloop,以便继续查找,直到调用
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()

对不起,如果我的英语不正确,在用[turtle]标记你的问题之前,你读过[turtle]是什么吗?如果没有请读哦。我不知道。我认为“海龟”是指python绘图函数。谢谢你提供的信息。非常感谢你提供的详细和描述性的答案