Skip to main content

Command Palette

Search for a command to run...

Day 20 - Building the Snake Game: Part 1

Mastering the Digital Serpent

Updated
2 min read
Day 20 - Building the Snake Game: Part 1
K

Just a man trying to code my life into existence. Software Engineer at Dealer Inspire working with #PHP, #Python, and #JS. Journaling my experiences on new languages and technologies at blacknerd.dev

The Objective

For the following days (Day 20 and Day 21), the objective is the build the classic Snake game. Angela breaks down the goals for the and part one will consist of:

  1. Creating the snake body by creating 3 squares on the screen last a given starting point.

  2. Moving the snake forward without breaking.

  3. Finally, add the ability to turn the snake.

There weren't any new concepts discussed in today's lesson, but, looking at Day 21, we touch base on Class Inheritance.

Making the Snake

This project has to be one of the hardest I've tried yet. More so on all the connected parts than the lack of understanding of certain concepts. We built the snake in our main.py file initially but then migrated the snake build into its own file and Class.

Snake Class:

class Snake:
    def __init__(self):
        self.full_snake = []
        self.create_snake()
        self.head = self.full_snake[0]

    def create_snake(self):
        for position in STARTING_POSITION:
            new_snake = Turtle(shape="square")
            new_snake.penup()
            new_snake.goto(position)
            self.full_snake.append(new_snake)

After getting it created, we worked on the movement. This part is where I struggled a bit due to needing to work with X and Y coordinates. Trying to get the 3 squares to align like a snake and then move smoothly took some more diving into the Python Turtle Graphics doc. After the snake got moving, we worked on adding the ability to turn it, which was a little bit easier to grasp and code up. After a couple of hours of early AM work, finished part 1! The snake moves forward and can turn without turning on itself.

Video: Snake Game Part 1 Example

Since I'm working on more than one file for this project, I won't be posting everything I wrote today but you can find it in the Day-19 folder on GitHub -> https://github.com/kdleonard93/100-Days-Of-Code_Python/tree/day-20/day-20

EOD

That wraps up Part 1 of the Snake Project. Since I worked on this before my normal work hours, there's a chance I'll be posting Part 2 (Day 21) later this evening. There I will make note of the new concept Class Inheritance and wrap up my first full Python game!

100 Days Of Code - Python

Part 8 of 28

My journey of learning Python by going through a 100 Days of Code course. The main focus is for me to reflect on what I learned and have written summaries to come back to for future reference.

Up next

Day 19 - Instances, State, and Higher-Order Functions

From Virtual Sketching to Reptilian Races: Mastering Instances, State and Higher-Order Functions through 'Etch-A-Sketch' and 'Turtle Race' Projects