Advertisement
AlphaPenguino

snake 3.0

Dec 3rd, 2023
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.92 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4.  
  5.  
  6. delay = 0.1
  7. game_Started = False
  8. restartable = False
  9.  
  10. score = 0
  11. hi_score = 100
  12.  
  13. wn = turtle.Screen()
  14. wn.title("The Ultimate Snek Game")
  15. wn.bgcolor("Blue")
  16. wn.setup(width = 600, height = 600)
  17. wn.tracer(0)
  18.  
  19. head = turtle.Turtle()
  20. head.speed(0)
  21. head.color("pink")
  22. head.shape("triangle")
  23. head.penup()
  24. head.goto(0,0)
  25. head.direction = "stop"
  26.  
  27. segments = []
  28.  
  29. food = turtle.Turtle()
  30. food.speed(0)
  31. food.shape("circle")
  32. food.color("black")
  33. food.penup()
  34. food.goto(0,100)
  35.  
  36. pen_text = turtle.Turtle()
  37. pen_text.speed(0)
  38. pen_text.shape("square")
  39. pen_text.color("white")
  40. pen_text.clear()
  41. pen_text.penup()
  42. pen_text.hideturtle()
  43. pen_text.goto(0, 260)
  44. pen_text.goto(0, 0)
  45. pen_text.write("Snake Game Original by Josh Mojica", align="center", font=('Times New Roman', 25, "bold"))
  46. pen_text.goto(-80, -100)
  47. pen_text.write("Press space to Start", align="center", font=('Times New Roman', 12, "bold"))
  48. pen_text.goto(80, -100)
  49. pen_text.write("Press X to Exit the game", align="center", font=('Times New Roman', 12, "bold"))
  50. pen_text.goto(-100, -260)
  51. pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Times New Roman', 12, "bold"))
  52. moveset = ["w", "a", "s", "d"]
  53.  
  54. def moveUp():
  55.     if head.direction != "down":
  56.         head.direction = "up"
  57. def moveDown():
  58.     if head.direction != "up":
  59.         head.direction = "down"
  60. def moveRight():
  61.     if head.direction != "left":
  62.         head.direction = "right"
  63. def moveLeft():
  64.     if head.direction != "right":
  65.         head.direction = "left"
  66.  
  67. moveMapping = {"up": lambda: (head.sety(head.ycor() + 20), head.setheading(90)),"down": lambda: (head.sety(head.ycor() - 20), head.setheading(270)),"left": lambda: (head.setx(head.xcor() - 20), head.setheading(180)),"right": lambda: (head.setx(head.xcor() + 20), head.setheading(0))}
  68. def move():
  69.     moveMapping.get(head.direction, lambda: ())()
  70.     for segment in segments:
  71.         if head.distance(segment) < 20:
  72.             gameOver(score, hi_score)
  73.             time.sleep(1)
  74.             game_started=False
  75. def closeGame():
  76.     if not game_Started:
  77.         turtle.bye()
  78.  
  79. def restartGame():
  80.     global restartable
  81.     if restartable:
  82.         wn.resetscreen
  83.         pen_text.clear()
  84.         turtle.clear()
  85.         head.goto(0,0)
  86.         food.setx(random.randint(-270, 270))
  87.         food.sety(random.randint(-270, 270))
  88.         pen_text.goto(0, 275)
  89.  
  90.         score = 0
  91.         pen_text.goto(0, 260)
  92.         pen_text.goto(0, 0)
  93.         pen_text.write("Snake Game Original by Josh Mojica", align="center", font=('Times New Roman', 25, "bold"))
  94.         pen_text.goto(-80, -100)
  95.         pen_text.write("Press space to Start", align="center", font=('Times New Roman', 12, "bold"))
  96.         pen_text.goto(80, -100)
  97.         pen_text.write("Press X to Exit the game", align="center", font=('Times New Roman', 12, "bold"))
  98.         pen_text.goto(-100, -260)
  99.         pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Times New Roman', 12, "bold"))
  100.         head.hideturtle()
  101.         food.hideturtle()
  102.  
  103. def placeFood():
  104.     food.hideturtle()
  105.     food.setx(random.randint(-270, 270))
  106.     food.sety(random.randint(-270, 270))
  107.     food.showturtle()
  108.  
  109. def gameOver(finscore,finalscore):
  110.     global restartable
  111.     restartable = True
  112.     for segment in segments:
  113.         segment.goto(1000,1000)
  114.     segments.clear()
  115.  
  116.     pen_text.clear()
  117.     pen_text.goto(0, -50)
  118.     pen_text.write("Score: {} Hi-score: {}".format(finscore, finalscore), align="center",
  119.                   font=("Consolas", 25, "bold"))
  120.     turtle.hideturtle()
  121.     turtle.clear()
  122.     turtle.goto(0,0)
  123.     turtle.color("white")
  124.     turtle.write("GAME OVER!", align="center", font=("Comic Sans MS", 25, "bold"))
  125.     pen_text.goto(-80, -100)
  126.     pen_text.write("Press P to restart", align="center", font=('Times New Roman', 12, "bold"))
  127.     pen_text.goto(80, -100)
  128.     pen_text.write("Press X to Exit the game", align="center", font=('Times New Roman', 12, "bold"))
  129.     pen_text.goto(-100, -260)
  130.     turtle.hideturtle()
  131.     head.goto(0, 10000)
  132.     head.color("white")
  133.     head.direction = "stop"
  134.     food.goto(0, 1000)
  135.  
  136.     wn.bgcolor("blue")
  137.  
  138.  
  139. def startGame():
  140.     global game_Started, delay, score, hi_score, restartable
  141.     if game_Started:
  142.         return
  143.     game_Started = True
  144.     restartable = False
  145.     wn.bgcolor("purple")
  146.     food.showturtle()
  147.     head.showturtle()
  148.     score = 0
  149.     pen_text.clear()
  150.     pen_text.goto(0,260)
  151.     pen_text.write("Score: {} Hi-score: {}".format(score, hi_score), align="center",
  152.                   font=("Consolas", 25, "bold"))
  153.  
  154.     while True:
  155.         wn.update()
  156.         if head.distance(food) < 20:
  157.             score += 5
  158.             placeFood()
  159.             new_segment = turtle.Turtle()
  160.             new_segment.speed(0)
  161.             new_segment.shape("circle")
  162.             new_segment.color("black")
  163.             new_segment.penup()
  164.             segments.append(new_segment)
  165.  
  166.  
  167.             delay -= 0.001
  168.  
  169.             if score > hi_score:
  170.                 hi_score = score
  171.             pen_text.clear()
  172.             pen_text.write("Score: {} Hi-score: {}".format(score, hi_score), align="center",
  173.                           font=("Consolas", 25, "bold"))
  174.  
  175.         for i in range(len(segments)-1, 0, -1):
  176.             segments[i].setx(segments[i - 1].xcor())
  177.             segments[i].sety(segments[i - 1].ycor())
  178.  
  179.         if len(segments) > 0:
  180.             segments[0].goto(head.xcor(), head.ycor())
  181.  
  182.         if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
  183.             game_Started = False
  184.             gameOver(score, hi_score)
  185.             time.sleep(1)
  186.  
  187.         move()
  188.         time.sleep(delay)
  189. wn.listen()
  190. mapping = {"w": moveUp, "s": moveDown, "a": moveLeft, "d": moveRight, "space": startGame, "x": closeGame, "p": restartGame}
  191. for key, action in mapping.items():
  192.     wn.onkeypress(lambda a=action: a(), key)
  193. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement