Data and AI Training

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

Python Snake Course Lesson 2 - Conditional Flow

Back to Python Snake Course Landing Page

Lesson 2 focuses on conditional flow in Python - using the while, for and if-elif-else keywords. It also introduces variables, constants, datatypes, the input() and print() functions.

We use the Python code at the end of lesson 1 as a starting point. We will create the main game loop so that our snake game can listen to events, act on some of them, and only exit when the user presses the close button on the window.

In this session, we make get the “snake” (still a single square) to change colour when certain keys are pressed.

It’s worth having a read of our guide to how we build the snake game, especially the section on colours and geometry.

The Python scripts are in the course GitHub repo in the lesson-2 folder.

Here is a YouTube video that summarise the lesson. It is 13 minutes long.

In this lesson, we edit two files. These files both start with a comment section that describe the steps that we will follow in the lesson. These files are:

snake_game.py is the code at the end of lesson 1. This code initialises the pygame package, shows a window for a few seconds, then exits. It was the minimal amount of code to check that we had set up our Python environment properly.

learn_conditional_flow.py is a separate script to learn how to use the three conditional flow code blocks (while, for, if - elif - else) before we apply them to the snake game.