Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views30 pages

Python Snake Game Project Guide

The document outlines an investigatory project for a Computer Science course at Aditya Vidyashram Residential School, focusing on the development of the classic Snake game using Python and Pygame. It includes sections on project certification, acknowledgments, hardware and software requirements, an introduction to Python, game rules, objectives, and the source code. The project aims to enhance students' programming skills and understanding of game development concepts.

Uploaded by

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

Python Snake Game Project Guide

The document outlines an investigatory project for a Computer Science course at Aditya Vidyashram Residential School, focusing on the development of the classic Snake game using Python and Pygame. It includes sections on project certification, acknowledgments, hardware and software requirements, an introduction to Python, game rules, objectives, and the source code. The project aims to enhance students' programming skills and understanding of game development concepts.

Uploaded by

aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

“SAMRAKSHANA”

The Total Protection With Affection

ADITYA VIDYASHRAM RESIDENTIAL SCHOOL


Poraiyur, Villianur, Puducherry – 605110

CBSE AISSCE: XII – 2024 - 2025

ALL INDIA SENIOR SCHOOL CERTIFICATE EXAMINATION

COMPUTER SCIENCE (083)

INVESTIGATORY PROJECT

CERTIFICATE

Name : Std& Sec:

Registration Number

Title of the Project:

Certified that this is a bonafide project work done by the above mentioned student in our school
during Senior Secondary course, in the year 2024– 2025

SUBJECT TEACHER PRINCIPAL

Project Submitted for the CBSE – AISSCE Practical examination held on / /2025.

INTERNAL EXAMINER EXTERNAL EXAMINER


TABLE OF
CONTENTS

1 Certificate
2 Acknowledgement
3 Hardware and Software Used
4 Minimum System Requirement
5 Introduction
6 Python Overview
7 How to install Python
8 Advantages of Snake_Game
9 Rules of the game
10 Objective of the Project
11 Modules Used and their Purposes
12 Flow Chart
13 Source Code
14 Output Screens
15 Limitations and Improvements
16 Bibliography

1|P a g e
CERTIFICATE

This is to certify that the project work on

based on the curriculum of CBSE has


been completed by of Class-XII
of Aditya Vidyashram Residential School, Pondicherry.

The above-mentioned project work has been completed


undermy guidance during the academic year 2024-25.

Signature of the Guide Teacher


Acknowledgement

I would like to extend my sincere and heartfelt obligation towards all those
who have helped me in making this project. Without theiractive guidance,
help, cooperation and encouragement, I would not have been able to present
the project on time.

I am extremely thankful and pay my sincere gratitude to my teacher Mrs. I.


Kanchana for her valuable guidance and support for completion of this
project. I extend my sincere gratitude to my Principal Dr. Nadesan
Kangueyan for the moral support extended during tenure of this project.

I also acknowledge with a deep sense of reverence, my gratitude towards


my parents, other faculty members of the school and friends for their
valuable suggestions given to me in completing the project.

Date:
Place: Signature of the Student
Hardware and Software Used

Hardware:
1. Processor: AMD Ryzen 5 4500 6-Core Processor 3.60 GHz

2. RAM: 16GB

3. System Architecture: 64-bit operating system

4. NVME SSD: 512Gigabyte


Software:
1. Operating System: Windows 11 Pro
2. Programmin Language: IDLE Python (Version: 3.12)

Minimum System Requirement

1. Any Operating System (x64 or x86) with Python (Version above 3.10.0) installed
will work.
2. Packages- pygame, time and random must be installed through pip.

4|P a g e
Introduction

