Site icon DataFlair

Snake Game in Python – Develop Snake Game Program

snake game in python

Python course with 57 real-time projects - Learn Python

The snake game is a very popular and fun game. Every time the snake eats the fruit, its length grows longer that makes the game more difficult

About Snake Game Python Project

The objective of this python project is to build a snake game project. In this python project, the player has to move a snake so it touches the fruit. If the snake touches itself or the border of the game then the game will over.

Project Prerequisites

To build the snake game project we used the turtle module, random module, time module, and concept of python.

Turtle module gives us a feature to draw on a drawing board

Random module will be used to generate random numbers

Time module is an inbuilt module in python. It provides the functionality of time.

To install python modules we use the pip install command in the command line:

pip install turtles
pip install random

Download Snake Game Python Project

Please download the source code of snake game python project: Python Snake Game Program

Project File Structure

Steps to build a snake game project in python:

1. Importing required module

import turtle
import random
import time

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

We require turtle, random, and time module to import

2. Creating game screen

screen = turtle.Screen()
screen.title('DATAFLAIR SNAKE GAME')
screen.setup(width = 700, height = 700)
screen.tracer(0)
turtle.bgcolor('turquoise')

turtle.speed(5)
turtle.pensize(4)
turtle.penup()
turtle.goto(-310,250)
turtle.pendown()
turtle.color('black')
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.penup()

Explanation

3. Creating snake and food

snake = turtle.Turtle()
snake.speed(0)
snake.shape('square')
snake.color("black")
snake.penup()
snake.goto(0,0)
snake.direction = 'stop'

fruit = turtle.Turtle()
fruit.speed(0)
fruit.shape('circle')
fruit.color('red')
fruit.penup()
fruit.goto(30,30)

old_fruit=[]

scoring = turtle.Turtle()
scoring.speed(0)
scoring.color("black")
scoring.penup()
scoring.hideturtle()
scoring.goto(0,300)
scoring.write("Score :",align="center",font=("Courier",24,"bold"))

Explanation

4. Keyboard binding

def snake_go_up():
    if snake.direction != "down":
        snake.direction = "up"

def snake_go_down():
    if snake.direction != "up":
        snake.direction = "down"

def snake_go_left():
    if snake.direction != "right":
        snake.direction = "left"

def snake_go_right():
    if snake.direction != "left":
        snake.direction = "right"

def snake_move():
    if snake.direction == "up":
        y = snake.ycor()
        snake.sety(y + 20)

    if snake.direction == "down":
        y = snake.ycor()
        snake.sety(y - 20)

    if snake.direction == "left":
        x = snake.xcor()
        snake.setx(x - 20)

    if snake.direction == "right":
        x = snake.xcor()
        snake.setx(x + 20)

screen.listen()
screen.onkeypress(snake_go_up, "Up")
screen.onkeypress(snake_go_down, "Down")
screen.onkeypress(snake_go_left, "Left")
screen.onkeypress(snake_go_right, "Right")

Explanation

screen.listen() function listen when key will press.

If the Up key will press then the snake will move in up direction.

If the Down key is pressed then the snake will move in the down direction.

If Left key will press then the snake will move in left direction.

If the Right key will press then the snake will move in the right direction

5. Snake and fruit collision

if snake.distance(fruit)< 20:
                x = random.randint(-290,270)
                y = random.randint(-240,240)
                fruit.goto(x,y)
                scoring.clear()
                score+=1
                scoring.write("Score:{}".format(score),align="center",font=("Courier",24,"bold"))
                delay-=0.001
                
                new_fruit = turtle.Turtle()
                new_fruit .speed(0)
                new_fruit .shape('square')
                new_fruit .color('red')
                new_fruit .penup()
                old_fruit.append(new_fruit )

for index in range(len(old_fruit)-1,0,-1):
                a = old_fruit[index-1].xcor()
                b = old_fruit[index-1].ycor()

                old_fruit[index].goto(a,b)
                                
        if len(old_fruit)>0:
                a= snake.xcor()
                b = snake.ycor()
                old_fruit[0].goto(a,b)
        snake_move()

Explanation

If the snake touches the fruit then the fruit will go at any random position and score will increase and the size of the snake will also increase

6. Snake and border collision

if snake.xcor()>280 or snake.xcor()< -300 or snake.ycor()>240 or snake.ycor()<-240:
                time.sleep(1)
                screen.clear()
                screen.bgcolor('turquoise')
                scoring.goto(0,0)
                scoring.write("   GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))

Explanation

If the snake touches the border of the game then the game will over.

screen.clear() will delete all the drawing of the turtle on the screen

7. When snake touch itself

for food in old_fruit:
                if food.distance(snake) < 20:
                        time.sleep(1)
                        screen.clear()
                        screen.bgcolor('turquoise')
                        scoring.goto(0,0)
                        scoring.write("    GAME OVER \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))

Snake Game Program Output

Summary

We successfully developed Snake game project in python. We learn how to used turtle modules and draw on the screen using a turtle. We also learn about random and time modules.

Exit mobile version