-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacman.cpp
More file actions
255 lines (249 loc) · 7.05 KB
/
Copy pathPacman.cpp
File metadata and controls
255 lines (249 loc) · 7.05 KB
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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
#include "Board.h"
#include <conio.h>
#include "resource.h"
#include <chrono>
/*********************************************
* ~TODO: file system with .map files
*[] ~Warm holes (according to protocol)
*[] ~~Fruit to collect more points
*[] ~~~Settings and more customization
*[] ~~~~Kill ghosts option
*[] ~~~~~Kill works bad
*[] ~~~~~~Try catch.. throw smth when killed so the catch will handle the death
*[*] ~~~~~~~Check double movement on pacman
*[] ~~~~~~~~Add check if 224 get-ch and then up down left right functions then function to check input generally
*********************************************/
/////////////////////////////////////////////
/* .map files will be in levels dir and will be read according to this protocol:
* line: command
* 1: rows
* 2: cols
* 3 -> rows*cols + 2: map
* map:
* @ - pacman
* # - wall
* g - ghost
* 0:9 - fruit, different colors
* w - warm hole (only one is allowed)
* . / space - points
*/
#define UP 72 /// try to get key without ASCII
#define DOWN 80
#define RIGHT 77
#define LEFT 75
#define ENTER 13
int WINNING_SCORE = 2500;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
void banner(); // make banner more change able, include settings and levels
int main()
{
/* Variables: */
auto ans = IDYES;
bool exit = true, win = false, out = false;
int score = false;
Board* b = new Board();
char mov = ' ', lastKey = ' ';
/* Console defaults */
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 24;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL + 300; // bold
std::wcscpy(cfi.FaceName, L"Consolas");
SetCurrentConsoleFontEx(h, FALSE, &cfi);
SetConsoleTitleA("Pacman");
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = false;
lpCursor.dwSize = 20;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &lpCursor);
/* Game start menu */
system("mode 40,14");
printf("\x1b[d");
banner(); // maybe will need to return value and built Board* b here.
/* Game start level */
system("mode 40,22");
b->printBoard(false);
// !_kbhit() for thread like
do
{
mov = _getch();
} while (mov != 'a' && mov != 'd' && mov != 's' && mov != 'w'
&& mov != RIGHT && mov != LEFT && mov != DOWN && mov != UP);
try
{
while (exit && b->isalive())
{
auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
while (!_kbhit())
{
dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
if (dur.count() > 150 && b->isalive())
{
b->ghost();
b->ghost();
if (b->isalive())
{
if (mov == 'a' || mov == 'd' || mov == 's' || mov == 'w'
|| mov == RIGHT || mov == LEFT || mov == DOWN || mov == UP)
{
lastKey = mov;
}
exit = b->move(lastKey);
score = b->getScore();
}
if (score >= WINNING_SCORE || !b->isalive())
{
out = false;
goto done;
break;
}
start = std::chrono::high_resolution_clock::now();
}
end = std::chrono::high_resolution_clock::now();
b->printBoard();
}
if (!out)
{
b->ghost();
b->ghost();
if (b->isalive())
{
mov = _getch();
if ((mov == 'a' || mov == 'd' || mov == 's' || mov == 'w'
|| mov == RIGHT || mov == LEFT || mov == DOWN || mov == UP))
{
exit = b->move(mov);
}
else
{
exit = b->move(lastKey);
}
}
score = b->getScore();
b->printBoard();
Sleep(150);
}
done:
if (score >= WINNING_SCORE)
{
win = true;
exit = false;
}
}
}
catch (...)
{
if (!b->isalive())
{
win = false;
}
}
/* End of the game */
(win) ? (PlaySoundW(TEXT("music/tada.wav"), NULL, SND_FILENAME | SND_ASYNC)) : (PlaySoundW(TEXT("music/death.wav"), NULL, SND_FILENAME | SND_ASYNC));
b->finish();
system("mode 40,25");
b->printBoard(false);
if (win)
{
std::cout << "----------------------------------------\n" << "-----------YOU-----------WIN!-----------\n\a" << "----------------------------------------\n";
ans = MessageBox(NULL, L"You succeed collecting all the points!\nDo you wanna play again?", L"Congratulation!", MB_YESNO);
}
else
{
std::cout << "----------------------------------------\n" << "-----------YOU-----------LOST-----------\n\a" << "----------------------------------------\n";
ans = MessageBox(NULL, L"You were killed by a ghost👻\nDo you wanna play again?", L"Oh No!", MB_YESNO);
}
if (ans == IDYES)
main();
lpCursor.bVisible = true;
lpCursor.dwSize = 10;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &lpCursor);
return 0;
}
void banner()
{
PlaySoundW(TEXT("music/intro.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
char ans = ' ';
int place = 0;
do
{
switch (ans)
{
case UP:
if (place == 0)
break;
else
place--;
break;
case DOWN:
if (place == 2)
break;
else
place++;
break;
default:
break;
}
std::cout << " \n";
std::cout << " \n";
std::cout << " Choose difficulty \n";
std::cout << " \n";
switch (place)
{
case 0:
SetConsoleTextAttribute(h, 159);
std::cout << " Easy ,--. \n";
SetConsoleTextAttribute(h, 15);
std::cout << " |oo | \n";
std::cout << " Medium |~~ | \n";
std::cout << " |/\\/\\| \n";
std::cout << " Hard \n";
std::cout << " \n";
break;
case 1:
std::cout << " ,--. Easy \n";
std::cout << " | oo| \n";
SetConsoleTextAttribute(h, 159);
std::cout << " | ~~| Medium \n";
SetConsoleTextAttribute(h, 15);
std::cout << " |/\\/\\| \n";
std::cout << " Hard \n";
std::cout << " \n";
break;
case 2:
std::cout << " Easy \n";
std::cout << " \n";
std::cout << " Medium .--. \n";
std::cout << " / _.-' \n";
SetConsoleTextAttribute(h, 159);
std::cout << " Hard \\ '-. \n";
SetConsoleTextAttribute(h, 15);
std::cout << " '--' \n";
break;
}
std::cout << " [Enter]: To select \n";
std::cout << " \n";
ans = _getch();
printf("\x1b[d");
} while (ans != ENTER);
switch (place)
{
case 0:
WINNING_SCORE = 1000;
break;
case 1:
WINNING_SCORE = 2500;
break;
case 2:
WINNING_SCORE = 5000;
break;
}
PlaySoundW(NULL, NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}