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
- score = 0
- hi_score = 100
- wn = turtle.Screen()
- wn.title("THIS IS THE TITLE")
- wn.bgcolor("blue")
- wn.setup(width=600, height=600)
- wn.tracer(0)
- #snake_head
- head = turtle.Turtle()
- head.speed(0)
- head.shape("triangle")
- head.color("black")
- head.penup()
- head.goto(0,0)
- head.direction = "stop"
- segments = []
- #food
- food= turtle.Turtle()
- food.speed(0)
- food.shape("circle")
- food.color("green")
- food.penup()
- food.goto(0,0)
- #text
- penText = turtle.Turtle()
- penText.speed(0)
- penText.shape("square")
- penText.color("white")
- penText.clear()
- penText.penup()
- penText.hideturtle()
- penText.goto(0, 260)
- penText.write("Press spacebar to start!", align="center", font=('Times New Roman', 25, "bold"))
- def move_up():
- if head.direction != "down":
- head.direction = "up"
- def move_down():
- if head.direction != "up":
- head.direction = "down"
- def move_left():
- if head.direction != "right":
- head.direction = "left"
- def move_right():
- if head.direction != "left":
- head.direction = "right"
- def move():
- if head.direction == "up":
- head.sety(head.ycor() + 20)
- head.setheading(90)
- if head.direction == "down":
- head.sety(head.ycor() - 20)
- head.setheading(270)
- if head.direction == "left":
- head.setx(head.xcor() - 20)
- head.setheading(180)
- if head.direction == "right":
- head.setx(head.xcor() + 20)
- head.setheading(0)
- for segment in segments:
- if segment.distance(head) < 20:
- time.sleep(1)
- head.goto(0, 0)
- head.direction = "stop"
- for segment in segments:
- segment.goto(1000, 1000)
- def placeFood():
- food.hideturtle()
- food.setx(random.randint(-270, 270))
- food.sety(random.randint(-270, 270))
- food.showturtle()
- def gameOver(score, hi_score):
- penText.clear()
- penText.write("Score: {} Hi-score: {}".format(score-5, hi_score), align="center",
- font=("Consolas", 25, "bold"))
- turtle.clear()
- turtle.penup()
- turtle.color("white")
- turtle.write("GAME OVER!", align="center", font=("Comic Sans MS", 25, "bold"))
- turtle.hideturtle()
- head.goto(1000,1000)
- head.color("blue")
- head.direction = "stop"
- food.goto(500,500)
- def startGame():
- global game_started, delay, score, hi_score
- if game_started:
- return
- game_started = True
- penText.clear()
- while True:
- wn.update()
- if head.distance(food) < 20:
- placeFood()
- newSegment = turtle.Turtle()
- newSegment.speed(0)
- newSegment.shape("circle")
- newSegment.color("white")
- newSegment.penup()
- segments.append(newSegment)
- if score > hi_score:
- hi_score = score
- penText.clear()
- penText.write("score: {} Hi-score: {}".format(score, hi_score), align="center",
- font=("Consolas", 25, "bold"))
- delay -= 0.001
- score += 5
- 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())
- #collision
- if int(head.xcor()) > 290 or int(head.xcor()) < -290 or int(head.ycor()) > 290 or int(head.ycor()) < -290:
- time.sleep(0)
- game_started = False
- gameOver(score, hi_score)
- move()
- time.sleep(delay)
- wn.listen()
- wn.onkeypress(move_up, "w")
- wn.onkeypress(move_down, "s")
- wn.onkeypress(move_left, "a")
- wn.onkeypress(move_right, "d")
- wn.onkeypress(startGame, "space")
- wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement