Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import time
- import random
- delay = 0.1
- game_Started = False
- restartable = False
- score = 0
- hi_score = 100
- wn = turtle.Screen()
- wn.title("The Ultimate Snek Game")
- wn.bgcolor("Blue")
- 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("black")
- food.penup()
- food.goto(0,100)
- 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=('Times New Roman', 25, "bold"))
- pen_text.goto(-80, -100)
- pen_text.write("Press space to Start", 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)
- pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Times New Roman', 12, "bold"))
- moveset = ["w", "a", "s", "d"]
- 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=('Times New Roman', 25, "bold"))
- pen_text.goto(-80, -100)
- pen_text.write("Press space to Start", 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)
- pen_text.write("Zunder Jacob A. Pacis", align="center", font=('Times New Roman', 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
- 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=("Consolas", 25, "bold"))
- turtle.hideturtle()
- turtle.clear()
- turtle.goto(0,0)
- turtle.color("white")
- turtle.write("GAME OVER!", align="center", font=("Comic Sans MS", 25, "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("blue")
- def startGame():
- global game_Started, delay, score, hi_score, restartable
- if game_Started:
- return
- game_Started = True
- restartable = False
- wn.bgcolor("purple")
- food.showturtle()
- head.showturtle()
- score = 0
- pen_text.clear()
- pen_text.goto(0,260)
- pen_text.write("Score: {} Hi-score: {}".format(score, hi_score), align="center",
- font=("Consolas", 25, "bold"))
- while True:
- wn.update()
- if head.distance(food) < 20:
- score += 5
- placeFood()
- new_segment = turtle.Turtle()
- new_segment.speed(0)
- new_segment.shape("circle")
- new_segment.color("black")
- 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=("Consolas", 25, "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)
- 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