-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNAKE.cpp
332 lines (272 loc) · 6.12 KB
/
SNAKE.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<Windows.h>
#include<dos.h>
#include<time.h>
using namespace std;
#define SPEED 2
int HEIGH_CONSOLE, WIDTH_CONSOLE;
void FixConsoleWindow() //hàm cố định màn hình console
{
HWND consoleWindow = GetConsoleWindow();
LONG style = GetWindowLong(consoleWindow, GWL_STYLE);
style = style & ~(WS_MAXIMIZEBOX)& ~(WS_THICKFRAME);
SetWindowLong(consoleWindow, GWL_STYLE, style);
}
void gotoxy(int x, int y) //Di chuyển trên tọa độ màn hình Console
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = { x, y }; //các thuộc tính liên quan đến WIN32 API
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
//hàm moveto(): là để di chuyển con trỏ trên màn hình đồ họa
/*void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}*/
}
void DRAWFRAME(int SIZE)
{
HEIGH_CONSOLE = WIDTH_CONSOLE = SIZE;
for (int i = 0; i <= WIDTH_CONSOLE; i++)
{
cout << "<>";
}
cout << endl;
for (int i = 1; i < HEIGH_CONSOLE-2; i++)
{
cout << "<>";
gotoxy(WIDTH_CONSOLE * 2, i);
cout << "<>" << endl;
}
for (int i = 0; i <= WIDTH_CONSOLE; i++)
{
cout << "<>";
}
}
struct SNAKE
{
char snake;
int x, y;
};
class C_SNAKE
{
public:
SNAKE mini_snake[2];
int scores = 0;
bool LIVE;
C_SNAKE(char SYMBOL, int x1, int y1)
{
mini_snake[1].snake = SYMBOL;
mini_snake[1].x = x1;
mini_snake[1].y = y1;
LIVE = TRUE;
}
void hien(int x,int y)
{
gotoxy(x, y);
cout << mini_snake[1].snake;
}
void GAME_SCORE(int x1, int y1)
{
gotoxy(x1, y1);
cout << "SCRORE:" << scores;
}
int SNAKE_EAT(int x1, int y1)
{
if (mini_snake[1].x == x1 && mini_snake[1].y == y1)
{
scores++;
return 1;
}
return 0;
}
void LOSE(char moving)
{
switch (moving)
{
case 'w':
{
if (mini_snake[1].y == 0)
LIVE=FALSE;
}
case 's':
{
if (mini_snake[1].y + 1 == HEIGH_CONSOLE - 1)
LIVE = FALSE;
}
case 'd':
{
if (mini_snake[1].x + 1 == WIDTH_CONSOLE * 2+1)
LIVE = FALSE;
}
case 'a':
{
if (mini_snake[1].x - 1 == 0)
LIVE = FALSE;
}
}
}
};
/*class C2_SNAKE : public C_SNAKE
{
public:
int is_snake[100][2]; //thay 100=HEIGH_CONSOLE* WIDTH_CONSOLE
void LOSE()
{
}
};*/
////////////****HÀM MOVE****//////////////
void up(int x, int &y, char ch_main)
{
gotoxy(x, y);
cout << " ";
y = y - 1;
}
void down(int x, int &y, char ch_main)
{
gotoxy(x, y);
cout << " ";
y = y + 1;
}
void right(int &x, int y, char ch_main)
{
gotoxy(x, y);
cout << " ";
x = x + 1;
}
void left(int &x, int y, char ch_main)
{
gotoxy(x, y);
cout << " ";
x = x - 1;
}
bool move_(char moving, char ch_main, int &x, int &y, int is_snake[100][2], int &n, int n1 = 2)
{
int after_x = is_snake[n][0], after_y=is_snake[n][1];
/////////
for (int i = n; i >= n1; i--)
{
is_snake[i][0] = is_snake[i - 1][0];
is_snake[i][1] = is_snake[i - 1][1];
}
switch (moving)
{
case 'w':
{
up(x, y, ch_main);
break;
}
case 's':
{
down(x, y, ch_main);
break;
}
case 'd':
{
right(x, y, ch_main);
break;
}
case 'a':
{
left(x, y, ch_main);
break;
}
}
gotoxy(x, y);
cout << ch_main;
for (int i = 1; i <= n; i++)
{
gotoxy(is_snake[i][0], is_snake[i][1]);
cout << ch_main;
}
gotoxy(is_snake[n][0], is_snake[n][1]);
Sleep(1000/4*SPEED);
cout << " ";
for (int i = 2; i <= n; i++)
{
if (is_snake[1][0] == is_snake[i][0] && is_snake[1][1] == is_snake[i][1])
{
return FALSE;
}
}
return TRUE;
}
void startgame()
{
gotoxy(WIDTH_CONSOLE / 2, HEIGH_CONSOLE / 2);
cout << "START GAME";
Sleep(1000);
system("cls");
}
void main()
{
char SYMBOL = '@';
int is_snake[100][2];
int SIZE=WIDTH_CONSOLE = HEIGH_CONSOLE = 20;
C_SNAKE meocon = C_SNAKE(SYMBOL, WIDTH_CONSOLE / 2, HEIGH_CONSOLE / 2);
meocon.LIVE = TRUE;
SNAKE EAT;
DRAWFRAME(SIZE);
startgame();
DRAWFRAME(SIZE);
srand((unsigned)time(NULL));
meocon.GAME_SCORE(WIDTH_CONSOLE * 2 + 3, HEIGH_CONSOLE/2);
EAT.snake = 'o';
EAT.x = rand() % (2*WIDTH_CONSOLE - 2) + 2 ;
EAT.y = rand() % (HEIGH_CONSOLE - 3) + 1;
gotoxy(EAT.x, EAT.y);
cout << EAT.snake;
meocon.hien(WIDTH_CONSOLE / 2, HEIGH_CONSOLE / 2); //HIỆN RẮN KHI BẮT ĐẦU GAME
char moving='d'; //CHO RẮN ĐI THẲNG TỚI
char key;
is_snake[1][0] = meocon.mini_snake[1].x;
is_snake[1][1] = meocon.mini_snake[1].y;
int sizeof_snake = 0;
do
{
while (!_kbhit() && meocon.LIVE == TRUE)
{
sizeof_snake = meocon.scores + 1;
meocon.mini_snake[1].x = is_snake[1][0];
meocon.mini_snake[1].y = is_snake[1][1];
meocon.LOSE(moving); //KIỂM TRA THUA CHƯA
if (meocon.LIVE == FALSE)
{
break;
}
meocon.LIVE=move_(moving, meocon.mini_snake[1].snake, is_snake[1][0], is_snake[1][1], is_snake, sizeof_snake);
if (meocon.LIVE == FALSE)
{
break;
}
gotoxy(WIDTH_CONSOLE * 2 + 3, 1+HEIGH_CONSOLE / 2); //HIỆN TỌA ĐỒ MỒI
cout << "TOA DO EAT (x,y): " << "(" << EAT.x << "," << EAT.y << ")";
for (int i = 1; i < sizeof_snake; i++)
{
gotoxy(WIDTH_CONSOLE * 2 + 3, 1 + HEIGH_CONSOLE / 2+i);
cout << is_snake[i][0] << ":" << is_snake[i][1];
}//KIỂM TRA TỌA DỘ MỒI
if (meocon.SNAKE_EAT(EAT.x, EAT.y) == 1)
{
meocon.GAME_SCORE(WIDTH_CONSOLE * 2 + 3, HEIGH_CONSOLE / 2);
EAT.x = rand() % (2 * WIDTH_CONSOLE - 2) + 2;
EAT.y = rand() % (HEIGH_CONSOLE - 2) + 1;
gotoxy(EAT.x, EAT.y);
cout << EAT.snake;
}
}
if (meocon.LIVE == FALSE)
{
break;
}
moving=key = _getch();
} while (key != 27 && meocon.LIVE == TRUE);
gotoxy(WIDTH_CONSOLE / 2, HEIGH_CONSOLE / 2); //KẾT THÚC GAME
cout << "HIHI RAN U DAU ROI!!!";
Sleep(1000);
_getch();
}