0% found this document useful (0 votes)
10 views

Pps Mini Project (Snake Game Python)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Pps Mini Project (Snake Game Python)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

MINI PROJECT REPORT

on

SNAKE GAME using Python

Submitted by
ABHISHEK CHERUKU (RA2311004010029)
SANDHA SAI TEJA (RA2311004010056)

Semester – II

Academic Year: 2023-24 Even

Under the guidance of

Dr. B. Priyalakshmi
Assistant Professor, Department of ECE

In partial fulfilment for the Course

of

21CSS101J -PROGRAMMING FOR PROBLEM SOLVING

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING


College of Engineering and Technology,
SRM Institute of Science and Technology
SRM Nagar, Kattankulathur – 603203, Kancheepuram District, Tamil Nadu.

April 2024
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

(Under Section 3 of UGC Act, 1956)

BONAFIDE CERTIFICATE

Certified that this activity report for the course 21CSS101J -PROGRAMMING FOR PROBLEM SOLVING is

the bonafide work of ABHISHEK CHERUKU (RA2311004010029) AND SANDHA SAI TEJA

(RA2311004010056) who carried out the work under my supervision.

SIGNATURE SIGNATURE

Dr. B. Priyalakshmi Dr. Shanthi Prince


Assistant Professor Head of The Department
Department of ECE Department of ECE
SRMIST SRMIST
Kattankulathur Kattankulathur
TABLE OF CONTENTS

S.NO. CONTENT PAGE NO.

1 ABSTRACT

2 OBJECTIVE

3 INTRODUCTION

4 SYSTEM DESIGN AND SOURCE CODE

5 RESULTS (SCREENSHOTS)

6 REFERENCES

3
Title: SNAKE GAME USING PYTHON

ABSTRACT:
The Snake Game is a classic arcade game where the player
controls a snake that grows in length as it consumes food items
appearing on the screen. This report presents the
implementation of the Snake Game using Python
programming language and the Pygame library. The objective
of this project is to provide an entertaining and engaging
gaming experience for users while demonstrating the
implementation of game logic, user input handling, and
graphical rendering. The report outlines the game components,
rules, features, and future improvements of the Snake Game
project. Through this implementation, users can experience the
nostalgia of playing a timeless arcade game while exploring
the capabilities of Python programming for game
development.

OBJECTIVE:

The objective of this project is to create a Snake Game using Python


and Pygame. The aim is to provide an enjoyable gaming experience
while also demonstrating fundamental programming concepts such as
game logic, user input handling, and graphical rendering. By
implementing the Snake Game, this project serves as both a fun activity
and a learning opportunity for individuals interested in Python
4
programming and game development. Through clear documentation
and straightforward code, the project aims to be accessible to
programmers of varying skill levels, fostering a deeper understanding of
Python programming principles in a gaming context.

INTRODUCTION:
The Snake Game is a classic arcade game where the player controls a
snake that grows in length as it consumes food items appearing on the
screen. The objective is to guide the snake to eat as much food as
possible without colliding with itself or the walls.

The Snake Game is implemented using the Pygame library in Python.


Pygame provides functionalities for creating games and multimedia
applications.

Game Components:

1. Snake: Represents the snake controlled by the player. It consists


of a series of connected segments.
2. Food: Represents the food items that the snake consumes to
grow.
3. Score: Keeps track of the player's score, incremented each time
the snake consumes food.
4. Game Window: The graphical interface where the game is
displayed and the player interacts.

Game Rules:

The snake moves continuously in one direction (up, down, left, or


right). The player can change the direction of the snake using arrow
5
keys or other designated keys. If the snake collides with itself or the
walls of the game window, the game ends. Each time the snake
consumes food, its length increases, and the player's score is
incremented. The game speed increases as the snake grows longer,
making it more challenging.

SYSTEM DESIGN AND SOURCE CODE:


(The codes below have been split; therefore, each code is being explained
thoroughly)

Step 1: First we are importing the necessary libraries. After that, we are
defining the width and height of the window in which the game will be
played. And define the color in RGB format that we are going to use in
our game for displaying text.

CODE 1:
# importing libraries
import pygame
import time
import random

snake_speed = 15

# Window size
window_x = 720
window_y = 480

# defining colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
6
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)

Step 2: After importing libraries we need to initialize Pygame using


pygame.init() method. Create a game window using the width and
height defined in the previous step. Here pygame.time.Clock() will be
used further in the main logic of the game to change the speed of the
snake.

CODE 2:

# Initialising pygame

pygame.init()

# Initialise game window

pygame.display.set_caption('GeeksforGeeks Snakes')

game_window = pygame.display.set_mode((window_x, window_y))

# FPS (frames per second) controller

fps = pygame.time.Clock()

Step 3: Initialize snake position and its size. After initializing snake
position, initialize the fruit position randomly anywhere in the defined
height and width. By setting direction to RIGHT we ensure that,
whenever a user runs the program/game, the snake must move right to
the screen.

