Source: www.edureka.co
A quick tutorial on how to build a Snake game using the PyGame library.
This will be broken down into 7x steps, where in each step I will walk through how to create aspects of the game - from building the screen, moving the Snalke to displaying the score.
- Step 1: Create the screen
- Step 2: Create the Snake
- Step 3: Moving the Snake
- Step 4: Game over when the Snake hits the boundaries
- Step 5: Adding the food
- Step 6: Increasing the length of the Snake
- Step 7: Displaying the score
To be able to use the PyGame library we have to install it on your machine.
pip install pygame
These will be the PyGame functions that we will be using along with the descriptions of what they do.
init()
This will initialise all of the imported PyGame modules. It returns a tuple telling the developer success/failure of initialisations.
display.set_mode()
Takes a tuple or a list as it's parameter to create a surface (a tuple is preferred).
update()
Updates the screen being used.
quit()
Used to uninitialise everything.
set_caption()
Will set the saption text at the top of the display screen.
event.get()
Returns a list of all events.
Surface.fill()
Will fill the surface with a solid colour.
time.Clock()
Helps track the time.
font.SysFont()
Will create a PyGame font from the System font resources. Once you have installed PyGame and read through the functions, you are ready to code!