Home | Prices | Contact Us | Courses: Power BI - Excel - Python - SQL - Generative AI - Visualising Data - Analysing Data
Python Snake Course Lesson 4 - Functions
Back to Python Snake Course Landing Page
Lesson 4 focuses on writing functions, and refactoring (improving the quality) of our code.
We use the Python code at the end of lesson 3 as a starting point. Our objective in this lessons are that the snake moves at a constant speed in the direction of the arrow keys and cannot turn back on itself, and the game is over if the snake moves off the screen.
This is the agenda:
- a recap and explanation of the current code (as a reminder and a brief introduction for anybody joining the course for the first time)
- a short lesson to introduce writing functions
- improve the geometry to think of the snake as moving over squares on a board, rather than pixels. This makes the subsequent tasks easier.
- make our snake move at constant speed in the direction of the latest arrow key pressed.
- restrict the snake’s movement so that it cannot double-back on itself
- check if the snake has moved off the screen and end the game if so
- refactor the code and write a few functions: for example, to draw the snake, and to change the snake’s direction