In this article, I will be creating a Snake Game in Python using the Turtle Module. I will guide you step-by-step to build this simple project. So, don’t worry and keep reading the below article.

Project Details
I’ve used the Python Turtle module to build this Snake game. It is easy to use and understandable. Users have to use the four arrow keys to control the snake’s movement around the screen and make it eat food.
For each food, the snake eats the user gets two points and makes the snake longer and faster. If the head of the snake touches or hits the wall, the game will be over.
TOP 7 COMPANIES THAT ARE HIRING DATA SCIENTISTS IN 2023
Requirements and Installation
Use pip3 instead of a pip for Linux.
☛Install Turtle: pip install PythonTurtle
Related:-TOP 7 USED PROGRAMMING LANGUAGE AMONG GAME DEVELOPERS
Steps:
- Create and set properties for edible snakes and turtles.
- Set initial values for these variables: snake size, snake speed, and score.
- Now bring the snake forward.
- Check whether the snake has eaten or not. If ‘no’, go to step 6, otherwise go to the next one.
- It means that the snake has eaten the food. Now increase the size and speed of the snake and update the current score by adding two together, then proceed to the next step.
- Now check whether the snake touches the wall or not. If the answer is ‘yes’, follow the next step; otherwise, go back to step 3.
- Shows ‘Game over’.
Python Project to Create a Snake Game in Python using Turtle Module
Copy the below code and paste in the code editor and run the code you will get a snake game
import random
import turtle as t
t.bgcolor('yellow')
t.title('Snake Game')
snake = t.Turtle()
snake.shape('square')
snake.color('red')
snake.speed(0)
snake.penup()
snake.hideturtle()
food = t.Turtle()
food.color('green')
food.shape('square')
food.speed(0)
food.penup()
food.hideturtle()
welcome_text = t.Turtle()
welcome_text.write('Press SPACE to Start', align='center', font=('Helvetica', 20, 'bold'))
welcome_text.hideturtle()
score_text = t.Turtle()
score_text.hideturtle()
def game_over():
snake.color('yellow')
food.color('yellow')
t.penup()
t.hideturtle()
t.write('GAME OVER!', align='center', font=('Helvetica', 40, 'bold'))
def boundary():
left_wall = -t.window_width() / 2
right_wall = t.window_width() / 2
top_wall = t.window_height() / 2
bottom_wall = -t.window_height() / 2
(x, y) = snake.pos()
boundary = (x<=left_wall or x>=right_wall or y<=bottom_wall or y>=top_wall)
return boundary
def display_score(current_score):
score_text.clear()
score_text.penup()
x = (t.window_width() / 2) - 50
y = (t.window_height() / 2) - 50
score_text.setpos(x, y)
score_text.write(str(current_score), align='right',
font=('Helvetica', 30, 'bold'))
def place_food():
# Hide Turtle
food.ht()
food.setx(random.randint(-150, 150))
food.sety(random.randint(-150, 150))
# Show Turtle
food.st()
def start_game():
score = 0
# Clear the Starting Screen
welcome_text.clear()
snake_speed = 1
snake_length = 2
snake.shapesize(1, snake_length, 1)
snake.showturtle()
display_score(score)
place_food()
place_food()
while True:
snake.forward(snake_speed)
# The snake eats the food when it is less than 30 pixels away
if snake.distance(food) < 30:
place_food()
snake_length += 0.5
snake.shapesize(1, snake_length, 1)
snake_speed += 0.4
score += 2
display_score(score)
if boundary():
game_over()
break
def go_left():
if snake.heading() == 90 or snake.heading() == 270:
# The head of the snake is being set at a 180 degree angle.
snake.setheading(180)
def go_right():
if snake.heading() == 90 or snake.heading() == 270:
snake.setheading(0)
def go_up():
if snake.heading() == 0 or snake.heading() == 180:
snake.setheading(90)
def go_down():
if snake.heading() == 0 or snake.heading() == 180:
snake.setheading(270)
t.onkey(start_game, 'space')
t.onkey(go_up, 'Up')
t.onkey(go_right, 'Right')
t.onkey(go_down, 'Down')
t.onkey(go_left, 'Left')
t.listen()
t.mainloop()