-3

I want a program that when I press the arrow keys on the keyboard, the Turtle will first rotate in that direction and then move in that direction with each consecutive press of the same direction.

Right now I have:

from turtle import *

def go_up():
    setheading(90)
    forward(100)

def go_Left():
    setheading(180)
    forward(100)

def go_down():
    setheading(270)
    forward(100)

def go_Right():
    setheading(0)
    forward(100)

shape('turtle')

listen()

onkeypress(go_up , 'Up')
onkeypress(go_Left , 'Left')
onkeypress(go_down , 'Down')
onkeypress(go_Right , 'Right')

But this makes the turtle turn AND move with each press. How can I separate it so on the first direction press the turtle only turns, and the next presses advances?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
DAB
  • 7
  • 4
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. There are many sites with introductory material on how to take actions based on keys. We expect you to use these before posting here. – Prune Oct 18 '20 at 20:39
  • Does this answer your question? [Python Turtle Graphics Window only Opens Briefly then Closes](https://stackoverflow.com/questions/19018243/python-turtle-graphics-window-only-opens-briefly-then-closes) – wjandrea Oct 18 '20 at 20:42
  • I just had to add `mainloop()` at the end, and it worked perfectly. – wjandrea Oct 18 '20 at 20:43
  • not working :((( – DAB Oct 18 '20 at 20:47
  • @DAB Please [edit] the question to explain what exactly isn't working. On my machine, the problem was that the turtle screen wasn't staying open. See [mre]. – wjandrea Oct 18 '20 at 20:48
  • sorry i will fix it – DAB Oct 18 '20 at 20:50
  • OK, what have you already tried, and what exactly do you need help with? SO is not a code-writing service, though I might write an answer anyway since I'm learning Turtle myself. – wjandrea Oct 18 '20 at 21:05
  • You might want to read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – wjandrea Oct 18 '20 at 21:16

1 Answers1

1

You just need to check the heading. If the turtle isn't facing that direction, turn, otherwise move.

As well, you can use functools.partial and a dict to DRY out the code.

from turtle import *
from functools import partial

def turn_then_go(angle):
    if heading() != angle:
        setheading(angle)
    else:
        forward(100)

directions = {'Right': 0, 'Up': 90, 'Left': 180, 'Down': 270}
for direction, angle in directions.items():
    onkeypress(partial(turn_then_go, angle), direction)

shape('turtle')
listen()
mainloop()
wjandrea
  • 28,235
  • 9
  • 60
  • 81