-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
664 lines (559 loc) · 18.6 KB
/
main.c
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
/*****************************************
* TITLE: Bagdad's Thief
* AUTHOR: Jay Sueksagan
******************************************/
/*****************************************
I am making a platform game where the player
has to reach the goal line at the end of
a long scrolling map.
The player can run, jump, frontflips and kill
the enemies jumping on them. Also the enemies
can kill the player.
There are four kinds of enemies. The game has
"coins", when getting 100 of them, the player
gets one more life. There are also CheckPoints
to save player's position in case you die. The
game also has moving platforms (magic carpets).
There are two tiled backgrounds which scroll
with different speeds creating a Parallax
effect of depth.
******************************************/
#include <stdio.h>
#include "myLib.h"
#include "levels.h"
#include "game.h"
#include "sound.h"
// Load screens
#include "images/ScreenStart.h"
#include "images/GameOver.h"
#include "images/Papyrus.h"
#include "images/PapyrusStory1.h"
#include "images/PapyrusStory2.h"
#include "images/PapyrusStory3.h"
#include "images/Controls.h"
#include "images/Level1.h"
#include "images/PauseScreen.h"
#include "images/TheEnd.h"
// Load sounds
#include "sound/Snd_Start.h"
#include "sound/Snd_Level1.h"
#include "sound/powerupsfx.h"
#include "sound/Snd_GameOver.h"
#include "sound/Snd_TheEnd.h"
#define PLAY_MUSIC 1
//#include "more.h"
#define MAXWIDTH 239
#define MAXHEIGHT 159
#define STARTSCREEN 0
#define MENUSCREEN 10
#define MCREDITSSCREEN 11
#define MCONTROLSSCREEN 12
#define MINSTRUCTIONSSCREEN 13
#define GAMESCREEN 1
#define LOSESCREEN 2
#define WINSCREEN 3
#define ENDCREDITSSCREEN 31
#define PAUSESCREEN 4
#define GAMEOVERSCREEN 5
int state;
int timerState; // Timer to make the texts "Press Start Button" blink
int timerDead; // Time to wait when the player dies
// Menus
int optionMenu = 0;
int *menuOptions[4] = { "PLAY", "INSTRUCTIONS", "CONTROLS", "CREDITS" };
int totalOptions = 4;
// Menu Font Colors
int COL_SHADOW = BLACK;
int COL_TEXT = BLECK;
int COL_TITLE = BLECK1;
int COL_SELECTED = RED;
// GameOver
int gameOverTopRow;
/* Game Vars */
int level;
/* Functions */
void start();
void menu();
void menuCredits();
void menuControls();
void menuInstructions();
void game();
void pause();
void win();
void lose();
void gameOver();
void drawCredits();
void initGameVars();
void initGameVars() {
cheatInvulnerable = 0;
lifes = 3;
checkPoint = -1;
level = 1;
myCoins = 0;
}
/* Main program */
int main() {
if (PLAY_MUSIC == 1) {
startSounds();
}
SETMODE(MODE3 | BG2_ENABLE);
state = STARTSCREEN;
timerState = 0;
drawImage(ScreenStartBitmap, 0, 0, 160, 240);
playSoundB(Snd_Start, SND_STARTLEN, SND_STARTFREQ, 1);
/* Game loop */
while(1) {
switch(state) {
case STARTSCREEN:
start();
break;
case MENUSCREEN:
menu();
break;
case MCREDITSSCREEN:
menuCredits();
break;
case MCONTROLSSCREEN:
menuControls();
break;
case MINSTRUCTIONSSCREEN:
menuInstructions();
break;
case GAMESCREEN:
game();
break;
case PAUSESCREEN:
pause();
break;
case WINSCREEN:
win();
break;
case ENDCREDITSSCREEN:
endCredits();
break;
case LOSESCREEN:
lose();
break;
case GAMEOVERSCREEN:
gameOver();
break;
default:
start();
break;
}
if (state != GAMESCREEN) {
waitForVblank();
}
}
return 0;
}
void start() {
// Show Press Start (blinking)
blinkPressStart(ScreenStartBitmap);
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
state = MENUSCREEN;
fillScreen3(BLACK);
drawImage(PapyrusBitmap, 0, 0, 160, 240);
int i;
for (i = 0; i < totalOptions; i++) {
//drawString(37+i*25, 120, menuOptions[i], COL_SHADOW);
//drawString(36+i*25, 120, menuOptions[i], COL_TEXT);
drawString(30+i*25, 120, menuOptions[i], COL_SHADOW);
drawString(29+i*25, 120, menuOptions[i], COL_TEXT);
}
}
}
void blinkPressStart(const unsigned short* bitmap) {
timerState++;
if (timerState == 2) {
drawString(127,120, "Press START", BLECK);
drawString(126,120, "Press START", WHITE);
} else if (timerState == 120) {
drawImage(bitmap, 0, 0, 160, 240);
} else if (timerState == 150){
timerState = 0;
}
}
void menu() {
int change = false;
int oldOption = optionMenu;
int i = 0;
// Write the current option
drawString(29+optionMenu*25, 120, menuOptions[optionMenu], COL_SELECTED);
drawString(29+optionMenu*25, 119, menuOptions[optionMenu], COL_SELECTED);
// Read buttons to change the optionMenu
if(KEY_DOWN_NOW(BUTTON_DOWN) && optionMenu < (totalOptions-1)) {
while(KEY_DOWN_NOW(BUTTON_DOWN));
optionMenu++;
change = true;
}
if(KEY_DOWN_NOW(BUTTON_UP) && optionMenu > 0) {
while(KEY_DOWN_NOW(BUTTON_UP));
optionMenu--;
change = true;
}
if (change) {
// Redraw background and "Black" options
drawImage(PapyrusBitmap, 0, 0, 160, 240);
for (i = 0; i < totalOptions; i++) {
drawString(30+i*25, 120, menuOptions[i], COL_SHADOW);
drawString(29+i*25, 120, menuOptions[i], COL_TEXT);
}
}
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
switch(optionMenu) {
case 0: // PLAY
state = GAMESCREEN;
fillScreen3(BLACK);
initGameVars();
loadLevel(level);
initSprites();
// Play looping sound
playSoundB(Snd_Level1, SND_LEVEL1LEN, SND_LEVEL1FREQ, 1);
break;
case 1: // INSTRUCTIONS
state = MINSTRUCTIONSSCREEN;
fillScreen3(BLACK);
drawImage(PapyrusStory1Bitmap, 0, 0, 160, 240);
timerState = 0;
break;
case 2: // CONTROLS
state = MCONTROLSSCREEN;
fillScreen3(BLACK);
drawImage(ControlsBitmap, 0, 0, 160, 240);
drawString(27, 120, "CONTROLS", COL_SHADOW);
drawString(26, 120, "CONTROLS", COL_TITLE);
drawString(72, 61, "move", COL_SHADOW);
drawString(72, 179, "jump", COL_SHADOW);
drawString(71, 61, "move", COL_TEXT);
drawString(71, 179, "jump", COL_TEXT);
drawString(106, 120, "Left,Right Move", COL_SHADOW);
drawString(121, 120, "Button A Jump", COL_SHADOW);
drawString(105, 120, "Left,Right Move", COL_TITLE);
drawString(120, 120, "Button A Jump", COL_TITLE);
break;
case 3: // CREDITS
state = MCREDITSSCREEN;
fillScreen3(BLACK);
drawImage(PapyrusBitmap, 0, 0, 160, 240);
drawString(27, 120, "CREDITS", COL_SHADOW);
drawString(26, 120, "CREDITS", COL_TITLE);
drawString(65, 120, "Jay Sueksagan", COL_SHADOW);
drawString(64, 120, "Jay Sueksagan", COL_TEXT);
drawString(64, 120, "Jay Sueksagan", COL_TEXT);
drawString(78, 120, "2012 0", COL_SHADOW);
drawString(77, 120, "2012 0", COL_TEXT);
drawString(79, 141, "c", COL_TEXT);
break;
default:
break;
}
}
}
void menuCredits() {
// Show who's your daddy ;)
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
state = MENUSCREEN;
fillScreen3(BLACK);
drawImage(PapyrusBitmap, 0, 0, 160, 240);
int i;
for (i = 0; i < totalOptions; i++) {
drawString(30+i*25, 120, menuOptions[i], COL_SHADOW);
drawString(29+i*25, 120, menuOptions[i], COL_TEXT);
}
}
}
void menuControls() {
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
state = MENUSCREEN;
fillScreen3(BLACK);
drawImage(PapyrusBitmap, 0, 0, 160, 240);
int i;
for (i = 0; i < totalOptions; i++) {
drawString(30+i*25, 120, menuOptions[i], COL_SHADOW);
drawString(29+i*25, 120, menuOptions[i], COL_TEXT);
}
}
}
void menuInstructions() {
//timerState++;
int COL = GREY;
if (timerState == 0) {
drawImage(PapyrusStory1Bitmap, 0, 0, 160, 240);
drawString(106, 120, "The wondrous city", COL);
drawString(121, 120, "of Bagdad is...", COL);
drawString(105, 120, "The wondrous city", COL_TEXT);
drawString(120, 120, "of Bagdad is...", COL_TEXT);
timerState++;
} else if (timerState == 2) {
drawImage(PapyrusStory1Bitmap, 0, 0, 160, 240);
drawString(106, 118, "Full of magic and", COL);
drawString(121, 119, "gold. Ahmed is a", COL);
drawString(105, 118, "Full of magic and", COL_TEXT);
drawString(120, 119, "gold. Ahmed is a", COL_TEXT);
timerState++;
} else if (timerState == 4) {
drawImage(PapyrusStory2Bitmap, 0, 0, 160, 240);
drawString(106, 119, "treasure hunter.", COL);
drawString(121, 119, "He will live the", COL);
drawString(105, 119, "treasure hunter,", COL_TEXT);
drawString(120, 119, "He will live the", COL_TEXT);
timerState++;
} else if (timerState == 6) {
drawImage(PapyrusStory2Bitmap, 0, 0, 160, 240);
drawString(106, 119, "best adventures,", COL);
drawString(121, 119, "escape the guards,", COL);
drawString(105, 119, "best adventures,", COL_TEXT);
drawString(120, 119, "escape the guards,", COL_TEXT);
timerState++;
} else if (timerState == 8) {
drawImage(PapyrusStory3Bitmap, 0, 0, 160, 240);
drawString(106, 119, "meet a beautiful", COL);
drawString(121, 120, "princess andFight", COL);
drawString(105, 119, "meet a beautiful", COL_TEXT);
drawString(120, 120, "princess andFight", COL_TEXT);
timerState++;
} else if (timerState == 10) {
drawImage(PapyrusStory3Bitmap, 0, 0, 160, 240);
drawString(106, 119, "the evil sultane", COL);
drawString(121, 119, "becoming a hero...", COL);
drawString(105, 119, "the evil sultane", COL_TEXT);
drawString(120, 119, "becoming a hero...", COL_TEXT);
timerState++;
}
if (KEY_DOWN_NOW(BUTTON_A)) {
while(KEY_DOWN_NOW(BUTTON_A));
timerState++;
}
if (KEY_DOWN_NOW(BUTTON_B)) {
while(KEY_DOWN_NOW(BUTTON_B));
timerState++;
}
if (KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
timerState++;
}
int goBack = false;
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
//goBack = true;
}
if (goBack || timerState >= 12) {
state = MENUSCREEN;
fillScreen3(BLACK);
drawImage(PapyrusBitmap, 0, 0, 160, 240);
int i;
for (i = 0; i < totalOptions; i++) {
drawString(30+i*25, 120, menuOptions[i], COL_SHADOW);
drawString(29+i*25, 120, menuOptions[i], COL_TEXT);
}
}
}
void game() {
int incX = 2;
if (player.alive == LIVE) {
/* Read buttons */
if(KEY_DOWN_NOW(BUTTON_LEFT)) {
if (playerX > 0) {
playerX -= incX;
}
}
if(KEY_DOWN_NOW(BUTTON_RIGHT)) {
playerX += incX;
}
if(KEY_DOWN_NOW(BUTTON_UP)) {
//voff--;
}
if(KEY_DOWN_NOW(BUTTON_DOWN)) {
//voff++;
}
if(KEY_DOWN_NOW(BUTTON_A)) { // Jump
if (isJumping == 0 && canJump == 1) {
//playSoundA(powerupsfx, POWERUPSFXLEN, POWERUPSFXFREQ, 0);
isJumping = true;
jumpInc = -MAX_JUMP;
timerJump = 10;
jumpY = playerY;
}
}
if(KEY_DOWN_NOW(BUTTON_L) && KEY_DOWN_NOW(BUTTON_R)) { // Jump
cheatInvulnerable = 1;
}
} else {
timerDead++;
if (timerDead >= 200) {
timerDead = 0;
REG_DISPCTL = MODE3 | BG2_ENABLE;
state = LOSESCREEN;
hoff = 0;
voff = 0;
lifes--;
fillScreen3(BLACK);
if (lifes <= 0) {
SETMODE(MODE3 | BG2_ENABLE);
state = GAMEOVERSCREEN;
timerState = 0;
gameOverTopRow = 140*3; //160*2
playSoundB(Snd_GameOver, SND_GAMEOVERLEN, SND_GAMEOVERFREQ, 1);
}
}
}
// PAUSE the game, only if player is alive!
if (player.alive == LIVE) {
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
stopSoundB();
playSoundA(powerupsfx, POWERUPSFXLEN, POWERUPSFXFREQ, 0);
state = PAUSESCREEN;
timerState = 0;
REG_DISPCTL = MODE3 | BG2_ENABLE;
fillScreen3(BLACK);
drawImage(PauseScreenBitmap, 0, 0, 160, 240);
drawString(60, 120, "PAUSE", WHITE);
}
}
/* End of read buttons */
// Check win
// WIN?
//if (hoff >= levelWidth() - 256) {
if (playerX >= levelWidth() - 40) {
REG_DISPCTL = MODE3 | BG2_ENABLE;
state = WINSCREEN;
fillScreen3(BLACK);
timerState = 0;
//drawImage(TheEndBitmap, 0, 0, 160, 240);
playSoundB(Snd_TheEnd, SND_THEENDLEN, SND_THEENDFREQ, 1);
}
// End of check win
//------Draw-------
waitForVblank();
loopGame(lifes, myCoins);
}
void pause() {
blinkPressStart(PauseScreenBitmap);
if (timerState == 120) {
drawString(60, 120, "PAUSE", WHITE);
}
// Wait until "START" pressed
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
state = GAMESCREEN;
fillScreen3(BLACK);
showLevel(level);
loadSprites();
small_hoff = hoff%8;
loadMapBlockCol(hoff/8, Level1Map);
// Play looping sound
playSoundB(Snd_Level1, SND_LEVEL1LEN, SND_LEVEL1FREQ, 1);
}
}
void win() {
timerState++;
if (timerState >= 100 && timerState <= 350) { //350
drawImage(TheEndBitmap, 0, 0, 160, 240);
// Now the BLACK parts:
drawRect3(timerState-100, 0, 160, 240, BLACK);
}
if (timerState < 200) return;
int credits = false;
// Wait until "START" button is pressed
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
credits = true;
}
if (credits || timerState > 350) {
SETMODE(MODE3 | BG2_ENABLE);
state = ENDCREDITSSCREEN;
timerState = 0;
fillScreen3(BLACK);
}
}
void endCredits() {
timerState++;
if (timerState < 50) {
drawString(50, 120, "THE THIEF", WHITE);
drawString(70, 120, "OF BAGDAD", WHITE);
} else if (timerState == 50) {
fillScreen3(BLACK);
} else if (timerState > 50 && timerState < 100) {
drawString(25, 120, "A game by", WHITE);
drawString(60, 120, "Jay", WHITE);
drawString(80, 120, "Sueksagan", WHITE);
} else if (timerState == 100) {
fillScreen3(BLACK);
} else if (timerState > 100 && timerState < 200) {
drawString(60, 120, "Special thanks", WHITE);
} else if (timerState == 200) {
fillScreen3(BLACK);
} else if (timerState > 200 && timerState < 250) {
drawString(60, 120, "DISNEYs Aladdin", WHITE);
} else if (timerState == 250) {
fillScreen3(BLACK);
} else if (timerState > 250 && timerState < 300) {
drawString(60, 120, "The Thief of Bagdad", WHITE);
} else if (timerState == 300) {
fillScreen3(BLACK);
} else if (timerState > 300 && timerState < 350) {
drawString(60, 120, "My mum and family", WHITE);
} else if (timerState == 350) {
fillScreen3(BLACK);
} else if (timerState > 350 && timerState < 400) {
drawString(60, 120, "And my friends", WHITE);
} else if (timerState == 400) {
fillScreen3(BLACK);
} else if (timerState > 450) {
drawString(50, 120, "THE THIEF", WHITE);
drawString(70, 120, "OF BAGDAD", WHITE);
}
int quitCredits = false;
// Wait until "START" button is pressed
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
quitCredits = true;
}
if ((timerState > 100 && quitCredits) || timerState > 650) {
SETMODE(MODE3 | BG2_ENABLE);
state = STARTSCREEN;
timerState = 0;
drawImage(ScreenStartBitmap, 0, 0, 160, 240);
playSoundB(Snd_Start, SND_STARTLEN, SND_STARTFREQ, 1);
}
}
void lose() {
/*
drawString(50,60, "TRY AGAIN", RED);
drawString(70,50, "Press START", WHITE);
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
*/
state = GAMESCREEN;
fillScreen3(BLACK);
//SETMODE(MODE0 | BG0_ENABLE | /*BG1_ENABLE |*/ BG2_ENABLE | SPRITE_ENABLE);
loadLevel(level);
initSprites();
//}
}
void gameOver() {
if (gameOverTopRow > 75 /*25*3*/) {
gameOverTopRow -= 2;
drawImage(GameOverBitmap, 0, 0, 160, 240);
// Now the BLACK parts:
drawRect3(0, 0, gameOverTopRow>>2, 240, BLACK);
return;
}
// Blink "Press Start"
blinkPressStart(GameOverBitmap);
// Wait until "START" pressed
if(KEY_DOWN_NOW(BUTTON_START)) {
while(KEY_DOWN_NOW(BUTTON_START));
SETMODE(MODE3 | BG2_ENABLE);
state = STARTSCREEN;
timerState = 0;
drawImage(ScreenStartBitmap, 0, 0, 160, 240);
playSoundB(Snd_Start, SND_STARTLEN, SND_STARTFREQ, 1);
}
}