如何在海龟中使用字母键?

2024-04-16 05:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

除了箭头键外,是否可以使用其他键,如海龟中的w a s d? 我可以用(w a s d)键让海龟移动吗? 或者我可以使用“A”键运行turtle.penup()!=乌龟


Tags: 海龟turtle箭头键penup
1条回答
网友
1楼 · 发布于 2024-04-16 05:05:39

您可以使用"a"来运行命令,甚至onkey()的文档也提到char"a"作为示例

import turtle

def forward():
    turtle.setheading(90)
    turtle.forward(100)

def backward():
    turtle.setheading(270)
    turtle.forward(100)

def left():
    turtle.setheading(180)
    turtle.forward(100)

def right():
    turtle.setheading(0)
    turtle.forward(100)

turtle.onkey(forward, 'w')
turtle.onkey(backward, 's')
turtle.onkey(left, 'a')
turtle.onkey(right, 'd')

turtle.listen()

#turtle.mainloop()
turtle.exitonclick()

相关问题 更多 >