Snake Game

I need help to make the tail, I've watched several guides but I can't understand how to do it. I need help with the logics and how I should print the tail and make it follow the head in every direction.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <conio.h>
#include "mbb.h"
#include <time.h>
#include <string>
using namespace std;
//-----------------------------------------------------
int x, y;
int dX = 0, dY = 0;
int speed = 100;
int width = 63;
int height = 24;
int fruitX = 0;
int fruitY = 0;
int score;
int tailX[100], tailY[100];
int nTail;

// Functions ------------------------------------------------------------
void runGame();
void handleKey(int);
void map();
int theENd();
int apple(int score);
void innanspel1();
int menu(int val);
//------------------------------------------------------------------------
int main()
{
	//menu();
	runGame();
	return 0;
}
//int menu(int val)
///{
    //int key = 0;
	//gotoxy(35, 1); 
	//cout << "S.N.A.K.E";

//	gotoxy(35, 4);
//	cout << "Inställningar";
//	gotoxy(35, 8);
//	cout << "Lätt";
//	gotoxy(35, 11);
//	cout << "Medel";
//	gotoxy(35, 14);
//	cout << "Svår";
//	gotoxy(35, 17);
//	cout << "Avsluta";
//	return 0;
//}
void innanspel1()
{
	fruitX = rand() % width;
	fruitY = rand() % height;

	gotoxy(fruitX, fruitY);
	cout << char(111);
	int score = 0;
	x = width / 2;
	y = height / 2;
}
void runGame()
{
	char gubbe = char(2);		// The head
	int key = 0;			// Stores the keys that is pressed down
	bool gameOn = true;			// Future use to interrupt 
	int score = 0;
	int prevX = tailX[0];
	int prevY = tailY[0];
	int prev2X, prev2Y;
	innanspel1();
	map();
	for (int i = 1; i < nTail; i++)
	{
		prev2X = tailX[i];
		prev2Y = tailY[i];
		tailX[i] = prevX;
		tailY[i] = prevY;
		prevX = prev2X;
		prevY = prev2Y;

	}
	while (key != 27 && gameOn == true)	// 27 == ESC som en int
	{
		// Erase the head
		gotoxy(x, y);
		cout << " ";

		// Check if any button is pushed down
		if (_kbhit())
		{
			key = _getch();
			if (key == 224 || key == 0)
			{
				key = _getch();
			}

			// Changes direction when a key is pushed down
			handleKey(key);
		}

		// Move the snake
		x = x + dX;
		y = y + dY;

		// Print the head
		gotoxy(x, y);
		cout << gubbe;

		score = apple(score);
		if (x == fruitX && y == fruitY)
		{
			score += 10;
			nTail++;
			
		}
		if (x <= 0 || x >= width || y <= 0 || y >= height)
		{
			theENd();
		}

		Sleep(speed);
	}
}
//------------------------------------------------------------------------
void handleKey(int keyPressed)
{
	if (keyPressed == 119 || keyPressed == 87)				// Key: W
	{
		dX = 0;
		dY = -1;
	}
	else if (keyPressed == 115 || keyPressed == 83)			// Key: S
	{
		dX = 0;
		dY = 1;
	}
	else if (keyPressed == 97 || keyPressed == 65)			// Key: A
	{
		dX = -1;
		dY = 0;
	}
	else if (keyPressed == 100 || keyPressed == 68)			// Key: D
	{
		dX = 1;
		dY = 0;
	}
}
//------------------------------------------------------------------------
void map()
{
	int q = 0;
	gotoxy(0, 0);

	while (q <= 78)
	{
		cout << char(45);
		q++;
	}

	q = 0;
	while (q < 24)
	{
		gotoxy(0, q);
		cout << char(124);
		gotoxy(63, q);
		cout << char(124);
		gotoxy(78, q);
		cout << char(124);
		q++;
	}
	cout << endl;
	q = 0;

	while (q <= 78)
	{
		cout << char(45);
		q++;
	}
	q = 0;
	while (q <= 13)
	{
		gotoxy(q+ 64, 5);
			cout << char(45);
			q++;
	}
	gotoxy(66, 7);
	cout << "Play with: " << endl;
	gotoxy(70, 9);
	cout << "W"; 
	gotoxy(68, 11);
	cout << "A";
	gotoxy(70, 11);
	cout << "S";
	gotoxy(72, 11);
	cout << "D";
	gotoxy(66, 1);
	cout << "S N A K E"; 
	

}
int theENd()
{
	system("CLS");
	char again = 0;
	cout << "Game Over" << endl;
	cout << "Play again? (y/n)" << endl;
	while (again != 'y' || again != 'n')
	{
		cin >> again;

	}
	return again;
}
int apple(int score)
{
	if (x == fruitX && y == fruitY)
	{
		fruitX = rand() % width;
		fruitY = rand() % height;

		gotoxy(fruitX, fruitY);
		cout << char(111);
		score = score + 10;
		gotoxy(66, 3);
		cout << "score: " << score;
	}
	return score;
}
Topic archived. No new replies allowed.