PnnK

fixed snake

Oct 11th, 2022
1,208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. # import required modules
  2. import turtle
  3. import random
  4. import time
  5.  
  6. delay = 0.1
  7. score = 0
  8. high_score = 0
  9.  
  10. # Creating a window screen
  11. wn = turtle.Screen()
  12. wn.title("Snake Game")
  13. wn.bgcolor("gray")
  14. # the width and height can be put as user's choice
  15. wn.setup(width=600, height=600)
  16. wn.tracer(0)
  17.  
  18. # head of the snake
  19. head = turtle.Turtle()
  20. head.shape("square")
  21. head.color("white")
  22. head.penup()
  23. head.goto(0, 0)
  24. head.direction = "Stop"
  25.  
  26. # food in the game
  27. food = turtle.Turtle()
  28. colors = random.choice(['red', 'green', 'black'])
  29. shapes = random.choice(['square', 'triangle', 'circle'])
  30. food.speed(0)
  31. food.shape(shapes)
  32. food.color(colors)
  33. food.penup()
  34. food.goto(0, 100)
  35.  
  36. pen = turtle.Turtle()
  37. pen.speed(0)
  38. pen.shape("square")
  39. pen.color("white")
  40. pen.penup()
  41. pen.hideturtle()
  42. pen.goto(0, 250)
  43. pen.write("Score : 0  High Score : 0", align="center",
  44.           font=("candara", 24, "bold"))
  45.  
  46.  
  47. # assigning key directions
  48. def group():
  49.     if head.direction != "down":
  50.         head.direction = "up"
  51.  
  52.  
  53. def godown():
  54.     if head.direction != "up":
  55.         head.direction = "down"
  56.  
  57.  
  58. def goleft():
  59.     if head.direction != "right":
  60.         head.direction = "left"
  61.  
  62.  
  63. def goright():
  64.     if head.direction != "left":
  65.         head.direction = "right"
  66.  
  67.  
  68. def move():
  69.     if head.direction == "up":
  70.         y = head.ycor()
  71.         head.sety(y + 20)
  72.     if head.direction == "down":
  73.         y = head.ycor()
  74.         head.sety(y - 20)
  75.     if head.direction == "left":
  76.         x = head.xcor()
  77.         head.setx(x - 20)
  78.     if head.direction == "right":
  79.         x = head.xcor()
  80.         head.setx(x + 20)
  81.  
  82.  
  83. wn.onkeypress(group, "w")
  84. wn.onkeypress(godown, "s")
  85. wn.onkeypress(goleft, "a")
  86. wn.onkeypress(goright, "d")
  87.  
  88. segments = []
  89.  
  90. # Main Gameplay
  91. while True:
  92.     wn.listen()
  93.     wn.update()
  94.     if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
  95.         time.sleep(1)
  96.         head.goto(0, 0)
  97.         head.direction = "Stop"
  98.         colors = random.choice(['red', 'blue', 'green'])
  99.         shapes = random.choice(['square', 'circle'])
  100.         for segment in segments:
  101.             segment.goto(1000, 1000)
  102.         segments.clear()
  103.         score = 0
  104.         delay = 0.1
  105.         pen.clear()
  106.         pen.write("Score : {} High Score : {} ".format(
  107.             score, high_score), align="center", font=("candara", 24, "bold"))
  108.     if head.distance(food) < 20:
  109.         x = random.randint(-270, 270)
  110.         y = random.randint(-270, 270)
  111.         food.goto(x, y)
  112.  
  113.         # Adding segment
  114.         new_segment = turtle.Turtle()
  115.         new_segment.speed(0)
  116.         new_segment.shape("square")
  117.         new_segment.color("orange")  # tail colour
  118.         new_segment.penup()
  119.         segments.append(new_segment)
  120.         delay -= 0.001
  121.         score += 10
  122.         if score > high_score:
  123.             high_score = score
  124.         pen.clear()
  125.         pen.write("Score : {} High Score : {} ".format(
  126.             score, high_score), align="center", font=("candara", 24, "bold"))
  127.     # Checking for head collisions with body segments
  128.     for index in range(len(segments) - 1, 0, -1):
  129.         x = segments[index - 1].xcor()
  130.         y = segments[index - 1].ycor()
  131.         segments[index].goto(x, y)
  132.     if len(segments) > 0:
  133.         x = head.xcor()
  134.         y = head.ycor()
  135.         segments[0].goto(x, y)
  136.     move()
  137.     for segment in segments:
  138.         if segment.distance(head) < 20:
  139.             time.sleep(1)
  140.             head.goto(0, 0)
  141.             head.direction = "stop"
  142.             colors = random.choice(['red', 'blue', 'green'])
  143.             shapes = random.choice(['square', 'circle'])
  144.             for segment in segments:
  145.                 segment.goto(1000, 1000)
  146.             segment.clear()
  147.  
  148.             score = 0
  149.             delay = 0.1
  150.             pen.clear()
  151.             pen.write("Score : {} High Score : {} ".format(
  152.                 score, high_score), align="center", font=("candara", 24, "bold"))
  153.     time.sleep(delay)
  154.  
Advertisement
Add Comment
Please, Sign In to add comment