How to make snake game in python
We make snake game in python with help of pygame module. pygame module helps in making games in python .
LETS START CODING
Import pygame and random
we have to import pygame and random module for making snake game. Random module helps to generate random position for food in the screen. for this we have to write
Creating game window
First we have to make game screen with the help of pygame. For this we have to write
Creating welcome screen
First we have to display welcome screen. For display screen we have to make a function . For this we have to write def welcome():
Creating snake
After that we make a function which take gameWindow , color , snake length , snake width and plot the snake .For this we have to write
NOTE - we take snk_list to increase the length of snake .
Moving the snake
After making the snake we have to move the snake . For this we use key event in the KEYDOWN class which is in pygame. We use K_UP , K_DOWN, K_LEFT , K_RIGHT to snake move up , down , left and right. After that we create velocity_x=0, velocity_y=0initvelocity=5
For make snake move we have to write
Game over when snake hit the boundaries and itself
When snake hit the boundary and itself then game over and game over screen open and print the score . For this we have to write
After game over a game over screen open where score are print
Adding food and score
For adding food we have to plot a rectangle in game screen. For this we have to write
When snake eat the food then add 10 score and also add snk_lenght which used to increase the size of snake . For this we have to write
Increasing the length of snake
To increase the length of snake, we have to make a list where then append position of snake in each frame in snk_list list . After that when snk_list more then snk_lenght , Then we delete the last element of list . For this we have to write
Game over screen
When snake hit the boundary and itself then game over window open and display the score . For this we have to write gameWindow.fill(white)
Here text screen is a function . We use text screen function to display the text. For this we have to write
HERE ARE THE CODE
OUTPUT