Advertisement
Guest User

Untitled

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