Advertisement
Guest User

Python Game Programming Tutorial: Snake Game Part 4

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