Advertisement
Sleddersquid

Snake

Jun 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4. import math
  5.  
  6. BoxWidth = 282
  7. BoxHeight = 282
  8.  
  9. speed1 = 20
  10.  
  11. delay = 0.1
  12.  
  13. score = 0
  14. high_score = 0
  15.  
  16. wn = turtle.Screen()
  17. wn.title("Snake")
  18. wn.bgcolor("dark green")
  19. wn.setup(width=600, height=600)
  20. wn.tracer(0)
  21.  
  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. food = turtle.Turtle()
  31. food.speed(0)
  32. food.shape("circle")
  33. food.color("red")
  34. food.penup()
  35. food.goto(0, 100)
  36.  
  37. segments = []
  38.  
  39. pen = turtle.Turtle()
  40. pen.speed(0)
  41. pen.shape("square")
  42. pen.color("white")
  43. pen.penup()
  44. pen.hideturtle()
  45. pen.goto(0, 260)
  46. pen.write("Score: 0  High Score: 0", align="center", font=("Calibri", 16, "normal"))
  47.  
  48. def go_up():
  49.     if head.direction != "down":
  50.         head.direction = "up"
  51.  
  52. def go_down():
  53.     if head.direction != "up":
  54.         head.direction = "down"
  55.  
  56. def go_left():
  57.     if head.direction != "right":
  58.         head.direction = "left"
  59.  
  60. def go_right():
  61.     if head.direction != "left":
  62.         head.direction = "right"
  63.  
  64. def move():
  65.     if head.direction == "up":
  66.         y = head.ycor()
  67.         head.sety(y + speed1)
  68.  
  69.     if head.direction == "down":
  70.         y = head.ycor()
  71.         head.sety(y - speed1)
  72.  
  73.     if head.direction == "left":
  74.         x = head.xcor()
  75.         head.setx(x - speed1)
  76.  
  77.     if head.direction == "right":
  78.         x = head.xcor()
  79.         head.setx(x + speed1)
  80.  
  81. wn.listen()
  82. wn.onkeypress(go_up, "w")
  83. wn.onkeypress(go_down, "s")
  84. wn.onkeypress(go_left, "a")
  85. wn.onkeypress(go_right, "d")
  86. wn.onkeypress(go_up, "Up")
  87. wn.onkeypress(go_down, "Down")
  88. wn.onkeypress(go_left, "Left")
  89. wn.onkeypress(go_right, "Right")
  90.  
  91. while True:
  92.     assert isinstance(wn, object)
  93.     wn.update()
  94.  
  95.     if head.xcor()>BoxWidth or head.xcor()<-BoxWidth or head.ycor()>BoxHeight or head.ycor()<-BoxHeight:
  96.         time.sleep(1)
  97.         head.goto(0,0)
  98.         head.direction = "stop"
  99.  
  100.         for segment in segments:
  101.             segment.goto(1000, 10000)
  102.  
  103.         segments.clear()
  104.  
  105.         score = 0
  106.  
  107.         delay = 0.1
  108.  
  109.         pen.clear()
  110.         pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Calibri", 16, "normal"))
  111.  
  112.  
  113.     if head.distance(food) < 24:
  114.         x = random.randint(-BoxWidth, BoxWidth)
  115.         y = random.randint(-BoxHeight, BoxHeight)
  116.         food.goto(x,y)
  117.  
  118.         new_segment = turtle.Turtle()
  119.         new_segment.speed(0)
  120.         new_segment.shape("square")
  121.         new_segment.color("grey")
  122.         new_segment.penup()
  123.         segments.append(new_segment)
  124.  
  125.         delay -= 0.01
  126.  
  127.         score += 1
  128.  
  129.         if score > high_score:
  130.             high_score = score
  131.  
  132.         pen.clear()
  133.         pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Calibri", 16, "normal"))
  134.  
  135.     for index in range(len(segments)-1, 0, -1):
  136.         x = segments[index-1].xcor()
  137.         y = segments[index-1].ycor()
  138.         segments[index].goto(x, y)
  139.  
  140.     if len(segments) > 0:
  141.         x = head.xcor()
  142.         y = head.ycor()
  143.         segments[0].goto(x,y)
  144.  
  145.     move()
  146.  
  147.     for segment in segments:
  148.         if segment.distance(head) < 20:
  149.             time.sleep(1)
  150.             head.goto(0,0)
  151.             head.direction = "stop"
  152.  
  153.             for segment in segments:
  154.                 segment.goto(1000, 1000)
  155.  
  156.             segments.clear()
  157.  
  158.             score = 0
  159.  
  160.             delay = 0.1
  161.  
  162.             pen.clear()
  163.             pen.write("Score: {}  High Score: {}".format(score, high_score), align="center", font=("Calibri", 16, "normal"))
  164.  
  165.     time.sleep(delay)
  166.  
  167.  
  168. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement