0

I am playing around with Python turtle. So far I can control it with arrow keys, however the direction is near random and I was thinking of resetting the direction that it's facing every time I press an arrow key. However I don't know how to do that exactly. Any help?

import turtle
turtle.color("Red")
turtle.title("Test")
turtle.pensize(5)


def tLeft():         #Edit everything later, most likely to be inaccurate.                                   
    turtle.right(180)
    turtle.forward(10)

def tRight():
    turtle.left(180)
    turtle.forward(10)

def tUp():
    turtle.right(90)
    turtle.forward(10)

def tDown():
    turtle.left(270)
    turtle.forward(10)

turtle.onkeypress(tUp, "Up")
turtle.onkeypress(tDown, "Down")
turtle.onkeypress(tRight, "Right")
turtle.onkeypress(tLeft, "Left")    #First test: When started the code did nothing, nothing showed up and no errors was shown. Edit:only needed "listen()"
turtle.listen()

Sorry if the code looks a little weird :P

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • https://stackoverflow.com/questions/36019509/moving-turtle-according-to-the-arrow-keys Check this out - see how you get on – dan6657 Nov 07 '17 at 10:24
  • assuming 'turtle.right(90)' turns your turtle 90 degrees to the right, pressing the up key 4 times in a row should draw a square with sides 10 long. You need to either store the direction your turtle is facing and calculate how to turn to the new direction on each turn command. Or, to reset the direction of your turtle to 'up' then turn in the opposite direction after each forward to the direction you turned before it. Then adjust the amount of turn to Up=0, left,right=90 and down=180. – R.Sharp Nov 07 '17 at 10:46

1 Answers1

0

Here are two ways to approach this, using relative turning or absolute turning. With relative turning, we let the turtle turn some amount left or right, and move some amount forward or backward:

import turtle

def tLeft():
    turtle.left(90)

def tRight():
    turtle.right(90)

def tForward():
    turtle.forward(10)

def tBackward():
    turtle.backward(10)

turtle.shape('turtle')
turtle.pensize(5)

turtle.onkeypress(tRight, 'Right')
turtle.onkeypress(tLeft, 'Left')
turtle.onkeypress(tForward, 'Up')
turtle.onkeypress(tBackward, 'Down')

turtle.listen()
turtle.mainloop()

With absolute turning, we use make the arrow keys correspond to compass positions and move the turtle to those absolute orientations:

import turtle

def tEast():
    turtle.setheading(0)

def tNorth():
    turtle.setheading(90)

def tWest():
    turtle.setheading(180)

def tSouth():
    turtle.setheading(270)

def tForward():
    turtle.forward(10)

turtle.shape('turtle')
turtle.pensize(5)

turtle.onkeypress(tEast, 'Right')
turtle.onkeypress(tWest, 'Left')
turtle.onkeypress(tNorth, 'Up')
turtle.onkeypress(tSouth, 'Down')

turtle.onkeypress(tForward, 'space')

turtle.listen()
turtle.mainloop()

Here I've added the space bar for forward motion. Note that the turns aren't necessarily optimal, I believe that's what partly what @R.Sharp is addressing with the additional code in his answer.

Which of the above approaches you use, or something completely different, depends on what your ultimate goal is in moving the turtle.

cdlane
  • 40,441
  • 5
  • 32
  • 81