-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPac_Man_Main.cpp
More file actions
291 lines (259 loc) · 8.53 KB
/
Pac_Man_Main.cpp
File metadata and controls
291 lines (259 loc) · 8.53 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
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
#include "Refresh_Draw.h"
#include "DataSave.h"
#include <iostream>
#include <cmath>
//bool newGame(PARAMETERS int level, )
//{
//First we create a window
//We load the scoreboards from the PLAYER_DATAPATH
//Now we load the UI elements
/*
LIVES ---------- LEVEL X SCORE: XXXXX
|--------------| |-------------|
| | | |
| CONTROLS | MAIN LEVEL FRAME | SCOREBOARD |
| | | |
|--------------| |-------------|
*/
//We load the level progression
//We start the gameloop
//IF the game is lost the data of the current player is saved
//}
//const float GENERAL_REFRESH_FPS = 29.4;
int main()
{
FreeConsole(); //Hides the console.
srand(time(NULL)); //Random numbers.
bool done = false, draw = false, nextLevel = false;
playerArray data;
playerData currentPlayer;
std::string introduce_name = "";
ALLEGRO_DISPLAY * display = nullptr;
ALLEGRO_TIMER *general_timer = nullptr;
ALLEGRO_EVENT_QUEUE *event_queue = nullptr;
ALLEGRO_KEYBOARD_STATE keyState;
ALLEGRO_COLOR back_color = al_map_rgb(0, 0, 0);
ALLEGRO_BITMAP *icon;
//Initialize Programme
initializeFramework(true, true, false, true, true, true, 2);
loadSprite(icon, "PacmanIcon.png");
createDisplay(display, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, "Pacman", standard, icon);
initializeEvent(display, event_queue, true, true, false);
//Creating Classes
Level level;
UserInterface UI;
Player pacman(level);
Enemy rg(RED, level);
Enemy bg(BLUE, level);
Enemy og(ORANGE, level);
Enemy pg(PINK, level);
//Load Data
LoadAndDecryptFile(data);
//Initial
UI.drawUI();
UI.refreshScoreboard(data);
drawLevel(level, pacman, rg, pg, og, bg, back_color, SCREEN_WIDTH, SCREEN_HEIGHT);
showScreen(back_color);
//Set Timer
createTimer(general_timer, GENERAL_REFRESH_FPS);
registerTimer(event_queue, general_timer);
startTimer(general_timer);
//Game Loop
while (!done) {
ALLEGRO_EVENT events;
waitEvent(event_queue, events);
//Keyboard events
if (events.type == EVENT_KEY_DOWN) {
if (UI.GetNameDone()) {
switch (events.keyboard.keycode) {
case KEY_D:
case KEY_RIGHT: {
if (!(UI.GetPause() || UI.GetReset() || UI.GetGameOver() || pacman.GetDead())) {
if (movementPossible(level.GetMap(), pacman.GetPositionX() + 1, pacman.GetPositionY())) {
pacman.SetDirection(RIGHT);
}
pacman.SetNextDirection(RIGHT);
}
}break;
case KEY_A:
case KEY_LEFT: {
if (!(UI.GetPause() || UI.GetReset() || UI.GetGameOver() || pacman.GetDead())) {
if (movementPossible(level.GetMap(), pacman.GetPositionX() - 1, pacman.GetPositionY())) {
pacman.SetDirection(LEFT);
}
pacman.SetNextDirection(LEFT);
}
}break;
case KEY_W:
case KEY_UP: {
if (!(UI.GetPause() || UI.GetReset() || UI.GetGameOver() || pacman.GetDead())) {
if (movementPossible(level.GetMap(), pacman.GetPositionX(), pacman.GetPositionY() - 1)) {
pacman.SetDirection(UP);
}
pacman.SetNextDirection(UP);
}
}break;
case KEY_S:
case KEY_DOWN: {
if (!(UI.GetPause() || UI.GetReset() || UI.GetGameOver() || pacman.GetDead())) {
if (movementPossible(level.GetMap(), pacman.GetPositionX(), pacman.GetPositionY() + 1)) {
pacman.SetDirection(DOWN);
}
pacman.SetNextDirection(DOWN);
}
}break;
case KEY_P: {
if (!UI.GetGameOver()) {
if (UI.GetPause()) {
UI.SetPause(false);
UI.SetAudio(true);
UI.PlayThisSong(UI.GetpacmanSongInstance());
}
else {
UI.SetPause(true);
UI.SetAudio(false);
}
}
}break;
case KEY_O: {
UI.SetKeyAudio();
}break;
case KEY_ENTER: {
if (UI.GetGameOver()) {
UI.SetGameOver(false);
UI.SetReset(true);
UI.SetResetCounter(2);
}
}break;
case KEY_N: {
nextLevel = true;
}break;
case KEY_ESCAPE: {
done = true;
}
}
}
else {
switch (events.keyboard.keycode) {
case KEY_A: if (introduce_name.length() < 10) introduce_name += 'a'; break;
case KEY_B: if (introduce_name.length() < 10) introduce_name += 'b'; break;
case KEY_C: if (introduce_name.length() < 10) introduce_name += 'c'; break;
case KEY_D: if (introduce_name.length() < 10) introduce_name += 'd'; break;
case KEY_E: if (introduce_name.length() < 10) introduce_name += 'e'; break;
case KEY_F: if (introduce_name.length() < 10) introduce_name += 'f'; break;
case KEY_G: if (introduce_name.length() < 10) introduce_name += 'g'; break;
case KEY_H: if (introduce_name.length() < 10) introduce_name += 'h'; break;
case KEY_I: if (introduce_name.length() < 10) introduce_name += 'i'; break;
case KEY_J: if (introduce_name.length() < 10) introduce_name += 'j'; break;
case KEY_K: if (introduce_name.length() < 10) introduce_name += 'k'; break;
case KEY_L: if (introduce_name.length() < 10) introduce_name += 'l'; break;
case KEY_M: if (introduce_name.length() < 10) introduce_name += 'm'; break;
case KEY_N: if (introduce_name.length() < 10) introduce_name += 'n'; break;
case KEY_O: if (introduce_name.length() < 10) introduce_name += 'o'; break;
case KEY_P: if (introduce_name.length() < 10) introduce_name += 'p'; break;
case KEY_Q: if (introduce_name.length() < 10) introduce_name += 'q'; break;
case KEY_R: if (introduce_name.length() < 10) introduce_name += 'r'; break;
case KEY_S: if (introduce_name.length() < 10) introduce_name += 's'; break;
case KEY_T: if (introduce_name.length() < 10) introduce_name += 't'; break;
case KEY_U: if (introduce_name.length() < 10) introduce_name += 'u'; break;
case KEY_V: if (introduce_name.length() < 10) introduce_name += 'v'; break;
case KEY_W: if (introduce_name.length() < 10) introduce_name += 'w'; break;
case KEY_X: if (introduce_name.length() < 10) introduce_name += 'x'; break;
case KEY_Y: if (introduce_name.length() < 10) introduce_name += 'y'; break;
case KEY_Z: if (introduce_name.length() < 10) introduce_name += 'z'; break;
case KEY_BACKSPACE: if (introduce_name.length() > 0) introduce_name.pop_back(); break;
case KEY_ENTER: {
UI.SetNameDone(true);
UI.SetPlayerName(introduce_name);
introduce_name = "";
UI.SetReset(true);
UI.SetSubLiveReset(false);
} break;
case KEY_ESCAPE: {
done = true;
}
}
if(!UI.GetNameDone()) UI.SetPlayerName(introduce_name);
}
}
//Display events
else if (events.type == EVENT_DISPLAY_CLOSE) {
done = true;
}
//Timer Events
else if (events.type == EVENT_TIMER) {
draw = true;
if (UI.GetNameDone()) {
if (!UI.GetPause() && !UI.GetReset() && !UI.GetGameOver()) {
refreshLevel(level, pacman, rg, pg, og, bg, UI);
if (pacman.GetProgress() == level.GetObjective() || nextLevel)
{
if (level.GetCurrentLevel() < level.GetNumMap()) {
level.NextLevel();
UI.increaseLevel();
UI.SetReset(true);
UI.SetSubLiveReset(false);
pacman.ResetProgress();
nextLevel = false;
}
else {
level.GameReset();
UI.resetLevel();
UI.SetReset(true);
UI.SetSubLiveReset(false);
pacman.ResetProgress();
nextLevel = false;
}
}
}
else if (UI.GetReset() && !UI.GetPause()) {
UI.SetAudio(false);
if (UI.GetResetCounter() == 1) {
UI.MakeReset(level, pacman, rg, pg, og, bg);
}
if (UI.GetResetCounter() < RESET_COUNTER_MAX) {
UI.SetResetCounter(UI.GetResetCounter() + 1);
}
else {
UI.SetAudio(true);
UI.PlayThisSong(UI.GetpacmanSongInstance());
UI.SetResetCounter(1);
UI.SetReset(false);
}
}
else if (UI.GetGameOver() && UI.GetAuxGameOver()) {
UI.SetAudio(false);
currentPlayer.name = UI.GetPlayerName();
currentPlayer.score = UI.GetScore();
AddData(data, currentPlayer);
UI.MakeGameOver(level, pacman, rg, pg, og, bg);
UI.SetAuxGameOver(false);
}
}
}
if (draw) {
UI.drawUI();
UI.refreshScoreboard(data);
drawLevel(level, pacman, rg, pg, og, bg, back_color, SCREEN_WIDTH, SCREEN_HEIGHT);
if (UI.GetReset() && !UI.GetPause()) {
UI.drawReset();
}
if (UI.GetGameOver()) {
UI.drawGameOver();
}
if (UI.GetPause() && !UI.GetGameOver()) {
UI.drawPause();
}
if (!UI.GetNameDone()) {
UI.drawName();
}
showScreen(back_color);
draw = false;
}
}
//Saving Data
SaveAndEncrypt(data);
//Destroying programme
destroyFramework(true, true, false, true, true, true);
return 0;
}