Advertisement
Guest User

code

a guest
May 25th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import turtle
  2. import time
  3. from turtle import Turtle
  4.  
  5. delay = 0.1
  6.  
  7. # screen setup
  8. wn = turtle.Screen()
  9. wn.title("Snake Game")
  10. wn.bgcolor("green")
  11. wn.setup(width=800, height=600)
  12. wn.tracer(0)
  13.  
  14. # Snake head
  15. head = turtle.Turtle()
  16. head.speed(0)
  17. head.shape("square")
  18. head.color("black")
  19. head.penup()
  20. head.goto(0, 0)
  21. head.direction("stop")
  22.  
  23. def go_up():
  24.     head.direction = "up"
  25.  
  26. def go_down():
  27.     head.direction = "down"
  28.  
  29. def go_left():
  30.     head.direction = "left"
  31.  
  32. def go_right():
  33.     head.direction = "right"
  34.  
  35. def move():
  36.     if head.direction == "up":
  37.         y = head.ycor()
  38.         head.sety(y + 20)
  39.     if head.direction == "down":
  40.         y = head.ycor()
  41.         head.sety(y - 20)
  42.     if head.direction == "left":
  43.         x = head.xcor()
  44.         head.setx(x - 20)
  45.     if head.direction == "right":
  46.         x = head.xcor()
  47.         head.setx(x + 20)
  48.  
  49. wn.listen()
  50. wn.onkeypress(go_up(), "w")
  51. wn.onkeypress(go_down(), "s")
  52. wn.onkeypress(go_left(), "a")
  53. wn.onkeypress(go_right(), "d")
  54.  
  55.  
  56. while True:
  57.     wn.update()
  58.     move()
  59.     time.sleep(delay)
  60.  
  61. # I needed to put another while because it said that code is unrechable
  62. while True:
  63.     wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement