【问题标题】:Moving turtle according to the arrow keys根据箭头键移动乌龟
【发布时间】:2016-03-15 18:41:13
【问题描述】:

我试图让我的乌龟根据键盘上的箭头键移动。有人知道如何根据我的箭头键的命令在 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] 标记您的问题之前,您有没有读过它是什么?如果没有请阅读*.com/tags/turtle/info
  • 哦。我不知道。我认为“乌龟”是指 python 绘图功能。感谢您提供信息。

标签: python turtle-graphics python-3.2


【解决方案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()

我制作了 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 变量。 “坦克”这个名字可以是任何你想要的名字,只要确保你在整个代码中都使用这个名字。 .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,-> 'Up' -> 上箭头 -> 'Right' -> 右箭头,依此类推。 **键名必须用引号括起来,如上例所示。如果你这样做 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()

【讨论】:

  • 非常感谢您提供详细的描述性答案