0% found this document useful (0 votes)
9 views7 pages

Python Snake Game Tutorial

The document outlines the development of a Snake game in Python using the turtle, random, and time modules. It details the game's objective, prerequisites, methodology, and provides code snippets for creating the game screen, snake, fruit, keyboard controls, and collision detection. The project concludes with a successful implementation of the game, showcasing the use of various Python modules.

Uploaded by

ramkumar463256
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Python Snake Game Tutorial

The document outlines the development of a Snake game in Python using the turtle, random, and time modules. It details the game's objective, prerequisites, methodology, and provides code snippets for creating the game screen, snake, fruit, keyboard controls, and collision detection. The project concludes with a successful implementation of the game, showcasing the use of various Python modules.

Uploaded by

ramkumar463256
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Snake Game in Python

Snake game program in Python with basic python modules like


turtle, random, time.

INTRODUCTION

About Snake Game Python Project


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

OBJECTIVE
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

METHODOLOGY

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 = [Link]()
[Link]('DATAFLAIR SNAKE GAME')
[Link](width = 700, height = 700)
[Link](0)
[Link]('turquoise')

[Link](5)
[Link](4)
[Link]()
[Link](-310,250)
[Link]()
[Link]('black')
[Link](600)
[Link](90)
[Link](500)
[Link](90)
[Link](600)
[Link](90)
[Link](500)
[Link]()

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 = [Link]()
[Link](0)
[Link]('square')
[Link]("black")
[Link]()
[Link](0,0)
[Link] = 'stop'

fruit = [Link]()
[Link](0)
[Link]('circle')
[Link]('red')
[Link]()
[Link](30,30)

old_fruit=[]

scoring = [Link]()
[Link](0)
[Link]("black")
[Link]()
[Link]()
[Link](0,300)
[Link]("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 [Link] != "down":
[Link] = "up"

def snake_go_down():
if [Link] != "up":
[Link] = "down"

def snake_go_left():
if [Link] != "right":
[Link] = "left"

def snake_go_right():
if [Link] != "left":
[Link] = "right"

def snake_move():
if [Link] == "up":
y = [Link]()
[Link](y + 20)

if [Link] == "down":
y = [Link]()
[Link](y - 20)

if [Link] == "left":
x = [Link]()
[Link](x - 20)

if [Link] == "right":
x = [Link]()
[Link](x + 20)

[Link]()
[Link](snake_go_up, "Up")
[Link](snake_go_down, "Down")
[Link](snake_go_left, "Left")
[Link](snake_go_right, "Right")

Explanation
[Link]() 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 [Link](fruit)< 20:
x = [Link](-290,270)
y = [Link](-240,240)
[Link](x,y)
[Link]()
score+=1
[Link]("Score:
{}".format(score),align="center",font=("Courier",24,"bold"))
delay-=0.001

new_fruit = [Link]()
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= [Link]()
b = [Link]()
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 [Link]()>280 or [Link]()< -300 or [Link]()>240 or [Link]()<-


240:
[Link](1)
[Link]()
[Link]('turquoise')
[Link](0,0)
[Link](" 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.

[Link]() will delete all the drawing of the turtle on the screen

7. When snake touch itself

for food in old_fruit:


if [Link](snake) < 20:
[Link](1)
[Link]()
[Link]('turquoise')
[Link](0,0)
[Link](" GAME OVER \n Your Score is
{}".format(score),align="center",font=("Courier",30,"bold"))

RESULT
Snake Game Program Output
CONCLUSION
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.

You might also like