Advertisement
Guest User

Snake

a guest
Jan 25th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1.  
  2.  
  3. import turtle
  4. import time
  5. import random
  6.  
  7. delay = 0.1
  8.  
  9. # Score
  10. score = 0
  11. height_score = 0
  12.  
  13.  
  14. # Set up the screen
  15. wn = turtle.Screen()
  16. wn.title("Snake Game by Henry Laser")
  17. wn.bgcolor("green")
  18. wn.setup(width=600, height=600)
  19. wn.tracer(0) # turns off the screen updates
  20.  
  21. # Snake Head
  22. head = turtle.Turtle()
  23. head.speed(0)
  24. head.shape("square")
  25. head.color("black")
  26. head.penup()
  27. head.goto(0,0)
  28. head.direction = "stop"
  29.  
  30. # Snake food
  31. food = turtle.Turtle()
  32. food.speed(0)
  33. food.shape("circle")
  34. food.color("red")
  35. food.penup()
  36. food.goto(0, 100)
  37.  
  38. segments = []
  39.  
  40. # Pen
  41. pen = turtle.Turtle()
  42. pen.speed(0)
  43. pen.shape("square")
  44. pen.color("white")
  45. pen.penup()
  46. pen.hideturtle()
  47. pen.goto(0, 260)
  48. pen.write("SCORE: 0  HIGH SCORE: 0", align="center", font=("Courier", 24, "normal"))
  49.  
  50.  
  51.  
  52.  
  53. # Functions
  54. def go_up():
  55.     if head.direction != "down":
  56.         head.direction = "up"
  57.  
  58. def go_down():
  59.     if head.direction != "up":
  60.         head.direction = "down"
  61.  
  62. def go_left():
  63.     if head.direction != "right":
  64.         head.direction = "left"
  65.  
  66. def go_right():
  67.     if head.direction != "left":
  68.         head.direction = "right"
  69.  
  70. def move():
  71.     if head.direction == "up":
  72.         y = head.ycor()
  73.         head.sety(y + 20)
  74.  
  75.     if head.direction == "down":
  76.         y = head.ycor()
  77.         head.sety(y - 20)
  78.  
  79.     if head.direction == "left":
  80.         x = head.xcor()
  81.         head.setx(x - 20)
  82.  
  83.     if head.direction == "right":
  84.         x = head.xcor()
  85.         head.setx(x + 20)
  86.  
  87. # Keyboard bindings
  88. wn.listen()
  89. wn.onkeypress(go_up, "w")
  90. wn.onkeypress(go_down, "s")
  91. wn.onkeypress(go_left, "a")
  92. wn.onkeypress(go_right, "d")
  93.  
  94.  
  95.  
  96.  
  97.      # Main game loop
  98. while True:
  99.     wn.update()
  100.  
  101.     # Check with a collision with the border
  102.     if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
  103.         time.sleep(1)
  104.         head.goto(0, 0)
  105.         head.direction = "stop"
  106.  
  107.  
  108.         # Hide the segments
  109.         for segments in segments:
  110.             segments.goto(1000, 1000)
  111.  
  112.             # Clear the segments list
  113.             segments = []
  114.  
  115.         #Reset the score
  116.         score = 0
  117.         pen.clear()
  118.         pen.write("Score: {}  High Score: {}".format(score, height_score), align="center", font=("Courier", 24, "normal"))
  119.  
  120.  
  121.     # Check for a collision with the food
  122.     if head.distance(food) < 20:
  123.        # Move the food to a random position
  124.        x = random.randint(-290, 290)
  125.        y = random.randint(-290, 290)
  126.        food.goto(x, y)
  127.  
  128.  
  129. # Add a segment
  130.        new_segment = turtle.Turtle()
  131.        new_segment.speed(0)
  132.        new_segment.shape("square")
  133.        new_segment.color("red")
  134.        new_segment.penup()
  135.        segments.append(new_segment)
  136.  
  137. # Inscrease the score
  138.     score+=10
  139.  
  140.     if score > height_score:
  141.        high_score = score
  142.  
  143.     pen.clear()
  144.     pen.write("Score: {}  High Score: {}".format(score, height_score), align="center", font=("Courier", 24, "normal"))
  145.  
  146.  
  147.  
  148.     # Move the end segments first in reverse order
  149.     for index in range(len(segments)-1, 0, -1):
  150.        x = segments[index-1].xcor()
  151.        y = segments[index-1].ycor()
  152.        segments[index].goto(x, y)
  153.  
  154.  
  155. # Move segment 0 to where the head is
  156.     if len(segments) > 0:
  157.         x = head.xcor()
  158.         y = head.ycor()
  159.         segments[0].goto(x, y)
  160.  
  161.     move()
  162.  
  163. # Check for head collision whit the body segments
  164.     for segment in segments:
  165.         if segment.distance(head) < 20:
  166.             time.sleep(1)
  167.             head.goto(0, 0)
  168.             head.direction = "stop"
  169.  
  170.             # Hide the segments
  171.             for segment in segments:
  172.                 segment.goto(1000, 1000)
  173.  
  174.             # Clear the segments list
  175.             segments.clear()
  176.  
  177.  
  178.     time.sleep(delay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement