JAWAHAR NAVODAYA VIDYALAYA
MAVINAKERE, HASSAN
INVESTIGATORY PROJECT
2021-22
TOPIC:
“TO CREATE A SNAKE GAME”
SUBMITTED BY:
NAME: JNANESHA KUMAR S.G
ROLL NO: 18164494
J N V M HASSAN
JAWAHAR NAVODAYA VIDYALAYA
MAVINAKERE, HASSAN
LAB CERTIFICATE
This project titled “TO CREATE A SNAKE
GAME”. Is bona field work of Master JNANESHA
KUMAR S.G of Roll.No: 18164494 of this
Vidyalaya. This project report is submitted of
AISSCE practical examination of 2021-22.
signature of signature of
Internal examiner external examiner
CONTENTS
ACKNOWLEDGEMENT
DECLARATION
AIM
INTRODUCTION
PYTHON CODE
OUTPUT
RESULT
BIBLIOGRAPHY
ACKNOLEDGEMENT
I wish to express my sincere gratitude to
Sri. K PARTHIBAN, Principal, JNV Hassan his
encouragement and all the facilities that have
been provided for this project I sincerely
acknowledge to my beloved teacher
Sri. KUMAR H.G, F.C.S.A for his valuable
guidance and constant support during course
of my project.
I also express my deep regards to my parents
for their moral encouragementand support.
Finally, I thank all thosewho helped me and
made my project to reach success.
Declaration
I JNANESHA KUMAR S.G of class XII hereby
declare that this project entitled “TO
CREATE A SNAKE GAME” Submitted
to Jawahar Navodaya Vidyalaya, Mavinkere,
Hassan, is an original work made by
investigating samples in the school
laboratory in the presence of our Computer
teacher during the period of academic
session 2021-22.
Name: Jnanesha Kumar S.G
Class: XII PCMC
Roll Number: 18164494
JNVM HASSAN
AIM
“TO CREATE
A
SNAKE GAME”
INTRODUCTION
It is a simple game in which
you have to move the snake
using the arrow keys in the
keyboard and help the snake to
find its food.
Use the arrow keys to change
the direction of the movement
of the snake. The score of the
user will be displayed on the
screen.
PYTHON CODE
#PROGRAM TO CREATE A SNAKE GAME
Import turtle
Import time
Import random
Delay=0.1
#score
Score=0
High_score=0
#SETTING UP THE SCREEN
Wn=turtle.screen()
Wn.title(“SNAKE GAME BY JNANESHA KUMAR S G”)
Wn.bgcolour(“black”)
Wn.setup(width=800,height=800)
Wn.tracer(0) #
#SNAKE HEAD
head=turtle.turtle()
head.speed(0)
head.shape(”circle”)
head.colour(“yellow”)
head.penup()
head.goto(0,0)
head.direction = “stop”
#SNAKE FOOD
Food=turtle.turtle()
Food.speed(0)
Food.shape(“circle”)
Food.colour(“red”)
Food.penup()
Food.goto(0,100)
Segments=[]
#PEN
Pen=turle.turtle()
Pen.speed(0)
Pen.shape(“square”)
Pen.colour(“white”)
Pen.penup()
Pen.hideturtle()
Pen.goto(0,200)
Pen.write(“score:0 high score:0,align=”centre”, font=(“Courier”,24,”normal”))
Pen.goto(0,260)
Pen.write(“game by: jnaneshakumars.g”,align=”centre”,font=(“Times
new roman”,20,”normal”))
#FUNCTIONS
Def go_up():
If head.direction!=”down”:
Head.direction=”up”
Def go_down():
If head.direction!=”up”:
Head.direction=”down”
Def go_left():
If head.direction!=”right”:
Head.direction=”left”
Def go_right():
If head.direction!=”left”:
Head.direction=”right”
Def go_move():
If head.direction==”up”:
Y=head.ycor()
Head.sety(y+20)
If head.direction==”down”:
Y=head.ycor()
Head.sety(y-20)
If head.direction==”left”:
x=head.xcor()
Head.setx(x-20)
If head.direction==”right”:
x=head.xcor()
Head.setx(x+20)
#keyboard binding
Wn.listen()
Wn.onkeypress(go_up,”up”)
Wn.onkeypress(go_down,”down”)
Wn.onkeypress(go_left,”left”)
Wn.onkeypress(go_right,”right”)
#main game loop
While true:
Wn.update()
#CHECK FOR COLLISON WITH THE BORDER
If head.xcor()>290 or head.xcor()< -290 orhead.ycor() >290 or
head.ycor() < -290:
Time.sleep(1)
Head.goto(0,0)
Head.direction = “stop”
#HIDE THE SEGMENTS
For segment in segments:
Segment.goto(1000,1000)
#CLEAR THE SEGMENT LIST
Segments.clear()
#RESET THE SCORE
Score = 0
#REST THE DELAY
Delay = 0.1
#UPDATE THE SCORE DISPLAY
Pen.clear()
Pen.write(“score:{} high
score:{}”,format(score,high_score),align=”centre,font=(“courier”,24,”nor
mal”))
#CHECK FOR THE COLLSION WITH THE FOOD
If head.distance(food)<20:
#move the food to a random spot
X = random.randint(-290,290)
Y = random.randint(-290,290)
Food.goto(x,y)
#ADD A SEGMENT
New_segment = turtlr.turtle()
New_segment.speed(0)
New_segment.shape(“circle”)
New_segment.colour(“green”)
New_segment.penup()
Segments.append(new_segment)
#SHORTEN THE DELAY
Delay-= 0.001
#INCREASE THE SCORE
Score+=10
If the score>high_score:
High_score=score
Pen.clear()
Pen.write(“score:{} high
score:{}.format(score,high_score,align=”centre,font+(“courier”,24,”norm
al”))
#MOVE HE END SEGMENTS FIRST IN THE REVERSE ORDER
For index in range(len(segments)-1,0,-1):
X=segments[index-1].xcor()
y=segments[index-1].ycor()
segments[index].goto(x,y)
#MOVE THE SEGMENT 0 TO WHERE THE HEAD IS
If len(segments)>0:
X=head.xcor()
Y=head.ycor()
Segments[0].goto(x,y)
Move()
#CHECK FOR COLLISION WITH THE BODY SEGMENTS
For segments in segments:
If segment.distance(head)<20:
Time.sleep(1)
Head.goto(0,0)
Head.direction=”stop”
#HIDE THE SEGMENTS
For segment in segments:
Segment.goto(1000,1000)
#CLEAR THE SEGMENTS LIST
Segment.clear()
#RESET THE SCORE
Score=0
#reset the delay
Delay=0.1
#UPDATE THE SCORE DISPLAY
Pen.clear()
Pen.write(“score:{}
high:{}.format(score,high_score),align=”centre”,font=(“couri
er”,24,”normal”))
Time.sleep(delay)
Wn.mainloop()
OUTPUT
AFTER RUNNING THE PROGRAM, THE OUTPUT IS:
Result
The program
executed
without any
error(s)
BIBLIOGRAPHY
COMPUTER SCIENCE with
python Text book for Class XII
https://www.computerpractica
ls.com/
Python Comprehensive book
for developers