Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import time
- import random
- delay = 0.1
- score = 0
- f = open(r'C:\Users\Dani2000\Desktop\SnakeGame', 'rb')
- high_score = f
- #set up the screen
- ge = turtle.Screen()
- ge.title("Snake game")
- ge.bgcolor("black")
- ge.setup(width = 600, height = 600)
- ge.tracer(0)
- #Snake Head
- head = turtle.Turtle()
- head.speed(0)
- head.shape("square")
- head.color("yellow")
- head.penup()
- head.goto(0,0)
- head.direction = "stop"
- #snake's food
- food = turtle.Turtle()
- food.speed(0)
- food.shape("square")
- food.color("red")
- food.penup()
- food.goto(0,150)
- body = []
- pen = turtle.Turtle()
- pen.speed(0)
- pen.shape("square")
- pen.color("white")
- pen.penup()
- pen.hideturtle()
- pen.goto(0, 260)
- pen.write("Score: 0 High Score: ", high_score, align="center", font=("Light", 20, "bold italic"))
- #Functions
- def go_up():
- if head.direction != "down":
- head.direction = "up"
- def go_right():
- if head.direction != "left":
- head.direction = "right"
- def go_left():
- if head.direction != "right":
- head.direction = "left"
- def go_down():
- if head.direction != "up":
- head.direction = "down"
- def move():
- if head.direction == "up":
- y = head.ycor()
- head.sety(y + 20)
- if head.direction == "down":
- y = head.ycor()
- head.sety(y - 20)
- if head.direction == "left":
- x = head.xcor()
- head.setx(x - 20)
- if head.direction == "right":
- x = head.xcor()
- head.setx(x + 20)
- #keyboard
- ge.listen()
- ge.onkeypress(go_up, "w")
- ge.onkeypress(go_right, "d")
- ge.onkeypress(go_down, "s")
- ge.onkeypress(go_left, "a")
- #main game loop
- while True:
- ge.update()
- #f = open('High_Score.txt', 'r')
- if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
- #time.sleep(1)
- #head.goto(0,0)
- #head.direction = "stop"
- pen.write("Game Over", align="center", font=("Light", 25, "bold italic"))
- f = open(r'C:\Users\Dani2000\Desktop\SnakeGame', 'w')
- f.close()
- #ge._root.mainloop()
- ge.exit()
- for i in body:
- i.goto(1000,1000)
- body.clear()
- score = 0
- pen.clear()
- pen.write("Score: {} High Score: {}".format(score, high_score),
- align="center", font=("Light", 20, "bold italic"))
- if head.distance(food) < 20:
- x = random.randint(-285, 285)
- y = random.randint(-285, 285)
- food.goto(x, y)
- newBody = turtle.Turtle()
- newBody.speed(0)
- newBody.shape("square")
- newBody.color("yellow", "green")
- newBody.penup()
- body.append(newBody)
- score += 5
- if score > high_score:
- high_score = score
- pen.clear()
- pen.write("Score: {} High Score: {}".format(score, high_score),
- align="center", font=("Light", 20, "bold italic"))
- for i in range(len(body)-1, 0, -1):
- x = body[i-1].xcor()
- y = body[i-1].ycor()
- body[i].goto(x, y)
- if len(body) > 0:
- x = head.xcor()
- y = head.ycor()
- body[0].goto(x,y)
- move()
- for i in body:
- if i.distance(head) < 20:
- #time.sleep(1)d
- #head.goto(0, 0)
- #head.direction = "stop"
- #for i in body:
- # i.goto(1000,1000)
- #body.clear()
- f = open(r'C:\Users\Dani2000\Desktop\SnakeGame', 'w')
- f.close()
- ge.exit()
- time.sleep(delay)
- ge.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement