Thursday, December 26, 2024
Google search engine
HomeLanguagesTurtle – Draw Lines using arrow keys

Turtle – Draw Lines using arrow keys

In this article, we will learn how to draw lines using the keyboard (arrow keys) in turtle graphics. Let’s first discuss some methods used in the implementation below:

  1. wn.listen(): Using this then we can give keyboard inputs
  2. wn.onkeypress(func, “key”): This function is used to bind fun to the key-release event of the key. In order to be able to register key-events, TurtleScreen must have focus.
  3. setx(position): This method is used to set the turtle’s second coordinate to x, leaving the first coordinate unchanged Here, whatever the position of the turtle is, it set the x coordinate to the given input keeping the y coordinate unchanged.
  4. sety(position): This method is used to set the turtle’s second coordinate to y, leaving the first coordinate unchanged Here, whatever the position of the turtle is, it set the y coordinate to the given input keeping the x coordinate unchanged.
  5. ycor(): This function is used to return the turtle’s y coordinate of the current position of the turtle. It doesn’t require any argument.
  6. xcor(): This function is used to return the turtle’s x coordinate of the current position of the turtle. It doesn’t require any argument
  7. head.penup: Picks the pen up so the turtle does not draw a line as it moves
  8. head.hideturtle: This method is used to make the turtle invisible. It’s a good idea to do this while you’re in the middle of a complicated drawing because hiding the turtle speeds up the drawing observably. This method does not require any argument.
  9. head.clear: This function is used to delete the turtle’s drawings from the screen
  10. head.write: This function is used to write text at the current turtle position.

Approach:

  1. Import the turtle modules.
  2. Get a screen to draw on
  3. Define two instances for the turtle one is a pen and another is the head.
  4. Head is for telling which key is currently pressed
  5. Define the functions for the up, down, left, right movement of the turtle.
  6. In the respective up, left, right and down functions set the arrow to move 100 units in up, left, right, and down directions respectively by changing the x and y coordinates.
  7. Use function listen() for giving keyboard inputs.
  8. Use onkeypress in order to register key-events.

Below is the Python implementation of the above approach:

Python3




# import for turtle module
import turtle
 
# making a workScreen
wn = turtle.Screen()
 
# defining 2 turtle instance
head = turtle.Turtle()
pen = turtle.Turtle()
 
# head is for telling which key is pressed
head.penup()
head.hideturtle()
 
# head is at 0,260 coordinate
head.goto(0, 260)
head.write("This is to tell which key is currently pressed",
           align="center", font=("courier", 14, "normal"))
 
 
def f():
    y = pen.ycor()
    pen.sety(y+100)
    head.clear()
    head.write("UP", align="center", font=("courier", 24, "normal"))
 
 
def b():
    y = pen.ycor()
    pen.sety(y-100)
    head.clear()
    head.write("Down", align="center", font=("courier", 24, "normal"))
 
 
def l():
    x = pen.xcor()
    pen.setx(x-100)
    head.clear()
    head.write("left", align="center", font=("courier", 24, "normal"))
 
 
def r():
    x = pen.xcor()
    pen.setx(x+100)
    head.clear()
    head.write("Right", align="center", font=("courier", 24, "normal"))
 
 
wn.listen()
wn.onkeypress(f, "Up"# when up is pressed pen will go up
wn.onkeypress(b, "Down"# when down is pressed pen will go down
wn.onkeypress(l, "Left"# when left is pressed pen will go left
wn.onkeypress(r, "Right"# when right is pressed pen will go right
# this is to stop the turtle window from closing until the close button is clicked
turtle.mainloop()


Output:

RELATED ARTICLES

Most Popular

Recent Comments