Writing About Snake Full Circle
One Sunday in May, I implemented a basic version of the classic game Snake in C++. Over the course of the following week I improved the code and added features suggested by Lana, who also created resources in the form of a font for drawing text and icon design. It runs in both Windows and on Mac.
The final version of the gameplay involves the collection of coloured boxes, and a segment of this colour being added to the tail of the snake to a maximum of 100 collected segments. Only one coloured box is displayed at a time and when consumed, another is generated with random position and colour. The snake always starts life as two pink boxes; Lana chose the palette from which the boxes’ colours are selected.
Before I began my next project, I made the decision to refactor this one. I made good progress at first, and then let it fall by the wayside for several weeks owing to a combination of laziness, awesome weather and general apathy toward refactoring. However, I’ve now got round to finishing it and releasing the final version two months down the line.
The final character set was implemented as 45 boolean arrays, each comprised of 35 monochrome pixels which Lana painstakingly mapped out, including numbers and punctuation (the girl even whipped up the @ symbol!). As image loading isn’t something I wanted to cover with this project, I avoided using an image map, and this approach allowed me to scale the text without aliasing and gave the whole thing an old school feel. Coming up with a text alignment system was tricky too.
Initially the game had involved two states - playing and not playing, but as development continued Lana wanted a starting screen which was separate to the “you’ve died” screen. With the ability to complete the game came the requirement for a “congratulations” screen, and finally I added a “paused” state.
The project also proved interesting as I wanted to make sure it ran in Windows and MacOS. Being primarily a Java dev by trade I don’t have to worry about compiling code on different platforms, and there were several occasions where the code behave perfectly on either of my chosen platforms and fail spectacularly on the other. Both platforms required different project formats so I had to learn how to use both XCode and Visual Studio a little better, as well as working out how they handle things like icons.
This has been a hugely interesting learning experience for me and it was nice to develop a game (even if it is just Snake!) in its entirety. Having Lana to collaborate with and challenge me was incredibly useful as lately I’ve tended to put coding to one side when I get home from work. Above all, it’s been amazing fun and has instilled in me the desire to make more things. As such, my refactored code base, while still juvenile and flawed, will serve as a great start for my next project - Tetris.