# How to Create a Snake game in Python using Turtle

In this Python tutorial, we will learn how to create a snake game in python using turtle. It is easy to create a Snake game in Python.

• The snake game concept originated in 1976, developed by a British company called Gremlin Interactive.
• The snake game is considered an interesting and skillful game. It is popularized among people for generations.
• The snake in the snake game is controlled using four directions, and if the snakehead hits the wall or hits itself, then the snake dies, and the game will be ended with the score.
• The player’s main aim in the game is to achieve maximum points as possible by collecting the food.
• So, if you are a beginner and you learned about turtle then you can use this module for making a snake game.

If you are new to Python turtle, check out Python turtle programming and Draw colored filled shapes using Python Turtle.

## Overview of Snake game in Python turtle

In this, we will be creating a Snake game in python using the following:

• Turtle – It is a pre-installed library in python which is used for creating shapes, picture, and game.
• Time – It is used for counting the number of seconds elapsed since the epoch.
• Random – This module is used to generate random numbers in python using the random module.
• penup() – It stops drawing of the turtle pen.
• speed() – It is an integer value in the range 0 to 10. So, 0 is fastest, 10 is fast, 6 is normal, 3 is slow, and 1 is slowest. If no argument is given, returns the current speed.
• color() – It returns or set pen color and fill color.
• shape() – It set turtle shape to the shape of a given name.
• hideturtle() – It makes the turtle invisible.
• xcor() – Return the turtle’s x coordinate.
• ycor() – Return the turtle’s y coordinate.

## Create a snake game using Python turtle (Step by Step)

Here, we will explain the easy way to code the snake game in python. It is recommended to go throw the below step.

Step 1:

Firstly, we will import all the modules into the program, and we will give the default value for the game.

``````import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0``````

Step 2:

• Now, we will create the window screen for the game, and also we will create the head of the snake and food for the snake. The score will be displayed at the header of the game.
• The function turtle.Screen() is used to create a window. In this code, our window is “wn” for the game.
• We have to give the window a name with the function “wn.title(“Snake Game”)”.
• To set the background color for the window we have used “wn.bgcolor(‘black’)”. Set the window height and width with the function “wn.setup(width=X, height=Y)”. Here, width=600 and height=600.
• The function window.tracer(0) turns off the screen updates. As, we do not need any screen updates other than the scoreboard, so it is set to 0.
• Now, we will create a snakehead, it is basically a turtle which will be a snake and it will move around.
• For creating a turtle we will use “turtle.Turtle()” and assign the name head. The head speed is set to 0 as we are just initializing and the head does not need to move.
• The function “head.penup()” makes sure that the path taken by the snake is not drawn.
• The “head.goto(0,0)” is used for snake position to be the center of the window and the direction to stop we will use head.direction = “stop”.
• And “pen.write()” function is used to write the text at the current turtle position.
• We need the functionality that increases the snake body every time it touches food. So, we used arrays for this. We create an array called segments, which is initialized empty.
``````wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor('black')
wn.setup(width=600, height=600)
wn.tracer(0)
food = turtle.Turtle()
food.speed(0)
food.shape("square")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("green")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write("Score:0 High score:0", align = "center", font=("Courier", 24, "normal"))``````

Step 3:

• Now, we need to define a function for each of these directions and set the head.direction to up, down, left, and right.
• After that, we will go ahead and make the snake move. So, we will define a function called move().
• If the head goes up, the “y” coordinate is increased, if the head goes down, the “y” coordinate decreases.
• If the head moves right, the “x” coordinate increases and if the head moves left, the “x” coordinate decreases.
``````def go_up():
def go_down():
def go_left():
def go_right():
def move():

Step 4:

