zsmb's prog site

Snake

  2015.01.26.

Introduction

This game hardly needs any introduction, but I'll give a short backstory to it. A fairly large amount of people chose this classic as their homework, which made me give it some thought too, this is the result. The whole project was about one day's work.

Description

There are two interesing mechanisms in the game, one is moving the snake, the other one is changing directions.

Moving the snake is the simplest thing if you can do linked lists, my solution for it was the following: delete the last item of the list on every timer event, add a new item to the beginning of the list at the same time, it's position relative to the previous first item determined by what direction the snake is moving. This makes the snake go forward. I added the exception not to delete the last block if the snake's head's coordinates match the coordinates of the food, which makes the snake grow as expected.

As for direction, the game takes every keypress, always puts the last one in a variable next_state. On every timer event it determines whether the next_state at that moment makes sense (for example, it can't be the exact opposite of the current direction), and then changes the state variable to whatever's in next_state if necessary.

I've provided the source code for it, although it's not exactly pretty, mostly due to my lousy integration of the multiplayer mode.

Features

The game includes single- and multiplayer game modes, and has a couple settings in the main menu. These are pixel size (adjust with up and down arrows) and speed (left and right arrows).

The game also keeps track of the high score (one high score for all game modes), which it simply stores in a text file.

Downloads

Downloading the zip file that contains the executable results in a warning (at least in Chrome) as a security measure, but you can choose to "Keep" it.

Name Description Size Date
snake.c Source code 24.1 KB 2015.01.26.
snake.zip Executable with dll's 282 KB 2015.01.26.