-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameWindow.cpp
548 lines (454 loc) · 15.9 KB
/
GameWindow.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "GameWindow.h"
#include "main.h"
#include <iostream>
#include <cmath>
#define WHITE al_map_rgb(255, 255, 255)
#define BLACK al_map_rgb(0, 0, 0)
#define ORANGE_LIGHT al_map_rgb(255, 196, 87)
#define ORANGE_DARK al_map_rgb(255, 142, 71)
#define PURPLE al_map_rgb(149, 128, 255)
#define BLUE al_map_rgb(77, 129, 179)
#define min(a, b) ((a) < (b)? (a) : (b))
#define max(a, b) ((a) > (b)? (a) : (b))
#define time_limit 3600
#define score_limit_1 10
#define score_limit_2 20
#define score_limit_3 35
#define score_limit_4 50
using namespace std;
int pause;
int click=0;
int posx,posy;
int mode;
int eaten=0;
int bonus=0;
void
GameWindow::game_init()
{
bool done=false;
int x=0;
int y=500;
const int maxFrame=15;
int curFrame=-1;
int curY=0;
int frameCount=0;
int frameDelay=5;
float frameWidth=256;
float frameHeight=249.333;
char buffer[50];
icon = al_load_bitmap("images/fish.png");
al_set_display_icon(display, icon);
c= al_load_bitmap("images/charlie_burned.png");
image=al_load_bitmap("images/test.jpg");
image0=al_load_bitmap("images/dark.jpg");
image10=al_load_bitmap("images/fish6.jpg");
al_convert_mask_to_alpha(image,al_map_rgb(106,776,48));
al_convert_mask_to_alpha(image0,al_map_rgb(106,776,48));
while(!done){
al_wait_for_event(event_queue,&event);
if(event.type==ALLEGRO_EVENT_TIMER){
if(event.timer.source=timer){
if(++frameCount>=frameDelay)
{
frameCount=0;
if(curFrame>=4){
curFrame=0;
curY++;
}
else curFrame++;
if(curY>=3)curY=0;
}
if(x<600){x+=5;}
if(x>=600){x=0;y=500;}
}
}
else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)throw 1;
else if(event.type== ALLEGRO_EVENT_MOUSE_BUTTON_DOWN&&event.mouse.x>=0){
cout<<"moving to main page"<<endl;
if(event.mouse.x>=0&&event.mouse.x<=256&&event.mouse.y>=0&&event.mouse.y<=249.333){mode=1;break;}
else if(event.mouse.x>=900-256&&event.mouse.x<=900&&event.mouse.y>=0&&event.mouse.y<=249.333){mode=2;level->level=1;break;}
}
al_draw_bitmap_region(image,curFrame*frameWidth,curY*frameHeight,frameWidth,frameHeight,0,0,0);
al_draw_bitmap_region(image,curFrame*frameWidth,curY*frameHeight,frameWidth,frameHeight,900-frameWidth,0,0);
al_draw_bitmap(image0,150,400,2);
al_draw_text(font, al_map_rgb(0, 0, 0), 100, 300,0, "Timing mode");
al_draw_text(font, al_map_rgb(0, 0, 0), 700, 300, 0, "Level mode");
al_draw_text(font0, al_map_rgb(12,37,204),350,300,0,"AQUA DINE");
al_flip_display();
al_clear_to_color(al_map_rgb(255,255,255));
}
background=al_load_bitmap("images/tex_Water.jpg");
al_draw_bitmap(background, 0, 0, 0);
al_flip_display();
al_reserve_samples(3);
al_set_display_icon(display, icon);
sample = al_load_sample("start.wav");
startSound = al_create_sample_instance(sample);
al_set_sample_instance_playmode(startSound, ALLEGRO_PLAYMODE_ONCE);
al_attach_sample_instance_to_mixer(startSound, al_get_default_mixer());
if(mode==2)sample = al_load_sample("sounds/waterwater.ogg");
else if(mode==1)sample =al_load_sample("sounds/mode1.ogg");
backgroundSound = al_create_sample_instance(sample);
al_set_sample_instance_playmode(backgroundSound, ALLEGRO_PLAYMODE_ONCE);
al_attach_sample_instance_to_mixer(backgroundSound, al_get_default_mixer());
level = new LEVEL((mode==1)?0:1);
}
bool
GameWindow::mouse_hover(int startx, int starty, int width, int height)
{
if(mouse_x >= startx && mouse_x <= startx + width)
if(mouse_y >= starty && mouse_y <= starty + height)
return true;
return false;
}
void
GameWindow::game_play()
{
int msg;
srand(time(NULL));
msg = -1;
game_reset();
game_begin();
while(msg != GAME_EXIT)
{
msg = game_run();
}
show_err_msg(msg);
}
void
GameWindow::show_err_msg(int msg)
{
if(msg == GAME_TERMINATE)
fprintf(stderr, "Game Terminated...");
else
fprintf(stderr, "unexpected msg: %d", msg);
game_destroy();
exit(9);
}
GameWindow::GameWindow()
{
if (!al_init())
show_err_msg(-1);
printf("Game Initializing...\n");
display = al_create_display(900, 800);
timer = al_create_timer(1.0 / FPS);
fish_pro = al_create_timer(1.0 / FPS);
event_queue = al_create_event_queue();
if(timer == NULL || fish_pro == NULL)
show_err_msg(-1);
if (display == NULL || event_queue == NULL)
show_err_msg(-1);
al_init_primitives_addon();
al_init_font_addon(); // initialize the font addon
al_init_ttf_addon(); // initialize the ttf (True Type Font) addon
al_init_image_addon(); // initialize the image addon
al_init_acodec_addon(); // initialize acodec addon
al_install_keyboard(); // install keyboard event
al_install_mouse(); // install mouse event
al_install_audio(); // install audio event
font = al_load_ttf_font("fonts/Caviar_Dreams_Bold.ttf",12,0); // load small font
Medium_font = al_load_ttf_font("fonts/Caviar_Dreams_Bold.ttf",24,0); //load medium font
Large_font = al_load_ttf_font("fonts/Caviar_Dreams_Bold.ttf",36,0); //load large font
font0=al_load_ttf_font("fonts/Fish.ttf",64,0);
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_mouse_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_timer_event_source(fish_pro));
al_stop_timer(timer);
al_start_timer(timer);
cout<<"before init"<<endl;
game_init();
cout<<"after init"<<endl;
}
void
GameWindow::game_begin()
{
printf(">>> Start Level[%d]\n", level->getLevel());
draw_running_map();
al_play_sample_instance(startSound);
al_get_sample_instance_playing(startSound);
al_play_sample_instance(backgroundSound);
al_start_timer(timer);
al_start_timer(fish_pro);
}
int
GameWindow::game_run()
{
char buffer[50];
int error = GAME_CONTINUE;
if (!al_is_event_queue_empty(event_queue)) {
error = process_event();
if(mode==1&&Coin_Inc_Count>=time_limit||eaten==1){
eaten=0;
al_stop_timer(timer);
al_clear_to_color(al_map_rgb(255,255,255));
cout<<"score: "<<score<<endl;
if(score>=score_limit_4&&level->level==4)sprintf(buffer,"YOU WIN !!!");
else sprintf(buffer, "Your Score Is: %d", score);
al_draw_text(Medium_font, al_map_rgb(0, 0, 0), 70, 300, 0, buffer);
sprintf(buffer, "Your Level Is: %d",level->level);
if(mode==2)al_draw_text(Medium_font, al_map_rgb(0, 0, 0), 70, 360, 0, buffer);
al_draw_text(Medium_font, al_map_rgb(0, 0, 0), 70, 600, 0, "PRESS ENTER TO MAIN MENU");
al_draw_bitmap(image10,300,100,0);
al_flip_display();
score=0;
al_destroy_sample_instance(backgroundSound);
while(1){
al_wait_for_event(event_queue,&event);
if(event.type==ALLEGRO_EVENT_KEY_DOWN){
if(event.keyboard.keycode==ALLEGRO_KEY_ENTER){
game_init();
game_reset();
game_begin();
break;
}
}
}
}
}
return error;
}
void
GameWindow::game_reset()
{
// reset game and begin
charlie->hide=false;
lastClicked = -1;
Coin_Inc_Count = 0;
Fish_Pro_Count = 0;
mute = false;
redraw = false;
time_begin=0;
bonus=0;
FishSet.clear();
//BubbleSet.clear();
// stop sample instance
al_stop_sample_instance(backgroundSound);
al_stop_sample_instance(startSound);
// stop timer
al_stop_timer(timer);
al_stop_timer(fish_pro);
}
void
GameWindow::game_destroy()
{
game_reset();
al_destroy_display(display);
al_destroy_event_queue(event_queue);
al_destroy_font(font);
al_destroy_font(Medium_font);
al_destroy_font(Large_font);
al_destroy_timer(timer);
al_destroy_timer(fish_pro);
al_destroy_bitmap(icon);
al_destroy_bitmap(background);
al_destroy_sample(sample);
al_destroy_sample_instance(startSound);
al_destroy_sample_instance(backgroundSound);
}
int
GameWindow::process_event()
{
int instruction = GAME_CONTINUE;
// offset for pause window
int offsetX = 600/2 - 200;
int offsetY = 800/2 - 200;
al_wait_for_event(event_queue, &event);
redraw = false;
if(event.type == ALLEGRO_EVENT_TIMER) {
if(event.timer.source == timer) {
redraw = true;
Coin_Inc_Count = (Coin_Inc_Count + 1);
int score_limit;
switch(level->level)
{
case 1:score_limit=score_limit_1;break;
case 2:score_limit=score_limit_2;break;
case 3:score_limit=score_limit_3;break;
case 4:score_limit=score_limit_4;break;
}
if(mode==2&&score>=score_limit)
{
//score=0;
if(level->level==4){
eaten=1;
al_stop_timer(timer);
//return GAME_EXIT;
}
else {
charlie->hide=false;
level->setLevel(level->level+1);
c= al_load_bitmap("images/charlie_burned.png");
al_stop_sample_instance(backgroundSound);
al_stop_sample_instance(startSound);
game_reset();
game_begin();
}
}
}
else{
if(Fish_Pro_Count == 0) {
Fish *m = create_fish();
if(m != NULL)FishSet.push_back(m);
}
Fish_Pro_Count = (Fish_Pro_Count + 1) % level->getMonsterSpeed();
}
}
else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
return GAME_EXIT;
}
else if(event.type == ALLEGRO_EVENT_KEY_DOWN) {
switch(event.keyboard.keycode) {
case ALLEGRO_KEY_P:
/*TODO: handle pause event here*/
cout<<"P"<<endl;
pause=1;
goto pause;
break;
case ALLEGRO_KEY_M:
cout<<"M"<<endl;
mute = !mute;
if(mute)
al_stop_sample_instance(backgroundSound);
else
al_play_sample_instance(backgroundSound);
break;
case ALLEGRO_KEY_RIGHT:
cout<<"RIGHT"<<endl;
al_set_sample_instance_gain(backgroundSound,float(110)/float(100));
break;
case ALLEGRO_KEY_LEFT:
cout<<"LEFT"<<endl;
al_set_sample_instance_gain(backgroundSound,float(90)/float(100));
break;
}
}
else if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
}
else if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP){
click=0;
}
else if(event.type == ALLEGRO_EVENT_MOUSE_AXES){
if(event.mouse.x<=600)charlie->pos_x=event.mouse.x-50;
charlie->pos_y=event.mouse.y-37.5;
}
if(redraw) {
// update each object in game
pause=0;
pause:
while(pause){
al_stop_sample_instance(backgroundSound);
al_wait_for_event(event_queue,&event);
if(event.type==ALLEGRO_EVENT_KEY_DOWN){
if(event.keyboard.keycode==ALLEGRO_KEY_P){
if(!mute)al_play_sample_instance(backgroundSound);
break;
}
}
}
instruction = game_update();
if(instruction==GAME_EXIT)return instruction;
// Re-draw map
draw_running_map();
redraw = false;
}
return instruction;
}
void
GameWindow::draw_running_map()
{
unsigned int i, j;
al_clear_to_color(al_map_rgb(100, 100, 100));
al_draw_bitmap(background, 0, 0, 0);
al_draw_filled_rectangle(700, 0, 900, 800, al_map_rgb(50, 50, 50));
char buffer[50];
sprintf(buffer, "Time: %d", Coin_Inc_Count/60);
al_draw_text(Medium_font, al_map_rgb(255, 255, 255), 750, 20 + 30, 0, buffer);
sprintf(buffer, "Score: %d", score);
al_draw_text(Medium_font, al_map_rgb(255, 255, 255), 750, 20 + 2*30, 0, buffer);
sprintf(buffer,"Level: %d",level->level);
if(mode==1){
al_draw_text(font0, WHITE,740,500,0,"Timing");
al_draw_text(font0, WHITE,755,600,0,"Mode");
}
else if(mode==2){
al_draw_text(font0, WHITE,710,500,0,buffer);
al_draw_text(font0, WHITE,755,600,0,"Mode");
}
//stuffs with hiding 5 seconds
if(charlie->hide==true&&time_begin==0){
time_begin=Coin_Inc_Count/60;
c= al_load_bitmap("images/charlie_hide.png");
cout<<"hideen"<<endl;
}
if(charlie->hide==true&&(Coin_Inc_Count/60)-time_begin>3){
cout<<"turing back"<<endl;
charlie->hide=false;
c= al_load_bitmap("images/charlie_burned.png");
time_begin=0;
}
//stuffs with bubble||contorlling||
if(bonus==1){
1;
}
al_draw_bitmap(c,charlie->pos_x,charlie->pos_y,0);
for(i=0; i<FishSet.size(); i++)
{
FishSet[i]->Draw();
}
al_flip_display();
}
int
GameWindow::game_update()
{
unsigned int i, j;
// update every fish
// check if it is destroyed
for(i=0; i < FishSet.size(); i++)
{
bool isDestroyed = false;
if(abs(charlie->pos_x-FishSet[i]->pos_x)<40&&abs(charlie->pos_y-FishSet[i]->pos_y)<40){
//collide with shark
bonus=0;
if(FishSet[i]->getScore()&&charlie->hide==false){
eaten=1;
isDestroyed=false;
}
//collide with shark,hidden
else if(FishSet[i]->getScore()&&charlie->hide==true){
eaten=0;
isDestroyed=false;
}
//collide with yellow
else if(FishSet[i]->getWorth()==2){
isDestroyed=true;
charlie->hide=true;
}
//collide with golden
else if(FishSet[i]->getWorth()==3){
bonus=1;
isDestroyed=true;
}
else isDestroyed=true;
score+=FishSet[i]->getWorth();
}
if(FishSet[i]->pos_x>=660||FishSet[i]->pos_x<0)isDestroyed=true;
FishSet[i]->Move();
if(isDestroyed)
{
Fish *m = FishSet[i];
FishSet.erase(FishSet.begin() + i);
i--;
delete m;
}
}
return GAME_CONTINUE;
}
Fish*
GameWindow::create_fish()
{
Fish *m = NULL;
m= new Fish(level->level);
return m;
}