Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import time
- from turtle import Turtle
- delay = 0.1
- # screen setup
- wn = turtle.Screen()
- wn.title("Snake Game")
- wn.bgcolor("green")
- wn.setup(width=800, height=600)
- wn.tracer(0)
- # Snake head
- head = turtle.Turtle()
- head.speed(0)
- head.shape("square")
- head.color("black")
- head.penup()
- head.goto(0, 0)
- head.direction("stop")
- 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)
- wn.listen()
- wn.onkeypress(go_up(), "w")
- wn.onkeypress(go_down(), "s")
- wn.onkeypress(go_left(), "a")
- wn.onkeypress(go_right(), "d")
- while True:
- wn.update()
- move()
- time.sleep(delay)
- # I needed to put another while because it said that code is unrechable
- while True:
- wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement