2016-06-03 2 views
0

turtle.speed(0)을 시도했지만 시도했습니다. turtle.goto(turtle.xcor(), turtle.ycor()) 아무 것도 작동하지 않는 것 같습니다. 파이썬 거북이는 어떻게 움직이지 않습니까?

import turtle 

def stopMovingTurtle(): 
    ## Here I Need To Stop The Turtle ## 

turtle.listen() 
turtle.onkey(stopMovingTurtle, 'Return') 
turtle.goto(-200, 0) 
turtle.goto(200, 0) 

그래서 내가 그것을 어떻게 중지합니까 :

코드인가?

+0

'onkey'는 함수를 키에 바인딩하기 때문에 나중에 키를 'listen'해야합니다. 'onkey'와'listen' 스위치. –

+0

그래,하지만 그건 내 질문에 도움이되지 않아. –

+1

시도해 보셨습니까? 'turtle.listen()'과'turtle.onkey()'의 순서를 바꾸는 아이디어를주었습니다. 왜냐하면'turtle.onkey'는 함수를 키에 바인딩하는 것 이외의 일은하지 않기 때문에'listen' 않습니다. –

답변

1

여기서 문제는 turtle.listen()turtle.onkey()의 순서가 아니며, 현재 작업이 완료 될 때까지 키 이벤트가 처리되지 않는 것입니다. turtle.goto(-200, 0) 모션을 작은 모션으로 세분화하면 키 이벤트가 작동 할 수있는 기회가됩니다. 여기에 거친 예입니다 (창으로 전환하고) 반환을 명중

import turtle 

in_motion = False 

def stopMovingTurtle(): 
    global in_motion 
    in_motion = False 

def go_segmented(t, x, y): 
    global in_motion 
    in_motion = True 

    cx, cy = t.position() 

    sx = (x > cx) - (x < cx) 
    sy = (y > cy) - (y < cy) 

    while (cx != x or cy != y) and in_motion: 
     if cx != x: 
      cx += sx 
     if cy != y: 
      cy += sy 

     t.goto(cx, cy) 

turtle.speed('slowest') 
turtle.listen() 
turtle.onkey(stopMovingTurtle, 'Return') 

go_segmented(turtle, -200, 0) 
go_segmented(turtle, 200, 0) 

turtle.done() 

경우, 거북이는 현재 선 그리기 중지됩니다.

관련 문제