Advertisement
AlphaPenguino

snake 2 from turtle

Nov 23rd, 2023
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4.  
  5. delay = 0.1
  6. game_started = False
  7.  
  8. score = 0
  9. hi_score = 100
  10.  
  11. wn = turtle.Screen()
  12. wn.title("THIS IS THE TITLE")
  13. wn.bgcolor("blue")
  14. wn.setup(width=600, height=600)
  15. wn.tracer(0)
  16.  
  17. #snake_head
  18. head = turtle.Turtle()
  19. head.speed(0)
  20. head.shape("triangle")
  21. head.color("black")
  22. head.penup()
  23. head.goto(0,0)
  24. head.direction = "stop"
  25.  
  26. segments = []
  27.  
  28. #food
  29. food= turtle.Turtle()
  30. food.speed(0)
  31. food.shape("circle")
  32. food.color("green")
  33. food.penup()
  34. food.goto(0,0)
  35.  
  36. #text
  37. penText = turtle.Turtle()
  38. penText.speed(0)
  39. penText.shape("square")
  40. penText.color("white")
  41. penText.clear()
  42. penText.penup()
  43. penText.hideturtle()
  44. penText.goto(0, 260)
  45. penText.write("Press spacebar to start!", align="center", font=('Times New Roman', 25, "bold"))
  46.  
  47. def move_up():
  48.     if head.direction != "down":
  49.         head.direction = "up"
  50.  
  51. def move_down():
  52.     if head.direction != "up":
  53.         head.direction = "down"
  54.  
  55. def move_left():
  56.     if head.direction != "right":
  57.         head.direction = "left"
  58.  
  59. def move_right():
  60.     if head.direction != "left":
  61.         head.direction = "right"
  62.  
  63. def move():
  64.     if head.direction == "up":
  65.         head.sety(head.ycor() + 20)
  66.         head.setheading(90)
  67.     if head.direction == "down":
  68.         head.sety(head.ycor() - 20)
  69.         head.setheading(270)
  70.     if head.direction == "left":
  71.         head.setx(head.xcor() - 20)
  72.         head.setheading(180)
  73.     if head.direction == "right":
  74.         head.setx(head.xcor() + 20)
  75.         head.setheading(0)
  76.  
  77.     for segment in segments:
  78.         if segment.distance(head) < 20:
  79.             time.sleep(1)
  80.             head.goto(0, 0)
  81.             head.direction = "stop"
  82.  
  83.             for segment in segments:
  84.                 segment.goto(1000, 1000)
  85.  
  86.  
  87.  
  88.  
  89. def placeFood():
  90.     food.hideturtle()
  91.     food.setx(random.randint(-270, 270))
  92.     food.sety(random.randint(-270, 270))
  93.     food.showturtle()
  94.  
  95. def gameOver(score, hi_score):
  96.     penText.clear()
  97.     penText.write("Score: {} Hi-score: {}".format(score-5, hi_score), align="center",
  98.                           font=("Consolas", 25, "bold"))
  99.     turtle.clear()
  100.     turtle.penup()
  101.     turtle.color("white")
  102.     turtle.write("GAME OVER!", align="center", font=("Comic Sans MS", 25, "bold"))
  103.  
  104.     turtle.hideturtle()
  105.     head.goto(1000,1000)
  106.     head.color("blue")
  107.     head.direction = "stop"
  108.     food.goto(500,500)
  109.  
  110.  
  111.  
  112.  
  113.  
  114. def startGame():
  115.     global game_started, delay, score, hi_score
  116.     if game_started:
  117.         return
  118.     game_started = True
  119.     penText.clear()
  120.  
  121.  
  122.     while True:
  123.         wn.update()
  124.         if head.distance(food) < 20:
  125.             placeFood()
  126.             newSegment = turtle.Turtle()
  127.             newSegment.speed(0)
  128.             newSegment.shape("circle")
  129.             newSegment.color("white")
  130.             newSegment.penup()
  131.             segments.append(newSegment)
  132.  
  133.             if score > hi_score:
  134.                 hi_score = score
  135.  
  136.             penText.clear()
  137.             penText.write("score: {}  Hi-score: {}".format(score, hi_score), align="center",
  138.                           font=("Consolas", 25, "bold"))
  139.             delay -= 0.001
  140.             score += 5
  141.  
  142.         for i in range(len(segments)-1, 0, -1):
  143.             segments[i].setx(segments[i-1].xcor())
  144.             segments[i].sety(segments[i-1].ycor())
  145.  
  146.         if len(segments) > 0:
  147.             segments[0].goto(head.xcor(), head.ycor())
  148.  
  149.  
  150.         #collision
  151.         if int(head.xcor()) > 290 or int(head.xcor()) < -290 or int(head.ycor()) > 290 or int(head.ycor()) < -290:
  152.  
  153.             time.sleep(0)
  154.             game_started = False
  155.             gameOver(score, hi_score)
  156.  
  157.         move()
  158.  
  159.  
  160.  
  161.         time.sleep(delay)
  162.  
  163.  
  164.  
  165. wn.listen()
  166. wn.onkeypress(move_up, "w")
  167. wn.onkeypress(move_down, "s")
  168. wn.onkeypress(move_left, "a")
  169. wn.onkeypress(move_right, "d")
  170. wn.onkeypress(startGame, "space")
  171.  
  172.  
  173. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement