COMPUTER SCIENCE INVESTIGATORY PROJECT
ON
SNAKE GAME
SUBMITTED BY
M.S.SANTOSH
UNDER THE GUIDANCE OF
MR.P.A.MANIRAM
AFFILIATION NO:1930311
ADHYAPANA SCHOOL CBSE
MADURAI-DINDIGUL, HIGHWAY PARAVAI POST,
MADURAI-625018
CERTIFICATE
This is to certify that M.S.SANTOSH has successfully
completed the Annual Investigatory Project on the topic
“SNAKE GAME” in the year 2024-25 as per the CBSE
syllabus for COMPUTER SCIENCE in Adhyapana School
CBSE, Madurai.
Internal Examiner External Examiner
Chief Superintendent
ACKNOWLEDGEMENT
I am thankful to our Correspondent Mrs. Aruna M Visvessvar
and our Dean of Academics Mr. Visesh Aiyer and our
Principal Mrs. Muthumala for their continuous support and
motivation.
I would like to express my gratitude to my computer
science teacher Mr.maniram for his vital support, guidance,
and encouragement without which this project would not have
come forth.
I am thankful to my parents and classmates for their
constant support.
Contents :
AIM
PROGRAM
OUTPUT
THEORY
ABSTRACT
FLOWCHART
RESULT
BIBILOGRAPHY
AIM:
To write a python program to create a simple snack game using
python.
PROGRAM:
import tkinter
import random
ROWS = 25
COLS = 25
TILE_SIZE = 25
WINDOW_WIDTH = TILE_SIZE * COLS #25*25 = 625
WINDOW_HEIGHT = TILE_SIZE * ROWS #25*25 = 625
class Tile:
def _init_(self, x, y):
self.x = x
self.y = y
#game window
window = tkinter.Tk()
window.title("Snake")
window.resizable(False, False)
canvas = tkinter.Canvas(window, bg = "black", width =
WINDOW_WIDTH, height = WINDOW_HEIGHT,
borderwidth = 0, highlightthickness = 0)
canvas.pack()
window.update()
#center the window
window_width = window.winfo_width()
window_height = window.winfo_height()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window_x = int((screen_width/2) - (window_width/2))
window_y = int((screen_height/2) - (window_height/2))
#format "(w)x(h)+(x)+(y)"
window.geometry(f"{window_width}x{window_height}+
{window_x}+{window_y}")
#initialize game
snake = Tile(TILE_SIZE * 5, TILE_SIZE * 5) #single tile,
snake's head
food = Tile(TILE_SIZE * 10, TILE_SIZE * 10)
velocityX = 0
velocityY = 0
snake_body = [] #multiple snake tiles
game_over = False
score = 0
#game loop
def change_direction(e): #e = event
# print(e)
# print(e.keysym)
global velocityX, velocityY, game_over
if (game_over):
return #edit this code to reset game variables to play
again
if (e.keysym == "Up" and velocityY != 1):
velocityX = 0
elif (e.key velocityY = -1
sym == "Down" and velocityY != -1):
velocityX = 0
velocityY = 1
elif (e.keysym == "Left" and velocityX != 1):
velocityX = -1
velocityY = 0
elif (e.keysym == "Right" and velocityX != -1):
velocityX = 1
velocityY = 0
def move():
global snake, food, snake_body, game_over, score
if (game_over):
return
if (snake.x < 0 or snake.x >= WINDOW_WIDTH or
snake.y < 0 or snake.y >= WINDOW_HEIGHT):
game_over = True
return
for tile in snake_body:
if (snake.x == tile.x and snake.y == tile.y):
game_over = True
return
#collision
if (snake.x == food.x and snake.y == food.y):
snake_body.append(Tile(food.x, food.y))
food.x = random.randint(0, COLS-1) * TILE_SIZE
food.y = random.randint(0, ROWS-1) * TILE_SIZE
score += 1
#update snake body
for i in range(len(snake_body)-1, -1, -1):
tile = snake_body[i]
if (i == 0):
tile.x = snake.x
tile.y = snake.y
else:
prev_tile = snake_body[i-1]
tile.x = prev_tile.x
tile.y = prev_tile.y
snake.x += velocityX * TILE_SIZE
snake.y += velocityY * TILE_SIZE
def draw():
global snake, food, snake_body, game_over, score
move()
canvas.delete("all")
#draw food
canvas.create_rectangle(food.x, food.y, food.x +
TILE_SIZE, food.y + TILE_SIZE, fill = 'red')
#draw snake
canvas.create_rectangle(snake.x, snake.y, snake.x +
TILE_SIZE, snake.y + TILE_SIZE, fill = 'lime green')
for tile in snake_body:
canvas.create_rectangle(tile.x, tile.y, tile.x + TILE_SIZE,
tile.y + TILE_SIZE, fill = 'lime green')
if (game_over):
canvas.create_text(WINDOW_WIDTH/2,
WINDOW_HEIGHT/2, font = "Arial 20", text = f"Game
Over: {score}", fill = "white")
else:
canvas.create_text(30, 20, font = "Arial 10", text =
f"Score: {score}", fill = "white")
window.after(100, draw) #call draw again every 100ms
(1/10 of a second) = 10 frames per second
draw()
window.bind("<KeyRelease>", change_direction) #when you
press on any key and then let go
window.mainloop() #used for listening to window events like
key presses
OUTPUT:
THEORY:
The game uses a combination of data structures, such as lists
and dictionaries, to store and manage game data, including the
snake's position, direction, and score. The game also uses
Python's built-in libraries, such as Pygame, to handle
graphics, sound, and user input.
The game's logic is based on the following principles:
1. Game Loop: The game runs in a continuous loop, where
the snake moves, checks for collisions, and updates the score.
2. Snake Movement: The snake moves in a specific direction
(up, down, left, or right) based on user input.
3. Collision Detection: The game checks for collisions
between the snake and food, obstacles, or the game boundary.
4. Scoring: The game updates the score based on the snake's
length and the number of food items eaten.
The theory behind the Snake Game can be broken down
into the following components:
1. Game Design: The game's design, including the game
mechanics, graphics, and sound.
2. Game Development: The process of creating the game
using Python programming language and Pygame library.
3. Game Logic: The game's logic, including the game loop,
snake movement, collision detection, and scoring.
ABSTRACT:
This project presents a simple implementation of the classic
Snake Game using Python programming language and
Pygame library. The game allows a player to control a snake
that moves around the screen, eats food, and avoids obstacles,
with the goal of achieving the highest score possible. The
game is designed using object-oriented programming
principles and uses a combination of data structures to store
and manage game data. The game's logic is based on a
continuous loop, where the snake moves, checks for
collisions, and updates the score. The project demonstrates the
basics of game development using Python and provides a fun
and engaging gaming experience.
RESULT:
Thus we have written a python program to create simple
snake game using python
BIBILOGRAPHY:
1.www.google.com
2.www.youtube.com
dilute
H2SO4 to
prevent of
dilute
H2SO4 to
prevent
hydrolysis
of
CuSO4.
Transfer
this to a
aving
100ml
distilled
water and
add 15ml
of
dilute
H2SO4 to
prevent
hydrolysis
of
CuSO4.
Transfer
this to a
beaker
having
100ml
distilled
water and
add 15ml
of
dilute
H2SO4 to
prevent
hydrolysis
of
CuSO4.
Transfer
this to a
beaker
having
100ml
distilled
water and
add 15ml
of
Transfer this to a beaker.