Making a Snake Game with PyGame

Rahul Barman
CodeX
Published in
3 min readAug 21, 2021

Snake was an iconic game which still gets its fair share of remakes for newer generation of mobile phones. This is because making a snake game is fairly simple but is still fun to play mostly because of nostalgia. So why not make our own snake game.

Making a snake game in python is simple but with pygame most of the work is already done, like making the board and printing the snake and updating the game every timestep. PyGame is python library which is used to make simple 2D games. It is one of the most famous library for making games in python. It can also be used to make environment for Reinforcement Learning agents.

So lets get started. First lets create an environment, activate it and install pygame in it.

Ok so we are all set for making the snake game.

OK I see this is too much of code at once, but this for those who only wants the code and don’t want the explanation for it.

Now lets break the code down to simple blocks.

First is the initialization of some of the parameters. The colors dose not have to be initialized but as they are being use multiple times I initialized it so as to make it easier to set them whenever required. The ‘x’ and ‘y’ are the coordinates of the snake. At start it is set to the middle of the board. The ‘foodx’ and ‘foody’ are the coordinates of the food of the snake. The position are set to random points inside the board. The equation is to set the position of the food away from the snake but inside the board. Although a simpler equation can be applied but it sometimes spawns the food right above the snake. The length of the snake is initially set to 1. And finally a clock is initialized to update the screen. It can be said as maintaining the fps of the game.

Now lets look at the main play function. First we draw the board and caption the display, i.e. set the title of the game. Then we run a loop to play the game until the player loses or presses the exit button. Pygame provides some very useful methods to recognize when a key on the keyboard is pressed along with which key has been pressed.

The snake is moving in a 2D space of it can either go left, right, up or down. Similarly its position will be changed as (-x,y), (+x,y), (x,-y) or (x,+y). That is what the if statements are checking. it will move in the direction that the user pressed by a unit of snake_block which is initialized above.

Now we set the new position of the snake after every timestep and redraw the board. We also check whether the head of the snake touches the body. If it does than the game ends. Also if the head is in the food’s position a new block is added to the body of the snake making its size bigger. Also if the head touches the food, the food gets erased and a new one spawns at another random location away from the snake.

Also to the game a little harder, I made it so that if the snake touches the border, the player looses.

These are two small functions to draw the board and refresh the board after every timestep.

And finally we make the snake game and play. Voila!! the game is finished. Although a full fledged game can have more logic and equations in it, this game still does all the basic functionalities. Also its quite fun to play our own made game.

Please check out the full code here. Thank you for reading.

--

--

Rahul Barman
CodeX
Writer for

I am a graduate from MIT School of Engineering with Bachelors in Computer Science and Engineering. I love making projects and reading books