Control the snake with arrow keys
Made in tynker.com, the Python 1 course, not cross-platform compatible but source code is below
import turtle, random screen = turtle.Screen() screen.bgcolor("lightgreen") sprite = turtle.Turtle() sprite.penup() sprite.speed(0) sprite.shape("square") sprite.goto(-1000,1000) running = False def update(): global snake if running: move() first = snake[0] x = first.xcor() y = first.ycor() if (x == food.xcor() and y == food.ycor()): createBody(x, y) food.ht() create_food() location = snake[0].pos() x1 = location[0] y1 = location[1] if x1 > 200 or y1 > 200: end_game() if x1 < -200 or y1 < -200: end_game() screen.ontimer(update, 350) def startGame(): global running running = True createBody(0,0) create_food() update() def end_game(): global food, snake food.ht() for i in snake: i.ht() snake = [] startGame() snake = [] def createBody(x,y): body = sprite.clone() body.goto(x,y) snake.append(body) food = None def create_food(): global food food = None food = sprite.clone() food.color('red') randX = random.randint(-8, 8) * 22 randY = random.randint(-8, 8) * 22 food.goto(randX, randY) dir = "Right" def move(): last = snake[len(snake)-1] first = snake[0] x = first.xcor() y = first.ycor() size = 22 if(dir == "Right"): last.goto((x + (size)), y) elif(dir == "Left"): last.goto((x - (size)), y) elif(dir == "Up"): last.goto(x, y + (size)) else: last.goto(x, y - (size)) snake.insert(0,last) snake.pop() def u(): global dir if(not dir == "Down"): dir = "Up" def d(): global dir if(not dir == "Up"): dir = "Down" def l(): global dir if(not dir == "Right"): dir = "Left" def r(): global dir if(not dir == "Left"): dir = "Right" screen.onkey(u, "Up") screen.onkey(d, "Down") screen.onkey(l, "Left") screen.onkey(r, "Right") screen.listen() startGame()