Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import Turtle, Screen
- import time
- import random
- delay = 0.1
- screen = Screen()
- screen.title("Snake Game")
- screen.bgcolor("white")
- screen.setup(width=650, height=750)
- screen.tracer(0)
- t = Turtle()
- t.width(15)
- t.hideturtle()
- t.left(180)
- t.up()
- t.forward(310)
- t.right(90)
- t.forward(310)
- t.right(90)
- t.down()
- t.forward(615)
- t.right(90)
- t.forward(615)
- t.right(90)
- t.forward(615)
- t.right(90)
- t.forward(615)
- food = Turtle()
- food.speed(0)
- food.shape("circle")
- food.color("red")
- food.penup()
- food.goto(0,100)
- hd = Turtle()
- hd.shape("square")
- hd.penup()
- hd.color("green")
- hd.speed(-1)
- hd.direction = "stop"
- pen = Turtle()
- pen.speed(-1)
- pen.color("Black")
- pen.up()
- pen.hideturtle()
- pen.goto(0, 330)
- pen.write("SNAKE LENGTH: 1",False,"center",font=("Arial Narrow",20,"bold"))
- length = 1
- segments = []
- def go_up():
- if hd.direction != "down":
- hd.direction = "up"
- def go_down():
- if hd.direction != "up":
- hd.direction = "down"
- def go_left():
- if hd.direction != "right":
- hd.direction = "left"
- def go_right():
- if hd.direction != "left":
- hd.direction = "right"
- def movement():
- if hd.direction == "up":
- y = hd.ycor()
- hd.sety(y + 20)
- if hd.direction == "down":
- y = hd.ycor()
- hd.sety(y - 20)
- if hd.direction == "left":
- x = hd.xcor()
- hd.setx(x - 20)
- if hd.direction == "right":
- x = hd.xcor()
- hd.setx(x + 20)
- screen.listen()
- screen.onkeypress(go_up, "Up")
- screen.onkeypress(go_up, "w")
- screen.onkeypress(go_down, "Down")
- screen.onkeypress(go_down, "s")
- screen.onkeypress(go_left, "Left")
- screen.onkeypress(go_left, "a")
- screen.onkeypress(go_right, "Right")
- screen.onkeypress(go_right, "d")
- gameover = False
- while True:
- screen.update()
- time.sleep(delay)
- if hd.distance(food) == 0:
- x = random.randint(-14, 14)
- y = random.randint(-14, 14)
- food.goto(x * 20, y * 20)
- length += 1
- pen.clear()
- pen.write("SNAKE LENGTH: {}".format(length),False,"center",font=("Arial Narrow",20,"bold"))
- new_segment = Turtle()
- new_segment.speed(-1)
- new_segment.shape("square")
- new_segment.color("limegreen")
- new_segment.penup()
- segments.append(new_segment)
- if delay > 0.001:
- delay -= 0.001
- if hd.xcor()>290 or hd.xcor()<-290 or hd.ycor()>290 or hd.ycor()<-290:
- pen.clear()
- pen.write("GAME OVER, YOUR FINAL LENGTH IS: {}".format(length),False,"center",font=("Arial Narrow",20,"bold"))
- gameover = True
- for i in range(len(segments)-1, 0, -1):
- x = segments[i-1].xcor()
- y = segments[i-1].ycor()
- segments[i].goto(x, y)
- if len(segments) > 0:
- x = hd.xcor()
- y = hd.ycor()
- segments[0].goto(x,y)
- movement()
- for segment in segments:
- if segment.distance(hd) == 0:
- pen.clear()
- pen.goto(0, 0)
- pen.write("GAME OVER, YOUR FINAL LENGTH IS: {}".format(length),False,"center",font=("Arial Narrow",20,"bold"))
- gameover = True
- break
- if gameover == True:
- break
- #https://www.youtube.com/channel/UCQor7IURWM-lGT-tmFbFSCw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement