如何用箭头键在Python 3中移动海龟

2024-04-19 13:46:27 发布

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

我很难让我的乌龟能够跟随箭头键,任何关于如何这样做的帮助将非常感谢。我敢肯定这个问题以前有人问过,虽然我似乎找不到,但我确实找到的是老版本的。

import turtle
#screen
wn=turtle.Screen()
wn.bgcolor("lightblue")

I plan on this being a spaceship game
#Turtle Player
spaceship= turtle.Turtle()
spaceship.color("red")
spaceship.penup()
speed=1

This is where I am stuck, I don't know how to make the turtle follow the arrow keys

#keyboard bindings

while True:
    spaceship.forward(speed)

Tags: theimport版本onscreenlightbluespeedturtle
2条回答

我有解决办法给你。代码并不理想,但它可以工作,您可以对其进行处理。你必须意识到,乌龟的位置不好,你必须调整它。这就是为什么我在设置方法我的乌龟抬头。

现在,您必须记住,right(deg)left(deg)方法说的是“请在给定方向上以这样的程度旋转”。

所以记住,你最后的方向是什么。

理解这一点的关键是,您在这里无法访问任何绝对值。你只能根据你现在的位置改变一些东西。所以,你不能左转,但如果你知道前一个方向,你知道多少度,你应该把你的乌龟真正左转。

我的工作代码是:

import turtle
wn = turtle.Screen()

last_pressed = 'up'

def setup(col, x, y, w, s, shape):
  turtle.up()
  turtle.goto(x,y)
  turtle.width(w)
  turtle.turtlesize(s)
  turtle.color(col)
  turtle.shape(shape)
  turtle.lt(90)
  turtle.down()
  wn.onkey(up, "Up")
  wn.onkey(left, "Left")
  wn.onkey(right, "Right")
  wn.onkey(back, "Down")
  wn.onkey(quitTurtles, "Escape")
  wn.listen()
  wn.mainloop()




#Event handlers
def up():
  global last_pressed
  if last_pressed == 'left':
    turtle.rt(90)
    turtle.fd(10)
  elif last_pressed == 'right':
    turtle.lt(90)
    turtle.fd(10)
  elif last_pressed == 'up':
    turtle.fd(10)
  else:
    turtle.rt(180)
    turtle.fd(10)

  last_pressed = 'up'

def left():
  global last_pressed
  if last_pressed == 'left':
    turtle.fd(10)
  elif last_pressed == 'right':
    turtle.lt(180)
    turtle.fd(10)
  elif last_pressed == 'up':
    turtle.lt(90)
    turtle.fd(10)
  else:
    turtle.rt(90)
    turtle.fd(10)

  last_pressed = 'left'


def right():
  global last_pressed
  if last_pressed == 'left':
    turtle.rt(180)
    turtle.fd(10)
  elif last_pressed == 'right':
    turtle.fd(10)
  elif last_pressed == 'up':
    turtle.rt(90)
    turtle.fd(10)
  else:
    turtle.lt(90)
    turtle.fd(10)

  last_pressed = 'right'

def back():
  global last_pressed
  if last_pressed == 'left':
    turtle.lt(90)
    turtle.fd(10)
  elif last_pressed == 'right':
    turtle.rt(90)
    turtle.fd(10)
  elif last_pressed == 'up':
    turtle.rt(180)
    turtle.fd(10)
  else:
    turtle.fd(10)

  last_pressed = 'down'

def quitTurtles():
  wn.bye()

setup("blue",-200,200,2,2,"turtle")

请记住,海龟要花一些时间才能真正转动,所以不要按键,点击它们。

我认为你可以更进一步。

避免在turtle图形程序中使用像while True:这样的无限循环,这样可以避免触发某些事件。

下面是我能想到的使你的宇宙飞船可以导航的最小代码。您应该能够在此基础上:

from turtle import Turtle, Screen

wn = Screen()
wn.bgcolor('lightblue')

spaceship = Turtle()
spaceship.color('red')
spaceship.penup()

speed = 1

def travel():
    spaceship.forward(speed)
    wn.ontimer(travel, 10)

wn.onkey(lambda: spaceship.setheading(90), 'Up')
wn.onkey(lambda: spaceship.setheading(180), 'Left')
wn.onkey(lambda: spaceship.setheading(0), 'Right')
wn.onkey(lambda: spaceship.setheading(270), 'Down')

wn.listen()

travel()

wn.mainloop()

在发出键盘命令之前,单击turtle图形窗口以确保它正在收听。另外,还有其他方法来确定按键的工作方式,我在这里使用了绝对运动,但您可能需要相对运动,每次按都会递增地修改您的方向。

相关问题 更多 >