Advertisement
Guest User

Snake Game in Python Code

a guest
Oct 22nd, 2018
3,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import turtle
  2. import time
  3.  
  4. delay = 0.1
  5.  
  6. # Set up the screen
  7. wn = turtle.Screen()
  8. wn.title("Snake Game by Oskar")
  9. wn.bgcolor("green")
  10. wn.setup(width=600, height=600)
  11. wn.tracer(0) # Turns off the screen updates
  12.  
  13. # Snake head
  14. head = turtle.Turtle
  15. head.speed(0)
  16. head.shape("square")
  17. head.color("black")
  18. head.penup()
  19. head.goto(0,0)
  20. head.direction = "right"
  21.  
  22.  
  23. # Functions
  24. def move():
  25.     if head.direction == "up":
  26.         y = head.ycor()
  27.         head.sety(y + 20)
  28.  
  29.     if head.direction == "down":
  30.         y = head.ycor()
  31.         head.sety(y - 20)
  32.  
  33.         if head.direction == "left":
  34.             x = head.xcor()
  35.             head.sety(x - 20)
  36.  
  37.             if head.direction == "right":
  38.                 x = head.xcor()
  39.                 head.sety(x + 20)
  40.  
  41. # Main game loop
  42. while True:
  43.     wn.update()
  44.  
  45.     move()
  46.  
  47.     time.sleep(delay)
  48.  
  49. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement