Data and AI Training

Home | Prices | Contact Us | Courses: Power BI - Excel - Python - SQL - Generative AI - Visualising Data - Analysing Data

Python Snake Guide

Here is some useful information when building the snake game in Python.

Use of Constants

We will use a lot of constants to define the screen size and width, the snake colours and so on. Constants are variables that are never intended to be changed once they are created. Python programmers uses upper case letters to name a constant., for example

SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480

Geometry

The snake moves on a two dimensional screen in the x, y plane. The top left corner of the screen has co-ordinates (0,0) and the bottom right has co-ordinates (SCREEN_WIDTH, SCREEN_HEIGHT).

+---------------------------------------------------------------+
| (0,0)                                                         |
|                                                               |
|                                                               |
|                                                               |
|                                                               |
|                                                 (SCREEN_WIDTH,|
|                                                 SCREEN_HEIGHT)|
+---------------------------------------------------------------+

Best to think of the screen as a grid of squares like a chessboard. The squares will have length of GRID_SIZE. The snake will move by GRID_SIZE on each step.

Note that the origin (0,0) is at the top left rather than being at the bottom left as you might expect. The y values of a position increase from top to bottom.

The snake geometry

The snake has a body which is a list of grid squares. Adjacent squares in the snake need to touch each other either the top side of one to the bottom side of the other, or the left side of one to the right side of the other. The first square is the head of the snake. The last square is the tail of the snake. The snake moves in the direction of the head. The tail follows the head.

The squares will have length of SNAKE_SIZE. The snake will move by SNAKE_SIZE on each step.

For convenience GRID_SIZE will be a factor of SCREEN_WIDTH and SCREEN_HEIGHT. That makes our geometry calculations easier - there will be an exact number of grid squares on the screen.

Functionality in scope. Rules of the game

The snake can move in four directions: up, down, left, and right. The snake moves based on the user pressing the arrow keys (up, down, left,, right) The snake can also change direction but cannot move in the opposite direction of its current direction. For example, if the snake is moving up, we can its direction to be left or right but not down. The snake can only move in the direction of the last key pressed. The snake can only move in one direction at a time.

At the start of the game our snake will be three squares arranged horizontally and positioned in the middle of the screen

In the early stages of development, we will implements our snake will be a single green square. It will change colour when the user presses the following keys

If the snake moves off the screen, the game ends.

The screen also shows some food (a red square randomly placed on the grid). The objective of the game for the snake to eat as much food as possible before the game ends. The snake eats the food when the head of the snake and the food are on the same grid square. When this happens, the snake grows by one grid square and new food is randomly placed on the grid.

Functionality out of scope for the foundation lesson

We won’t keep a score of how many times the snake has eaten food.

When the snake goes off the screen, the game (and program) ends. We won’t set up a new game and reset the score

The pygame module

pygame is a Python module designed for writing video games and multimedia applications. It provides functionality for:

Apart from understanding the main pygame loop and the geometry we don’t need to know a lot about pygame.

Step By Step Approach

We build our snake game in small steps, running the game as a test after each change.

We will take care to keep our code as simple and readable as possible. This will often mean refactoring our code. (Refactoring makes the code simpler without changing the functionality.)

Step - Understand the boilerplate sample code

We start with some sample boiler plate code to set up the pygame environment and show a screen. We will work through this code line by line to understand how it works.

Step - Draw the grid squares on the screen

Even though we don’t typically see a grid on the screen, this will be helpful to understanding how our snake moves later.

Step - Create a snake as a single square in the centre of the screen

Use constants to set up the (starting) position of the snake

Draw a green snake, comprising of a single square, on the screen.

Step - Make the snake change colour depending on a key press

Although this is not part of the typical game play, we do this as a simple way of detecting and acting on key presses with the game loop.

Refactor Step - Create a dictionary to map keystrokes to colours

Rather than have many if-elif clauses, one for each key - colour combination, we will create a dictionary to map the key pressed to the corresponding colour and use this.

Step - Make the snake move based on the arrow keys

We make the snake move up, down , left right based on the corresponding arrow keys. Each keystroke moves the snake one step. We do this in a simple way, not the final way of how the snake moves, based on the rules above.

Refactor Step - Create a module to hold the constants

Our game will end up using many constants - screen dimensions, colours, keystrokes etc. We will organise these into a separate module and import specific constants into other scripts as necessary

Refactor Step - Create a class for the snake

We will create a Snake class so that we can keep all the details specific to the snake in one place. We will ensure that the Snake class does not depend on any pygame functions so if we later want to replace pygame with a different game module, this will be easier.

Step - Extend snake to a set of squares

Improve the functionality of the Snake class so that it

Add some food

Create a food item (a red square). Place on the screen at a random location on the grid.

Implement functionality when the snake eats the food

Detect when the head of the snake is on the same grid location as the food. If so: