-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathgame-MemPointer.cpp
More file actions
269 lines (242 loc) · 6.58 KB
/
game-MemPointer.cpp
File metadata and controls
269 lines (242 loc) · 6.58 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
#define ALLEGRO_STATICLINK
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <stdio.h>
#include "..\allegro_linkers.h"
#include "Location-MemPointer.h"
bool testRunning = false;
int testState = 0;
int testTime = 0;
Location selfLocation(12, 16);
LocationPointerSimulator* ballLocation = LocationPointerSimulator::NewLocationPointer(new Location(15, 13));
BYTE map[24][32];
int keys[255];
bool checkWin()
{
Location* blocation = ballLocation->getLocation();
return (map[blocation->y][blocation->x] == 4);
}
//using a method to make the backgroumd
void drawBackground()
{ for (int i = 0; i <= 24; i++)
{
for (int t = 0; t <= 32; t++)
{
if (map[i][t] == 1)
al_draw_filled_rectangle(t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, al_map_rgb(128, 255, 255));
else if(map[i][t] == 2)
al_draw_filled_rectangle(t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, al_map_rgb(255, 128, 0));
else if(map[i][t] == 3)
al_draw_filled_rectangle(t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, al_map_rgb(255, 0, 0));
else if(map[i][t] == 4)
al_draw_filled_rectangle(t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, al_map_rgb(0, 0, 0));
}
}
}
//method used to draw objects in desired location
void drawObjects()
{
Location* blocation = ballLocation->getLocation();
al_draw_filled_rectangle(selfLocation.x * 20, selfLocation.y * 20,
(selfLocation.x + 1) * 20, (selfLocation.y + 1) * 20,
al_map_rgb(255, 255, 255));
al_draw_filled_circle(blocation->x * 20 + 10, blocation->y * 20 + 10,
10, al_map_rgb(100, 100, 100));
}
//make th specified HUD
void drawHUD(ALLEGRO_FONT *font)
{
if (!testRunning)
{
al_draw_text(font, al_map_rgb(50, 50, 50), 10, 10, 0, "Arrow Keys: Move");
al_draw_text(font, al_map_rgb(50, 50, 50), 10, 25, 0, "Spacebar: Swap");
al_draw_text(font, al_map_rgb(50, 50, 50), 10, 40, 0, "Tab: Begin Test");
}
else
{
al_draw_text(font, al_map_rgb(50, 50, 50), 10, 10, 0, "Automated test running, input disabled.");
char text[128];
sprintf(text, "Wins: %d/10", testState);
al_draw_text(font, al_map_rgb(50, 50, 50), 10, 25, 0, text);
sprintf(text, "Time: %1.1fs", testTime / 1000.00);
al_draw_text(font, al_map_rgb(50, 50, 50), 10, 40, 0, text);
}
}
//taking inputs from keyboards
void keyboardEvent()
{
if (testRunning)
return;
Location* blocation = ballLocation->getLocation();
Location moveOffset;
if (keys[ALLEGRO_KEY_UP])
moveOffset = Location(0, -1);
else if (keys[ALLEGRO_KEY_DOWN])
moveOffset = Location(0, 1);
else if (keys[ALLEGRO_KEY_LEFT])
moveOffset = Location(-1, 0);
else if (keys[ALLEGRO_KEY_RIGHT])
moveOffset = Location(1, 0);
else if (keys[ALLEGRO_KEY_TAB])
{
testRunning = true;
return;
}
else if (keys[ALLEGRO_KEY_SPACE])
{
moveOffset = Location(blocation->x, blocation->y) - selfLocation;
if (abs(moveOffset.x) + abs(moveOffset.y) <= 1)
{
moveOffset = selfLocation; //placehold
selfLocation.x = blocation->x;
selfLocation.y = blocation->y;
blocation->x = moveOffset.x;
blocation->y = moveOffset.y;
}
return;
}
else
return;
Location newSelf = selfLocation + moveOffset;
Location newBall = Location(blocation->x, blocation->y);
if (newSelf == newBall)
newBall += moveOffset;
if (map[newBall.y][newBall.x] == 1 || map[newSelf.y][newSelf.x] == 1)
return;
blocation->x = newBall.x;
blocation->y = newBall.y;
selfLocation = newSelf;
}
//main method to execute
int main(void)
{
char* error = "";
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
do
{
ZeroMemory(&keys, 255);
FILE* file = fopen("game.map", "rb");
if (!file)
{
error = "failed to initialize map!";
break;
}
else
{
fread(map, 1, 24*32, file);
fclose(file);
}
if(!al_init())
{
error = "failed to initialize allegro!";
break;
}
if(!al_init_primitives_addon())
{
error = "failed to initialize primitives!";
break;
}
al_init_font_addon();
if(!al_init_ttf_addon())
{
error = "failed to initialize ttfs!";
break;
}
if(!al_install_keyboard())
{
error = "failed to initialize keyboard!";
break;
}
if(!(font = al_load_ttf_font("arial.ttf", 18, 0)))
{
error = "failed to initialize font!";
break;
}
if(!(timer = al_create_timer(1.0 / 10)))
{
error = "failed to initialize timer!";
break;
}
if(!(display = al_create_display(640, 480)))
{
error = "failed to initialize display!";
break;
}
if(!(event_queue = al_create_event_queue()))
{
error = "failed to initialize event_queue!";
break;
}
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_start_timer(timer);
while(1)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
break;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
break;
else if (ev.keyboard.keycode <= 255)
keys[ev.keyboard.keycode] = true;
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP)
{
if (ev.keyboard.keycode <= 255)
keys[ev.keyboard.keycode] = false;
}
else if (ev.type == ALLEGRO_EVENT_KEY_CHAR)
keyboardEvent();
if (testRunning)
testTime += 100;
if (al_is_event_queue_empty(event_queue))
{
bool won = checkWin();
al_clear_to_color(al_map_rgb(255, 255, 255));
drawBackground();
drawObjects();
if (won && testRunning && testState < 50)
{
testState++;
auto clean = ballLocation;
ballLocation = LocationPointerSimulator::NewLocationPointer(new Location(15 + (testState % 4), 13 + (testState % 2)));
delete clean;
}
else if (testRunning && testState >= 50)
{
al_draw_text(font, al_map_rgb(200, 200, 200), 320, 240, 1, "Good job! You've completed the lab!");
al_flip_display();
continue;
}
else if (testRunning && testTime >= 11000)
{
al_draw_text(font, al_map_rgb(200, 200, 200), 320, 240, 1, "Lab failed! Ball must be moved to the winning spot 50 times in 10 seconds!");
al_flip_display();
continue;
}
drawHUD(font);
al_flip_display();
}
}
} while (0);
if (strlen(error) > 0)
{
al_show_native_message_box(NULL, NULL, "error", error, NULL, NULL);
return -1;
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}