2017-04-17 164 views
0

移动海龟在Python 3中,如何做到这一点任何帮助,将不胜感激。我确信这个问题之前已经被问到了,尽管我似乎无法找到它,而我发现的问题是针对旧版本的。如何与我有麻烦我的乌龟能跟随箭头键箭头键

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 

这是我在哪里卡住了,我不知道如何让乌龟遵循 箭头键

#keyboard bindings 

while True: 
    spaceship.forward(speed) 
+0

看看这个:http://www.dreamincode.net/forums/topic/274724-drawing-with-arrow-keys-turtle-module/page__p__1599693&#entry1599693用户名为'atraub'的答案解释了如何做它。 – jedruniu

+0

叫我白痴,虽然我似乎无法将其实施到我的程序 –

回答

-1

我对你的解决方案。代码不是理想的,但它的工作原理,你可以在它上面工作。你必须知道,那只乌龟已经失去了位置,你必须它。这就是为什么我指出我的乌龟要查找的设置方法。

现在,你必须记住,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") 

请记住,它需要一些时间,乌龟实际转,所以不按键,点击它们。

我认为你可以继续这样做。

+0

这应该工作,但我因为执行(last_pressed +硬编码车削)downvoted。有很多更好的方法来做到这一点。无论是'setheading'就像另一个答案所暗示的那样,或者在当前的标题和期望的标题之间找到差异,并且转换正确的数量可能会更清晰。这个例子违反了不要重复你自己的想法。 – viraptor

2

避免使用诸如while True:乌龟图形程序内的无限循环,它可以让你的一些事件烧制而成。

下面是最少的代码,我可以拿出来让你的飞船navigatible。你应该能够在此建:

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

点击键盘发出之前乌龟图形窗口中的命令,以确保它听。此外,还有其他方法的关键应该如何工作,我用绝对运动在这里,但你可能想相对每个按增量改变你的方向。