![]() |
That's the look of utmost happiness |
The Python Snake game project was my Grade 10 Final Project submission. Back then it was just a rudimentary shell of what it is today. It only implemented the single-player mode of the game, and it didn't have any of the bells and whistles it does today. So here's my final post on this topic.
Initially, I didn't plan on working on this project. I didn't feel like creating a game for the second time and decided I wanted to do something with a real-world value.
My plan was to expand on the idea of the COVID Calculator I had mentioned in one of my previous posts. With all this stuff going on in the world right now, what could get more 'real-world' than a program that predicts the rate of contracting the coronavirus? I wanted to make it an actual thing with all the bells and whistles I had mentioned in that earlier post.
To handle the algorithm, I needed someone better than me in Mathematics to gather COVID stats and formulate a rough equation of sorts to input all our variables into. I phoned my friend and he seemed eager to work with me. But he had a different idea. He suggested we work on project related to Fourier Transforms.
Now, Fourier Transforms is a mathematical concept that is used in a variety of fields, but the most simple and widespread use of it is in acoustics and sound design. Fourier transform essentially allows you to input sound samples with mixed frequencies and output separate frequencies. It allow people to identify the different frequencies in a mixed sample.
I thought the idea sounded splendid and immediately abandoned my COVID Calculator. We did some research, and I was able to find some really convincing articles that used Python to implement Fourier Transforms. Unfortunately, my friend pulled out of the project almost a week after we had finalized all our plans.
He felt that the project was eating up too much time and he'd prefer using that time to study for exams. So we went our separate ways and I decided to continue with the project.
Funnily enough, the code was the easiest part of the project. I finished it in about two days and it all amounted to basically 10 lines of code.
I set the project aside, deciding to return to it sometime closer to the deadline. That's when the incident happened.
Imagine this: I'm sitting in my room; my blanket is draped over my legs and my laptop is resting on my lap on top of the blanket. My hard drive is connected to the laptop and I'm currently in the middle of a distro-hop (sneak peek). My dad calls me, and I set aside the laptop and swing the blanket off of my legs to free them. The hard drive which was resting on the blanket gets flung off the bed and it crashes hard on the ground.
Total silence.
I rush to pick up the hard drive but the damage has been done. There are no physical impairments, but the hard drive is no longer detectable when I connect it to my laptop.
Why was this hard drive so important? Because it held everything. My coding projects, my media library, my photos. Everything gone due to a freak accident.
I tried for a week. I know hard drive recovery is an actual thing, but I wasn't going to pay that much for the stuff in the drive. My media library is disposable (pirated movies and cartoon shows), my code was re-writable (I wrote all of that, so I can write it again), but my photos was the biggest loss. Some of that stuff I don't think I will ever recover, but I'm pretty sure most of it is backed up somewhere anyway.
After that, I lost all interest in the project. Sure, the code was only 10 lines and really easy to copy from external sources write, but I didn't feel like I liked what I was doing. So, I took one long look at the project, and dumped it.
I cloned the Snake game repo from my GitHub profile and sighed. There's gonna be a lot of work to do.
After two weeks of sleepless nights and repetitive coding, it was done. I proudly submitted my project and slumped back in my chair. shoot, now I gotta study for the upcoming exam.
Now that that's all behind me, and after that super long and frankly, irrelevant intro, let's get into what this baby can really do:
If you remember my earlier post on this, you would remember that I got stuck on the project when I tried implementing the multiplayer feature in the game. Well, I'm glad to say, that's all taken care of. This project really showed me how much I've matured as a programmer. I remember back in Grade 10 when I went around frantically asking anyone to look at my code and solve a problem I could've solved on my own.
Debugging is an important skill for programmers in a field where errors are to be expected and perfect code is unexpected. Sadly, Grade 10 me was an idiot and didn't know what to do.
When I left the game, I had created additional attributes for the snake class that allowed you to input the direction keys to be used with the snake. This allowed me to create two different instances of the snake class, and thereby, two different snakes. But the two snakes weren't behaving as expected.
As I found out later, the reason why I was getting unexpected results was because of the sequential order of the commands. The key presses for the second snake could only be registered if the keys were being pressed for the first snake. I solved this by extracting the movement algorithm out of the snake classes and making it into its own thing. Then I had a loop in my main function that checked for key presses instead of it being placed inside the snake classes themselves. This allowed for the key presses to be registered independent of the movement of the snakes.
The second hurdle I had to cross was integrating a 'Computer-Mode' in the game. Essentially, it was an algorithm that allows the computer to control the snake. I didn't do much for this part of the program tbh. I had initially planned to integrate path-finding in my program, but I decided to chuck it and just implement an algorithm that identified the position of the snake-head and the fruit, and kept moving the snake in ways where the snake-head's co-odinates became closer and closer to the fruit until it ate the fruit.
The consequence of this basic algorithm is that the Computer-Snake is too fast and efficient, until it gets long enough to bamboozle itself and die. So I lovingly named the algorithm 'Edward' and I openly challenge anyone to win a game against him.
There are four modes of game-play in my game:
1) Single-player Easy : The standard snake game we've all come to love.
2) Single-player hard :
This game-mode adds four boundaries so you can no longer wrap around the screen. It also makes the snake progressively faster as it gets longer.
3) Multiplayer Co-op :
Two snakes controlled by two players. One snake follows the arrow keys while the other snake follows the WASD format. The game ends when you accidentally run into yourself or run into the other player. You win the game by having the most points when the game ends. I'm aware this creates an unfair advantage for the player in the lead, as it will just lead him to committing suicide to win the game, but hey, life's unfair, even in snakes.
4) Multiplayer Computer :
You go head to head with Edward. All the same rules as Co-op apply here, except you're going against an algorithm instead of a human player. Edward is fast and efficient, but he's also stupid at the moment. He currently can't detect where his body is and will absolutely run into himself if it means he's in the way of getting to the objective. No matter what, Edward will win because he will always emerge with the most points. You have one advantage though; he's just an algorithm, but you're a human. He doesn't realize that the screen wraps around itself so you use that to your advantage and get to the fruits faster.
Other than these four modes, I also did a lot of SQL programming. You see, my Grade 12 final project required me to create a program that utilized the python-MySQL connector module, so I had to find a way to shoehorn it in somehow. The only logical place to put it in was the leaderboard. In my game, you will be prompted to enter the details of your MySQL login so the program can access your account and save names and scores into a leaderboard. The game still works without MySQL installed on your system, but it will just error out when the game ends instead of the smooth transitions I had put in place.
Speaking of transitions, there's a lot of cool aesthetics in this game that I'm super proud of. The first one I want to talk about is the dot pattern that appears on the game screen when playing the game.
Ik it doesn't look like much, but you have no idea how much I like this aesthetic. I was so proud of myself for implementing it; almost to an illogical degree. It was one of those things where I actually applied my school education about nested loops into practice and I did it all by myself without having to look it up anywhere. It felt so good.
I picked yellow and blue as my game colors because that's what the Python logo had. I decided to commit to the joke and make the game an actual 'Python' game.
I wanted to use an apple sprite to replace the red block, but the process proved too difficult and I decided to just adopt the blocky art-style my game had. I imported a specific font that emulated 8-bit video-game font and the rest was set in place.
Like always, I set up an introduction screen, a main menu, an instructions screen for all four game modes, a leaderboard screen etc etc.
I also implemented game sounds. I had to do a lot of searching to find the right sounds, but I'm pretty satisfied with what ended up getting put into the game.
(Pardon the resolution, these were ripped out of my Project Report and the formatting didn't work well. I could relaunch the game and take new screenshots, but I'm too lazy to bother, so you're stuck with this low-res stuff.)
![]() | ||
Pretty classy start screen..... |
![]() |
Single-player select screen |
![]() |
Single-player easy instructions screen |
![]() | |
Singe-player game screen. I love those dots so much. |
![]() |
Game over : Score display screen |
![]() |
MySQL account details input |
![]() |
Game over : Final screen |
![]() |
Multiplayer Game Screen |
![]() |
The Leaderboard |
The game can be downloaded from here.
In the future, if I'm really that bored, I might return to this project to do either of two things:
1) Improve the Edward algorithm and possibly implement actual path-finding or AI to him, although I'm pretty sure I'd rather just let the new AI implementation be its own thing.
2) Package the game into an .exe file, so that non-technical people can play the game without having to learn scary phrases like 'clone the repo' and stuff. This would require removing the MySQL implementation from the game, which should be easy enough to do, since I hate query programming.
I'm glad you got to finish it after all
ReplyDelete