Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Python Project File

This document describes a snake game project created using the Python programming language. It provides information on the student who created the project, Khushi Sahu, and outlines the various components of the snake game program including installing Pygame, creating the screen, creating the snake, moving the snake, adding game over conditions, adding food, increasing the snake's length, and displaying the score. The document is intended as a project submission to the DOEACC Society for the student's "O" Level certificate.

Uploaded by

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

Python Project File

This document describes a snake game project created using the Python programming language. It provides information on the student who created the project, Khushi Sahu, and outlines the various components of the snake game program including installing Pygame, creating the screen, creating the snake, moving the snake, adding game over conditions, adding food, increasing the snake's length, and displaying the score. The document is intended as a project submission to the DOEACC Society for the student's "O" Level certificate.

Uploaded by

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

1

PROJECT WORK
CODED THROUGH PROGRAMMING LANGUAGE
”PYTHON”
SNAKE GAME PROGRAM USING “PYTHON”

SESSION2020-21

PROJECT BY UNDER GUIDANCE OF


KHUSHI SAHU Mrs.Sonal Srivastava
“O”-LEVEL Signature-
Registration number – 1379805 (Computer Teacher)
E-mail- pankhurishiv2002@gmail.com
IICE COMPUTER EDUCATION
[LUCKNOW]
2

STUDENT
INFORMATION

NAME KHUSHI SAHU


REGISTRATION NO.1379805
LEVEL”O”-Level
PROJECT ONPYTHON Language
TOPIC SNAKE GAME PROGRAM
E-MAIL
pankhurishiv2002@gmail.com
3

To,

DOEACC SOCIETY,

New Delhi.

Subject: Project Submission

Respected madam/sir,

Myself Khushi Sahu,Registration No.-1379805;I have


cleared all theory and practical exams of “O” Level,and my project
work is completed with coded source file and compiled exe file.

I am sending you a hard copy including complete


program coding and respective output.

Kindly arrange to send us “O”-Level certificate as soon as


possible.

Thanking You,

Regards,

D/O,Sushil Kumar Sahu

347/8,Mansukh Das ka Hata,

Old Tikaitganj,Lucknow-226003.

Contact-6386653417

E-mail-pankhurishiv2002@gmail.com

Signature-

Date-
4

PROJECT CERTIFICATION FOR”O”-LEVEL CANDIDATE

This is to certify that the project work done at


__________________________________________ by
Miss._______________________(DOEACC Registration
No.:___________) in partial fulfillment of ”O”-Level
Examination has been found satisfactory.
This report had not been submitted for any other
examination and does not form a part of any other
course undergone by the candidate.
It is further certified that she has appeared in all the
four modules.

___________________________
Signature
(By head of the Institution with PROV NO./FULL NO.)

OR

(By head of the Organisation/Division)


5

PROGRAM OF SNAKE GAME IN PYTHON

Yes,I know you all have played the Snake Game and
definitely,you never wanted to lose.As kids,we all loved
looking for cheats in order to never see the “Game
Over” message but as techies, I know you would want
to make this ‘Snake’ dance to your beats.This is what I
will be showing you all on Snake Game in Python.
Before moving on,let’s have a quick look at all the sub-
bits that build the Snake Game in Python.

Sub-bits
1)Installing Pygame Page No. 6 to 7
2)Create the screen Page No. 7 to 11
3)Create the snake Page No. 11 to 12
4) Moving the snake Page No. 12 to 16
5) Game over when snake Page No. 16 to 19
hits the boundries
6)Adding the food Page No. 20 to 25
7)Increasing the length of Page No. 26 to 32
the snake
8)Displaying the score Page No. 33 to 39
6

Installing Pygame
The first thing you all need to do in order to
create games using Pygame is to install it in on
your systems.To do that,you can simply use the
following command.
pip install pygame
Once that is done,just import Pygame and start
off with your game development.Before moving
on,take a look at the Pygame function that have
been use in this snake game along with their
descriptions.
FUNCTION DESCRIPTION
init() Initializes all of the
imported Pygame
modules (returns a
tuple indicating success
and failure of
initializations)
display.set_mode() Takes a tuple or alist as
its parameter of create
a surface(tuple
preferred)
update() Updates the screen
7

quit() Use to uninitialized


everything
set_caption() Will set the caption text
on the top of the display
screen
event.get() Returns list of all events

Surface.fill() Will fill the surface with a


solid color
time.Clock() Helps track time
font.SysFont() Will create a Pygame font
from the System font
resources

CREATE THE SCREEN:


To create the screen using Pygame you will need to
make a use of the display.set_mode() function.Also,
you will have to make use of the init() and the quit()
method to initialize and uninitialize everything at the
start and the end of the code.The update() method is
used to update any changes made to the screen.There
is another method i.e.flip() that works similarly to the
update() function.The difference is that the update()
method updates only the changes that are
8

made(however,if no parameters are passed,updates


the complete screen)but the flip() method redoes the
complete screen again.
CODE

import pygame

pygame.init()

dis=pygame.dislay.set_mode((400,300))