About Snake_Game:
The Snake game, a classic arcade game, has been popular since the late 1970s,
originally gaining fame on early Nokia mobile phones. Developing the Snake game in
Python is a common project for beginners learning the language, utilizing libraries
such as Pygame for handling graphics and game logic. The objective of the game is to
control a snake, directing it to eat food items that appear on the screen, causing the
snake to grow in length with each item consumed. The game continues until the snake
either runs into itself or the screen's boundaries, resulting in a game over. The
simplicity of the game's mechanics makes it an excellent project for understanding
basic programming concepts such as loops, conditionals, and event handling. In
Python, Pygame facilitates the creation of the game window, rendering graphics, and
handling user input through keyboard events. The logic of the game typically involves
maintaining a list of the snake's body segments, updating their positions as the snake
moves, and detecting collisions. Additionally, random module is used to place food
items at random locations on the game board. Implementing the Snake game not only
helps in grasping fundamental programming concepts but also provides a tangible and
enjoyable way to see the results of one's coding efforts.

5|P a g e
Python Overview

Python is a general-purpose high-level programming language. It is an open- source


language, released under a GPL-Compatible license. Python Software Foundation
(PSF), a non-profit organization, holds the copy-right of python. Guido Van Rossum
conceived python in the late 1980s. It was released in 1991 at Centrum Wickenden &
Informatica (CWI) in the Netherlands as a successor to the ABC language. He named
this language after a popular comedy show called “Monty Python’s Flying Circus”
(and not after python - the snake). In the last few years, its popularity has increased
immensely. According to stackoverflow.com’s recent survey, python is in the top ten
most popular technologies in 2018. It is also dynamically-typed because it carries out
type-checking at run time. It does so to make sure that the type of construct matches
what we except it to be. The distinctive feature of python is that it is an interpreted
language. The Python IDLE (Integrated Development & Learning Environment)
executes instruction one line at a time. The python programming language is one of
the richest languages

6|P a g e
How to install Python

1. Point your web browser to the download page on the Python website
(www.python.org).
2. Select the latest Windows x86 MSI Installer and click on the link to
download the .msi installer.
3. Run the installer.

4. Select “Install for all users‟ and click the Next > button.

5. Keep the default option (C:\Python32\) as the destination directory


and click Next > again.
6. Don’t make any changes in the “Customize Python 3.2.3”
dialog, just click Next > again.
7. Click Yes if asked if this program should be allowed to install software
on your system.
8. Click the Finish button when installation completes.

7|P a g e
Advantages of Snake_Game:

Learning Opportunity: Creating a Snake game helps beginners understand


fundamental programming concepts such as loops, conditionals, and data
structures (like lists).
Graphics and Event Handling: Using libraries like Pygame, developers gain
hands-on experience with graphical user interfaces (GUIs), rendering, and
handling user inputs, which are essential skills for game development and other
interactive applications.
Problem-Solving Skills: Designing the game mechanics, such as managing the
snake’s movement, detecting collisions, and implementing game-over
conditions, enhances problem-solving and logical thinking abilities.
Immediate Feedback: The visual nature of the game provides immediate
feedback, making it easier to see the results of code changes, which is
motivating and helps with debugging and understanding the impact of different
programming constructs.
Project Completion: Completing a project like Snake offers a sense of
accomplishment, boosting confidence and encouraging further learning and
exploration in programming.
Community and Resources: Python's large and active community provides
extensive resources, tutorials, and forums for troubleshooting and
improvement, making it easier to learn and

8|P a g e
develop the game.
Reusability and Enhancement: The game can be extended with new features,
such as different levels, obstacles, or power-ups, allowing for ongoing learning
and creativity in game development.

Rules of the game

Objective: The primary goal is to control a snake to eat as much food as possible,
which causes the snake to grow longer with each food item consumed.
Movement: The snake can move in four directions – up, down, left, and right –
typically controlled by the arrow keys on a keyboard.
Eating Food: Food items appear at random locations on the game board. When
the snake's head reaches the food, the snake eats it, resulting in the snake
growing longer by one segment, and a new food item appears.
Collision with Walls: If the snake's head collides with the boundaries of the
game board, the game is over.
Self-Collision: If the snake's head collides with any part of its own body, the
game is over.
Score: The score is usually determined by the number of food items eaten. Each
piece of food increases the score by one point (or more, depending on game
variations).
Game Speed: As the game progresses, the speed at which the snake

9|P a g e
moves may increase, adding to the challenge.

Game Over: The game ends when the snake collides with the walls or itself. The
final score is displayed, and the player can typically start a new game.

10 | P a g e
Objective of the Project

The objective of this project is to let the students apply the programming
knowledge into a real-world situation/problem and exposed the students how
programming skills help in developing a good software.

Some key points are as follows :-

Utilizing modern software tools for programming games.

Apply object-oriented programming principles effectively when developing


small to medium sized projects.
Write effective procedural code to solve small to medium sized problems.
Demonstrate a breadth of knowledge in computer science, as exemplified in
the area of game development.
Demonstrate ability to conduct a research or applied computer science project,
requiring writing and presentation skills which exemplify scholarly style in
computer science

11 | P a g e
Modules Used and their Purposes

 pygame: A set of Python modules designed for writing video games.

pygame.init(): Initializes all the imported pygame modules.


pygame.display.set_mode(): Sets up the game window with the given
width and height.
pygame.display.set_caption(): Sets the window title.
pygame.time.Clock(): Creates a clock object to help control the frame rate.
pygame.font.SysFont(): Creates a font object from the system fonts.
pygame.draw.rect(): Draws a rectangle on the display.
pygame.display.update(): Updates the display with any changes made.
pygame.event.get(): Gets a list of all the events currently in the event
queue.
pygame.quit(): Uninitializes all pygame modules and quits the game.

 time: A standard Python module that provides various time-related functions.

Not explicitly used in the provided code, but often used for timing
operations.

12 | P a g e
 random: A standard Python module that implements pseudo-random number
generators.

random.randrange(): Generates a random number within a


specified range.
round(): Rounds a number to the nearest integer.

 Built-in Functions Used:

len(): Returns the length (the number of items) of an object. open():


Opens a file and returns a corresponding file object.
 open(filename, mode): Opens a file in the specified mode ('r' for
reading, 'w' for writing).
int(): Converts a number or string to an integer.
str(): Converts an object to a string.

13 | P a g e
Flow Chart
Start Continuous at next page

Initialize Pygame

Set up variables
Control Frame Rate

Main Game Loop

(GameLoop) Update Display

Render Everything
Check Events
-Draw background
-If QUIT, exit
-Draw food
-If arrow key,
-Draw snake
Update direction

(if snake eats food)

Update Snake Position Update Snake Length

Check Boundaries Check Self Collision


-If true, set game -If true, set game
OVER OVER
14 | P a g e
Game Over Loop

Display Game Over

Message

Check Events
-If ‘Q’, exit

-if ‘C’, restart

Quit Pygame

End program
15 | P a g e
Source
Code

16 | P a g e
import pygame
import time import
random

pygame.init()

# Define colors
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# Screen dimensions
dis_width = 900
dis_height = 600

# Create display
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Snake Game @A.Shailendhirah')

clock = pygame.time.Clock()

17 | P a g e
# Snake block size
snake_block = 10
snake_speed = 15 # Speed of the Snake

# Fonts
font_style = pygame.font.SysFont("bahnschrift", 25) score_font =
pygame.font.SysFont("comicsansms", 35)

def our_snake(snake_block, snake_list): for x in


snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])

def your_score(score):
value = score_font.render("Your Score: " + str(score*10), True, white) dis.blit(value,
[0, 0])

def message(msg, color):


mesg = font_style.render(msg, True, color) dis.blit(mesg,
[dis_width / 6, dis_height / 3])

def gameLoop():
game_over = False
game_close = False

18 | P a g e
x1 = dis_width / 2 y1 =
dis_height / 2

x1_change = 0
y1_change = 0

snake_List = []
Length_of_snake = 1

foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0


foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0

high_score = 0
try:
with open("highscore.txt", "r") as f:
high_score = int(f.read())
except:
pass

while not game_over: while

game_close:

19 | P a g e
dis.fill(blue)
message("You Lost! Press Q-Quit or C-Play Again", red)
your_score(Length_of_snake - 1) pygame.display.update()

for event in pygame.event.get():


