Snake Game in Python – Develop Snake Game Program
Master Python with 70+ Hands-on Projects and Get Job-ready - 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:
- Importing libraries
- Creating a game screen
- Creating snake and food
- Keyboard binding
- Game mainloop
1. Importing required module
import turtle import random import time
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
- title() will set the desired title of the screen
- setup() used to set the height and width of the screen
- tracer(0) will turn off the screen update
- bgcolor() will set the background color
- forward() will use to move the turtle in a forwarding direction for a specified amount
- right() used to turn the turtle clockwise and left() used to turn the turtle anticlockwise
- penup() will not draw while its move
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
- Turtle() will be used to create a new turtle object
- hideturtle() will use to hide the turtle
- goto() used to move the turtle at x and y coordinates
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.
Your opinion matters
Please write your valuable feedback about DataFlair on Google
hi sir,
just i got screen with color in absence of snake and its food .
Did you get any error, please post the stack trace, we will look into the issue.
where should i compile this program .while i’m compiling in online compilers im not so many errors
You can run the snake game code on any ide like vscode, atom, pycharm, sublime or any other
can u provide the source code as a zip file it would be great help or at least give them in whole.
Please download the source code of snake game project from downloads section
the output is nothing
nice
the program is not working the game screen just comes for a second and closes on its own
just screen layout is visible but snake and fruit doesn’t appear in output
can you provide the report for this project?pls
very nice keep it up
This site saved me, bro!