PURPOSE
Nowadays, GAME DEVELOPMENT has emerged as an integral
field in the sector of technological advancement. Gamers and
gaming professional are slowly improving on this sector and every
now and then new games are being developed by such programmers.
The motive of this project is to just provide a basic idea of how we
can develop a simple game as like ‘Snake Xenia’ in Nokia using
the codes of Python Programming Language.
For the ones who wish to make a career in this field, this small project
can be regarded as their first step. “A small step for man is a giant
leap for mankind.” These words by Neil Armstrong can be used
as a motivation for young programmers and game developers to
create a different sort of thing which is useful as well as entertaining
to the masses.
3
What is python?
PYTHON is an interactive cross-platform programming language
that offers various features. Python is an interpreted, high-level,
general-purpose programming language. Guido Van Rossum created
Python in the late 90’s. It wasn’t named after a dangerous snake.
Rossum was a fan of a comedy series from late seventies. The
name “Python” was adopted from the same series “Monty
Python’s Flying Circus”. It has the largest community for learners and
Collaborators. Its an open-source programming language whose
source code is also available.
4
Why python?
Why is python so popular?
1. Syntax is extremely simple to read and follow
2. Python is very beginner-friendly
3. Millions of happy learners
4. Easier than other programming language
Why should we learn python?
1. General-purpose language
2. Wide range of applications
3. Web development-Django bottle
4. Mathematical computations(Numpy and simpy)
5. Graphical user interface (Panda 3D)
6. Length of the code is relatively short
7. Fun to work with
5
ABOUT THE GAME AND HOW IT WORKS
In the game of Snake, the player uses the arrow keys to move
a "snake" around the board. As the snake finds food, it eats the food,
and thereby grows larger. The game ends when the snake either
moves off the screen or moves into itself. The goal is to make the
snake as large as possible before that happens.
The player is represented as snake, which grows if it eats an apple.
The goal of the game is to eat as many apples as possible without
colliding into you. This is very easy in the early phase of the game but
is increasingly more difficult as the length of the snake grows.
The snake game has some rules:
1. If the snake eats an apple, the apple moves to a new position.
2. If the snake eats an apple, the snake’s length grows.
3. If a snake collapses with itself, game over.
A player object can be created and variables can be modified
using the movement methods. We link those methods to the
events. In the PYGAME module we can import/define functions
which would help us in creating the game in an interactive
manner.
6
MODULES/FUNCTIONS USED IN CODING
1. PYGAME
2. TIME
3. RANDOM
STATEMENTS/LOOPS USED IN CODING
1. FOR/WHILE LOOP
2. IF/ELIF/ELSE STATEMENT
7
Source code for the Snake Game
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)
# Define window size
dis_width = 600
dis_height = 400
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Snake Game')
# Snake settings
snake_block = 10
8
snake_speed = 15
# Fonts and clock
clock = pygame.time.Clock()
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
# Function to display the player's score
def Your_score(score):
value = score_font.render("Your Score: " + str(score), True, yellow)
dis.blit(value, [0, 0])
# Function to draw the snake
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])
# Function to display messages
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
9
# Main game loop
def gameLoop():
game_over = False
game_close = False
# Starting position of the snake
x1 = dis_width / 2
y1 = dis_height / 2
# Change in position
x1_change = 0
y1_change = 0
# Snake body
snake_list = []
length_of_snake = 1
# Food position
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
10
while not game_over:
while game_close:
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()
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
11
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)
if len(snake_list) > length_of_snake:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
12
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)
pygame.quit()
quit()
gameLoop()
13
OUTPUT
14
Conclusion
We learned how to create the snake game in Python along with
concepts such as collision detection, image loading and event
handling. Many things could be added to this little toy game but this
serves as a very simple example.
The good thing about this game and our solution is that it is very
simple. The approach is pretty simple and easy to understand
even for beginners. This game could run on many platforms.
There can be many more features which can be added to this game to
make it more interactive and interesting.
15
BIBLIOGRAPHY
Books used:
1. Sumita Arora Computer text book for class XI
2. Sumita Arora Computer text book for class XII
Websites:
1. https://www.codementor.io/arsya/build-snake-game-using-curses-
du107zpmw
2. https://pythonspot.com/snake-with-pygame/
3. http://programarcadegames.com/python_examples/f.php?
file=snake.py
16