What is this code doing?

So i'm following this book called SFML Game Development By Example and I've run into some code I'm not too sure what its doing.

This entire function I have slight understanding whats happening but since I didnt write the code im having a hard time following. This code is for a snake game clone for some context. This function grows the snake when it touches an apple. Heres the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
void Snake::Extend()
{
	if (mSnakeBody.empty())
	{
		return; 
	}

	SnakeSegment& tail_head = mSnakeBody[mSnakeBody.size() - 1];

	if(mSnakeBody.size() > 1)
	{
		SnakeSegment& tail_bone = mSnakeBody[mSnakeBody.size() - 2];

		//These two blocks of code are responsible for growing the snake when eating an apple from all directions
		//however i have noticed that when commenting out the code, the snake will seem to still grow if the tail
		//section is on a different axis than the head. Not sure if this is actually happening or not, or if this
		//is a bug or is supposed to happen, need to confirm.

		//This block seems to be responsible for making the snake longer when eating an apple if the snake is
		//hitting the apple from the top and bottom.
		if(tail_head.position.x == tail_bone.position.x)
		{
			if(tail_head.position.y > tail_bone.position.y)
			{
				mSnakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y + 1));
			} 
			else 
			{
				mSnakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y - 1));
			}
		} 
		
		//This block does the opposite of the other block, it makes it so the snake grows in length whne eating
		//the apple from the left and right side.
		else if(tail_head.position.y == tail_bone.position.y)
		{
			if(tail_head.position.x > tail_bone.position.x)
			{
				mSnakeBody.push_back(SnakeSegment(tail_head.position.x + 1, tail_head.position.y));
			} 
			else 
			{
				mSnakeBody.push_back(SnakeSegment(tail_head.position.x - 1, tail_head.position.y));
			}
		}
	} 

	else 
	{
		if(mDir == Direction::Up)
		{
			mSnakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y + 1));
		} 
		
		else if (mDir == Direction::Down)
		{
			mSnakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y - 1));
		} 

		else if (mDir == Direction::Left)
		{
			mSnakeBody.push_back(SnakeSegment(tail_head.position.x + 1, tail_head.position.y));
		} 

		else if (mDir == Direction::Right)
		{
			mSnakeBody.push_back(SnakeSegment(tail_head.position.x - 1, tail_head.position.y));
		}
	}
}


What im having a hard time understanding is this:

 
if(tail_head.position.x == tail_bone.position.x)


So if the head position equals the same coordinates as the tail? or if its on the same axis as the tail?

 
if(tail_head.position.y > tail_bone.position.y)


And this?


 
mSnakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y + 1));


Why does this add one and the else statement subtract?
Last edited on
Hello. This code is a good exercise to understand "complex" logical conditions. When your snake eats an apple, there are multiple comparisons to know where the tail is according to your head position because you could grow like a column - or in other case, like a row...

if(tail_head.position.x == tail_bone.position.x)
The first condition checks if your snake is like a column (same x coordinate?).

if(tail_head.position.y > tail_bone.position.y)
The second condition checks if the head is lower than the tail (goes it to the bottom?).

mSnakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y + 1));
The last line - here you add a new element in your array for the body and the snake grows like a column (because it goes up).

The Else statement is only if your snake has just one element as a body :)
It allows to avoid the previous comparisons...

All these conditions determine where the head and the tail are in-game when the snake eats an apple - so how to grow the snake. Not easy to draw it in mind, but really logical ++
Last edited on
Topic archived. No new replies allowed.