Snak
e Gam
in pyt e
hon
By: Jatin Shetty, Varun
Nair, Venkatesh Bindu,
Pranav Mendon
Page 1
Page 2
Page 3
Index
Sr. No Topic Page No.
1 Cover Page 1
2 Certi cate 2
3 Acknowledgement 3
4 Index 4
5 Assets 5
6 Setup 6
7 Code 7-14
8 Result 15
9 Bibliography 16
Page 4
fi
Assets
Download the assets from this QR code which links
to a google drive
Contains These Files:
tail_left.png tail_right.png tail_left.png tail_down.png head_up.png head_right.png body_horizo
ntal.png
body_tr.png body_br.png body_bl.png body_tl.png body_vertic head_down.p head_right.png apple.png
al.png ng
Page 5
Setup
Use command line or terminal to install pygame. The
command “pip install pygame” or “pip3 install pygame” is
used.
Page 6
Code
First we import the libraries we need to run this code. All of
these except pygame come pre-installed with Python,
which we have shown how to install. After that we can
start with programming our rst object, the Snake. We use
this code to de ne the snake textures with the images we
downloaded earlier.
Page 7
fi
fi
We then de ne the automatic movement of the Snake so
that the textures move with the Snake hitbox.
We set up the Head graphics and the Tail graphics to
change with the movement.
Page 8
fi
This code is used to make the snake grow longer as well
as to destroy it if it hits itself
Now we start de ning the fruit, which is the thing the
snake must collect to grow in size. We set it randomise its
position and import its textures
Page 9
fi
Now we can nally start coding the game mechanics. We
rst start by de ning the initial game state. We also de ne
the elements like the background, the update trigger and
the collision check system. We set up the trigger when the
snake touches the fruit, causing the fruit to go to another
random position
Here we setup the code to check the fail state to check if
the game is over and show a game over message and to
automatically reset it after a set amount of time (12
seconds here).
Page 10
fi
fi
fi
fi
We now properly set up the background grass by setting
up its look. We also de ne a way to measure progress by
creating a score count in the bottom right of the corner
that increases the score by 10 every time a fruit is eaten.
The code here his used to draw a Game Over message on
the screen.
Page 11
fi
Here we set up a pre-game menu with start instructions.
The key to start has been set to SPACE here.
We now set some code to shorten the process. We also
set up the screen update.
Page 12
The code here is used to bind the movements of the snake
to the arrow keys, as well as to de ne fps(frames per
second), Window size and Quitting.
Page 13
fi
Result
Page 14
Bibliography
https://youtu.be/QFvqStqPCRU?si=mTfJkS9YmHu6VsS8
https://github.com/clear-code-projects/Snake
Page 15