7
CODE 3:

# defining snake default position

snake_position = [100, 50]

# defining first 4 blocks of snake

# body

snake_body = [ [100, 50],

[90, 50],

[80, 50],

[70, 50]

# fruit position

fruit_position = [random.randrange(1, (window_x//10)) * 10,

random.randrange(1, (window_y//10)) * 10]

fruit_spawn = True

# setting default snake direction

# towards right

direction = 'RIGHT'

change_to = direction

8
Step 4: Create a function to display the score of the player. In this
function, firstly we’re creating a font object i.e. the font color will go
here. Then we are using render to create a background surface that we
are going to change whenever our score updates. Create a rectangular
object for the text surface object (where text will be refreshed) Then,
we are displaying our score using blit. blit takes two argument
screen.blit(background,(x,y))

CODE 4:

# initial score

score = 0

# displaying Score function

def show_score(choice, color, font, size):

# creating font object score_font

score_font = pygame.font.SysFont(font, size)

# create the display surface object

# score_surface

score_surface = score_font.render('Score : ' + str(score), True, color)

9
# create a rectangular object for the

# text surface object

score_rect = score_surface.get_rect()

# displaying text

game_window.blit(score_surface, score_rect)

Step 5: Now create a game over function that will represent the score
after the snake is hit by a wall or itself. In the first line, we are creating
a font object to display scores. Then we are creating text surfaces to
render scores. After that, we are setting the position of the text in the
middle of the playable area. Display the scores using blit and updating
the score by updating the surface using flip(). We are using sleep(2) to
wait for 2 seconds before closing the window using quit().

CODE 5:

# game over function

def game_over():

# creating font object my_font

my_font = pygame.font.SysFont('times new roman', 50)

# creating a text surface on which text


1
0
# will be drawn

game_over_surface = my_font.render('Your Score is : ' + str(score),


True, red)

# create a rectangular object for the text

# surface object

game_over_rect = game_over_surface.get_rect()

# setting position of the text

game_over_rect.midtop = (window_x/2, window_y/4)

# blit will draw the text on screen

game_window.blit(game_over_surface, game_over_rect)

pygame.display.flip()

# after 2 seconds we will quit the

# program

time.sleep(2)

# deactivating pygame library

pygame.quit()

# quit the program


1
1
quit()

Step 6: Now we will be creating our main function that will do the
following things: We will be validating the keys that will be responsible
for the movement of the snake, then we will be creating a special
condition that the snake should not be allowed to move in the opposite
direction instantaneously. After that, if snake and fruit collide we will
be incrementing the score by 10 and new fruit will be spanned. After
that, we are checking that is the snake hit with a wall or not. If a snake
hits a wall we will call game over function. If the snake hits itself, the
game over function will be called. And in the end, we will be displaying
the scores using the show_score function created earlier.

CODE 6:

# Main Function

while True:

# handling key events

for event in pygame.event.get():

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_UP:

change_to = 'UP'

if event.key == pygame.K_DOWN:

change_to = 'DOWN'
1
2
if event.key == pygame.K_LEFT:

change_to = 'LEFT'

if event.key == pygame.K_RIGHT:

change_to = 'RIGHT'

# If two keys pressed simultaneously

# we don't want snake to move into two directions

# simultaneously

if change_to == 'UP' and direction != 'DOWN':

direction = 'UP'

if change_to == 'DOWN' and direction != 'UP':

direction = 'DOWN'

if change_to == 'LEFT' and direction != 'RIGHT':

direction = 'LEFT'

if change_to == 'RIGHT' and direction != 'LEFT':

direction = 'RIGHT'

# Moving the snake

if direction == 'UP':

snake_position[1] -= 10

if direction == 'DOWN':

snake_position[1] += 10
1
3
if direction == 'LEFT':

snake_position[0] -= 10

if direction == 'RIGHT':

snake_position[0] += 10

# Snake body growing mechanism

# if fruits and snakes collide then scores will be

# incremented by 10

snake_body.insert(0, list(snake_position))

if snake_position[0] == fruit_position[0] and snake_position[1] ==


fruit_position[1]:

score += 10

fruit_spawn = False

else:

snake_body.pop()

if not fruit_spawn:

fruit_position = [random.randrange(1, (window_x//10)) * 10,

random.randrange(1, (window_y//10))
* 10]

fruit_spawn = True

1
4
game_window.fill(black)

for pos in snake_body:

pygame.draw.rect(game_window, green, pygame.Rect(

pos[0], pos[1], 10, 10))

pygame.draw.rect(game_window, white, pygame.Rect(

fruit_position[0], fruit_position[1], 10, 10))

# Game Over conditions

if snake_position[0] < 0 or snake_position[0] > window_x-10:

game_over()

if snake_position[1] < 0 or snake_position[1] > window_y-10:

game_over()

# Touching the snake body

for block in snake_body[1:]:

if snake_position[0] == block[0] and snake_position[1] ==


block[1]:

game_over()

# displaying score continuously

1
5
show_score(1, white, 'times new roman', 20)

# Refresh game screen

pygame.display.update()

# Frame Per Second /Refresh Rate

fps.tick(snake_speed)

STEP 7: IMPLEMENTATION,

CODE 7:

# importing libraries

import pygame

import time

import random

snake_speed = 15

# Window size

window_x = 720

window_y = 480

# defining colors
1
6
black = pygame.Color(0, 0, 0)

white = pygame.Color(255, 255, 255)

red = pygame.Color(255, 0, 0)

green = pygame.Color(0, 255, 0)

blue = pygame.Color(0, 0, 255)

# Initialising pygame

pygame.init()

# Initialise game window

pygame.display.set_caption('GeeksforGeeks Snakes')

game_window = pygame.display.set_mode((window_x, window_y))

# FPS (frames per second) controller

fps = pygame.time.Clock()

# defining snake default position

snake_position = [100, 50]

# defining first 4 blocks of snake body

snake_body = [[100, 50],

[90, 50],
1
7
[80, 50],

[70, 50]

# fruit position

fruit_position = [random.randrange(1, (window_x//10)) * 10,

random.randrange(1, (window_y//10)) * 10]

fruit_spawn = True

# setting default snake direction towards

# right

direction = 'RIGHT'

change_to = direction

# initial score

score = 0

# displaying Score function

def show_score(choice, color, font, size):

# creating font object score_font

score_font = pygame.font.SysFont(font, size)


1
8
# create the display surface object

# score_surface

score_surface = score_font.render('Score : ' + str(score), True, color)

# create a rectangular object for the text

# surface object

score_rect = score_surface.get_rect()

# displaying text

game_window.blit(score_surface, score_rect)

# game over function

def game_over():

# creating font object my_font

my_font = pygame.font.SysFont('times new roman', 50)

# creating a text surface on which text

# will be drawn

game_over_surface = my_font.render(

'Your Score is : ' + str(score), True, red)


1
9
# create a rectangular object for the text

# surface object

game_over_rect = game_over_surface.get_rect()

# setting position of the text

game_over_rect.midtop = (window_x/2, window_y/4)

# blit will draw the text on screen

game_window.blit(game_over_surface, game_over_rect)

pygame.display.flip()

# after 2 seconds we will quit the program

time.sleep(2)

# deactivating pygame library

pygame.quit()

# quit the program

quit()

2
0
# Main Function

while True:

# handling key events

for event in pygame.event.get():

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_UP:

change_to = 'UP'

if event.key == pygame.K_DOWN:

change_to = 'DOWN'

if event.key == pygame.K_LEFT:

change_to = 'LEFT'

if event.key == pygame.K_RIGHT:

change_to = 'RIGHT'

# If two keys pressed simultaneously

# we don't want snake to move into two

# directions simultaneously

if change_to == 'UP' and direction != 'DOWN':

direction = 'UP'

if change_to == 'DOWN' and direction != 'UP':

direction = 'DOWN'
2
1
if change_to == 'LEFT' and direction != 'RIGHT':

direction = 'LEFT'

if change_to == 'RIGHT' and direction != 'LEFT':

direction = 'RIGHT'

# Moving the snake

if direction == 'UP':

snake_position[1] -= 10

if direction == 'DOWN':

snake_position[1] += 10

if direction == 'LEFT':

snake_position[0] -= 10

if direction == 'RIGHT':

snake_position[0] += 10

# Snake body growing mechanism

# if fruits and snakes collide then scores

# will be incremented by 10

snake_body.insert(0, list(snake_position))

if snake_position[0] == fruit_position[0] and snake_position[1] ==


fruit_position[1]:

score += 10

2
2
fruit_spawn = False

else:

snake_body.pop()

if not fruit_spawn:

fruit_position = [random.randrange(1, (window_x//10)) * 10,

random.randrange(1, (window_y//10))
* 10]

fruit_spawn = True

game_window.fill(black)

for pos in snake_body:

pygame.draw.rect(game_window, green,

pygame.Rect(pos[0], pos[1], 10, 10))

pygame.draw.rect(game_window, white, pygame.Rect(

fruit_position[0], fruit_position[1], 10, 10))

# Game Over conditions

if snake_position[0] < 0 or snake_position[0] > window_x-10:

game_over()

if snake_position[1] < 0 or snake_position[1] > window_y-10:

2
3
game_over()

# Touching the snake body

for block in snake_body[1:]:

if snake_position[0] == block[0] and snake_position[1] ==


block[1]:

game_over()

# displaying score continuously

show_score(1, white, 'times new roman', 20)

# Refresh game screen

pygame.display.update()

# Frame Per Second /Refresh Rate

fps.tick(snake_speed)

2
4
RESULT:

REFERENCE:
Geeksforgeeks.org

THANK YOU

2
5

You might also like