One of the most fun things about Python is its versatility. You can do just about anything with Python, including making a snake game! Today we’ll build a simple snake game from scratch with the following components:
Create a window
In order to create a window, you’ll need to import the python library for creating windows. Then, you can create a new window by calling the function pygame.display.set_mode(). This function takes one argument: the size of your window in pixels (width and height). In this case, let’s say we want our game to be 300px by 300px.
Once you have created your main window object, there are several other properties that can be set on it: title bar text (termed as “title bar text”), screen position (termed as “screen position”), background color(termed as “background color), foreground color(termed as “foreground color)
Draw the snake
You will now be using the “import” command to load Python modules into your program. The first module you will import is pygame, which contains all of the functions necessary to draw graphics on the screen and respond to user input.
Next, we’ll import random so that we can randomly generate colors for our snake’s tail.
Next, we’ll import time and sys so that we can use time functions (such as sleep()) and system information (such as which operating system you’re running).
We’ll also need math so that we can use sin(), cos(), etc..
Finally, os allows us access to files and folders on your computer. In this case it’s just going to be used so that we have somewhere else besides our current working directory (the directory where python is being run) where our images live!
Create the snake’s food
First, you need to create a list of food items. Each item should be randomly assigned a color, size and location on the canvas. The speed with which the food moves across the screen should also be randomized.
Here’s how that looks in code:
food_list = 
for i in range(0, NUMBER_OF_ITEMS):
# Create random size for food item (1-10)
size = random.randint(1, 10)
# Create random color for food item (red, green or blue – 0-255)
color = random.randint(0, 255)
# Create random location x coordinate (between 10 and 100 pixels from left edge of canvas) xcoord = random.randint(10, 100) ycoord = random.randint(-20 + 50 * size/10., 20 + 50 * size/10.)
# Create path object to draw image onto canvas at specified coordinates path1=Path() path1.moveTo((xcoord , ycoord)) # Add it to our current list of paths path2=Path() path2 .lineTo((xcoord , ycoord)) .curveTo((xcoord+10 , ycoord-20), ((xcoord+70 , y coord+80)), ((xcoord+100 ,ycoor d+(50*siz e/ 1 0).c urv eT o((xi coo rd-(50 *siz e/ 10).cur v eT o((xi codesin ge ttofbu rn ingandtheflamesflic keringamongtheruins .l aneofth esh elteri ngfirest lapedsidewa sterectheabandonedbuildingsa ndcrumbledg roundswithoutb reathtakingt ravelers’effortsto c ontinuetravelling
Create a scoreboard
After the snake game is over, we want to be able to see how well we did. We can do this by creating a scoreboard for each player that displays their score. There are several ways you could implement this:
- Use a list to store the score.
- Use a dictionary to store the score.
- Use a class to store the score.
- Use a text file to store the score (for example, if you’re using Python 3).
- Store your scores in a database using SQLite or another relational database system like MySQL or PostgreSQL
Check for collisions
You can use the ‘colliderect’ function to check for collisions. This function takes in two rectangles as arguments and returns True if the collision between them was detected, or False otherwise. You can also use a list of rectangles to check for collisions with this function by calling it with an iterable argument (i.e., something that acts like a list).
If you’re using Python 3, there’s also another way to do this in this case:
In : import pymunk
In : collisions = pymunk.colliderect(snake_body_shape)
In : collisions Out: True
In : collisions = pymunk.colliderect(snake_body_shape), snake_head_shape)
Move the snake in certain directions
The snake will move in a straight line until it hits the wall.
In this case, we need to know if the snake can move in a certain direction (eg: up or down), or whether it has to stay on its current tile. We’ll use Booleans for these options (true and false).
You can make Snake in Python.
If you want to make a Python snake game, here are some steps and components to help you on your way.
- You’ll need the following: Python, Pygame, and the ability to use Google or other search engine of choice.
- Start by making the snake code (listed below). Then add in all the logic necessary for movement (e.g., left/right keys), collision detection, etc.
- To make things fun, try adding in some exciting graphics for when your character eats fruit or something like that!
If you have any suggestions or questions, please leave them in the comments below. Good luck and have fun!