Professional Documents
Culture Documents
A
Project Report
SUBMITTED TO THE
Integrated Institute of Technology
(Affiliated to GGSIPU)
SUBMITTED BY
NAVEEN KUMAR
04750102019
Designation-
I hereby declare that the project work entitled “SNAKE GAME” submitted to
Integrated Institute of Technology, Dwarka, is a record of an original work done
by me under the guidance of “ARUN DABAS”. This project work is submitted in
the partial fulfilment of the requirements for the award of the degree of Bachelor
of Computer Application. The results embodied in this report have not been
submitted to any other University or Institute for the award of any degree or
diploma.
Signature of Candidate
NAVEEN KUMAR,
04750102019
INTRODUCTION
Overview:
Snake game is one of the most popular arcade games of all time. In this project,
we will be using PYGMAE to create this snake game.
PYGAME is an open-source library that is designed for making video games. It has
inbuilt graphics and sounds libraries. It is also beginner-friendly, and cross-
platform.
How to play the game:
The main objective of the player is to catch the maximum number of fruits
without hitting the wall or itself.
The snake can be controlled by the keyboard UP key to move Upward, DOWN key
to move Downward, LEFT key to move Left and RIGHT key to move Right.
What is Python?
Python is a computer programming language often used to build websites and
software, automate tasks, and conduct data analysis. Python is a general purpose
language, meaning it can be used to create a variety of different programs and
isn’t specialized for any specific problems. This versatility, along with its beginner-
friendliness, has made it one of the most-used programming languages today. A
survey conducted by industry analyst firm RedMonk found that it was the most
popular programming language among developers in 2020.
1. Data Analysis and Machine Learning: Python has become a staple in data
science, allowing data analysis and other professionals to use the language
to conduct complex statistical calculations, create data visualizations,
build machine learning algorithms, manipulate and analyze data, and
complete other data-related tasks.
5. Game Development: PYGAME: "the original and still very much active
package for game development using Python. It allows Python to talk
to SDL, a cross-platform, multimedia library. Because it needs to be
compiled for each platform and each Python version, there can be a lag
when a new Python version comes along."
Methods used in Making of the Snake Game-
Types of constructors:
Default constructor: The default constructor is a
simple constructor which doesn’t accept any
arguments. Its definition has only one argument which
is a reference to the instance being constructed.
Parameterized constructor: constructor with
parameters is known as parameterized constructor.
The parameterized constructor takes its first argument
as a reference to the instance being constructed
known as self and the rest of the arguments are
provided by the programmer.
8. List Slicing: In Python, list slicing is a common practice and it is
the most used technique for programmers to solve efficient
problems. Consider a python list, In-order to access a range of
elements in a list, you need to slice a list. One way to do this is to
use the simple slicing operator i.e. colon(:)
List_name[ Starting_index : Stop_index : Step ]
Python Modules used in the project
3. SYS Module: The sys module in Python provides various functions and
variables that are used to manipulate different parts of the Python
runtime environment.
Installations of the module in the IDE
1. PYGAME: To install PYGAME in your IDE, open the terminal of your IDE
and in the terminal write this command:
pip install pygame
Features of VS Code:
VS Code supports a wide array of programming languages from
Java, C++, and Python to CSS, Go, and Dockerfile. Moreover, VS
Code allows you to add on and even creating new extensions
including code linters, debuggers, and cloud and web
development support.
How to exit from the game: As the above explained the game
runs in an infinite loop, then to exit from the game, pygame
has provided an method to do so.
Taking the inputs from the user: To move the snake around the
screen to reach the fruits. We have to get the access of the every
key pressed by the user on the keyboard. After getting the user
input we have to check for the specific input is pressed or not. If
the user press UP key than we call another pygame function
which control the object on the screen and manipulate the
snake using that function.
Here how we do the above logic in the code:
1. First accessing the every input from the user:
for event in pygame.event.get():
import sys
import random
pygame.init()
cell_size = 30
cell_num = 20
APPLE = pygame.image.load("Graphics/apple.png")
game_font = pygame.font.Font("Font/PoetsenOne-Regular.ttf",25)
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()
class FRUIT:
self.randomize()
def draw_fruit(self):
#create a rectangle
screen.blit(APPLE,fruit_rect)
def randomize(self):
self.x = random.randint(0,cell_num - 1)
self.y = random.randint(0,cell_num - 1)
self.pos = Vector2(self.x,self.y)
class SNAKE():
self.body = [Vector2(5,5),Vector2(4,5),Vector2(3,5)]
self.direction = Vector2(1,0)
self.new_block = False
self.head_up =
pygame.image.load('Graphics/head_up.png').convert_alpha()
self.head_down =
pygame.image.load('Graphics/head_down.png').convert_alpha()
self.head_right =
pygame.image.load('Graphics/head_right.png').convert_alpha()
self.head_left =
pygame.image.load('Graphics/head_left.png').convert_alpha()
self.tail_up =
pygame.image.load('Graphics/tail_up.png').convert_alpha()
self.tail_down =
pygame.image.load('Graphics/tail_down.png').convert_alpha()
self.tail_right =
pygame.image.load('Graphics/tail_right.png').convert_alpha()
self.tail_left =
pygame.image.load('Graphics/tail_left.png').convert_alpha()
self.body_vertical =
pygame.image.load('Graphics/body_vertical.png').convert_alpha()
self.body_horizontal =
pygame.image.load('Graphics/body_horizontal.png').convert_alpha()
self.body_tr =
pygame.image.load('Graphics/body_tr.png').convert_alpha()
self.body_tl =
pygame.image.load('Graphics/body_tl.png').convert_alpha()
self.body_br =
pygame.image.load('Graphics/body_br.png').convert_alpha()
self.body_bl =
pygame.image.load('Graphics/body_bl.png').convert_alpha()
def draw_snake(self):
self.update_head_graphics()
self.update_tail_graphics()
block_rect = pygame.Rect(pos_x,pos_y,cell_size,cell_size)
if index == 0:screen.blit(self.head,block_rect)
screen.blit(self.tail,block_rect)
else:
if previous_block.x ==
next_block.x:screen.blit(self.body_vertical,block_rect)
screen.blit(self.body_horizontal,block_rect)
else:
screen.blit(self.body_tl,block_rect)
screen.blit(self.body_bl,block_rect)
elif previous_block.x == 1 and next_block.y == -1 or
previous_block.y == -1 and next_block.x == 1:
screen.blit(self.body_tr,block_rect)
screen.blit(self.body_br,block_rect)
def update_head_graphics(self):
def update_tail_graphics(self):
if self.new_block:
body_copy = self.body[:]
body_copy.insert(0,body_copy[0] + self.direction)
self.body = body_copy[:]
self.new_block = False
else:
body_copy = self.body[:-1]
body_copy.insert(0,body_copy[0] + self.direction)
self.body = body_copy[:]
def add_block(self):
self.new_block = True
def reset(self):
self.body = [Vector2(5,5),Vector2(4,5),Vector2(3,5)]
class MAIN():
self.snake = SNAKE()
self.fruit = FRUIT()
def update(self):
self.snake.move_snake()
self.check_collision()
self.check_fail()
def draw_elements(self):
self.fruit.draw_fruit()
self.snake.draw_snake()
self.draw_score()
def check_collision(self):
if self.fruit.pos == self.snake.body[0]:
self.fruit.randomize()
def check_fail(self):
self.game_over()
self.game_over()
if block == self.snake.body[0]:
self.game_over()
def game_over(self):
self.snake.reset()
def draw_score(self):
score_text = str(len(self.snake.body) - 3)
score_surface = game_font.render(score_text,True,(56,74,12))
score_x = int(cell_size * cell_num - 60)
apple_rect = APPLE.get_rect(midright =
(score_rect.left,score_rect.centery))
screen.blit(score_surface,score_rect)
screen.blit(APPLE,apple_rect)
main_game = MAIN()
SCREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCREEN_UPDATE,150)
while True:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == SCREEN_UPDATE:
main_game.update()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if main_game.snake.direction.y !=1:
main_game.snake.direction = Vector2(0,-1)
if event.key == pygame.K_DOWN:
if main_game.snake.direction.y !=-1:
main_game.snake.direction = Vector2(0,+1)
if event.key == pygame.K_LEFT:
if main_game.snake.direction.x !=1:
main_game.snake.direction = Vector2(-1,0)
if event.key == pygame.K_RIGHT:
if main_game.snake.direction.x !=-1:
main_game.snake.direction = Vector2(+1,0)
screen.fill((175,215,70))
main_game.draw_elements()
pygame.display.update()
clock.tick(60)
Results and Findings
Conclusion