Advertisement
sweeneyde

Untitled

Mar 13th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. #This program allows the user to control two turtles with the keyboard.
  2.  
  3. import turtle
  4.  
  5. print("Use arrow keys to move the green turtle.")
  6. print("Use the w, d, a, and s keys to move the red turtle")
  7.  
  8. #These two variables make two turtles.
  9. turtle_1 = turtle.Turtle()
  10. turtle_2 = turtle.Turtle()
  11.  
  12. #These two lines of code pick the turtle color.
  13. turtle_1.color("red")
  14. turtle_2.color("green")
  15.  
  16. #These two lines of code pick the shape of the turtle.
  17. turtle_1.shape("turtle")
  18. turtle_2.shape("turtle")
  19.  
  20. #This function moves turtle_1 up 3.
  21. def move_up_1():
  22.     turtle_1.setheading(90)
  23.     turtle_1.forward(30)
  24.  
  25. #This function moves turtle_1 right 3.
  26. def move_right_1():
  27.     turtle_1.setheading(0)
  28.     turtle_1.forward(30)
  29.  
  30. #This function moves the turtle down 3.
  31. def move_down_1():
  32.     turtle_1.setheading(270)
  33.     turtle_1.forward(30)
  34.  
  35. #This function moves the turtle left 3.
  36. def move_left_1():
  37.     turtle_1.setheading(180)
  38.     turtle_1.forward(30)
  39.  
  40. #This function moves the turtle up 2.
  41. def move_up_2():
  42.     turtle_2.setheading(90)
  43.     turtle_2.forward(30)
  44.  
  45. #This function moves the turtle right 2.
  46. def move_right_2():
  47.     turtle_2.setheading(0)
  48.     turtle_2.forward(30)
  49.  
  50. #This function moves the turtle down 2.
  51. def move_down_2():
  52.     turtle_2.setheading(270)
  53.     turtle_2.forward(30)
  54.  
  55. #This function moves the turtle left 2.
  56. def move_left_2():
  57.     turtle_2.setheading(180)
  58.     turtle_2.forward(30)
  59.  
  60. turtle.listen()
  61.  
  62. #The functions below shows what keys move the turtle
  63. turtle.onkey(move_up_1, "Up")
  64. turtle.onkey(move_right_1, "Right")
  65. turtle.onkey(move_left_1, "Left")
  66. turtle.onkey(move_down_1, "Down")
  67. turtle.onkey(move_up_2, "w")
  68. turtle.onkey(move_right_2, "d")
  69. turtle.onkey(move_left_2, "a")
  70. turtle.onkey(move_down_2, "s")
  71.  
  72. turtle.exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement