Advertisement
AlphaPenguino

Snake using Turtle 4.0 (Obstacle + Rainbow)

Dec 5th, 2023
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.13 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4. import colorsys
  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 Snake Game")
  15. wn.bgcolor("#476A77")
  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("red")
  33. food.penup()
  34. food.goto(0,100)
  35.  
  36. obstacles = []
  37.  
  38. pen_text = turtle.Turtle()
  39. pen_text.speed(0)
  40. pen_text.shape("square")
  41. pen_text.color("white")
  42. pen_text.clear()
  43. pen_text.penup()
  44. pen_text.hideturtle()
  45. pen_text.goto(0, 260)
  46. pen_text.goto(0, 0)
  47. pen_text.write("Snake Game Original by Josh Mojica", align="center", font=('Bauhaus 93', 25, "bold"))
  48. pen_text.goto(-100, -100)
  49. pen_text.write("Press 'space' to Start", align="center", font=('Grotesque', 12, "bold"))
  50. pen_text.goto(100, -100)
  51. pen_text.write("Press 'X' to Exit the game", align="center", font=('Grotesque', 12, "bold"))
  52. pen_text.goto(-100, -260)
  53. pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Grotesque', 12, "bold"))
  54. moveset = ["w", "a", "s", "d"]
  55.  
  56.  
  57. def create_obstacle():
  58.     obstacle = turtle.Turtle()
  59.     obstacle.shape("square")
  60.     obstacle.color("black")
  61.     obstacle.penup()
  62.     obstacle.speed(0)
  63.     obstacle.goto(random.randint(-280, 280), random.randint(-280, 280))
  64.     obstacles.append(obstacle)
  65.  
  66. def remove_obstacles():
  67.     for obstacle in obstacles:
  68.         obstacle.hideturtle()  # Alternatively, you can use obstacle.clear() to clear the obstacle from the screen
  69.     obstacles.clear()
  70.  
  71.  
  72. def generate_rainbow_color(step):
  73.     r, g, b = colorsys.hsv_to_rgb(step, 1, 1)
  74.     return (r, g, b)
  75.  
  76. def check_collision():
  77.     for obstacle in obstacles:
  78.         if head.distance(obstacle) < 20:
  79.             return True
  80.     return False
  81.  
  82.  
  83. def moveUp():
  84.     if head.direction != "down":
  85.         head.direction = "up"
  86. def moveDown():
  87.     if head.direction != "up":
  88.         head.direction = "down"
  89. def moveRight():
  90.     if head.direction != "left":
  91.         head.direction = "right"
  92. def moveLeft():
  93.     if head.direction != "right":
  94.         head.direction = "left"
  95.  
  96.  
  97.  
  98.  
  99. 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))}
  100. def move():
  101.     moveMapping.get(head.direction, lambda: ())()
  102.     for segment in segments:
  103.         if head.distance(segment) < 20:
  104.             gameOver(score, hi_score)
  105.             time.sleep(1)
  106.             game_started=False
  107. def closeGame():
  108.     if not game_Started:
  109.         turtle.bye()
  110.  
  111. def restartGame():
  112.    
  113.     global restartable
  114.     if restartable:
  115.         wn.resetscreen
  116.         pen_text.clear()
  117.         turtle.clear()
  118.         head.goto(0,0)
  119.         food.setx(random.randint(-270, 270))
  120.         food.sety(random.randint(-270, 270))
  121.         pen_text.goto(0, 275)
  122.  
  123.         score = 0
  124.         pen_text.goto(0, 260)
  125.         pen_text.goto(0, 0)
  126.         pen_text.write("Snake Game Original by Josh Mojica", align="center", font=('Bauhaus 93', 25, "bold"))
  127.         pen_text.goto(-100, -100)
  128.         pen_text.write("Press 'space' to Start", align="center", font=('Grotesque', 12, "bold"))
  129.         pen_text.goto(100, -100)
  130.         pen_text.write("Press 'X' to Exit the game", align="center", font=('Grotesque', 12, "bold"))
  131.         pen_text.goto(-100, -260)
  132.         pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Grotesque', 12, "bold"))
  133.         head.hideturtle()
  134.         food.hideturtle()
  135.  
  136. def placeFood():
  137.     food.hideturtle()
  138.     food.setx(random.randint(-270, 270))
  139.     food.sety(random.randint(-270, 270))
  140.     food.showturtle()
  141.  
  142. def gameOver(finscore,finalscore):
  143.     global restartable
  144.     remove_obstacles()
  145.     restartable = True
  146.     for segment in segments:
  147.         segment.goto(1000,1000)
  148.     segments.clear()
  149.  
  150.     pen_text.clear()
  151.     pen_text.goto(0, -50)
  152.     pen_text.write("Score: {} Hi-score: {}".format(finscore, finalscore), align="center",
  153.                   font=("Grotesque", 22, "bold"))
  154.     turtle.hideturtle()
  155.     turtle.clear()
  156.     turtle.goto(0,0)
  157.     turtle.color("white")
  158.     turtle.write("GAME OVER!", align="center", font=("Bauhaus 93", 30, "bold"))
  159.     pen_text.goto(-80, -100)
  160.     pen_text.write("Press P to restart", align="center", font=('Times New Roman', 12, "bold"))
  161.     pen_text.goto(80, -100)
  162.     pen_text.write("Press X to Exit the game", align="center", font=('Times New Roman', 12, "bold"))
  163.     pen_text.goto(-100, -260)
  164.     turtle.hideturtle()
  165.     head.goto(0, 10000)
  166.     head.color("white")
  167.     head.direction = "stop"
  168.     food.goto(0, 1000)
  169.  
  170.     wn.bgcolor("#476A77")
  171.  
  172.  
  173. def startGame():
  174.     global game_Started, delay, score, hi_score, restartable
  175.     if game_Started:
  176.         return
  177.     game_Started = True
  178.     restartable = False
  179.     wn.bgcolor("#4B0082")
  180.     food.showturtle()
  181.     head.showturtle()
  182.     score = 0
  183.     delay = 0.1
  184.     pen_text.clear()
  185.     pen_text.goto(0,260)
  186.     pen_text.write("Score: {} Hi-score: {}".format(score, hi_score), align="center",
  187.                   font=("Grotesque", 22, "bold"))
  188.  
  189.     while True:
  190.  
  191.         wn.update()
  192.  
  193.         if head.distance(food) < 20:
  194.             score += 5
  195.             create_obstacle()
  196.             placeFood()
  197.             # Generate a rainbow color based on the number of segments
  198.             rainbow_step = len(segments) / 10  # Adjust 10 to control the color transition speed
  199.             rainbow_color = generate_rainbow_color(rainbow_step)
  200.             new_segment = turtle.Turtle()
  201.             new_segment.speed(0)
  202.             new_segment.shape("circle")
  203.             new_segment.color(rainbow_color)
  204.             new_segment.penup()
  205.             segments.append(new_segment)
  206.  
  207.  
  208.             delay -= 0.001
  209.  
  210.             if score > hi_score:
  211.                 hi_score = score
  212.             pen_text.clear()
  213.             pen_text.write("Score: {} Hi-score: {}".format(score, hi_score), align="center",
  214.                           font=("Grotesque", 22, "bold"))
  215.  
  216.         for i in range(len(segments)-1, 0, -1):
  217.             segments[i].setx(segments[i - 1].xcor())
  218.             segments[i].sety(segments[i - 1].ycor())
  219.  
  220.         if len(segments) > 0:
  221.             segments[0].goto(head.xcor(), head.ycor())
  222.  
  223.         if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
  224.             game_Started = False
  225.             gameOver(score, hi_score)
  226.             time.sleep(1)
  227.  
  228.         if check_collision():
  229.             game_Started = False
  230.             gameOver(score, hi_score)
  231.             remove_obstacles()
  232.             break
  233.             time.sleep(1)
  234.  
  235.         move()
  236.         time.sleep(delay)
  237. wn.listen()
  238. mapping = {"w": moveUp, "s": moveDown, "a": moveLeft, "d": moveRight, "space": startGame, "x": closeGame, "p": restartGame}
  239. for key, action in mapping.items():
  240.     wn.onkeypress(lambda a=action: a(), key)
  241. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement