How To Make Snake Game In Python

Welcome back! Let’s do some game development within Python! Now this specific method contains code that was written from this article so make sure to give them credit! Let’s go ahead and get building! First things first, we will need to have Python installed, read my article here to make sure you have Python and some IDE installed.

Building The Game

First off, let’s go ahead and install / import these packages:

from turtle import *
from random import randrange
from freegames import square, vector

The freegames package allows us to import the square and vector options. Next up, let’s setup our attributes in this game:

food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)

These are basically our assets within the game: food, snake & aim. Next, let’s create our first function. This function will pretty much change the direction of the snake within the game:

def change(x, y):
"Change snake direction."
aim.x = x
aim.y = y

Our next function will basically “end” our snake if it comes within our boundary:

def inside(head):
"Return True if head inside boundaries."
return -200 < head.x < 190 and -200 < head.y < 190

The next function sets up our moving object (the moving snake), moves the snake, sets the food, moves the food, and ends the game when needed:

def move():
"Move snake forward one segment."
head = snake[-1].copy()
head.move(aim)

if not inside(head) or head in snake:
square(head.x, head.y, 9, 'red')
update()
return

snake.append(head)

if head == food:
print('Snake:', len(snake))
food.x = randrange(-15, 15) * 10
food.y = randrange(-15, 15) * 10
else:
snake.pop(0)

clear()

for body in snake:
square(body.x, body.y, 9, 'black')

square(food.x, food.y, 9, 'green')
update()
ontimer(move, 100)

Finally, we have to set the canvas, keys and attributes for the game, we do that by bringing in the next couple of lines:

setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: change(10, 0), 'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
done()

Believe it or not we are pretty much done, Our final code should look like this:

from turtle import *
from random import randrange
from freegames import square, vector

food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)

def change(x, y):
"Change snake direction."
aim.x = x
aim.y = y

def inside(head):
"Return True if head inside boundaries."
return -200 < head.x < 190 and -200 < head.y < 190

def move():
"Move snake forward one segment."
head = snake[-1].copy()
head.move(aim)

if not inside(head) or head in snake:
square(head.x, head.y, 9, 'red')
update()
return

snake.append(head)

if head == food:
print('Snake:', len(snake))
food.x = randrange(-15, 15) * 10
food.y = randrange(-15, 15) * 10
else:
snake.pop(0)

clear()

for body in snake:
square(body.x, body.y, 9, 'black')

square(food.x, food.y, 9, 'green')
update()
ontimer(move, 100)

setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: change(10, 0), 'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
done()

Let’s go ahead and run this code, our output should be something like this:

Congrats! Enjoy building this game out using Python!

Data Scientist / Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store