Help needed in Snake Game

I am having problem in Snake game , my snake is moving rightwards only , it doesn't moves in any direction , I am confused what is wrong with my code . Please help me I will be grateful.

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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# include <iostream>
# include <cstdlib>
# include <string>
# include <Windows.h>
# include <ctime>
# include <cstdio>
# include <conio.h>

//# define UP 8;
//# define DOWN 2;
//# define Left 4;
//# define Left 6;

using namespace std;

void gotoxy(int x, int y )
{
   COORD Cord;
   Cord.X = x;
   Cord.Y = y;
   SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ),Cord );
}
struct SnakeStruct
{
	char SnakeArr[20][78];
	int Length;
	int Head_dir;
	int Head_x;
	int Head_y;
	int Tail_x;
	int Tail_y;
	int Tail_dir;
	int Food_x;
	int Food_y;
	int Score;
};


//void Display_Snake(char Snak[][79], Snake * Sarr);
void Print (SnakeStruct * Snake);
void Boundary_Initializer(SnakeStruct * Snake);
void Snake_Initializer(SnakeStruct * Snake);
void Snake_Movement(SnakeStruct * Snake);
void User_Press(SnakeStruct * Snake);

void main ()
{
		cout<<"\n\n\n\n\n\n\n";
	system("color 0C");
    cout<<"\t\t _______  __    _  _______  ___   _  _______   "<<endl;
	Sleep(200);
	system("color 0A");
    cout<<"\t\t|       ||  |  | ||   _   ||   | | ||       |  "<<endl;
	Sleep(200);
	system("color 0B");
    cout<<"\t\t|  _____||   |_| ||  |_|  ||   |_| ||    ___|  "<<endl;
	Sleep(200);
	system("color 0C");
    cout<<"\t\t| |_____ |       ||       ||      _||   |___   "<<endl;
    Sleep(200);
	system("color 0D");
	cout<<"\t\t|_____  ||  _    ||       ||     |_ |    ___| "<<endl;;
    Sleep(200);
	system("color 0E");
	cout<<"\t\t _____| || | |   ||   _   ||    _  ||   |___  "<<endl;
    Sleep(200);
	system("color 0A");
	cout<<"\t\t|_______||_|  |__||__| |__||___| |_||_______|  "<<endl<<endl;
	Sleep(200);
	system("color 0E");
	cout<<"                     .";
	for (int i=0;i<17;i++)
	{
		Sleep(100);
		cout<<" .";
	}
	Sleep(600);
	cout<<endl<<endl<<"                                !!!! START !!!!                                     "<<endl;
	Sleep(2500);
	system("CLS");
	SnakeStruct * Snake = new SnakeStruct;
	Snake_Initializer(Snake);//Initialize all the peremeters of Snake
	Boundary_Initializer(Snake);// Will Draw the boudary
	Print(Snake);
	while (1)
	{
		User_Press(Snake);
		Snake_Movement(Snake);
		//Display_Snake ( Snak , Sarr );
		Print(Snake);
		//Sleep(500);
		//Collision_Detect(Snakes);
	}
}
void Boundary_Initializer(SnakeStruct * Snake)
{
	gotoxy(0,0);
	cout<<"*******************************************************************************"<<endl;
	cout<<"********************* SNAKES GAME *********************** Highest Score : "<<endl;
	cout<<"*******************************************************************************"<<endl;
	cout<<"           Press 8 for UP , 2 for DOWN , 6 for RIGHT and 4 for Left           "<<endl;
	
	for(int i=0;i<20;i++)
	{
		Snake->SnakeArr[i][0] = '#';
		Snake->SnakeArr[i][77] = '#';
		Snake->SnakeArr[i][1] = '#';
		Snake->SnakeArr[i][76] = '#';
	}
	for(int i=0;i<78;i++)
	{
		Snake->SnakeArr[0][i] = '#';
		Snake->SnakeArr[19][i] = '#';
		Snake->SnakeArr[1][i] = '#';
		Snake->SnakeArr[18][i] = '#';
	}
		Snake->SnakeArr[4][5] = '#';
		Snake->SnakeArr[5][5] = '#';
		Snake->SnakeArr[6][5] = '#';
		Snake->SnakeArr[7][5] = '#';
		
		Snake->SnakeArr[4][5] = '#';
		Snake->SnakeArr[4][6] = '#';
		Snake->SnakeArr[4][7] = '#';
		Snake->SnakeArr[4][8] = '#';

		Snake->SnakeArr[15][67] = '#';
		Snake->SnakeArr[15][68] = '#';
		Snake->SnakeArr[15][69] = '#';
		Snake->SnakeArr[15][70] = '#';

		Snake->SnakeArr[15][71] = '#';
		Snake->SnakeArr[14][71] = '#';
		Snake->SnakeArr[13][71] = '#';
		Snake->SnakeArr[12][71] = '#';
	
}
void Snake_Initializer(SnakeStruct * Snake)
{
	Snake -> Length = 8;
	Snake -> Head_x = 9;
	Snake -> Head_y = 10;
	Snake -> Tail_x = 9;
	Snake -> Tail_y = 2;
	Snake -> Head_dir =  77;
	Snake -> Score = 0;
	for(int i=0;i<20;i++)
	{
		for (int j=0;j<78;j++)
		{
			Snake ->SnakeArr[i][j] = ' ';
		}
	}
	for(int i = Snake -> Head_y ; i > Snake -> Tail_y ; i--)
	{
		Snake->SnakeArr[Snake ->Tail_x][i] = '@';
	}
}
void Snake_Movement(SnakeStruct * Snake)
{
	int i;
	for(i = Snake -> Head_y ; i > Snake -> Tail_y ; i--)
	{
		Snake->SnakeArr[Snake ->Tail_x][i+1] = Snake->SnakeArr[Snake ->Tail_x][i];
	}
	Snake->SnakeArr[Snake ->Tail_x][i] =' ';
	if (Snake->Head_dir == 72)//Snake will move UP
	{
		Snake -> Head_x--;
	}
	if (Snake ->Head_dir == 80)//Snake will move Down
	{
		Snake ->Head_x++;
	}
	if (Snake ->Head_dir == 75)//Snake will move Left
	{
		Snake ->Tail_y--;
		Snake ->Head_y--;
	}
	if (Snake ->Head_dir == 77)//Snake will move RIGHT
	{
		Snake ->Tail_y++;
		Snake ->Head_y++;
	}
}
void Print(SnakeStruct * Snake)
{
	gotoxy(0,4);
	for(int i=0;i<20;i++)
	{
		cout<<" ";
		for(int j=0;j<78;j++)
		{
			cout<<Snake -> SnakeArr[i][j];
		}
		cout<<endl;
	}
}
void User_Press( SnakeStruct* Snake)
{ 
	int dir;
	if (kbhit())
	{
		dir = getch();
		if ((dir == 72)&&(Snake -> Head_dir!=72)&&(Snake->Head_dir!=80))//Snake will move UP
		{
			Snake->Head_dir = dir;
		}
		else if ((dir == 77)&&(Snake -> Head_dir!=77)&&(Snake->Head_dir!=75))//Snake will move RIGHT
		{
			Snake->Head_dir = dir;
		}
		else if ((dir == 80)&&(Snake -> Head_dir!=80)&&(Snake->Head_dir!=72))//Snake will move Down
		{
			Snake->Head_dir = dir;
		}
		else if ((dir == 75)&&(Snake -> Head_dir!=75)&&(Snake->Head_dir!=77))//Snake will move Left
		{
			Snake->Head_dir = dir;
		}
		if ((dir == 80)||(dir == 72)&&(Snake -> Head_dir == 72))
			cout<<"\a";
		else if ((dir == 75)||(dir == 77)&&(Snake -> Head_dir == 77))
			cout<<"\a";
		else if ((dir == 72)||(dir == 80)&&(Snake -> Head_dir == 80))
			cout<<"\a";
		else if  ((dir == 77)||(dir == 75)&&(Snake -> Head_dir == 75))
			cout<<"\a";

	if (Snake->Head_dir == 72)//Snake will move UP
	{
		Snake -> Head_x--;
	}
	if (Snake ->Head_dir == 80)//Snake will move Down
	{
		Snake ->Head_x++;
	}
	if (Snake ->Head_dir == 75)//Snake will move Left
	{
		Snake ->Tail_y--;
		Snake ->Head_y--;
	}
	if (Snake ->Head_dir == 77)//Snake will move RIGHT
	{
		Snake ->Tail_y++;
		Snake ->Head_y++;
	}
			//else if (dir == 27)// Snake game will exit
			//exit(-1);
	}
	
}

Regards
Ahmad Shoaib
I believe the problem may be derived from the fact that COORD (on line 18) is never defined, whether it be a struct, class, or whatever. Since it is NEVER defined/created/initialized, you CANNOT create and object for it, and thus errors will occur.
Sleep(2500);
system("CLS");

there are betters ways to do this that avoid system functions.
Topic archived. No new replies allowed.