750bytes Snake game [ August 02, 2003 ] by Mattias Stridsman, alias Strille
Strille has created a simple game that possibly is the smallest snake game ever created! It just takes 750 bytes (yes, it is less than 1kb!). Very fast to download, indeed. We've asked him to explain us something more about his ultra-small game.
I don't know why, but I just thought it would be a fun exercise to see how small I could write a fully functional snake game in Flash MX. And by small I mean the file size of the swf file. The rules I set up from the very beginning was:
The playability should not suffer in order to keep the file size down.
I could only use 100% ActionScript. No pre-drawn graphics on the stage, no symbols in the library.
Many snake games in flash that I've played are poorly written regarding the control of the snake. If you make two or more consecutive turns very fast you often only end up making one turn (the old key press is "overwritten" by the new one).
The solution I've implemented in my version is very simple; store all key presses in a queue and then perform one on each frame. This gives you great control of the snake.
Because the graphics only consists of a rectangle as a background, squares to represent the snake and the food and a text field to show the score in, it is easily created with ActionScript. It would actually be a bit less efficient to have those made into symbols from a file size point of view.
In order to keep it small I've tried to use as few variables and functions as possible. In this version, there are two functions (anonymous, they don't have names to save some bytes) and 7 variables. Unfortunately variable name length matters, so all variables are named using a single character.
The source code can be a bit difficult to grasp at first because several things are made at the same time, and some things in the syntax looks a bit strange, but in pseudo code, the game looks like this:
Create the background and a text field with the instruction to press space to start.
Define a function to run when a key is pressed:
If one of the arrow keys has been pressed, add that key to the turn queue and exit function.
Setup a new game and define a main function to run on every frame:
Take a key press from the queue. If it's a new direction change current direction.
Move the snake to a new position depending on direction
If the new position is on the snake itself or outside the gaming area then show "game over", exit
Draw a block on the new position
If there's a food block on the new position, increase score, place food on new position and set eat counter