Data and AI Training

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

Python Foundation (Snake Game Version) Course

python snake game

Who should attend

Anybody who want to learn Python. This course focusses on the features of the language itself. It hopes to do this in a way that is more fun and interesting than a more traditional approach, and so possibly more relevant to people who have not coded before

Course Length

This is run either as a 2 day courses, or as a series of six 2-hour sessions.

Learning Objectives

This will introduce you to Python and give you the opportunity to practice many of the features of the language: variables and constants, writing functions, creating and using classes, data structures, importing packages, looping and branching.

Pre-requisites

None. This is a beginner level course.

Course Content

We will learn Python by writing the classic game of snake. This may seem a frivolous exercise compared to a more serious use case of, for example, data analysis. However, writing a game like this one forces us almost immediately to use many of the features of the language. We start by creating variables and constants to set up our screen and snake. We implement our snake firstly as a Python data structure (a list of tuples) and use list methods such as append and pop to make our snake move. To keep our code organised and readable, we create a Snake class so that our snake can move, hiss and eat. We use looping, (with the for and while keywords) so that the snake responds to our key presses and conditional flow (with the if keyword) so that it eats and grows when it finds food. We learn how to install and import packages.

We also use AI tools occasionally to help us with the grunt work of getting the exact correct syntax we need. But the hard thinking how to design and implement the game is up to us.

One of the good things about this case study is that it is always apparent when things have gone wrong e.g. our snake refuses to move or grow, or disappears off the screen. Testing involves playing the game a lot!

If run as a series of weekly sessions, the order is roughly

Session 1 - Setup

Install Python and the VSCode editor on a laptop, then create a Python environment, install some packages and write/run a tiny script to prove everything is set up properly.

Session 2 - Set up the game loop

We create the main code to run a game (using a popular Python package, pygame). We create our first version of the snake - which is only a single square and does not move yet.

Session 3 - respond to user input

We get our snake to change colours and to move up / down / left / right when the user presses the arrow keys on the keyboard.

Session 4 - improve our snake (part 1)

Our snake takes shape and behaves more like the classic snake in the game.

Session 5 - improve our snake (part 2)

The snake easts food and grows.

Session 6 - applying the rules

The game ends when the snake leaves teh screen or collides with itself. If time allows we may implement a score.

Joining Instructions

Before the course, attendees need to install Python and VSCode on their PC/ Mac. This course does need a local installation of Python. A remote installation such as Google Colab or GitHub workspaces will not work.

Here are the instructions.

Download Python from https://www.python.org/downloads/ and install on your PC/Mac.

Download VSCode from https://code.visualstudio.com/download and install on your PC/Mac.

Launch VSCode then:

  1. install the Python extension,
  2. create a new project and folder
  3. create a new .venv virtual environment for this project
  4. create a terminal window and type “pip install pygame” (This is the package that contains some basic functionality that we need)

Copy this Python file into the project folder.

Run the Python file. In the terminal window, type “py snake_game.py”. This should open a new window similar to the snapshot below

Close the window. You are now ready to start the course.

There is a guidance page of useful information before the course here.