if event.type == pygame.KEYDOWN: if
event.key == pygame.K_q:
game_over = True game_close
= False
if event.key == pygame.K_c:
gameLoop()

for event in pygame.event.get(): if


event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN: if
event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block y1_change =
0
elif event.key == pygame.K_UP:

20 | P a g e
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block x1_change =
0

if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:


game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(blue)
pygame.draw.rect(dis, green, [foodx, foody, snake_block,
snake_block])
snake_Head = [] snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake: del
snake_List[0]

for x in snake_List[:-1]: if x
== snake_Head:
game_close = True

21 | P a g e
our_snake(snake_block, snake_List)
your_score(Length_of_snake - 1)

pygame.display.update()

if x1 == foodx and y1 == foody:


foodx = round(random.randrange(0, dis_width - snake_block) /
10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) /
10.0) * 10.0
Length_of_snake += 1

clock.tick(snake_speed)

if Length_of_snake - 1 > high_score:


high_score = Length_of_snake - 1 with
open("highscore.txt", "w") as f:
f.write(str(high_score))

pygame.quit()
quit()
gameLoop()

22 | P a g e
Output
Screens

23 | P a g e
1. Starting Screen of the Game “Snake_Game”.

2. When the game is playing.

24 | P a g e
Limitation and Improvements

Limitation:
1. No Pause Functionality

The game does not have a pause feature, which can be inconvenient for players
who need to take a break.

2. Simple Graphics

The graphics are very basic, with plain coloured rectangles representing the snake
and the food. Adding images or more complex graphics could make the game more
visually appealing.

3. Rigid Controls

The controls are straightforward but lack the smoothness and responsiveness that
can be found in more polished games. Implementing a way to handle multiple key
presses or smoother turning could improve gameplay.

4. No Sound Effects

The game lacks sound effects, which could enhance the player's experience by
providing audio feedback for actions like eating food or losing the game.

25 | P a g e
5. No Levels or Difficulty Settings

The game has a fixed speed and difficulty. Introducing levels or difficulty settings
could provide more challenge and variety.

6. Limited Screen Adaptability

The game is designed for a fixed screen size. Making it adaptable to different
screen sizes and resolutions would be a significant improvement.

7. No Obstacle or Special Items

The game could be made more interesting by adding obstacles, power- ups, or
special items that the snake can interact with.

8. Limited High Score Handling

The high score is stored in a simple text file. Implementing a more robust high score
system, possibly with a leaderboard, could provide more competitive gameplay.

9. No Multiplayer Option

The game is single-player only. Adding a multiplayer mode could make it more
engaging.

26 | P a g e
10. Inefficient Snake Collision Detection

The current method of checking for collisions involves iterating through the entire
snake body, which can become inefficient as the snake grows longer. Optimizing
this process could improve performance.

11. Inconsistent Frame Rate

The game uses a fixed frame rate (clock.tick(snake_speed)), which might not be
consistent across different machines. Ensuring a consistent frame rate or adapting
the game speed to the performance of the machine could enhance the user
experience.

12. No Smooth Transition

The snake moves in discrete steps, which can look jerky. Implementing smoother
transitions between movements could make the game feel more fluid.

27 | P a g e
Improvement:

Add a pause function.


Enhance the graphics with better visual elements.
Improve controls for smoother gameplay.
Introduce sound effects.
Add levels, obstacles, and power-ups.
Implement a more robust high score system.
Introduce multiplayer mode.
Ensure a consistent frame rate.
Implement smoother movement for the snake.

28 | P a g e
Bibliography

1. https://www.slideshare.net/slideshow/computer-investigatory-project- class-xii-
cbselatest-syllabuspython-project/205420112

2. https://www.scribd.com/document/518899904/Computer-Science- Class-XII-
2021-22-Investigatory-Project

3. https://www.geeksforgeeks.org/snake-game-in-python-using-pygame- module/

4. https://gist.github.com/wynand1004/ec105fd2f457b10d971c09586ec4 4900#file-
snake_game-py

5. Friends

6. Teachers

7. Reference Materials

29 | P a g e

You might also like