Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. # Simple Snake Game in Python 3 for Beginners
  2. # By @TokyoEdTech
  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. # Set up the screen
  15. wn = turtle.Screen()
  16. wn.title("Snake Game by @TokyoEdTech")
  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. # Functions
  52. def go_up():
  53. if head.direction != "down":
  54. head.direction = "up"
  55.  
  56.  
  57. def go_down():
  58. if head.direction != "up":
  59. head.direction = "down"
  60.  
  61.  
  62. def go_left():
  63. if head.direction != "right":
  64. head.direction = "left"
  65.  
  66.  
  67. def go_right():
  68. if head.direction != "left":
  69. head.direction = "right"
  70.  
  71.  
  72. def move():
  73. if head.direction == "up":
  74. y = head.ycor()
  75. head.sety(y + 20)
  76.  
  77. if head.direction == "down":
  78. y = head.ycor()
  79. head.sety(y - 20)
  80.  
  81. if head.direction == "left":
  82. x = head.xcor()
  83. head.setx(x - 20)
  84.  
  85. if head.direction == "right":
  86. x = head.xcor()
  87. head.setx(x + 20)
  88.  
  89.  
  90. # Keyboard bindings
  91. wn.listen()
  92. wn.onkeypress(go_up, "w")
  93. wn.onkeypress(go_down, "s")
  94. wn.onkeypress(go_left, "a")
  95. wn.onkeypress(go_right, "d")
  96.  
  97. # Main game loop
  98. while True:
  99. wn.update()
  100.  
  101. # Check for 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. # Hide the segments
  108. for segment in segments:
  109. segment.goto(1000, 1000)
  110.  
  111. # Clear the segments list
  112. segments.clear()
  113.  
  114. # Reset the score
  115. score = 0
  116.  
  117. # Reset the delay
  118. delay = 0.1
  119.  
  120. pen.clear()
  121. pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
  122.  
  123. # Check for a collision with the food
  124. if head.distance(food) < 20:
  125. # Move the food to a random spot
  126. x = random.randint(-290, 290)
  127. y = random.randint(-290, 290)
  128. food.goto(x, y)
  129.  
  130. # Add a segment
  131. new_segment = turtle.Turtle()
  132. new_segment.speed(0)
  133. new_segment.shape("square")
  134. new_segment.color("grey")
  135. new_segment.penup()
  136. segments.append(new_segment)
  137.  
  138. # Shorten the delay
  139. delay -= 0.001
  140.  
  141. # Increase the score
  142. score += 10
  143.  
  144. if score > high_score:
  145. high_score = score
  146.  
  147. pen.clear()
  148. pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
  149.  
  150. # Move the end segments first in reverse order
  151. for index in range(len(segments) - 1, 0, -1):
  152. x = segments[index - 1].xcor()
  153. y = segments[index - 1].ycor()
  154. segments[index].goto(x, y)
  155.  
  156. # Move segment 0 to where the head is
  157. if len(segments) > 0:
  158. x = head.xcor()
  159. y = head.ycor()
  160. segments[0].goto(x, y)
  161.  
  162. move()
  163.  
  164. # Check for head collision with the body segments
  165. for segment in segments:
  166. if segment.distance(head) < 20:
  167. time.sleep(1)
  168. head.goto(0, 0)
  169. head.direction = "stop"
  170.  
  171. # Hide the segments
  172. for segment in segments:
  173. segment.goto(1000, 1000)
  174.  
  175. # Clear the segments list
  176. segments.clear()
  177.  
  178. # Reset the score
  179. score = 0
  180.  
  181. # Reset the delay
  182. delay = 0.1
  183.  
  184. # Update the score display
  185. pen.clear()
  186. pen.write("Score: {} High Score: {}".format(score, high_score), align="center",
  187. font=("Courier", 24, "normal"))
  188.  
  189. time.sleep(delay)
  190.  
  191. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement