Advertisement
50IQ_YT

A Python Snake Game!

Jun 30th, 2019
7,782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. # Simple Snake Game in Python 3 for Beginners
  2. # By @50IQ Make Sure to go like and subscribe for more! (Check out my youtube video on my channel for how the game works and to find out more games, which you can play with your freinds. Now, If I find out you have copied my code I will video strike you and if your making alot of money using this code and selling it to people I will sue you. Other than that enjoy! Code BELOW!
  3.  
  4. import turtle
  5. import time
  6. import random
  7.  
  8. delay = 0.1
  9.  
  10. # Score
  11. score = 0
  12. high_score = 0
  13.  
  14. wn = turtle.Screen()
  15. wn.title("Snake Game by @50IQ")
  16. wn.bgcolor("Brown")
  17. wn.setup(width=600, height=400)
  18. wn.tracer(0) # Turns off the screen updates
  19.  
  20. # Snake head
  21. head = turtle.Turtle()
  22. head.speed(0)
  23. head.shape("circle")
  24. head.color("Purple")
  25. head.penup()
  26. head.goto(0,0)
  27. head.direction = "stop"
  28.  
  29. # Snake food
  30. food = turtle.Turtle()
  31. food.speed(0)
  32. food.shape("triangle")
  33. food.color("cyan")
  34. food.penup()
  35. food.goto(0,100)
  36.  
  37. segments = []
  38.  
  39. # Pen
  40. pen = turtle.Turtle()
  41. pen.speed(0)
  42. pen.shape("square")
  43. pen.color("white")
  44. pen.penup()
  45. pen.hideturtle()
  46. pen.goto(0, 260)
  47. pen.write("Score: 0  High Score: 0", align="center", font=("Courier", 14, "normal"))
  48.  
  49. # Functions
  50. def go_up():
  51.     if head.direction != "down":
  52.         head.direction = "up"
  53.  
  54. def go_down():
  55.     if head.direction != "up":
  56.         head.direction = "down"
  57.  
  58. def go_left():
  59.     if head.direction != "right":
  60.         head.direction = "left"
  61.  
  62. def go_right():
  63.     if head.direction != "left":
  64.         head.direction = "right"
  65.  
  66. def move():
  67.     if head.direction == "up":
  68.         y = head.ycor()
  69.         head.sety(y + 20)
  70.  
  71.     if head.direction == "down":
  72.         y = head.ycor()
  73.         head.sety(y - 20)
  74.  
  75.     if head.direction == "left":
  76.         x = head.xcor()
  77.         head.setx(x - 20)
  78.  
  79.     if head.direction == "right":
  80.         x = head.xcor()
  81.         head.setx(x + 20)
  82.  
  83. # Keyboard bindings
  84. wn.listen()
  85. wn.onkeypress(go_up, "w")
  86. wn.onkeypress(go_down, "s")
  87. wn.onkeypress(go_left, "a")
  88. wn.onkeypress(go_right, "d")
  89.  
  90. # Main game loop
  91. while True:
  92.     wn.update()
  93.  
  94.     # Check for a collision with the border
  95.     if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
  96.         time.sleep(1)
  97.         head.goto(0,0)
  98.         head.direction = "stop"
  99.  
  100.         # Hide the segments
  101.         for segment in segments:
  102.             segment.goto(1000, 1000)
  103.        
  104.         # Clear the segments list
  105.         segments.clear()
  106.  
  107.         # Reset the score
  108.         score = 0
  109.  
  110.         # Reset the delay
  111.         delay = 0.1
  112.  
  113.         pen.clear()
  114.         pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
  115.  
  116.  
  117.     # Check for a collision with the food
  118.     if head.distance(food) < 20:
  119.         # Move the food to a random spot
  120.         x = random.randint(-290, 290)
  121.         y = random.randint(-290, 290)
  122.         food.goto(x,y)
  123.  
  124.         # Add a segment
  125.         new_segment = turtle.Turtle()
  126.         new_segment.speed(0)
  127.         new_segment.shape("square")
  128.         new_segment.color("Blue")
  129.         new_segment.penup()
  130.         segments.append(new_segment)
  131.  
  132.  
  133.         # Shorten the delay
  134.         delay -= 0.001
  135.  
  136.         # Increase the score
  137.         score += 10
  138.  
  139.         if score > high_score:
  140.             high_score = score
  141.        
  142.         pen.clear()
  143.         pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
  144.  
  145.     # Move the end segments first in reverse order
  146.     for index in range(len(segments)-1, 0, -1):
  147.         x = segments[index-1].xcor()
  148.         y = segments[index-1].ycor()
  149.         segments[index].goto(x, y)
  150.  
  151.     # Move segment 0 to where the head is
  152.     if len(segments) > 0:
  153.         x = head.xcor()
  154.         y = head.ycor()
  155.         segments[0].goto(x,y)
  156.  
  157.     move()    
  158.  
  159.     # Check for head collision with the body segments
  160.     for segment in segments:
  161.         if segment.distance(head) < 20:
  162.             time.sleep(1)
  163.             head.goto(0,0)
  164.             head.direction = "stop"
  165.        
  166.             # Hide the segments
  167.             for segment in segments:
  168.                 segment.goto(1000, 1000)
  169.        
  170.             # Clear the segments list
  171.             segments.clear()
  172.  
  173.             # Reset the score
  174.             score = 0
  175.  
  176.             # Reset the delay
  177.             delay = 0.1
  178.        
  179.             # Update the score display
  180.             pen.clear()
  181.             pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Courier", 30, "normal"))
  182.  
  183.     time.sleep(delay)
  184.  
  185. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement