banner



How To Draw With Arrow Keys Python

First, nosotros got to empathize the nuts. In guild for the user to be able to interact with the turtle through key presses, nosotros need to let the window heed for central presses. Since your screen is named wn, that tin be done simply by calling wn.heed().

At present that the turtle graphics are listening for key presses, how volition you tell the program to do something through cardinal presses? Functions! Let's say yous want a new turtle to be created every time a key is pressed; you will demand to ascertain a function similar so (you can use lambda for the function, only for now, let's stick to def):

          def create_new_turtle():     new_turtle = turtle.Turtle()                  

Keep in mind that you shall not laissez passer any positional arguments into the brackets when defining the function, as you will non be able to laissez passer your arguments in when you lot utilise the office, resulting in an TypeError.

At present, permit's get into how nosotros can actually call these function during run-fourth dimension when a key is pressed. With wn.heed() initiated, at present all y'all'll need is wn.onkey, or wn.onkeypress. Taking the part to a higher place, if you desire a new turtle to exist created every time the user presses the SPACE key:

          wn.onkey(create_new_turtle, 'space')                  

Exercise you lot see why we tin't laissez passer positional arguments into the function? As you can see, when using the function inside wn.onkey, we exercise not call it (as in, we did not add brackets on the right side of the role); wn.onkey does it for us.

Taking what we've learned, let's come across them in activeness:

          import turtle  #Screen wn = turtle.Screen() wn.bgcolor("lightblue")  #Turtle Player spaceship = turtle.Turtle() spaceship.color("red") spaceship.penup()  #Abiding speed = 1  def upwards():     spaceship.setheading(xc)  def down():     spaceship.setheading(270)      def left():     spaceship.setheading(180)  def right():     spaceship.setheading(0)  wn.heed() wn.onkey(up, 'Upward') wn.onkey(downwards, 'Down') wn.onkey(left, 'Left') wn.onkey(right, 'Right')  while Truthful:     spaceship.forward(speed)                  

Can you lot guess what this does? It'due south pretty obvious; when the user hits the 'Upwardly' arrow, the upwards function defined above volition be called, when the user hits the 'Down' arrow, the down function defined above will be called, an so on.

Defining a whole role for a single command doesn't seem right, and nosotros can't simply do

          wn.onkey(spaceship.setheading(90), 'Upward') wn.onkey(spaceship.setheading(270), 'Down') wn.onkey(spaceship.setheading(180), 'Left') wn.onkey(spaceship.setheading(0), 'Correct')                  

Like in the nigh upvoted reply, the solution is to use lambda, where the error causing code correct above can be corrected to

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

Lastly, if you desire your turtle to plow 90 degrees maximum on each turn, y'all can avoid 180 caste turn with if statements in the functions (which, as the functions become more advanced, it is better to use def to define the functions instead of using lambda):

          import turtle  #Screen wn = turtle.Screen() wn.bgcolor("lightblue")  #Turtle Player spaceship = turtle.Turtle() spaceship.color("cerise") spaceship.penup()  #Constant speed = 1  def upwards():     if spaceship.heading() != 270:         spaceship.setheading(90)  def downwardly():     if spaceship.heading() != xc:         spaceship.setheading(270)      def left():     if spaceship.heading() != 0:         spaceship.setheading(180)  def right():     if spaceship.heading() != 180:         spaceship.setheading(0)  wn.listen() wn.onkey(upward, 'Up') wn.onkey(downward, 'Downwards') wn.onkey(left, 'Left') wn.onkey(right, 'Correct')  while True:     spaceship.forward(speed)                  

Test run:

enter image description here

Source: https://stackoverflow.com/questions/43449702/how-to-move-turtles-in-python-3-with-arrow-keys

Posted by: washingtondishemeard.blogspot.com

0 Response to "How To Draw With Arrow Keys Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel