Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import time
- import random
- import colorsys
- delay = 0.1
- game_Started = False
- restartable = False
- score = 0
- hi_score = 100
- wn = turtle.Screen()
- wn.title("The Ultimate Snake Game")
- wn.bgcolor("#476A77")
- wn.setup(width = 600, height = 600)
- wn.tracer(0)
- head = turtle.Turtle()
- head.speed(0)
- head.color("pink")
- head.shape("triangle")
- head.penup()
- head.goto(0,0)
- head.direction = "stop"
- segments = []
- food = turtle.Turtle()
- food.speed(0)
- food.shape("circle")
- food.color("red")
- food.penup()
- food.goto(0,100)
- obstacles = []
- pen_text = turtle.Turtle()
- pen_text.speed(0)
- pen_text.shape("square")
- pen_text.color("white")
- pen_text.clear()
- pen_text.penup()
- pen_text.hideturtle()
- pen_text.goto(0, 260)
- pen_text.goto(0, 0)
- pen_text.write("Snake Game Original by Josh Mojica", align="center", font=('Bauhaus 93', 25, "bold"))
- pen_text.goto(-100, -100)
- pen_text.write("Press 'space' to Start", align="center", font=('Grotesque', 12, "bold"))
- pen_text.goto(100, -100)
- pen_text.write("Press 'X' to Exit the game", align="center", font=('Grotesque', 12, "bold"))
- pen_text.goto(-100, -260)
- pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Grotesque', 12, "bold"))
- moveset = ["w", "a", "s", "d"]
- def create_obstacle():
- obstacle = turtle.Turtle()
- obstacle.shape("square")
- obstacle.color("black")
- obstacle.penup()
- obstacle.speed(0)
- obstacle.goto(random.randint(-280, 280), random.randint(-280, 280))
- obstacles.append(obstacle)
- def remove_obstacles():
- for obstacle in obstacles:
- obstacle.hideturtle() # Alternatively, you can use obstacle.clear() to clear the obstacle from the screen
- obstacles.clear()
- def generate_rainbow_color(step):
- r, g, b = colorsys.hsv_to_rgb(step, 1, 1)
- return (r, g, b)
- def check_collision():
- for obstacle in obstacles:
- if head.distance(obstacle) < 20:
- return True
- return False
- def moveUp():
- if head.direction != "down":
- head.direction = "up"
- def moveDown():
- if head.direction != "up":
- head.direction = "down"
- def moveRight():
- if head.direction != "left":
- head.direction = "right"
- def moveLeft():
- if head.direction != "right":
- head.direction = "left"
- 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))}
- def move():
- moveMapping.get(head.direction, lambda: ())()
- for segment in segments:
- if head.distance(segment) < 20:
- gameOver(score, hi_score)
- time.sleep(1)
- game_started=False
- def closeGame():
- if not game_Started:
- turtle.bye()
- def restartGame():
- global restartable
- if restartable:
- wn.resetscreen
- pen_text.clear()
- turtle.clear()
- head.goto(0,0)
- food.setx(random.randint(-270, 270))
- food.sety(random.randint(-270, 270))
- pen_text.goto(0, 275)
- score = 0
- pen_text.goto(0, 260)
- pen_text.goto(0, 0)
- pen_text.write("Snake Game Original by Josh Mojica", align="center", font=('Bauhaus 93', 25, "bold"))
- pen_text.goto(-100, -100)
- pen_text.write("Press 'space' to Start", align="center", font=('Grotesque', 12, "bold"))
- pen_text.goto(100, -100)
- pen_text.write("Press 'X' to Exit the game", align="center", font=('Grotesque', 12, "bold"))
- pen_text.goto(-100, -260)
- pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Grotesque', 12, "bold"))
- head.hideturtle()
- food.hideturtle()
- def placeFood():
- food.hideturtle()
- food.setx(random.randint(-270, 270))
- food.sety(random.randint(-270, 270))
- food.showturtle()
- def gameOver(finscore,finalscore):
- global restartable
- remove_obstacles()
- restartable = True
- for segment in segments:
- segment.goto(1000,1000)
- segments.clear()
- pen_text.clear()
- pen_text.goto(0, -50)
- pen_text.write("Score: {} Hi-score: {}".format(finscore, finalscore), align="center",
- font=("Grotesque", 22, "bold"))
- turtle.hideturtle()
- turtle.clear()
- turtle.goto(0,0)
- turtle.color("white")
- turtle.write("GAME OVER!", align="center", font=("Bauhaus 93", 30, "bold"))
- pen_text.goto(-80, -100)
- pen_text.write("Press P to restart", align="center", font=('Times New Roman', 12, "bold"))
- pen_text.goto(80, -100)
- pen_text.write("Press X to Exit the game", align="center", font=('Times New Roman', 12, "bold"))
- pen_text.goto(-100, -260)
- turtle.hideturtle()
- head.goto(0, 10000)
- head.color("white")
- head.direction = "stop"
- food.goto(0, 1000)
- wn.bgcolor("#476A77")
- def startGame():
- global game_Started, delay, score, hi_score, restartable
- if game_Started:
- return
- game_Started = True
- restartable = False
- wn.bgcolor("#4B0082")
- food.showturtle()
- head.showturtle()
- score = 0
- delay = 0.1
- pen_text.clear()
- pen_text.goto(0,260)
- pen_text.write("Score: {} Hi-score: {}".format(score, hi_score), align="center",
- font=("Grotesque", 22, "bold"))
- while True:
- wn.update()
- if head.distance(food) < 20:
- score += 5
- create_obstacle()
- placeFood()
- # Generate a rainbow color based on the number of segments
- rainbow_step = len(segments) / 10 # Adjust 10 to control the color transition speed
- rainbow_color = generate_rainbow_color(rainbow_step)
- new_segment = turtle.Turtle()
- new_segment.speed(0)
- new_segment.shape("circle")
- new_segment.color(rainbow_color)
- new_segment.penup()
- segments.append(new_segment)
- delay -= 0.001
- if score > hi_score:
- hi_score = score
- pen_text.clear()
- pen_text.write("Score: {} Hi-score: {}".format(score, hi_score), align="center",
- font=("Grotesque", 22, "bold"))
- for i in range(len(segments)-1, 0, -1):
- segments[i].setx(segments[i - 1].xcor())
- segments[i].sety(segments[i - 1].ycor())
- if len(segments) > 0:
- segments[0].goto(head.xcor(), head.ycor())
- if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
- game_Started = False
- gameOver(score, hi_score)
- time.sleep(1)
- if check_collision():
- game_Started = False
- gameOver(score, hi_score)
- remove_obstacles()
- break
- time.sleep(1)
- move()
- time.sleep(delay)
- wn.listen()
- mapping = {"w": moveUp, "s": moveDown, "a": moveLeft, "d": moveRight, "space": startGame, "x": closeGame, "p": restartGame}
- for key, action in mapping.items():
- wn.onkeypress(lambda a=action: a(), key)
- wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement