Skip to content
Menu
Daniel J. Pierce
  • C#
  • Games
  • Graphics
  • Math
  • Python
  • Tynker
  • Unity
Daniel J. Pierce

Snake eats apple

Posted on September 4, 2019November 11, 2019

Restart Game

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()

Projects

  • Particle Simulator
  • AP Computer Science Paint Program
  • Maybe Good Platformer
  • Custom Gravity Test
  • Triangle Types
  • Unity Obstacle Course v1
  • Collapsing rows
  • Java Tic Tac Toe (No AI)
  • Mersenne Prime Tester
  • Rainbow
  • Pong
  • Simple Tkinter Calculator
  • Hexidecimal Converter
  • Snake eats apple
  • Protect the Galactic Empire
  • Hangman
  • Beautiful Colorful Thing
  • Four Dots
  • Frogga
  • Escape asteroids
  • Portals
  • Guess the Number 2.0
  • Illogical
  • Tic Tac Toe
  • Guess the Number
  • Ultimate Calculator 1.0.0a0
  • Prime Number Tester
  • Number Base Converter 2.0a0
  • Number Base Converter 1.1
  • Python Equation Calculator
©2026 Daniel J. Pierce | WordPress Theme by Superb WordPress Themes