Developing a Snake game


The point quite a while a go was for me to update this blog more often publishing my current progress in basically everything. I did not have a lot of knowledge from running a blog previously but I have learned that its not that easy keeping it up to date while at the same time learning about new technologies and programming in general.

I have for the last 3 months planned that I should go to university this year. I am taking a test to increase my chances to be accepted at the 9th of April. I have been studying math and looked at previous tests now for quite i while but I still feel like Im not on the top of my game, I guess i will have to stay at it and just try learn as much as possible and then do my best at the test date. While my current biggest focus is going to school i have also been learning more programming on my own. My latest project was to develop a Snake Clone. So for this project the idea was not for me to learn a lot about fancy graphics or game development but to learn how to implement Algorithms into a product as well as I know some Math could be implemented in a project like this and my math is not my top subject so why not practice a bit of math as well?

The Math In Snake

So first of all i decided to create a Snake clone were you can turn 360 degrees instead of just 90 degree angles. This gives the game something fresh and new at the same time as making it more complicated for the developer.

So math in games are ALOT about Trigonometry. The good part is that you do not have to be a math genius to know trigonometry and you can get very far with just good base understanding and some common sense.

So you might have not thought of it before but games is full of triangles. If we woul imagine a 2D game right now where you have your character on the screen together with an enemy. Lets say we would like to calculate the distance between theese two objects (The Player and the Enemy). In your code you have the X and Y cordinate for both objects so what we could do is basically draw a line between the objects. But how do you know how long that line is? Well simply draw a line from the center position of each object together with the first drawn line to form a triangle.


Notice also that the triangle we have drawn have a 90 degree angle. When you have this kind of triangle in your game you can easily start expressing yourself using some predifined math functions.

First of lets look at how a triangle is built:

Something to remember is the the hypotenuse is always the longest side, that will help you spot it out fast. Then just place in the opposite and adjacent sides in the target triangle.

The cool thing with trigonometry is that you only have to know 2 parts of the triangle then you can easily find out the value of the other by using the sine, cosine and tangent functions. For example you know the angle together with the length of one side:

sin(angle) = opposite / hypotenuse

cos(angle) = adjacent / hypotenuse

tan(angle) = opposite / adjacent

There is a great deal to learn when it comes to Trigonometry for games and i recommend you to first read up on some just normal math about trigonometry then move on to applying it to your code. I have previously tweeted about one resource i found out being great

What i did in my Snake Game

So we got some background on how trigonometry can be helpful but what parts did i use for my game? What i had to do when i developed my 360 degree snake was to find out how to always more the Snake “Forward” what i mean with that is how to move the Snake in one direction depending on the current angle. We can already here spot that we have something we could use together with the functions we talked about before (angle).

When i made the game it wasnt just as easy as popping in some values into a function, the reason was that the Library i used (SpriteKit) has their own rotation code for their Node objects and it is being calculated in Radians from ~3.141 Radians to ~-3.141 Radians (Both is about 180 degrees). It was not just the issue with it being calcualted in Radians but it goes from a positive to a negative number. So how i solved it was to create my own new angle variable and adding or subtracting the rotation speed to both values both my own created Angle variable and the SpriteKit implemented rotation value.

Lets briefly look at some code:

newXPosition = snake.position.x - sinf(DEGREES_TO_RADIANS(angle)) * [snake snakeSpeed];

 newYPosition = snake.position.y + cosf(DEGREES_TO_RADIANS(angle)) * [snake snakeSpeed];
 snake.position = CGPointMake(newXPosition, newYPosition);

So what’s going on here is that we are using Trigonometry to decide where the new X and Y position should be depending on the angle. If we would draw up this example to visualise it it would look like this:


Since in my game the speed for the Snake is a constand we always have the same value there.  That is one way to use Trigonometry in game developmend but i found an easier example to understand and that is to calculate the distance between objects. That would look something like this:

deltaX = playerX – enemyX

deltaY = playerY – enemyY

Then use the arctangent to get the angle, most programming languages has this implemented:
atan2f(deltaY, deltaX);

From this there is an easy function you can just pop in the values to get the actual distance between the objects:

Sqrt(deltaX^2 + deltaY^2)


This should explain a bit of what i have been working with. Do not get me wrong Im not an expert on this subject in fact im pretty at math as I’ve said before. I will continue learning more about trigonometry since I am starting to find math in general more and more interesting. I will also try to update this blog more often.

Until next time!