Advertisement
Guest User

Snake game

a guest
Feb 21st, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4.  
  5. delay = 0.1
  6.  
  7.  
  8. #setup the screen
  9. wn = turtle.Screen()
  10. wn.title('Snake')
  11. wn.bgcolor('blue')
  12. wn.setup(width=600, height=600)
  13. wn.tracer(0)
  14.  
  15. #snake head
  16. head = turtle.Turtle()
  17. head.speed(0)
  18. head.shape('square')
  19. head.color('green')
  20. head.penup()
  21. head.goto(0,0)
  22. head.direction = "stop"
  23.  
  24.  
  25. #snake food
  26. food = turtle.Turtle()
  27. food.speed(0)
  28. food.shape('circle')
  29. food.color('red')
  30. food.penup()
  31. food.goto(0,100)
  32.  
  33. segments = []
  34.  
  35.  
  36.  
  37. #functions
  38. def go_up():
  39.     head.direction = "up"
  40.    
  41. def go_down():
  42.     head.direction = "down"
  43.    
  44. def go_right():
  45.     head.direction = "right"
  46.    
  47. def go_left():
  48.     head.direction = "left"
  49.    
  50. def move():
  51.     if head.direction == "up":
  52.         y = head.ycor()
  53.         head.sety(y + 20)
  54.        
  55.     if head.direction == "down":
  56.         y = head.ycor()
  57.         head.sety(y - 20)
  58.        
  59.     if head.direction == "left":
  60.         x = head.xcor()
  61.         head.setx(x - 20)
  62.        
  63.     if head.direction == "right":
  64.         x = head.xcor()
  65.         head.setx(x + 20)
  66.  
  67. #Keyboard bindings
  68. wn.listen()
  69. wn.onkeypress(go_up, "w")
  70. wn.onkeypress(go_down, "s")
  71. wn.onkeypress(go_left, "a")
  72. wn.onkeypress(go_right, "d")
  73.  
  74. # main game loop
  75. while True:
  76.     wn.update()
  77.  
  78. #check for a collision with the food
  79.     if head.distance(food) < 20:
  80.        
  81.         #move the food to a random spot
  82.         x = random.randint(-290, 290)
  83.         y = random.randint(-290, 290)
  84.         food.goto(x,y)
  85.  
  86.         # add a segment
  87.         new_segment = turtle.Turtle()
  88.         new_segment.speed(0)
  89.         new_segment.shape("square")
  90.         new_segment.color("grey")
  91.         new_segment.penup()
  92.         segments.append(new_segment)
  93.  
  94.         # move the end segments first in reverse order
  95.         for index in range(len(segments)-1, 0, -1):
  96.             x = segments[index-1].xcor()
  97.             y = segments[index-1].ycor()
  98.             segments[index].goto(x, y)
  99.  
  100.         # move segment 0 to where the head is
  101.         if len(segments) > 0:
  102.             x = head.xcor()
  103.             y = head.ycor()
  104.             segments[0].goto(x,y)
  105.            
  106.            
  107.     move()
  108.  
  109.     time.sleep(delay)
  110.    
  111.    
  112.  
  113. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement