Day 21 - Building the Snake Game Pt 2

Day 21 - Building the Snake Game Pt 2

Leveling Up the games functionality.


3 min read

Today's Objective

As I mentioned yesterday today we learned about class inheritance and how we're going to use it to build out the other classes to keep score of the game and to detect collisions with the food, the wall, and the snake's tail.

Class Inheritance

To sum it up: Class Inheritance is the ability to inherit the properties of another class in a newly created one. Imagine you have a class called Parent that has special features and methods. Now, you can make another class, let's call it the Child class, that can utilize everything from the Parent class and can add its own unique methods. So, instead of starting from scratch every time, we can reuse and extend the features of existing classes to make our code more efficient and organized.

To get this initialized you would have to call super().__init__() within the 1 __init__ function. I've added a code snippet to give a simple visual:

class Parent:
    def __init__(self, parent_attribute):
        self.parent_attribute = parent_attribute

class Child(Parent):
    def __init__(self, parent_attribute, child_attribute):
        self.child_attribute = child_attribute

confession = Child("I'm the Pappy!", "That's the truth!")


The above code will print:

This is very useful for minimizing the need to rinse and repeat for class and method creation.

The Project

So today we need to finalize the game by completing 4 objectives:

  1. Detect food collision.

  2. Create a scoreboard.

  3. Detect collision with the wall.

  4. Detect collision with the snake's tail.

This called for the creation of some new files and classes that can be found in the day-20's folder (since I'm not starting today in a new folder). Detecting the food and re-assigning it to a random spot inherited methods from the Turtle() class. We used that to create a new turtle and customized it to represent food. We created refresh method as well that moves the food to a random X & Y coordinate once the head of the snake was {number-value} away from it depending on what you set. The same thing goes for the scoreboard in which we created a new Turtle, but, had to dive into the docs again to alter it to work as a scoreboard. Finally, added the work to detect wall and tail collision to wrap up!

You can find the final project here -> If you want, clone the code from Day 20 and give the game a try yourself to see what high score your can get!


There's the game in all its glory! Gonna go ahead and find a way to throw this up on the App Store and start raking in secondary income ๐Ÿ˜‚. But all jokes aside, creating this game was pretty fun, and was actually able to apply a bit of what I learned at work earlier today ๐Ÿ˜.

Did you find this article valuable?

Support Kyle Leonard by becoming a sponsor. Any amount is appreciated!