pygame.display.update()

pygame.quit()

quit()

OUTPUT:
9

But when you run this code,the screen will appear,but will
immediately close as well.To fix that,you should make use of a game
loop using the while loop before I actually quit the game as follows:

import pygame

pygame.init()

dis=pygame.dislay.set_mode((400,300))

pygame.display.update()

pygame.display.set_caption('snake game by edureka')

game_over=False

while not game_over:

for event in pygame.event.get():

print(event) #print out all the action that takes place on the
screen

pygame.quit()

quit()

When you run this code,you will see that the screen that you saw
earlier does not quit and also,it returns all the actions that take place
over it.I have done that using thr event.get() function.Also,I have
named the screen as ”Snake Game by Edureka” using the
display.set_caption() function.

OUTPUT:
10

Now, you have a screen to play your Snake Game, but when you try
to click on the close button, the screen does not close. This is
because you have not specified that your screen should exit when
you hit that close button. To do that, Pygame provides an event
called “QUIT” and it should be used as follows:

import pygame

pygame.init()

dis=pygame.display.set_mode((400,300))

pygame.display.update()

pygame.display.set_caption('Snake game by Edureka')

game_over=False

while not game_over:

for event in pygame.event.get():

if event.type==pygame.QUIT:

game_over=True
11

pygame.quit()

quit()

So now your screen is all set. The next part is to draw our snake on
the screen

Create the Snake:


To create the snake, I will first initialize a few color variables in order
to color the snake, food, screen, etc. The color scheme used in
Pygame is RGB i.e “Red Green Blue”. In case you set all these to 0’s,
the color will be black and all 255’s will be white. So our snake will
actually be a rectangle. To draw rectangles in Pygame, you can make
use of a function called draw.rect() which will help yo draw the
rectangle with the desired color and size.

import pygame

pygame.init()

dis=pygame.display.set_mode((400,300))

pygame.display.set_caption('Snake game by Edureka')

blue=(0,0,255)

red=(255,0,0)

game_over=False

while not game_over:


12

for event in pygame.event.get():

if event.type==pygame.QUIT:

game_over=True

pygame.draw.rect(dis,blue,[200,150,10,10])

pygame.display.update()

pygame.quit()

quit()

OUTPUT:

As you can see, the snakehead is created as a blue rectangle. The


next step is to get your snake moving.

Moving the Snake:


To move the snake, you will need to use the key events present in
the KEYDOWN class of Pygame. The events that are used over here
13

are, K_UP, K_DOWN, K_LEFT, and K_RIGHT to make the snake move
up, down, left and right respectively. Also, the display screen is
changed from the default black to white using the fill() method.

I have created new variables x1_change and y1_change in order to


hold the updating values of the x and y coordinates.

import pygame

pygame.init()

white = (255, 255, 255)

black = (0, 0, 0)

red = (255, 0, 0)

dis = pygame.display.set_mode((800, 600))

pygame.display.set_caption('Snake Game by Edureka')

game_over = False

x1 = 300

y1 = 300
14

x1_change = 0

y1_change = 0

clock = pygame.time.Clock()

while not game_over:

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 = -10

y1_change = 0

elif event.key == pygame.K_RIGHT:

x1_change = 10

y1_change = 0

elif event.key == pygame.K_UP:

y1_change = -10

x1_change = 0

elif event.key == pygame.K_DOWN:

y1_change = 10
15

x1_change = 0

x1 += x1_change

y1 += y1_change

dis.fill(white)

pygame.draw.rect(dis, black, [x1, y1, 10, 10])

pygame.display.update()

clock.tick(30)

pygame.quit()

quit()

OUTPUT
16

Game Over when Snake hits the boundaries:


In this snake game, if the player hits the boundaries of the screen,
then he loses. To specify that, I have made use of an ‘if’ statement
that defines the limits for the x and y coordinates of the snake to be
less than or equal to that of the screen. Also, make a not over here
that I have removed the hardcodes and used variables instead so
that it becomes easy in case you want to make any changes to the
game later on.

import pygame

import time

pygame.init()

white = (255, 255, 255)

black = (0, 0, 0)

red = (255, 0, 0)

dis_width = 800

dis_height = 600

dis = pygame.display.set_mode((dis_width, dis_width))

pygame.display.set_caption('Snake Game by Edureka')

game_over = False
17

x1 = dis_width/2

y1 = dis_height/2

snake_block=10

x1_change = 0

y1_change = 0

clock = pygame.time.Clock()

snake_speed=30

font_style = pygame.font.SysFont(None, 50)

def message(msg,color):

mesg = font_style.render(msg, True, color)

dis.blit(mesg, [dis_width/2, dis_height/2])

while not game_over:

for event in pygame.event.get():

if event.type == pygame.QUIT:

game_over = True
18

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:

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_over = True

x1 += x1_change

y1 += y1_change

dis.fill(white)

pygame.draw.rect(dis, black, [x1, y1, snake_block, snake_block])


19

pygame.display.update()

clock.tick(snake_speed)

