Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import
- import turtle
- import time
- import random
- # turtle head
- delay = 0.1
- wn = turtle.Screen()
- wn.title("Snake Game")
- wn.bgcolor("green")
- wn.setup(width=600, height=600)
- wn.tracer(0)
- head = turtle.Turtle()
- head.speed(0)
- head.shape("square")
- head.color("black")
- head.penup()
- head.goto(0,0)
- head.direction = "stop"
- # snake food
- food = turtle.Turtle()
- food.speed(0)
- food.shape("circle")
- food.color("red")
- food.penup()
- food.goto(0,100)
- segments = ()
- # functions
- def go_up():
- head.direction = "up"
- def go_down():
- head.direction = "down"
- def go_left():
- head.direction = "left"
- def go_right():
- head.direction = "right"
- 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 bindings
- wn.listen()
- wn.onkeypress(go_up, "w")
- wn.onkeypress(go_down, "s")
- wn.onkeypress(go_left, "a")
- wn.onkeypress(go_right, "d")
- # main game loop
- while True:
- wn.update()
- if head.distance(food) < 20:
- x = random.randint(-290, 290)
- y = random.randint(-290, 290)
- food.goto(x,y)
- # add a segment
- new_segment = turtle.Turtle()
- new_segment.speed(0)
- new_segment.shape = "square"
- new_segment.color("grey")
- new_segment.penup()
- segments.append(new_segment)
- for index in range(len(segments)-1, 0, -1):
- x = segments[index-1].xcor()
- y = segments[index-1].ycor()
- segments[index].goto(x, y)
- if len(segments) > 0:
- x = head.xcor()
- y = head.ycor()
- segments[0].goto(x, y)
- move()
- time.sleep(delay)
- wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement