Hi, Today we are going to build a simple but interesting snake game in python using its module named pygame. Pygame is a 2d game development module.
Let's discuss the code and some logic's to build this game.
We are going to use the latest version of python (when I am making this game it was python 3.9).
First of all, you have to download the Pygame module by using pip install pygme in your terminal. After downloading the module, open your desired IDLE and start coding the World's best game 'Snake game' (well jokes apart).
Now import the pygame module and after that initialize all the modules of the pygame module by using the "pygame.init()" function. Our next step is to create the display window of our game by using the display function.
Now we are going to give our game window a name or a title by using the set caption function of pygame.
After doing this we are now going to make various variables but as of now, we make only two variables, like game_exit and game_over these variables are of boolean type, and their initial value is true.
We are going to code the main part of our game, which we are going to call game loop. For making a game loop we are going to use a while loop. Inside of the while loop, our main coding is going to happen. Now inside the while loop, we are handling all the events which are going to happen(In the case of the pygame module we can understand events like we press any key or using our mouse all this thing is known as an event).
For better understanding, we also print the events by using the print() function of python, later we comment it out or erase it.
pygame.quit() and quit() are also functions and are used to close the game window.
Now we are going to handle two events first one is pygame.QUIT and the second one is pygame.KEYDOWN. The first function is used to quit the game and the second is to detect that any key is pressed or not.
Adding color variables and using white color in the background color of our game window by using the .fill() function of pygame. Note after any changes we have to update our changes by using the .display.update() function of pygame. So, we simply write it at the bottom of our while loop or game loop.
Adding coordinates of initial snake position, and creating snakehead by making rectangle by using the .draw.rect() function of the pygame, this function is also written in-game loop or while loop.
Now we are going to add a clock to maintain the fps(frame rate per second) in the game by using the .time.clock.() function. After doing this we add 4 more events in the loop to handle the movement of the snake in all four directions.
As we all know that in a snake game, the snake is moving so that now we have to give velocity to our snake so that it can move. (In the below code snake will not move continuously when we press a key then only snake movies a step.)
Now in this code, our snake moves continuously.
These variables will help us to create food for snakes.
Now we add a random module so that our food will plot at a random place in the game window.
This will make a rectangle for food of red color.
As we all know that when playing any game it will show a welcome screen before the actual game starts. In the below slice of code, we make a welcome window.
Now our almost game is completed. Now we have to add some more logic like:-
1. Increment of length in snake, when our snake eats food. For doing this we use the .append() function of python.
2. Increment score when our snake eats food and print in on the game window.
3. Handling if snake colids with his own body.
4. Show a high score on the game window and store it in a file and update if someone will break the high score.
5. We can also add music to this game. When our game is over then there is a funny sound that will play after the game is over. (Rahul Gandhi's voice, "bye-bye khatam ho gya goodbye"). In case you want to change the sound you can do it very easily. Simply delete the "khatam" file and paste a new song or sound and make sure that its name is the same as the earlier file's name.
6. You can also use cheat code by pressing the 'y' key and it will increase your score by 10.
7. For resetting the high score to zero '0', Simply delete the high score folder.
Enjoy the game. Thanks