• We will assign a key to the snake movements. By clicking the keywords, we can move the snake up, down, left, and right direction.
• We need the system to listen to our control keypress, so we will add a function called wn.listen() that listens to the key pressed.
• Every keypress needs to be bound to a function that carries out an action. We will use the function ” wn.onkeypress(function, “key”) “ for all four. Here, I have used “y” for up, “h” for down, “g” for left, and “j” for right.
• Now, we can operate the movement of a snake on the screen.
``````wn.listen()
wn.onkeypress(go_up, "y")
wn.onkeypress(go_down, "h")
wn.onkeypress(go_left, "g")
wn.onkeypress(go_right, "j")``````

Step 5:

• So, now the function does nothing until it’s called. We need to call the function every time we update the screen or the window.
• We have to be sure that the snakes die when it collides with the border. We already have the coordinate of the border. So, we just need to reset the snakehead position when it touches the coordinates.
• Also, the snake needs to stop moving and hence change the direction to stop.
• To slow down the snake movement we need to use the time module otherwise the default behavior for the move function is very fast.
• So, we will use the function time.sleep() to reduce turtle speed.
• The segment needs to disappear when the snake dies.
• So, now we need to set the position of the segments outside the window coordinates. The game restarts and hence clear the segment list.
• We need to add a segment to the snake body every time it touches the food. So, we have the condition that checks for the head’s collision with food.
• Create a new_segment, define its speed, shape, and color and append it to segments array.
• Now, adding the segment to the snakehead is not enough. These segments need to move when the snakehead moves.
• To move the last segment which is in the position x to x-1 and x-1 to x-2 and so on.
• The snake needs to die if it touches itself. So, we are going to check if the distance between the segment and head is less than 20. If it is, reset the head position and head direction.
• At last, we need to see the situation when the score increases. The first one is when the head collides with the food. Increase the score and update the high_score.
• We used pen.write() function to write the score on the screen.
• We need to reset the score when the snakehead collides with the border and with its own tail
• And then call the function time.sleep(delay) to reduce turtle speed.
``````while True:
wn.update()
time.sleep(1)
for segment in segments:
segment.goto(1000,1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score: {} High score: {}".format(score, high_score),align="center", font=("Courier", 24, "normal"))
x = random.randint(-290,290)
y = random.randint(-290,290)
food.goto(x,y)
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("white")
new_segment.penup()
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score: {} High score: {}".format(score,high_score), align="center", font=("Courier", 24, "normal"))
for index in range(len(segments)-1,0,-1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x,y)
if len(segments)>0:
segments.goto(x,y)
move()
for segment in segments:
time.sleep(1)
for segment in segments:
segment.goto(1000,1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score: {} High score: {}".format(score,high_score), align="center", font=("Courier", 24, "normal"))
time.sleep(delay)
wn.mainloop()``````

Complete code for Snake Game in python using turtle:

``````import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor('black')
wn.setup(width=600, height=600)
wn.tracer(0)
food = turtle.Turtle()
food.speed(0)
food.shape("square")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("green")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write("Score:0 High score:0", align = "center", font=("Courier", 24, "normal"))
def go_up():
def go_down():
def go_left():
def go_right():
def move():
wn.listen()
wn.onkeypress(go_up, "y")
wn.onkeypress(go_down, "h")
wn.onkeypress(go_left, "g")
wn.onkeypress(go_right, "j")
while True:
wn.update()
time.sleep(1)
for segment in segments:
segment.goto(1000,1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score: {} High score: {}".format(score, high_score),align="center", font=("Courier", 24, "normal"))
x = random.randint(-290,290)
y = random.randint(-290,290)
food.goto(x,y)
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("white")
new_segment.penup()
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score: {} High score: {}".format(score,high_score), align="center", font=("Courier", 24, "normal"))
for index in range(len(segments)-1,0,-1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x,y)
if len(segments)>0:
segments.goto(x,y)
move()
for segment in segments:
time.sleep(1)
for segment in segments:
segment.goto(1000,1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score: {} High score: {}".format(score,high_score), align="center", font=("Courier", 24, "normal"))
time.sleep(delay)
wn.mainloop()``````

Output 1: