-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
1234 lines (1162 loc) · 31.8 KB
/
main.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// QUARTERMASTER: Collect the falling food cans!
//
// Copyleft <--(C)--> Copyright 2019 Hi-Ban
//
// Original concept and graphics by wargaming.net
// Game code, sound and part of the graphic assets by Hi-Ban.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <string>
#include <sys/stat.h>
#include <iostream>
#include <ctime>
#include "SFont.h"
//---------------------------------------------------//
SDL_Surface *screen;
SDL_Surface *allcans;
SDL_Surface *background;
SDL_Surface *borders;
SDL_Surface *help;
SDL_Surface *p_upleft;
SDL_Surface *p_upleft_b;
SDL_Surface *p_upright;
SDL_Surface *p_upright_b;
SDL_Surface *p_downleft;
SDL_Surface *p_downleft_b;
SDL_Surface *p_downright;
SDL_Surface *p_downright_b;
Mix_Chunk *sound_lvl_01 = NULL;
Mix_Chunk *sound_lvl_02 = NULL;
Mix_Chunk *sound_lvl_03 = NULL;
Mix_Chunk *sound_lvl_04 = NULL;
Mix_Chunk *sound_lvl_05 = NULL;
Mix_Chunk *sound_lvl_06 = NULL;
Mix_Chunk *sound_lvl_07 = NULL;
Mix_Chunk *sound_lvl_08 = NULL;
Mix_Chunk *sound_lvl_09 = NULL;
Mix_Chunk *sound_lvl_10 = NULL;
Mix_Chunk *sound_lvl_11 = NULL;
Mix_Chunk *sound_move = NULL;
Mix_Chunk *sound_good = NULL;
Mix_Chunk *sound_bad = NULL;
Mix_Chunk *sound_miss = NULL;
Mix_Chunk *sound_gameover = NULL;
Mix_Chunk *sound_music = NULL;
static SFont_Font* font;
static SDL_Surface *font_bitmap_surface = NULL;
//init global game variables
int quit = 0;
int gameab = 0; // 0 - game A / 1 - game B
int gameover = 0;
int quittomenu = 0;
int pausegame = 0;
int score = 0;
int level = 1;
int highestlevel = 1;
int misses = 0;
int speed = 928;
int cans[5][2] = {{8, 8}, {8, 8}, {8, 8}, {8, 8},{8, 8}}; // {color, position} - [0 = white can / 1 = black can] - [0 = upleft / 1 = upright / 2 = downleft / 3 = downright] // 8 is just a forced non-valid value so it treats it as "no-cans"
int player = 0; // 0 = upleft / 1 = upright / 2 = downleft / 3 = downright
int dirty = 0;
int can_coordx = 1;
int can_coordy = 14;
int dest_coordx = 1;
int dest_coordy = 38;
int can_width = 24;
int can_height = 24;
int alternate = 0;
int dropped = 0;
int badmiss = 0;
int blink = 0;
int hiscore_a = 0;
int hiscore_b = 0;
int hiscore_aplus = 0;
int hiscore_bplus = 0;
int konamicode = 0; // what's this thing?
uint32_t curr_time = 0;
uint32_t old_time = 0;
std::string homedir = getenv("HOME");
void loadGraphics() {
allcans = IMG_Load("assets/trans_cans.png");
background = IMG_Load("assets/background.png");
borders = IMG_Load("assets/borders.png");
help = IMG_Load("assets/help.png");
p_upleft = IMG_Load("assets/player_left_up.png");
p_upleft_b = IMG_Load("assets/player_left_up_b.png");
p_upright = IMG_Load("assets/player_right_up.png");
p_upright_b = IMG_Load("assets/player_right_up_b.png");
p_downleft = IMG_Load("assets/player_left_down.png");
p_downleft_b = IMG_Load("assets/player_left_down_b.png");
p_downright = IMG_Load("assets/player_right_down.png");
p_downright_b = IMG_Load("assets/player_right_down_b.png");
if ((!allcans) || (!background) || (!borders) || (!help) || (!p_upleft) || (!p_upright) || (!p_downleft) || (!p_downright) || (!p_upleft_b) || (!p_upright_b) || (!p_downleft_b) || (!p_downright_b)) {
printf("Graphics failed loading\n");
exit(1);
} else {
printf("Graphics loaded successfully\n");
}
}
void loadSounds() {
sound_move = Mix_LoadWAV("assets/move.wav");
sound_good = Mix_LoadWAV("assets/good.wav");
sound_bad = Mix_LoadWAV("assets/bad.wav");
sound_miss = Mix_LoadWAV("assets/miss.wav");
sound_gameover = Mix_LoadWAV("assets/gameover.wav");
sound_music = Mix_LoadWAV("assets/music.wav");
sound_lvl_01 = Mix_LoadWAV("assets/lvl_01.wav");
sound_lvl_02 = Mix_LoadWAV("assets/lvl_02.wav");
sound_lvl_03 = Mix_LoadWAV("assets/lvl_03.wav");
sound_lvl_04 = Mix_LoadWAV("assets/lvl_04.wav");
sound_lvl_05 = Mix_LoadWAV("assets/lvl_05.wav");
sound_lvl_06 = Mix_LoadWAV("assets/lvl_06.wav");
sound_lvl_07 = Mix_LoadWAV("assets/lvl_07.wav");
sound_lvl_08 = Mix_LoadWAV("assets/lvl_08.wav");
sound_lvl_09 = Mix_LoadWAV("assets/lvl_09.wav");
sound_lvl_10 = Mix_LoadWAV("assets/lvl_10.wav");
sound_lvl_11 = Mix_LoadWAV("assets/lvl_11.wav");
if ((!sound_move) || (!sound_good) || (!sound_bad) || (!sound_miss) || (!sound_gameover) || (!sound_music) || (!sound_lvl_01) || (!sound_lvl_02) || (!sound_lvl_03) || (!sound_lvl_04) || (!sound_lvl_05) || (!sound_lvl_06) || (!sound_lvl_07) || (!sound_lvl_08) || (!sound_lvl_09) || (!sound_lvl_10) || (!sound_lvl_11)) {
printf("Sounds failed loading\n");
exit(1);
} else {
printf("Sounds loaded successfully\n");
}
}
void freeSounds() {
Mix_FreeChunk(sound_lvl_01);
Mix_FreeChunk(sound_lvl_02);
Mix_FreeChunk(sound_lvl_03);
Mix_FreeChunk(sound_lvl_04);
Mix_FreeChunk(sound_lvl_05);
Mix_FreeChunk(sound_lvl_06);
Mix_FreeChunk(sound_lvl_07);
Mix_FreeChunk(sound_lvl_08);
Mix_FreeChunk(sound_lvl_09);
Mix_FreeChunk(sound_lvl_10);
Mix_FreeChunk(sound_lvl_11);
Mix_FreeChunk(sound_move);
Mix_FreeChunk(sound_good);
Mix_FreeChunk(sound_bad);
Mix_FreeChunk(sound_miss);
Mix_FreeChunk(sound_gameover);
Mix_FreeChunk(sound_music);
}
void init_font() { // loads SFont bitmap font
SDL_FreeSurface(font_bitmap_surface);
font_bitmap_surface = IMG_Load("assets/font_watch.png");
if (!font_bitmap_surface) {
fprintf(stderr, "font load error\n");
exit(1);
}
font = SFont_InitFont(font_bitmap_surface);
if (!font) {
fprintf(stderr, "font init error\n");
exit(1);
}
}
void saveHiscores(){ // writes high scores to a file
if ((gameab == 0) && (konamicode != 10) && (score >= hiscore_a)) {
hiscore_a = score;
} else if ((gameab == 1) && (konamicode != 10) && (score >= hiscore_b)) {
hiscore_b = score;
} else if ((gameab == 0) && (konamicode == 10) && (score >= hiscore_aplus)) {
hiscore_aplus = score;
} else if ((gameab == 1) && (konamicode == 10) && (score >= hiscore_bplus)) {
hiscore_bplus = score;
}
std::string hiscorefile = (homedir + "/.quartermaster/hiscore.hi");
FILE * cfile;
cfile = fopen(hiscorefile.c_str(), "w");
if (cfile == NULL) {
printf("Failed to open hi-score file for writing.\n");
return;
}
fprintf(cfile,
"GAMEA %d\n"
"GAMEB %d\n"
"GAME+A %d\n"
"GAME+B %d\n",
hiscore_a,
hiscore_b,
hiscore_aplus,
hiscore_bplus);
fclose(cfile);
}
void loadHiscores(){ // reads high scores from a file
std::string hiscorefile = (homedir + "/.quartermaster/hiscore.hi");
FILE * cfile;
char line[4096];
cfile = fopen(hiscorefile.c_str(), "r");
if (cfile == NULL) {
printf("Failed to open hi-score file for reading.\n");
return;
}
while (fgets(line, sizeof(line), cfile)) {
char *arg = strchr(line, ' ');
int value;
if (!arg) {
continue;
}
*arg = '\0';
arg++;
if (!strcmp(line, "GAMEA")) {
sscanf(arg, "%d", &value);
hiscore_a = value;
} else if (!strcmp(line, "GAMEB")) {
sscanf(arg, "%d", &value);
hiscore_b = value;
} else if (!strcmp(line, "GAME+A")) {
sscanf(arg, "%d", &value);
hiscore_aplus = value;
} else if (!strcmp(line, "GAME+B")) {
sscanf(arg, "%d", &value);
hiscore_bplus = value;
}
}
fclose(cfile);
}
void getCanCoords(int number, int color, int position){ // determines the coordinates of source and destination surfaces for the can sprites.
can_coordx = 1;
can_coordy = 14;
dest_coordx = 1;
dest_coordy = 54;
can_width = 24;
can_height = 24;
if (number == 1) {
can_coordx += 23;
can_coordy += 9;
dest_coordx += 23;
dest_coordy += 9;
} else if (number == 2) {
can_coordx += 46;
can_coordy += 16;
dest_coordx += 46;
dest_coordy += 16;
} else if (number == 3) {
can_coordx += 69;
can_coordy += 27;
dest_coordx += 69;
dest_coordy += 27;
}
if ((position == 2) || (position == 3)) {
can_coordy += 48;
dest_coordy += 48;
}
if (number == 4) {
can_coordx = 76;
can_coordy = 137;
dest_coordx = 76;
dest_coordy = 177;
can_width = 32;
can_height = 24;
}
if (color == 0) {
can_coordy += 24;
}
if ((position == 1) || (position == 3)) {
can_coordx = 320 - can_width - can_coordx;
dest_coordx = 320 - can_width - dest_coordx;
}
if ((position > 3) || (color > 1)) {
can_coordx = 0;
can_coordy = 0;
dest_coordx = 0;
dest_coordy = 0;
can_width = 0;
can_height = 0;
}
}
void writeScore(SDL_Surface *surface, int score) {
char buffer[8];
sprintf(buffer, "%d", score);
if (score < 10) { // single digit (1 digit - 15 pixels)
SFont_Write(surface, font, 299, 19, buffer); // paint current score
} else if ((score >= 10) && (score < 100)) { // two digits
SFont_Write(surface, font, 284, 19, buffer); // paint current score
} else if ((score >= 100) && (score < 1000)) { // three digits
SFont_Write(surface, font, 269, 19, buffer); // paint current score
} else if ((score >= 1000) && (score < 10000)) { // four digits
SFont_Write(surface, font, 254, 19, buffer); // paint current score
}
if (((gameab == 0) && (konamicode != 10) && (score >= hiscore_a)) || ((gameab == 1) && (konamicode != 10) && (score >= hiscore_b)) || ((gameab == 0) && (konamicode == 10) && (score >= hiscore_aplus)) || ((gameab == 1) && (konamicode == 10) && (score >= hiscore_bplus))) { // if current score is equal or higher than hi-score
SDL_Rect hiscor1;
hiscor1.x = 218;
hiscor1.y = 5;
hiscor1.w = 37;
hiscor1.h = 12;
SDL_Rect hiscor2;
hiscor2.x = 218;
hiscor2.y = 21;
hiscor2.w = 37;
hiscor2.h = 12;
SDL_BlitSurface(allcans, &hiscor1, screen, &hiscor2); // if current score is equal or higher than the current mode high-score, paint "hi-score" text
}
}
void writeHiScore() {
char buffer[8];
if ((gameab == 0) && (konamicode != 10)) {
sprintf(buffer, "%d", hiscore_a);
if (hiscore_a < 10) { // single digit
SFont_Write(screen, font, 299, 19, buffer); // paint "game a" high score
} else if ((hiscore_a >= 10) && (hiscore_a < 100)) { // two digits
SFont_Write(screen, font, 284, 19, buffer); // paint "game a" high score
} else if ((hiscore_a >= 100) && (hiscore_a < 1000)) { // three digits
SFont_Write(screen, font, 269, 19, buffer); // paint "game a" high score
} else if ((hiscore_a >= 1000) && (hiscore_a < 10000)) { // four digits
SFont_Write(screen, font, 254, 19, buffer); // paint "game a" high score
}
} else if ((gameab == 1) && (konamicode != 10)) {
sprintf(buffer, "%d", hiscore_b);
if (hiscore_b < 10) { // single digit
SFont_Write(screen, font, 299, 19, buffer); // paint "game b" high score
} else if ((hiscore_b >= 10) && (hiscore_b < 100)) { // two digits
SFont_Write(screen, font, 284, 19, buffer); // paint "game b" high score
} else if ((hiscore_b >= 100) && (hiscore_b < 1000)) { // three digits
SFont_Write(screen, font, 269, 19, buffer); // paint "game b" high score
} else if ((hiscore_b >= 1000) && (hiscore_b < 10000)) { // four digits
SFont_Write(screen, font, 254, 19, buffer); // paint "game b" high score
}
} else if ((gameab == 0) && (konamicode == 10)) {
sprintf(buffer, "%d", hiscore_aplus);
if (hiscore_aplus < 10) { // single digit
SFont_Write(screen, font, 299, 19, buffer); // paint "game a" high score
} else if ((hiscore_aplus >= 10) && (hiscore_aplus < 100)) { // two digits
SFont_Write(screen, font, 284, 19, buffer); // paint "game a" high score
} else if ((hiscore_aplus >= 100) && (hiscore_aplus < 1000)) { // three digits
SFont_Write(screen, font, 269, 19, buffer); // paint "game a" high score
} else if ((hiscore_aplus >= 1000) && (hiscore_aplus < 10000)) { // four digits
SFont_Write(screen, font, 254, 19, buffer); // paint "game a" high score
}
} else if ((gameab == 1) && (konamicode == 10)) {
sprintf(buffer, "%d", hiscore_bplus);
if (hiscore_bplus < 10) { // single digit
SFont_Write(screen, font, 299, 19, buffer); // paint "game b" high score
} else if ((hiscore_bplus >= 10) && (hiscore_bplus < 100)) { // two digits
SFont_Write(screen, font, 284, 19, buffer); // paint "game b" high score
} else if ((hiscore_bplus >= 100) && (hiscore_bplus < 1000)) { // three digits
SFont_Write(screen, font, 269, 19, buffer); // paint "game b" high score
} else if ((hiscore_bplus >= 1000) && (hiscore_bplus < 10000)) { // four digits
SFont_Write(screen, font, 254, 19, buffer); // paint "game b" high score
}
}
SDL_Rect hiscor1;
hiscor1.x = 218;
hiscor1.y = 5;
hiscor1.w = 37;
hiscor1.h = 12;
SDL_Rect hiscor2;
hiscor2.x = 218;
hiscor2.y = 21;
hiscor2.w = 37;
hiscor2.h = 12;
SDL_BlitSurface(allcans, &hiscor1, screen, &hiscor2); // paint "hi-score" text
}
int checkStep(){
if (cans[4][1] == player) { // if player grabbed a can
if (cans[4][0] == 0) {
dropped = 0;
badmiss = 0;
return 1; // can is white (+1 point)
} else if (cans[4][0] == 1) {
dropped = 0;
badmiss = 1;
return 2; // can is black (-3 points)
} else {
badmiss = 0;
return 0; // there's no can (nothing happens)
}
} else { // if player didnt grab a can
if (cans[4][0] == 0) {
dropped = 1;
badmiss = 1;
return 3; // white can fell to the floor (+1 misses)
} else if (cans[4][0] == 1) {
dropped = 1;
badmiss = 0;
return 4; // black can fell to the floor (nothing happens)
} else {
badmiss = 0;
return 0; // there's no can (nothing happens)
}
}
}
void nextStep() {
for (int i = 4; i > 0; --i) { //move the existing cans one step forward
cans[i][0] = cans[i-1][0];
cans[i][1] = cans[i-1][1];
}
if (alternate == 0){ // no can
cans[0][1] = 8;
cans[0][0] = 8;
alternate = 1;
} else if (alternate == 1) {
cans[0][1] = ((rand() % 4)); // determine a random position for the new can
if (gameab == 0) {
cans[0][0] = 0;
} else if (gameab == 1) {
int anum = ((rand() % 30));
if (anum >= level) {
cans[0][0] = 0; //can is white
} else {
cans[0][0] = 1; //can is black
}
}
alternate = 0;
}
int levelup = 5; // level goes up every X score. speed increases every level, but speed and level do not decrease, even if the score decreases.
if (konamicode == 10) {
levelup = 5; // the "game plus" modes were originally intended to level up faster than normal modes, but now normal and plus modes level up equally.
}
if ((score >= levelup) && (highestlevel == 1)) {
level = 2;
highestlevel = 2;
speed = 774;
} else if ((score >= (levelup * 2)) && (highestlevel == 2)) {
level = 3;
highestlevel = 3;
speed = 644;
} else if ((score >= (levelup * 3)) && (highestlevel == 3)) {
level = 4;
highestlevel = 4;
speed = 537;
} else if ((score >= (levelup * 4)) && (highestlevel == 4)) {
level = 5;
highestlevel = 5;
speed = 448;
} else if ((score >= (levelup * 5)) && (highestlevel == 5)) {
level = 6;
highestlevel = 6;
speed = 373;
} else if ((score >= (levelup * 6)) && (highestlevel == 6)) {
level = 7;
highestlevel = 7;
speed = 311;
} else if ((score >= (levelup * 7)) && (highestlevel == 7)) {
level = 8;
highestlevel = 8;
speed = 260;
} else if ((score >= (levelup * 8)) && (highestlevel == 8)) {
level = 9;
highestlevel = 9;
speed = 216;
} else if ((score >= (levelup * 9)) && (highestlevel == 9)) {
level = 10;
highestlevel = 10;
speed = 180;
} else if ((score >= (levelup * 10)) && (highestlevel == 10) && (konamicode == 10)) { // extra harder speed level only for secret "game plus" modes
level = 11;
highestlevel = 11;
speed = 160;
}
}
void redraw(){
SDL_FillRect(screen, NULL, 0x000000); // paint screen black
SDL_Rect borda;
borda.x = 0;
borda.y = 0;
borda.w = 320;
borda.h = 16;
SDL_Rect bordb;
bordb.x = 0;
bordb.y = 0;
bordb.w = 320;
bordb.h = 16;
SDL_BlitSurface(borders, &borda, screen, &bordb); // paint upper border
SDL_Rect bordc;
bordc.x = 0;
bordc.y = 16;
bordc.w = 320;
bordc.h = 16;
SDL_Rect bordd;
bordd.x = 0;
bordd.y = 224;
bordd.w = 320;
bordd.h = 16;
SDL_BlitSurface(borders, &bordc, screen, &bordd); // paint lower border
SDL_Rect rect;
rect.x = 0;
rect.y = 16;
rect.w = 320;
rect.h = 208;
if (badmiss == 0){ // paint normal character
if (player == 0) { // 0 = upleft / 1 = upright / 2 = downleft / 3 = downright
SDL_BlitSurface(p_upleft, NULL, screen, &rect);
} else if (player == 1) {
SDL_BlitSurface(p_upright, NULL, screen, &rect);
} else if (player == 2) {
SDL_BlitSurface(p_downleft, NULL, screen, &rect);
} else if (player == 3) {
SDL_BlitSurface(p_downright, NULL, screen, &rect);
}
} else if (badmiss == 1) { // paint sad character (character gets sad if he misses a white can or grabs a black can)
if (player == 0) { // 0 = upleft / 1 = upright / 2 = downleft / 3 = downright
SDL_BlitSurface(p_upleft_b, NULL, screen, &rect);
} else if (player == 1) {
SDL_BlitSurface(p_upright_b, NULL, screen, &rect);
} else if (player == 2) {
SDL_BlitSurface(p_downleft_b, NULL, screen, &rect);
} else if (player == 3) {
SDL_BlitSurface(p_downright_b, NULL, screen, &rect);
}
}
for (int i = 0; i < 5; ++i) {
getCanCoords(i, cans[i][0], cans[i][1]);
SDL_Rect rect1;
rect1.x = can_coordx;
rect1.y = can_coordy;
rect1.w = can_width;
rect1.h = can_height;
SDL_Rect rect2;
rect2.x = dest_coordx;
rect2.y = dest_coordy;
rect2.w = can_width;
rect2.h = can_height;
if ((i < 4) || ((i == 4) && (dropped == 1) && (blink == 0))){
SDL_BlitSurface(allcans, &rect1, screen, &rect2); // paint cans
}
}
if ((misses >= 1) && (misses <=3)) {
SDL_Rect rect3;
rect3.x = 98;
rect3.y = 184;
rect3.w = 28 + (29 * misses);
rect3.h = 24;
SDL_Rect rect4;
rect4.x = 98;
rect4.y = 200;
rect4.w = 28 + (29 * misses);
rect4.h = 24;
SDL_BlitSurface(allcans, &rect3, screen, &rect4); // paint "miss" cans
}
if (pausegame == 1){
SDL_Rect rect5;
rect5.x = 5;
rect5.y = 184;
rect5.w = 36;
rect5.h = 24;
SDL_Rect rect6;
rect6.x = 5;
rect6.y = 200;
rect6.w = 36;
rect6.h = 24;
SDL_BlitSurface(allcans, &rect5, screen, &rect6); // paint pause text
}
if (gameab == 0) { // game A
SDL_Rect rect7a;
rect7a.x = 140;
rect7a.y = 7;
rect7a.w = 40;
rect7a.h = 12;
SDL_Rect rect8a;
rect8a.x = 4;
rect8a.y = 23;
rect8a.w = 40;
rect8a.h = 12;
if (konamicode == 10) {
rect7a.w = 47;
rect8a.w = 47;
}
SDL_BlitSurface(allcans, &rect7a, screen, &rect8a); // paint upper "game a" text
} else if (gameab == 1) { // game B
SDL_Rect rect7b;
rect7b.x = 140;
rect7b.y = 19;
rect7b.w = 40;
rect7b.h = 12;
SDL_Rect rect8b;
rect8b.x = 4;
rect8b.y = 35;
rect8b.w = 40;
rect8b.h = 12;
if (konamicode == 10) {
rect7b.w = 47;
rect8b.w = 47;
}
SDL_BlitSurface(allcans, &rect7b, screen, &rect8b); // paint upper "game b" text
}
if (gameover == 1) {
SDL_Rect rect9;
rect9.x = 255;
rect9.y = 184;
rect9.w = 59;
rect9.h = 24;
SDL_Rect rect10;
rect10.x = 255;
rect10.y = 200;
rect10.w = 59;
rect10.h = 24;
SDL_BlitSurface(allcans, &rect9, screen, &rect10); // paint "game over" text
}
writeScore(screen, score);
}
void redrawMenu(){
SDL_FillRect(screen, NULL, 0x000000); // paint screen black
SDL_Rect borda;
borda.x = 0;
borda.y = 0;
borda.w = 320;
borda.h = 16;
SDL_Rect bordb;
bordb.x = 0;
bordb.y = 0;
bordb.w = 320;
bordb.h = 16;
SDL_BlitSurface(borders, &borda, screen, &bordb); // paint upper border
SDL_Rect bordc;
bordc.x = 0;
bordc.y = 16;
bordc.w = 320;
bordc.h = 16;
SDL_Rect bordd;
bordd.x = 0;
bordd.y = 224;
bordd.w = 320;
bordd.h = 16;
SDL_BlitSurface(borders, &bordc, screen, &bordd); // paint lower border
SDL_Rect rect;
rect.x = 0;
rect.y = 16;
rect.w = 320;
rect.h = 208;
SDL_BlitSurface(background, NULL, screen, &rect); // paint background
SDL_Rect rect1;
rect1.x = 104;
rect1.y = 65;
rect1.w = 112;
rect1.h = 38;
SDL_Rect rect2;
rect2.x = 104;
rect2.y = 111;
rect2.w = 112;
rect2.h = 38;
SDL_BlitSurface(allcans, &rect1, screen, &rect2); // paint menu text
SDL_Rect rect3;
rect3.x = 132;
rect3.y = 39;
if (gameab == 1) {
rect3.y = 52;
}
rect3.w = 54;
rect3.h = 12;
SDL_Rect rect4;
rect4.x = 132;
rect4.y = 98;
rect4.w = 54;
rect4.h = 12;
SDL_BlitSurface(allcans, &rect3, screen, &rect4); // paint "game a" / "game b" menu text
if (gameab == 0) { // game A
SDL_Rect rect7a;
rect7a.x = 140;
rect7a.y = 7;
rect7a.w = 40;
rect7a.h = 12;
SDL_Rect rect8a;
rect8a.x = 4;
rect8a.y = 23;
rect8a.w = 40;
rect8a.h = 12;
if (konamicode == 10) {
rect7a.w = 47;
rect8a.w = 47;
}
SDL_BlitSurface(allcans, &rect7a, screen, &rect8a); // paint upper "game a" text
} else if (gameab == 1) { // game B
SDL_Rect rect7b;
rect7b.x = 140;
rect7b.y = 19;
rect7b.w = 40;
rect7b.h = 12;
SDL_Rect rect8b;
rect8b.x = 4;
rect8b.y = 35;
rect8b.w = 40;
rect8b.h = 12;
if (konamicode == 10) {
rect7b.w = 47;
rect8b.w = 47;
}
SDL_BlitSurface(allcans, &rect7b, screen, &rect8b); // paint upper "game b" text
}
writeHiScore();
}
void flushKeyEvents(){ // used to discard all pending key events (usually called after a long SDL_Delay)
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
default:
break;
}
default:
break;
}
}
}
void missAnimation(){ // playts the "miss" animation (broken can blinking)
SDL_Event event;
blink = 1;
int i = 0;
while (i < 21) {
if (pausegame == 0){
if ((i == 3) || (i == 9) || (i == 15)) {
blink = 1;
} else if ((i == 0) || (i == 6) || (i == 12) || (i == 18)) {
blink = 0;
}
}
redraw();
SDL_Flip(screen);
SDL_Delay(100);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_RETURN: // Start button - Pause
if (pausegame == 0){
pausegame = 1;
Mix_PlayChannel(-1, sound_move, 0);
} else if (pausegame == 1) {
pausegame = 0;
Mix_PlayChannel(-1, sound_move, 0);
}
dirty = 1;
break;
case SDLK_ESCAPE: // Select button - Exit to menu when paused
if (pausegame == 1) {
quittomenu = 1;
}
break;
default:
break;
}
default:
break;
}
}
if(pausegame == 0) {
i++;
} else if (quittomenu == 1) {
return;
}
}
blink = 0;
curr_time = SDL_GetTicks();
old_time = curr_time;
if (misses >= 3) {
gameover = 1; // if player accumulates 3 misses, game is over
}
}
void iterate(){
nextStep();
if ((checkStep() == 0) || (checkStep() == 4)){
// nothing happens
if (level == 1) {
Mix_PlayChannel(-1, sound_lvl_01, 0);
} else if (level == 2) {
Mix_PlayChannel(-1, sound_lvl_02, 0);
} else if (level == 3) {
Mix_PlayChannel(-1, sound_lvl_03, 0);
} else if (level == 4) {
Mix_PlayChannel(-1, sound_lvl_04, 0);
} else if (level == 5) {
Mix_PlayChannel(-1, sound_lvl_05, 0);
} else if (level == 6) {
Mix_PlayChannel(-1, sound_lvl_06, 0);
} else if (level == 7) {
Mix_PlayChannel(-1, sound_lvl_07, 0);
} else if (level == 8) {
Mix_PlayChannel(-1, sound_lvl_08, 0);
} else if (level == 9) {
Mix_PlayChannel(-1, sound_lvl_09, 0);
} else if (level == 10) {
Mix_PlayChannel(-1, sound_lvl_10, 0);
} else if (level == 11) {
Mix_PlayChannel(-1, sound_lvl_11, 0);
}
} else if (checkStep() == 1){
score++; // add one point to score
Mix_PlayChannel(-1, sound_good, 0);
} else if (checkStep() == 2){
score = score - 3; // subtract 3 from score
if (score < 0){
score = 0; //if score goes below zero, score is zero
}
Mix_PlayChannel(-1, sound_bad, 0);
} else if (checkStep() == 3){
misses++; // add one miss
Mix_PlayChannel(-1, sound_miss, 0);
missAnimation();
}
}
void startNewGame() {
// reset game variables to initial state
quit = 0;
gameover = 0;
pausegame = 0;
quittomenu = 0;
score = 0;
level = 1;
highestlevel = 1;
misses = 0;
speed = 928;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 2; ++j)
{
cans[i][j] = 8;
}
}
player = 0;
dirty = 0;
alternate = 0;
dropped = 0;
badmiss = 0;
blink = 0;
Mix_PlayChannel(-1, sound_music, 0);
redraw();
SDL_Flip(screen);
SDL_Delay(1500);
flushKeyEvents();
curr_time = SDL_GetTicks();
old_time = curr_time;
SDL_Event event;
while (gameover == 0) {
dirty = 0;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
exit(0);
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_UP: // upleft
if ((player != 0) && (pausegame == 0)) {
Mix_PlayChannel(-1, sound_move, 0);
player = 0;
dirty = 1;
}
break;
case SDLK_DOWN: // downleft
if ((player != 2) && (pausegame == 0)) {
Mix_PlayChannel(-1, sound_move, 0);
player = 2;
dirty = 1;
}
break;
case SDLK_SPACE: // upright
if ((player != 1) && (pausegame == 0)) {
Mix_PlayChannel(-1, sound_move, 0);
player = 1;
dirty = 1;
}
break;
case SDLK_LALT: // downright
if ((player != 3) && (pausegame == 0)) {
Mix_PlayChannel(-1, sound_move, 0);
player = 3;
dirty = 1;
}
break;
case SDLK_RETURN: // Start button - Pause/unpause
if (pausegame == 0){
pausegame = 1;
Mix_PlayChannel(-1, sound_move, 0);
dirty = 1;
} else if (pausegame == 1) {
pausegame = 0;
curr_time = SDL_GetTicks();
old_time = curr_time;
Mix_PlayChannel(-1, sound_move, 0);
dirty = 1;
}
break;
case SDLK_ESCAPE: // Select button - Exit to menu when paused
if (pausegame == 1) {
quittomenu = 1;
dirty = 1;
}
break;
default:
break;
}
default:
break;
}
}
SDL_Delay(0);
if (pausegame == 0) {
curr_time = SDL_GetTicks();
if (curr_time >= old_time + speed) {
old_time = curr_time;
iterate();
dirty = 1;
}
}
if (quittomenu == 1) {
return;
}
if (dirty == 1) {
redraw();
SDL_Flip(screen);
}
}
saveHiscores();
SDL_Delay(50);
redraw();
SDL_Flip(screen);
Mix_PlayChannel(-1, sound_gameover, 0);
SDL_Delay(600);
flushKeyEvents();
while (quittomenu == 0){
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_RETURN: // Start button - Return to main menu
case SDLK_ESCAPE: // Select button