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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
| #include <windows.h>
#include <vector>
#include <ctime>
#define xSPEED_DELAY 120
#define xCOLOR_BACKGROUND 22
#define xCOLOR_USER (3 | FOREGROUND_INTENSITY)
DWORD __stdcall ThreadInputDevice(LPVOID param);
void BeginGame(void);
int InputKey(USHORT key);
void DrawDot(SHORT x, SHORT y, WORD color, CHAR ch);
bool IsCollision(const COORD* apos, const COORD* bpos);
bool IsDeath(const COORD* pos);
void DrawFrame(void);
void DrawStatus(BOOL reset);
void ShufflyFrak(void);
void MessageConsole(void);
void DrawButton(const CHAR* str, SHORT x, SHORT y, SHORT width, SHORT height, WORD color);
int Rollover(SHORT x, SHORT y, PSMALL_RECT b_yes, PSMALL_RECT b_no);
USHORT ckey = VK_DELETE;
HANDLE hout = NULL;
HANDLE hin = NULL;
HANDLE thread = NULL;
COORD screen = {0, 0};
SHORT doom = 0;
std::vector<COORD> dots;
COORD bonus;
CHAR sign;
WORD scolor;
int main(void) {
CONSOLE_SCREEN_BUFFER_INFO cinfo;
CONSOLE_CURSOR_INFO cur;
ZeroMemory(&cinfo, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
ZeroMemory(&cur, sizeof(CONSOLE_CURSOR_INFO));
if((hout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE ||
(hin = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
return 1;
GetConsoleScreenBufferInfo(hout, &cinfo);
screen.X = cinfo.dwSize.X;
screen.Y = 25;
SetConsoleScreenBufferSize(hout, screen);
GetConsoleCursorInfo(hout, &cur);
cur.bVisible = FALSE;
SetConsoleCursorInfo(hout, &cur);
SetConsoleMode(hin, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
SetConsoleTitleW(L"ЗМЕЙКА: автор игры(©) Фролов Алексей Алексеевич.");
thread = CreateThread(NULL, 0u, (LPTHREAD_START_ROUTINE)ThreadInputDevice, NULL, 0u, NULL);
if(thread == NULL)
return 2;
::BeginGame();
screen.X -= screen.X / 7;
COORD dot;
prev:
ZeroMemory(&dot, sizeof(COORD));
for(int d = 5; d > 0; d--) {
dot.X = screen.X / 2 - d;
dot.Y = screen.Y / 2;
dots.push_back(dot);
}
srand(time(NULL));
::DrawFrame();
::ShufflyFrak();
::DrawStatus(TRUE);
while(TRUE) {
switch(ckey) {
case VK_DOWN:
dots[0].Y += 1;
break;
case VK_UP:
dots[0].Y -= 1;
break;
case VK_LEFT:
dots[0].X -= 1;
break;
case VK_RIGHT:
dots[0].X += 1;
break;
case VK_DELETE:
continue;
break;
case VK_ESCAPE:
goto aborted;
break;
}
if(::IsDeath(&dots[0])) {
MessageBeep(MB_ICONERROR);
::MessageConsole();
doom = 1;
while(doom & 1);
if(doom & 2) {
dots.clear();
ckey = VK_RIGHT;
goto prev;
} else if(doom & 4)
goto aborted;
}
::DrawDot(dots[dots.size() - 1].X, dots[dots.size() - 1].Y, xCOLOR_BACKGROUND, ' ');
for(size_t i = dots.size() - 1; i > 0; i--) {
dots[i].X = dots[i - 1].X;
dots[i].Y = dots[i - 1].Y;
::DrawDot(dots[i].X, dots[i].Y, xCOLOR_USER, 4);
::DrawDot(dots[0].X, dots[0].Y, xCOLOR_USER, 1);
}
::DrawDot(bonus.X, bonus.Y, scolor, sign);
if(::IsCollision(&bonus, &dots[0])) {
::DrawDot(bonus.X, bonus.Y, xCOLOR_BACKGROUND, ' ');
dots.push_back(bonus);
::ShufflyFrak();
::DrawStatus(FALSE);
MessageBeep(MB_ICONINFORMATION);
}
Sleep(xSPEED_DELAY);
}
aborted:
DWORD result = WaitForSingleObject(thread, 3000u);
switch(result) {
case WAIT_OBJECT_0:
CloseHandle(thread);
break;
case WAIT_TIMEOUT:
TerminateThread(thread, 0u);
CloseHandle(thread);
break;
}
return 0;
}
// Поточная функция по ожиданию прерываний от устройств ввода(клавиатуры и мыши)
DWORD __stdcall ThreadInputDevice(LPVOID param) {
INPUT_RECORD records[128] = {0};
DWORD dread = 0u;
DWORD size = sizeof(records) / sizeof(INPUT_RECORD);
SMALL_RECT rc = { 0, 0, 0, 0 };
while(TRUE) {
if(! ReadConsoleInput(hin, records, size, &dread))
break;
for(DWORD i = 0u; i < dread; i++) {
switch(records[i].EventType) {
case KEY_EVENT:
if(records[i].Event.KeyEvent.bKeyDown) {
if(InputKey(records[i].Event.KeyEvent.wVirtualKeyCode) != 0)
goto jump;
}
break;
case MOUSE_EVENT:
if(doom != 1)
break;
::Rollover(records[i].Event.MouseEvent.dwMousePosition.X, records[i].Event.MouseEvent.dwMousePosition.Y, &rc, &rc);
if((records[i].Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) && (records[i].Event.MouseEvent.dwEventFlags == 0)) {
if((doom = ::Rollover(records[i].Event.MouseEvent.dwMousePosition.X, records[i].Event.MouseEvent.dwMousePosition.Y, &rc, &rc)) == 4)
goto jump;
}
break;
}
}
Sleep(0u);
}
jump: ;
return 0u;
}
// Начальный экран
void BeginGame(void) {
DWORD dwrite = 0u;
COORD pos;
CHAR buf[64];
CharToOemA("Start. Enter ", buf);
pos.Y = screen.Y / 2 - 1;
for(const CHAR* iter = buf; *iter; *iter++) {
pos.X = screen.X / 2 - (iter - buf) / 2;
::FillConsoleOutputAttribute(hout, 11, DWORD(iter - buf), pos, &dwrite);
::WriteConsoleOutputCharacterA(hout, buf, DWORD(iter - buf), pos, &dwrite);
MessageBeep(MB_ICONQUESTION);
Sleep(300u);
}
while(ckey == VK_DELETE);
}
// Функция по установке бонуса
void ShufflyFrak(void) {
static int index = 0;
WORD colors[] = { FOREGROUND_RED, FOREGROUND_GREEN, 27, 22, 26 };
scolor = colors[index] | FOREGROUND_INTENSITY;
sign = 1 + rand() % 6;
bonus.X = SHORT(1 + rand() % (screen.X - 2));
bonus.Y = SHORT(1 + rand() % (screen.Y - 2));
++index %= 5;
}
// Функция по расчёту столкновения змейки с бонусом
bool IsCollision(const COORD* apos, const COORD* bpos) {
short x = apos->X - bpos->X;
short y = apos->Y - bpos->Y;
if((x == 0) && (y == 0))
return true;
return false;
}
// Функция столновения змейки со стенками или самим собой
bool IsDeath(const COORD* pos) {
if((pos->X >= screen.X - 1) || (pos->X <= 0) || (pos->Y <= 0) || (pos->Y >= screen.Y - 1))
return true;
for(size_t i = 5u; i < dots.size(); i++) {
if(::IsCollision(pos, &dots[i]))
return true;
}
return false;
}
// Функция обработчик клавишей. Описание их в правом блоке описанно ниже.
int InputKey(USHORT key) {
static USHORT tkey = VK_RIGHT;
if((key == VK_LEFT) || (key == 'A'))
ckey = VK_LEFT;
else if((key == VK_RIGHT) || (key == 'D'))
ckey = VK_RIGHT;
else if((key == VK_UP) || (key == 'W'))
ckey = VK_UP;
else if((key == VK_DOWN) || (key == 'S'))
ckey = VK_DOWN;
else if(key == VK_RETURN) // старт игры
ckey = tkey;
else if(key == VK_DELETE) { // пауза
tkey = ckey;
ckey = VK_DELETE;
} else if(key == VK_ESCAPE) { // выход из игры
ckey = VK_ESCAPE;
return 1;
}
return 0;
}
// Вывод в правый игровой блок кол-во очков и прочию информацию
void DrawStatus(BOOL reset) {
static ULONG bonus = 0UL;
if(reset)
bonus = 0UL;
else
bonus += 1;
DWORD dwr;
CHAR buf[32];
DWORD len = wsprintfA(buf, "%u", bonus);
COORD pos = { (screen.X + 1) + 9 / 2 - len / 2, 2 };
WriteConsoleOutputCharacterA(hout, buf, len, pos, &dwr);
for(WORD attr = 11; len > 0; --len, ++pos.X)
WriteConsoleOutputAttribute(hout, &attr, 1, pos, &dwr);
}
// Функция каркаса
void DrawFrame(void) {
DWORD dwrite = 0u;
CHAR ch = 31;
COORD pos = { 0, 0 };
WORD color = 24;
CHAR buf[32];
//фон
ch = ' ';
for(SHORT i = 0; i < screen.Y - 1; i++) {
pos.X = 1;
pos.Y = i;
FillConsoleOutputCharacterA(hout, ch, screen.X - 1, pos, &dwrite);
FillConsoleOutputAttribute(hout, xCOLOR_BACKGROUND, screen.X - 1, pos, &dwrite);
}
//верхняя граница
ch = 31;
pos.X = pos.Y = 1;
FillConsoleOutputCharacterA(hout, ch, screen.X, pos, &dwrite);
FillConsoleOutputAttribute(hout, color, screen.X, pos, &dwrite);
//нижняя граница
pos.Y = screen.Y - 1;
ch = 30;
FillConsoleOutputCharacterA(hout, ch, screen.X, pos, &dwrite);
FillConsoleOutputAttribute(hout, color, screen.X, pos, &dwrite);
for(SHORT y = 0; y < screen.Y; y++) {
pos.X = 0;
pos.Y = y;
//левая граница
ch = 16;
FillConsoleOutputCharacterA(hout, ch, 1u, pos, &dwrite);
FillConsoleOutputAttribute(hout, color, 1u, pos, &dwrite);
//правая граница
pos.X = screen.X - 1;
ch = 17;
FillConsoleOutputCharacterA(hout, ch, 1u, pos, &dwrite);
FillConsoleOutputAttribute(hout, color, 1u, pos, &dwrite);
}
//правык блок
CharToOemA("Point", buf);
pos.X = screen.X + 1;
pos.Y = 0;
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
lstrcpyA(buf, "---------");
for(SHORT k = 1; k < 4; k += 2) {
pos.Y = k;
FillConsoleOutputAttribute(hout, 5, lstrlenA(buf), pos, &dwrite);
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
}
CharToOemA("Comands", buf);
pos.Y = 9;
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
lstrcpyA(buf, "_________");
pos.Y = 10;
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
pos.X += 2;
pos.Y = 11;
const CHAR* ctrls[6] = { "Enter", "Start", "Delete", "Pause", "Escape", "Exit" }; //правое меню
for(SHORT j = 0; j < 6; j++, ++pos.Y) {
CharToOemA(ctrls[j], buf);
if(!(j%2)) {
++pos.Y;
FillConsoleOutputAttribute(hout, 6, lstrlenA(buf), pos, &dwrite);
}
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
}
pos.X -= 2;
lstrcpyA(buf, "_________");
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
}
// Функция рисует точку
void DrawDot(SHORT x, SHORT y, WORD color, CHAR ch) {
DWORD dwrite = 0u;
COORD pos = { x, y };
FillConsoleOutputCharacterA(hout, ch, 1u, pos, &dwrite);
FillConsoleOutputAttribute(hout, color, 1u, pos, &dwrite);
}
// Функция проявляет рисованое-сообщение в случае проигрыша
void MessageConsole(void) {
DWORD dwrite = 0u;
CHAR buf[32];
COORD pos;
SMALL_RECT rect1, rect2;
pos.X = screen.X / 2 - screen.X / 4;
for(SHORT y = 0; y < 10; y++) {
pos.Y = screen.Y / 2 - 5 + y;
FillConsoleOutputAttribute(hout, 71, screen.X / 2, pos, &dwrite);
FillConsoleOutputCharacterA(hout, ' ', screen.X / 2, pos, &dwrite);
}
CharToOemA("GAME OVER !", buf);
pos.Y = screen.Y / 2 - 4;
pos.X = screen.X / 2 - lstrlenA(buf) / 2;
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
CharToOemA("RESTART YES / NO ?", buf);
pos.Y += 2;
pos.X = screen.X / 2 - lstrlenA(buf) / 2;
WriteConsoleOutputCharacterA(hout, buf, lstrlenA(buf), pos, &dwrite);
rect1.Left = pos.X + 6;
rect1.Top = pos.Y + 2;
rect1.Right = 8;
rect1.Bottom= 3;
rect2.Left = pos.X + 15;
rect2.Top = pos.Y + 2;
rect2.Right = 8;
rect2.Bottom= 3;
::Rollover(-2, -2, &rect1, &rect2);
}
// Функция создающее ролловер для кнопок и обработка прямоугольных областей кнопок
int Rollover(SHORT x, SHORT y, PSMALL_RECT b_yes, PSMALL_RECT b_no) {
static SMALL_RECT yes;
static SMALL_RECT no;
static int type = 0;
if((x == -2) && (y == -2)) {
CopyMemory(&yes, b_yes, sizeof(SMALL_RECT));
CopyMemory(&no, b_no, sizeof(SMALL_RECT));
::DrawButton("YES", yes.Left, yes.Top, yes.Right, yes.Bottom, 233);
::DrawButton("NO", no.Left, no.Top, no.Right, no.Bottom, 233);
return 0;
}
if((x >= yes.Left && x <= yes.Left + yes.Right) && (y >= yes.Top && y <= yes.Top + yes.Bottom)) {
if(type & 2 || type & 4) {
::DrawButton("YES", yes.Left, yes.Top, yes.Right, yes.Bottom, 189);
::DrawButton("NO", no.Left, no.Top, no.Right, no.Bottom, 233);
}
type = 1;
return 2;
}
else if((x >= no.Left && x <= no.Left + no.Right) && (y >= no.Top && y <= no.Top + no.Bottom)) {
if(type & 1 || type & 4) {
::DrawButton("NO", no.Left, no.Top, no.Right, no.Bottom, 189);
::DrawButton("YES", yes.Left, yes.Top, yes.Right, yes.Bottom, 233);
}
type = 2;
return 4;
} else {
if(type & 1)
::DrawButton("YES", yes.Left, yes.Top, yes.Right, yes.Bottom, 233);
else if(type & 2)
::DrawButton("NO", no.Left, no.Top, no.Right, no.Bottom, 233);
type = 4;
}
return 0;
}
// Функция выводит кнопку в консоль
void DrawButton(const CHAR* str, SHORT x, SHORT y, SHORT width, SHORT height, WORD color) {
CHAR buf[32];
COORD pos;
DWORD dwrite = 0u;
DWORD len = lstrlenA(str);
CharToOemBuffA(str, buf, len + 1);
pos.X = x;
for(SHORT i = 0; i < height; i++) {
pos.Y = y + i;
FillConsoleOutputAttribute(hout, color, width, pos, &dwrite);
}
pos.Y = y + height / 2;
pos.X = x + width / 2 - len / 2;
WriteConsoleOutputCharacterA(hout, buf, len, pos, &dwrite);
} |