Python Project File
Python Project File
PROJECT WORK
CODED THROUGH PROGRAMMING LANGUAGE
”PYTHON”
SNAKE GAME PROGRAM USING “PYTHON”
SESSION2020-21
STUDENT
INFORMATION
To,
DOEACC SOCIETY,
New Delhi.
Respected madam/sir,
Thanking You,
Regards,
Old Tikaitganj,Lucknow-226003.
Contact-6386653417
E-mail-pankhurishiv2002@gmail.com
Signature-
Date-
4
___________________________
Signature
(By head of the Institution with PROV NO./FULL NO.)
OR
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
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()
game_over=False
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()
game_over=False
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
import pygame
pygame.init()
dis=pygame.display.set_mode((400,300))
blue=(0,0,255)
red=(255,0,0)
game_over=False
if event.type==pygame.QUIT:
game_over=True
pygame.draw.rect(dis,blue,[200,150,10,10])
pygame.display.update()
pygame.quit()
quit()
OUTPUT:
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.
import pygame
pygame.init()
black = (0, 0, 0)
red = (255, 0, 0)
game_over = False
x1 = 300
y1 = 300
14
x1_change = 0
y1_change = 0
clock = pygame.time.Clock()
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
x1_change = 10
y1_change = 0
y1_change = -10
x1_change = 0
y1_change = 10
15
x1_change = 0
x1 += x1_change
y1 += y1_change
dis.fill(white)
pygame.display.update()
clock.tick(30)
pygame.quit()
quit()
OUTPUT
16
import pygame
import time
pygame.init()
black = (0, 0, 0)
red = (255, 0, 0)
dis_width = 800
dis_height = 600
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
def message(msg,color):
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
x1_change = snake_block
y1_change = 0
y1_change = -snake_block
x1_change = 0
y1_change = snake_block
x1_change = 0
game_over = True
x1 += x1_change
y1 += y1_change
dis.fill(white)
pygame.display.update()
clock.tick(snake_speed)
message("You lost",red)
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
OUTPUT:
20
import pygame
import time
import random
pygame.init()
black = (0, 0, 0)
red = (255, 0, 0)
dis_width = 800
dis_height = 600
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 30
game_over = False
game_close = False
x1 = dis_width / 2
y1 = dis_height / 2
22
x1_change = 0
y1_change = 0
dis.fill(white)
pygame.display.update()
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
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
x1_change = snake_block
y1_change = 0
y1_change = -snake_block
x1_change = 0
y1_change = snake_block
x1_change = 0
game_close = True
24
x1 += x1_change
y1 += y1_change
dis.fill(white)
pygame.display.update()
print("Yummy!!")
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
25
OUTPUT:
Terminal:
26
CODE:
import pygame
import time
import random
pygame.init()
black = (0, 0, 0)
dis_width = 600
dis_height = 400
27
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
for x in snake_list:
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
dis.fill(blue)
pygame.display.update()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
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
x1_change = snake_block
y1_change = 0
y1_change = -snake_block
x1_change = 0
y1_change = snake_block
x1_change = 0
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(blue)
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
31
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()
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
32
quit()
gameLoop()
OUTPUT:
33
CODE:
import pygame
import time
import random
pygame.init()
black = (0, 0, 0)
dis_width = 600
dis_height = 400
34
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
def Your_score(score):
for x in snake_list:
35
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
dis.fill(blue)
Your_score(Length_of_snake - 1)
pygame.display.update()
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
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
x1_change = snake_block
y1_change = 0
y1_change = -snake_block
x1_change = 0
y1_change = snake_block
x1_change = 0
game_close = True
x1 += x1_change
y1 += y1_change
38
dis.fill(blue)
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
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()
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
OUTPUT: