Building a Snake game in Python using Turtle

For those of you who have started learning Python, it can be awesome to start a project. Let’s build a game in Python. Remember the good old days when everybody used to play the snake game? We will be building a simple GUI-based Snake game using Python. So, let’s start.

A bit of history

Blockade — arcade snake game

Later the game became viral and everyone loved it because in the Nokia phone this snake game was pre-installed. In the age of modern technology, that time cell phone was the greatest innovation, and that innovation came with another pre-installed game.

Nowadays almost every platform has the Snake game with some cool interface.

Main module: Turtle

What we will build

Snake Game

I’d recommend using any Python IDE(i,e VSCode, Pycharm, Spyder, etc). You are good to go with Jupyter Notebook also.

You can find resource files on github, go to and clone the repository. Let’s jump ahead.

Rules of the game

1. The game screen has a fixed size(i.e width = 600px, height = 600px)

2. In the beginning, the game starts with the initial snake and apple at certain positions.

3. The user has control of the snake by presses specific arrow keys (i,e. ⬅️ = Move to Left, ⬆️ = Move to Upward, ➡️ = Move to Right and ⬇️ = Move to Downward).

4. Once the user presses a key, the snake keeps moving and waiting for the next direction otherwise it will move in the direction that the user has pressed previously.

5. The initial score is zero, and it will increase as the snake grab the apple.

6. Once the snake hits the wall or his own body, the game will be over at that point.

Importing the libraries

Turtle: This is a pre-installed python graphical library that enables users to create pictures and shapes by providing them with a virtual canvas. With this library, you can draw and create various types of shapes and images.

Time: This library helps us with time-related functions.

Random: Random helps us to generate randomize position for spawning the apple.

Setting the Graphics

Now we need some graphical items to visualize the snake body and the apple.

1. head.gif

2. tail.gif

3. apple.gif

I’ve put all the source files in the github repository. You can clone the repository to access them all. We need a GIF(.gif) image for graphical use because another format of the image is unsupported in the turtle. So we must use gif images for avoiding any error.

Defining the variables

At the beginning of the game, the initial score is zero. It will increase when the snake grabs an apple. Now initial the scoring view.

Controls and Helpers

Let's write down the code for controls our snake.

After all of this, we need a helper function to use all of this code. So here need another function to move our snake. When a user presses the desired control key to control the snake, the snake will move according to the command and reach a new position. The position will change according to the X or the Y coordinate.

After moving out snake successfully now we have to spawn the apple. If the snake grabs the current apple, after a minimum of delays there will be another spawn of another apple at another random position. If the snake grabs an apple then the score will be increased. Every apple is counted as a 10 point. When the snake grabs an apple, its size will increase. After eating an apple the snake size will be incremented by tail size and so on.

So we have just made the basic python snake game using turtle. But there are some issues. The snake takes input with keys to change its direction accordingly. It doesn’t change its direction to the opposite side. If the snake is moving upward and the user wants to change the direction of the snake downward then he/she can not. He/she can only change the direction either left or right but not downwards. In any case, the user presses the wrong direction and the snake touches its own body, there will be a collision that means GAME OVER.

So, we have completed our snake game. If you need source files for this project, go to .

Thanks, happy coding!

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