How I implemented Snake and Apple Game using PyGame in Python
By: Muhammad Saad Hassan
Tags: Python Game, Python3, Vscode, Github, Pygame

Game Main Screen

Install PyGame

Pygame is a cross-platform set of Python modules which is used to create video games. It includes computer graphics and sound libraries designed to be used with the Python programming language.

You can install it by using following command,

pip install pygame

For more information, check official documentation here.

Introduction to the Game

Building a snake and apple game in pygame and python. Snake and apple is a simple game where when a snake eats an apple it gets longer and your score increases. If a snake hits any part of its body it will die.

Resources

Project complete media resources can be downloaded from master branch of my GitHub rep here:

Project complete source code is available on my GitHub rep, you can check it here.

Note: Resources include all photos, videos, audio tracks and background music used in this project.

Classes

Now we have talked about complete game setup, so it’s time to crack the main code into three major classes, that will help us understand and manage the code well and make it readable.

So we will have these three class:

Snake Class:

In snake class we will have an initial functional call __init__ that will let the screen ready, will do all other initialization for us like sound, screen size, snake length, snake initial direction etc. This class will also contain functions that will ensure the functionality of snake like, drawing snake, snake length increment, snake move, snake moving directions (left, right, up and down) and snake walk function.

Code

Apple Class:

In the context of the game, Apple is food that a snake eats. Now Apple class has some initial look (photo) and Apple size declared in __init__ function. It has draw and move functions that support its functionality in the game.

Code

Game Class:

This is the main controller class of this game. It’s __init__ function will set up all the things required to start the game like all kinds of music/sounds, background image, snake and apple object, ready the surface to play game upon, and draw snake and apple on the surface. Some major functions are calculate and rate the collisions (Snake-snake collision, snake-apple collision), rendering background function, play background music and sounds upon collision functions, play game function that is supposed to be a main handler, run the snake in all directions function, reset score, display score, show game over function.

Code

Run-time Functionality:

This includes:

Score Count

Increment in snake length upon eating apple

Sounds on eating apple, hitting boundary and game over

Background music throughout the game

Replay option once game is over

Quit the game any time option

Conclusion:

So this is supposed to be an easy to implement snake and apple game using PyGame in Python if and only if you break the logic into small modules that are manageable easily and also easily replicate the same logic into clean and well looking code. I hope you like this blog, and if you want to get complete source code and media resources, feel free to download it from here.