message("You lost",red)

pygame.display.update()

time.sleep(2)

pygame.quit()

quit()

OUTPUT:
20

Adding the Food:


Here, I will be adding some food for the snake and when the
snake crosses over that food, I will have a message saying
“Yummy!!”. Also, I will be making a small change wherein I will
include the options to quit the game or to play again when the
player loses.

import pygame

import time

import random

pygame.init()

white = (255, 255, 255)

black = (0, 0, 0)

red = (255, 0, 0)

blue = (0, 0, 255)

dis_width = 800

dis_height = 600

dis = pygame.display.set_mode((dis_width, dis_height))

pygame.display.set_caption('Snake Game by Edureka')


21

clock = pygame.time.Clock()

snake_block = 10

snake_speed = 30

font_style = pygame.font.SysFont(None, 30)

def message(msg, color):

mesg = font_style.render(msg, True, color)

dis.blit(mesg, [dis_width/3, dis_height/3])

def gameLoop(): # creating a function

game_over = False

game_close = False

x1 = dis_width / 2

y1 = dis_height / 2
22

x1_change = 0

y1_change = 0

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


10.0) * 10.0

foody = round(random.randrange(0, dis_width - snake_block) /


10.0) * 10.0

while not game_over:

while game_close == True:

dis.fill(white)

message("You Lost! Press Q-Quit or C-Play Again", red)

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()
23

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:

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
24

x1 += x1_change

y1 += y1_change

dis.fill(white)

pygame.draw.rect(dis, blue, [foodx, foody, snake_block,


snake_block])

pygame.draw.rect(dis, black, [x1, y1, snake_block, snake_block])

pygame.display.update()

if x1 == foodx and y1 == foody:

print("Yummy!!")

clock.tick(snake_speed)

pygame.quit()

quit()

gameLoop()
25

OUTPUT:

Terminal:
26

Increasing the Length of the Snake:


The following code will increase the size of our sake when it eats the
food. Also, if the snake collides with his own body, the game is over
and you ill see a message as “You Lost! Press Q-Quit or C-Play Again“.
The length of the snake is basically contained in a list and the initial
size that is specified in the following code is one block.

CODE:
import pygame

import time

import random

pygame.init()

white = (255, 255, 255)

yellow = (255, 255, 102)

black = (0, 0, 0)

red = (213, 50, 80)

green = (0, 255, 0)

blue = (50, 153, 213)

dis_width = 600

dis_height = 400
27

dis = pygame.display.set_mode((dis_width, dis_height))

pygame.display.set_caption('Snake Game by Edureka')

clock = pygame.time.Clock()

snake_block = 10

snake_speed = 15

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 message(msg, color):

mesg = font_style.render(msg, True, color)

dis.blit(mesg, [dis_width / 6, dis_height / 3])


28

def gameLoop():

game_over = False

game_close = False

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

while not game_over:


29

while game_close == True:

dis.fill(blue)

message("You Lost! Press C-Play Again or Q-Quit", red)

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
30

elif event.key == pygame.K_RIGHT:

x1_change = snake_block

y1_change = 0

elif event.key == pygame.K_UP:

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)
31

if len(snake_List) > Length_of_snake:

del snake_List[0]

for x in snake_List[:-1]:

if x == snake_Head:

game_close = True

our_snake(snake_block, snake_List)

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)

pygame.quit()
32

quit()

gameLoop()
OUTPUT:
33

Displaying the Score:


Last but definitely not the least, you will need to display the score of
the player. To do this, I have created a new function as “Your_score”.
This function will display the length of the snake subtracted by 1
because that is the initial size of the snake.

CODE:

import pygame

import time

import random

pygame.init()

white = (255, 255, 255)

yellow = (255, 255, 102)

black = (0, 0, 0)

red = (213, 50, 80)

green = (0, 255, 0)

blue = (50, 153, 213)

dis_width = 600

dis_height = 400
34

dis = pygame.display.set_mode((dis_width, dis_height))

pygame.display.set_caption('Snake Game by Edureka')

clock = pygame.time.Clock()

snake_block = 10

snake_speed = 15

font_style = pygame.font.SysFont("bahnschrift", 25)

score_font = pygame.font.SysFont("comicsansms", 35)

def Your_score(score):

value = score_font.render("Your Score: " + str(score), True, yellow)

dis.blit(value, [0, 0])

def our_snake(snake_block, snake_list):

for x in snake_list:
35

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


snake_block])

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

x1 = dis_width / 2

y1 = dis_height / 2

x1_change = 0

y1_change = 0

snake_List = []

Length_of_snake = 1
36

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

while not game_over:

while game_close == True:

dis.fill(blue)

message("You Lost! Press C-Play Again or Q-Quit", 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()
37

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:

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
38

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

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
39

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


/ 10.0) * 10.0

Length_of_snake += 1

clock.tick(snake_speed)

pygame.quit()

quit()

gameLoop()

OUTPUT:

PYTHON PROJECT COMPLETED……………………………………………………………………………………………………………………THANK YOU!!!!!

You might also like