Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
main.cpp
00001 // Project includes 00002 #include "globals.h" 00003 #include "hardware.h" 00004 #include "map.h" 00005 #include "graphics.h" 00006 #include "speech.h" 00007 00008 #include <stdlib.h> 00009 int menu = 0; 00010 int health = 3; 00011 int omni_pressed = 0; 00012 int run = 0; 00013 int quest = 0; // 0 is before quest, 1 is during quest no key, 2 is with key, 3 is back talking 00014 int door = 0; // 1 if door unlocked, 0 if door locked 00015 int chest = 0; 00016 int game_over = 0; 00017 char* task[5] = {"talk to the dog to start quest", "go to Snowdin", "go into the room to start the puzzle", "come back to the castle", "go to the castle"}; 00018 char* hint[5] = {"Just talk to him", "talk to the riverman", "read the sign", "talk to the riverman", "walk down south"}; 00019 00020 char* flowey_wink[4] = {"Howdy!", "I'm FLOWEY", "FLOWEY THE", "FLOWER"}; 00021 char* flowey_creepy[4] = {"In this","world...","It's KILL","or BE killed!"}; 00022 char* flowey_friendly1[4] = {"That shirt...", "it seems familiar", "reminds me of", "someone"}; 00023 char* flowey_friendly2[4] = {"are you sure", "it's yours?", "ugh I hate", "that dog"}; 00024 char** flowey_friendly[2] = {flowey_friendly1, flowey_friendly2}; 00025 00026 char* sign1[4] = {"Welcome to","Snowdin!", "here you have to","solve a puzzle"}; 00027 char* sign2[4] = {"Choose the right", "button. One will", "take you to", "the next level"}; 00028 char* sign3[4] = {"Two will kick you","out of the room", "Three strikes", "and you're out"}; 00029 char** sign[3] = {sign1, sign2, sign3}; 00030 00031 char* toby_pre1[4] = {"ruff ruff", "yo!", "I'm just a dog"}; 00032 char* toby_pre2[4] = {"how are ya", "you must be", "new here!", "This is the"}; 00033 char* toby_pre3[4] = {"the ruins! We", "have monsters and", "all kind of"}; 00034 char* toby_pre4[4] = {"stuff down here", "...", "There is not", "much to do tho"}; 00035 char* toby_pre5[4] = {"... oh wait", "there is!! I'm", "hungry.. can you", "get me some bone?"}; 00036 char* toby_pre6[4] = {"Where? Snowdin!","it's quite a", "distance from here", "so i suggest"}; 00037 char* toby_pre7[4] = {"you get going!", "Bring me a bone", "asap, and ill" }; 00038 char* toby_pre8[4] = {"give you something","in return!","good luck!!","..."}; 00039 char** toby_pre[8] = {toby_pre1, toby_pre2, toby_pre3,toby_pre4,toby_pre5,toby_pre6,toby_pre7,toby_pre8}; 00040 char* toby_bone1[4] = {"Om nom nom", "nom..", "oh hey!", "look at ya"}; 00041 char* toby_bone2[4] = {"How did you get", "here so fast?", ".. nom nom", "nom"}; 00042 char* toby_bone3[4] = {"what's with the", "look? Didnt", "I tell you I", "could teleport?"}; 00043 char* toby_bone4[4] = {"Yea!Like the dude", "on the boat", "pretty nice,","isnt it?"}; 00044 char* toby_bone5[4] = {"You just came", "here for nothing!", "bamboozled!","..."}; 00045 char* toby_bone6[4] = {"Lol don't be mad", "I'll give ya", "this pretty dope", "shirt!"}; 00046 char* toby_bone7[4] = {"The old prince", "threw it away", "you can", "basically go"}; 00047 char* toby_bone8[4] = {"anywhere with it", "...", "It's", "pretty nice!"}; 00048 char* toby_bone9[4] = {"imma go!", "baiiii", "i'll see ya at", "the starting point"}; 00049 char** toby_bone[9] = {toby_bone1, toby_bone2,toby_bone3,toby_bone4,toby_bone5,toby_bone6,toby_bone7,toby_bone8,toby_bone9}; 00050 00051 char* river_man_line1[4] = {"Tra la la", "The waters are", "wild today.", "That is bad luck"}; 00052 char* river_man_line2[4] = {"Tra la la", "The waters are", "wild today.", "That is good luck"}; 00053 char* river_man_line3[4] = {"Tra la la", "What's inside an", "acorn? It's" ,"just a mystery."}; 00054 char* river_man_line4[4] = {"Tra la la", "Dancing on a boat","is danger.", "But good exercise"}; 00055 char* river_man_line5[4] = {"Tra la la", "Why don't you", "sing with me.", "Tra la la."}; 00056 char* river_man_line6[4] = {"Tra la la", "Uh oh.", "Suddenly, feeling" ,"tropical..."}; 00057 char* river_man_line7[4] = {"Tra la la", "The water is", "very dry", "today"}; 00058 char* river_man_line8[4] = {"Tra la la", "The water is", "very wet", "today"}; 00059 char* river_man_line9[4] = {"Tra la la", "Tri li li.", "Tre le le.", "..."}; 00060 char* river_man_line10[4] = {"Tra la la", "What's my name?" ,"It doesn't", "really matter..."}; 00061 char* river_man_line11[4] = {"Tra la la." "I heard dogs" "have a favorite" "food. It's bone!"}; 00062 char* river_man_line12[4] = {"Tra la la. You", "can never have", "too many hot dogs", "This is not true"}; 00063 char* river_man_line13[4] = {"Tra la la.", "Beware of", "the man who", "speaks in hands"}; 00064 char* river_man_line14[4] = {"Tra la la.", "Beware of the", "man who came from", "the other world"}; 00065 char* river_man_line15[4] = {"Tra la la.", "Humans, monsters", "...", "flowers"}; 00066 char** river_man_line[15] = {river_man_line13, river_man_line3,river_man_line14, river_man_line1, river_man_line15, river_man_line6, river_man_line11, river_man_line8, river_man_line9, river_man_line10,river_man_line7,river_man_line12,river_man_line2,river_man_line4,river_man_line5}; 00067 // Functions in this file 00068 int get_action (GameInputs inputs); 00069 int update_game (int action); 00070 void draw_game (int init); 00071 void init_main_map (); 00072 void intit_start(); 00073 void init_menu(char* task, char* hint, int health, int key, int chest, int run); 00074 int go_up(int x, int y); 00075 int go_down(int x, int y); 00076 int go_left(int x, int y); 00077 int go_right(int x, int y); 00078 int main (); 00079 int interact(int x, int y); 00080 void check_health(); 00081 /** 00082 * The main game state. Must include Player locations and previous locations for 00083 * drawing to work properly. Other items can be added as needed. 00084 */ 00085 struct { 00086 int x,y; // Current locations 00087 int px, py; // Previous locations 00088 int has_key; 00089 } Player; 00090 00091 /** 00092 * Given the game inputs, determine what kind of update needs to happen. 00093 * Possbile return values are defined below. 00094 */ 00095 #define NO_ACTION 0 00096 #define ACTION_BUTTON 1 00097 #define MENU_BUTTON 2 00098 #define GO_LEFT 3 00099 #define GO_RIGHT 4 00100 #define GO_UP 5 00101 #define GO_DOWN 6 00102 #define RUN 7 00103 int get_action(GameInputs inputs) 00104 { 00105 if(!inputs.b1) { 00106 return ACTION_BUTTON; 00107 } 00108 if(!inputs.b2) { 00109 return MENU_BUTTON; 00110 } 00111 if(inputs.ax < -0.49) { 00112 return GO_LEFT; 00113 } 00114 if(inputs.ax >= 0.49) { 00115 return GO_RIGHT; 00116 } 00117 if(inputs.ay >=0.45) { 00118 return GO_UP; 00119 } 00120 if(inputs.ay <= -0.45) { 00121 return GO_DOWN; 00122 } 00123 if (!inputs.b3) { 00124 omni_pressed = !omni_pressed; 00125 } 00126 return NO_ACTION; 00127 } 00128 00129 /** 00130 * Update the game state based on the user action. For example, if the user 00131 * requests GO_UP, then this function should determine if that is possible by 00132 * consulting the map, and update the Player position accordingly. 00133 * 00134 * Return values are defined below. FULL_DRAW indicates that for this frame, 00135 * draw_game should not optimize drawing and should draw every tile, even if 00136 * the player has not moved. 00137 */ 00138 #define NO_RESULT 0 00139 #define GAME_OVER 1 00140 #define FULL_DRAW 2 00141 int update_game(int action) 00142 { 00143 // Save player previous location before updating 00144 Player.px = Player.x; 00145 Player.py = Player.y; 00146 00147 // Do different things based on the each action. 00148 // You can define functions like "go_up()" that get called for each case. 00149 switch(action) 00150 { 00151 case GO_UP: 00152 if(go_up(Player.px, Player.py)) { 00153 return FULL_DRAW; 00154 } else { 00155 break; 00156 } 00157 case GO_LEFT: 00158 if(go_left(Player.px, Player.py)) { 00159 return FULL_DRAW; 00160 } else { 00161 break; 00162 } 00163 case GO_DOWN: 00164 if(go_down(Player.px, Player.py)) { 00165 return FULL_DRAW; 00166 } else { 00167 break; 00168 } 00169 case GO_RIGHT: 00170 if(go_right(Player.px, Player.py)) { 00171 return FULL_DRAW; 00172 } else { 00173 break; 00174 } 00175 case ACTION_BUTTON: 00176 if(interact(Player.x,Player.y)) { 00177 return FULL_DRAW; 00178 } else { 00179 break; 00180 } 00181 case MENU_BUTTON: 00182 uLCD.cls(); 00183 init_menu(task[menu], hint[menu], health, Player.has_key, chest, run); 00184 wait_ms(300); 00185 draw_game(true); 00186 return FULL_DRAW; 00187 default: break; 00188 } 00189 return NO_RESULT; 00190 } 00191 00192 int interact(int x, int y) { 00193 MapItem *item = get_south(x, y); 00194 00195 if(item->type == CHEST) { 00196 speech("you found a", "chest!!!"); 00197 draw_game(true); 00198 wait_ms(150); 00199 map_erase(x,y+1); 00200 draw_game(true); 00201 wait_ms(150); 00202 chest = 1; 00203 map_erase(x,y-2); 00204 draw_game(true); 00205 quest = 99; 00206 wait_ms(150); 00207 00208 add_leaf(11, 10); 00209 add_wood(11, 11); 00210 add_wood(11, 12); 00211 return 1; 00212 } 00213 00214 if(item->type == WOOD) { 00215 if(run == 0) { 00216 speech("Hi kid!,","Im a tree"); 00217 draw_game(true); 00218 wait_ms(150); 00219 speech("...","uhm so..."); 00220 draw_game(true); 00221 wait_ms(150); 00222 speech("some kid left", "their running"); 00223 draw_game(true); 00224 wait_ms(150); 00225 speech("shoes here, imma", "give it to you"); 00226 draw_game(true); 00227 wait_ms(150); 00228 run = 1; 00229 } else { 00230 speech("oh you dont", "want the shoes"); 00231 draw_game(true); 00232 wait_ms(150); 00233 speech("no mo? Ill", "take it back then"); 00234 draw_game(true); 00235 wait_ms(150); 00236 run = 0; 00237 } 00238 } 00239 if(item->type == TOBY) { 00240 if(quest == 0) { 00241 for(int i =0; i<8;i++) { 00242 for(int j = 0; j < 4;j+=2) { 00243 speech(toby_pre[i][j], toby_pre[i][j+1]); 00244 draw_game(true); 00245 } 00246 } 00247 wait_ms(150); 00248 quest=1; 00249 menu = 1; 00250 menu++; 00251 } else if(quest == 1) { 00252 speech("well..","what are you waiting for!"); 00253 draw_game(true); 00254 wait_ms(300); 00255 menu = 1; 00256 } else if(quest == 3) { 00257 speech("ruff ruff,","looking good!!"); 00258 draw_game(true); 00259 wait_ms(300); 00260 speech("you can go to", "the castle"); 00261 draw_game(true); 00262 wait_ms(300); 00263 speech("down south now!", "Have fun"); 00264 draw_game(true); 00265 wait_ms(300); 00266 menu = 4; 00267 } else if(quest == 99) { 00268 speech("congrats! you","finish the game"); 00269 draw_game(true); 00270 wait_ms(300); 00271 game_over = 1; 00272 } 00273 00274 return 1; 00275 } 00276 if(item->type == FLOWWINK) { 00277 // generate a 500Hz tone using PWM hardware output 00278 speaker.period(1.0/500.0); // 500hz period 00279 speaker =0.5; //50% duty cycle - max volume 00280 wait(1); 00281 speaker=0.0; // turn off audio 00282 wait(1); 00283 if(quest == 2) { 00284 add_flowey_friendly(x, y+1); 00285 draw_game(true); 00286 wait_ms(100); 00287 for(int i = 0; i <2;i++) { 00288 for(int j = 0; j < 4;j+=2) { 00289 speech(flowey_friendly[i][j], flowey_friendly[i][j+1]); 00290 draw_game(true); 00291 } 00292 } 00293 draw_nothing(x,y+1); 00294 draw_game(true); 00295 wait_ms(100); 00296 } else { 00297 for(int i = 0; i < 4;i+=2) { 00298 speech(flowey_wink[i], flowey_wink[i+1]); 00299 draw_game(true); 00300 } 00301 add_flowey_creepy(x, y+1); 00302 draw_game(true); 00303 return 1; 00304 } 00305 return 1; 00306 } 00307 if(item->type == FLOWCREEPY) { 00308 // generate a 500Hz tone using PWM hardware output 00309 speaker.period(1.0/500.0); // 500hz period 00310 speaker =0.5; //50% duty cycle - max volume 00311 wait(1); 00312 speaker=0.0; // turn off audio 00313 wait(1); 00314 00315 if(quest != 2) { 00316 for(int i = 0; i < 4;i+=2) { 00317 speech(flowey_creepy[i], flowey_creepy[i+1]); 00318 draw_game(true); 00319 } 00320 add_flowey_wink(x, y+1); 00321 draw_game(true); 00322 return 1; 00323 } else { 00324 add_flowey_friendly(x, y+1); 00325 draw_game(true); 00326 wait_ms(100); 00327 for(int i = 0; i <2;i++) { 00328 for(int j = 0; j < 4;j+=2) { 00329 speech(flowey_friendly[i][j], flowey_friendly[i][j+1]); 00330 draw_game(true); 00331 } 00332 } 00333 draw_nothing(x,y+1); 00334 draw_game(true); 00335 wait_ms(100); 00336 } 00337 return 1; 00338 } 00339 if(item->type == FLOWFRIEND) { 00340 // generate a 500Hz tone using PWM hardware output 00341 speaker.period(1.0/500.0); // 500hz period 00342 speaker =0.5; //50% duty cycle - max volume 00343 wait(1); 00344 speaker=0.0; // turn off audio 00345 wait(1); 00346 for(int i = 0; i <2;i++) { 00347 for(int j = 0; j < 4;j+=2) { 00348 speech(flowey_friendly[i][j], flowey_friendly[i][j+1]); 00349 draw_game(true); 00350 } 00351 } 00352 wait_ms(100); 00353 draw_nothing(x,y+1); 00354 draw_game(true); 00355 wait_ms(100); 00356 return 1; 00357 } 00358 if(item->type == SIGN) { 00359 door = 1; 00360 for(int i = 0; i < 3;i++) { 00361 for(int j = 0; j < 4;j+=2) { 00362 speech(sign[i][j], sign[i][j+1]); 00363 draw_game(true); 00364 wait_ms(200); 00365 } 00366 } 00367 return 1; 00368 } 00369 00370 if(item->type == DOORLOCKED) { 00371 if(x == 2) { 00372 speech("Door locked", "Not a royalty"); 00373 draw_game(true); 00374 wait_ms(150); 00375 } else if(x== map_width()-3) { 00376 speech("You use the","key you just found"); 00377 draw_game(true); 00378 wait_ms(150); 00379 add_door_unlocked(x,y+1); 00380 draw_game(true); 00381 menu = 2; 00382 } else { 00383 if(door == 0) { 00384 speech("door locked", "read the sign"); 00385 draw_game(true); 00386 wait_ms(300); 00387 speech("first to unlock", "this door!"); 00388 draw_game(true); 00389 wait_ms(300); 00390 } else if(door == 1) { 00391 speech("You hear the", "door opens"); 00392 draw_game(true); 00393 wait_ms(150); 00394 add_door_unlocked(x, y+1); 00395 draw_game(true); 00396 } 00397 } 00398 return 1; 00399 } 00400 00401 if(item->type == DOORUNLOCKED){ 00402 wait_ms(150); 00403 Player.y += 2; 00404 draw_game(true); 00405 return 1; 00406 } 00407 00408 if(item->type == REDBUTTON) { 00409 if(y+1 == map_height()-38) { 00410 waver.dac_out(); 00411 speech("uh oh", "wrong one"); 00412 draw_game(true); 00413 wait_ms(150); 00414 Player.x = map_width()-35; 00415 Player.y = map_height()-43; 00416 draw_game(true); 00417 health--; 00418 return 1; 00419 } else if(y+1 == map_height()-34) { 00420 speech("uh oh", "wrong one"); 00421 draw_game(true); 00422 wait_ms(150); 00423 Player.x = map_width()-35; 00424 Player.y = map_height()-43; 00425 draw_game(true); 00426 health--; 00427 return 1; 00428 } else if(y+1 == map_height()-31) { 00429 speech("You hear", "a click!"); 00430 draw_game(true); 00431 wait_ms(150); 00432 Player.y += 3; 00433 draw_game(true); 00434 } else if(y+1 == map_height()-27) { 00435 speech("You hear", "a click!"); 00436 draw_game(true); 00437 wait_ms(150); 00438 Player.y += 3; 00439 draw_game(true); 00440 wait_ms(300); 00441 00442 } 00443 return 1; 00444 } 00445 if(item->type == GREENBUTTON) { 00446 if(y+1 == map_height()-38) { 00447 speech("You hear", "a click!"); 00448 draw_game(true); 00449 wait_ms(150); 00450 Player.y += 3; 00451 draw_game(true); 00452 } else if(y+1 == map_height()-34) { 00453 speech("uh oh", "wrong one"); 00454 draw_game(true); 00455 wait_ms(150); 00456 Player.x = map_width()-35; 00457 Player.y = map_height()-43; 00458 draw_game(true); 00459 health--; 00460 } else if(y+1 == map_height()-31) { 00461 speech("uh oh", "wrong one"); 00462 draw_game(true); 00463 wait_ms(150); 00464 Player.x = map_width()-35; 00465 Player.y = map_height()-43; 00466 draw_game(true); 00467 health--; 00468 } else if(y+1 == map_height()-27) { 00469 speech("uh oh", "wrong one"); 00470 draw_game(true); 00471 wait_ms(150); 00472 Player.x = map_width()-35; 00473 Player.y = map_height()-43; 00474 draw_game(true); 00475 health--; 00476 } 00477 return 1; 00478 } 00479 if(item->type == BLUEBUTTON) { 00480 if(y+1 == map_height()-38) { 00481 speech("uh oh", "wrong one"); 00482 draw_game(true); 00483 wait_ms(150); 00484 Player.x = map_width()-35; 00485 Player.y = map_height()-43; 00486 draw_game(true); 00487 health--; 00488 } else if(y+1 == map_height()-34) { 00489 speech("You hear", "a click!"); 00490 draw_game(true); 00491 wait_ms(150); 00492 Player.y += 3; 00493 draw_game(true); 00494 } else if(y+1 == map_height()-31) { 00495 speech("uh oh", "wrong one"); 00496 draw_game(true); 00497 wait_ms(150); 00498 Player.x = map_width()-35; 00499 Player.y = map_height()-43; 00500 draw_game(true); 00501 health--; 00502 } else if(y+1 == map_height()-27) { 00503 speech("uh oh", "wrong one"); 00504 draw_game(true); 00505 wait_ms(150); 00506 Player.x = map_width()-35; 00507 Player.y = map_height()-43; 00508 draw_game(true); 00509 health--; 00510 } 00511 return 1; 00512 } 00513 if(item->type == TOBYBONE) { 00514 for(int i = 0; i < 9; i++) { 00515 for(int j = 0; j < 4;j+=2) { 00516 speech(toby_bone[i][j], toby_bone[i][j+1]); 00517 draw_game(true); 00518 wait_ms(150); 00519 } 00520 } 00521 map_erase(x,y+1); 00522 draw_game(true); 00523 wait_ms(300); 00524 Player.has_key = 1; 00525 quest=2; 00526 00527 draw_game(true); 00528 wait_ms(300); 00529 map_erase(x-3, y+2); 00530 menu = 3; 00531 return 1; 00532 } 00533 00534 if(item->type == RIVERMAN) { 00535 if(quest == 1) { // head to snowdin 00536 speech("Tra la la", "Everything is"); 00537 draw_game(true); 00538 wait_ms(150); 00539 speech("not how they", "seem to be"); 00540 draw_game(true); 00541 wait_ms(150); 00542 menu = 2; 00543 Player.x = map_width()-36; 00544 Player.y = map_height()-45; 00545 draw_game(true); 00546 wait_ms(150); 00547 } else if(quest==2) { // got the keys 00548 speech("Tra la la", "Did I"); 00549 draw_game(true); 00550 wait_ms(150); 00551 speech("not warn", "ya..."); 00552 draw_game(true); 00553 wait_ms(150); 00554 uLCD.cls(); 00555 Player.x = 5; 00556 Player.y = 5; 00557 draw_game(true); 00558 wait_ms(300); 00559 quest++; 00560 } else { 00561 int index = rand()%15; 00562 for(int i = 0; i <4;i+=2 ) { 00563 speech(river_man_line[index][i], river_man_line[index][i+1]); 00564 draw_game(true); 00565 wait_ms(150); 00566 } 00567 } 00568 } 00569 return 0; 00570 } 00571 00572 void init_start() { 00573 GameInputs in = read_inputs(); 00574 draw_start(); 00575 while(in.b1) { 00576 Timer t; t.start(); 00577 in = read_inputs(); 00578 t.stop(); 00579 int dt = t.read_ms(); 00580 if(dt < 100) wait_ms(100-dt); 00581 } 00582 uLCD.cls(); 00583 } 00584 00585 void init_menu(char *a, char *b, int health_count, int key, int chest, int run) { 00586 GameInputs in = read_inputs(); 00587 draw_menu(a, b, health_count, key, chest, run); 00588 uLCD.cls(); 00589 } 00590 00591 void init_game_over() { 00592 GameInputs in = read_inputs(); 00593 draw_game_over(); 00594 uLCD.cls(); 00595 } 00596 /** 00597 * Entry point for frame drawing. This should be called once per iteration of 00598 * the game loop. This draws all tiles on the screen, followed by the status 00599 * bars. Unless init is nonzero, this function will optimize drawing by only 00600 * drawing tiles that have changed from the previous frame. 00601 */ 00602 void draw_game(int init) 00603 { 00604 // Draw game border first 00605 if(init) draw_border(); 00606 00607 // Iterate over all visible map tiles 00608 for (int i = -5; i <= 5; i++) // Iterate over columns of tiles 00609 { 00610 for (int j = -4; j <= 4; j++) // Iterate over one column of tiles 00611 { 00612 // Here, we have a given (i,j) 00613 00614 // Compute the current map (x,y) of this tile 00615 int x = i + Player.x; 00616 int y = j + Player.y; 00617 00618 // Compute the previous map (px, py) of this tile 00619 int px = i + Player.px; 00620 int py = j + Player.py; 00621 00622 // Compute u,v coordinates for drawing 00623 int u = (i+5)*11 + 3; 00624 int v = (j+4)*11 + 15; 00625 00626 // Figure out what to draw 00627 DrawFunc draw = NULL; 00628 if (init && i == 0 && j == 0) // Only draw the player on init 00629 { 00630 draw_player(u, v, Player.has_key); 00631 continue; 00632 } 00633 else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map 00634 { 00635 MapItem* curr_item = get_here(x, y); 00636 MapItem* prev_item = get_here(px, py); 00637 if (init || curr_item != prev_item) // Only draw if they're different 00638 { 00639 if (curr_item) // There's something here! Draw it 00640 { 00641 draw = curr_item->draw; 00642 } 00643 else // There used to be something, but now there isn't 00644 { 00645 draw = draw_nothing; 00646 } 00647 } 00648 } 00649 else if (init) // If doing a full draw, but we're out of bounds, draw the walls. 00650 { 00651 draw = draw_wall; 00652 } 00653 00654 // Actually draw the tile 00655 if (draw) draw(u, v); 00656 } 00657 } 00658 00659 // Draw status bars 00660 draw_upper_status(Player.x, Player.y); 00661 draw_lower_status(); 00662 } 00663 00664 00665 /** 00666 * Initialize the main world map. Add walls around the edges, interior chambers, 00667 * and plants in the background so you can see motion. 00668 */ 00669 void init_main_map() 00670 { 00671 // "Random" plants 00672 Map* map = set_active_map(0); 00673 00674 for(int i = map_width() + 3; i < map_area(); i += 40) 00675 { 00676 if((i%map_width() > (map_width()-40)) && (i%map_width() < (map_width()-20))) { 00677 if((i/map_height() > (map_height()-40)) && (i/map_width() < (map_height()-20))) { 00678 00679 } else { 00680 add_plant(i % map_width(), i / map_width()); 00681 } 00682 } else { 00683 add_plant(i % map_width(), i / map_width()); 00684 } 00685 00686 00687 } 00688 pc.printf("plants\r\n"); 00689 00690 pc.printf("Adding walls!\r\n"); 00691 add_wall(0, 0, HORIZONTAL, map_width()); 00692 add_wall(0, map_height()-1, HORIZONTAL, map_width()); 00693 add_wall(0, 0, VERTICAL, map_height()); 00694 add_wall(map_width()-1, 0, VERTICAL, map_height()); 00695 pc.printf("Walls done!\r\n"); 00696 00697 add_river_man(1, 3); 00698 00699 add_toby(4, 8); 00700 add_river_man(map_width()-36,map_height()-44); 00701 00702 add_wall(map_width()-40, map_height()-40, HORIZONTAL, 20); //add puzzle room 00703 add_wall(map_width()-40, map_height()-40, VERTICAL, 20); 00704 add_wall(map_width()-40, map_height()-20, HORIZONTAL, 20); 00705 add_wall(map_width()-20, map_height()-40, VERTICAL, 20); 00706 00707 add_wall(map_width()-40, map_height()-37, HORIZONTAL,20); // add floor 00708 add_wall(map_width()-40, map_height()-33, HORIZONTAL,20); 00709 add_wall(map_width()-40, map_height()-30, HORIZONTAL,20); 00710 add_wall(map_width()-40, map_height()-26, HORIZONTAL,20); 00711 00712 00713 add_red_button(map_width()-37, map_height()-38); // first floor button 00714 add_green_button(map_width()-31, map_height()-38); 00715 add_blue_button(map_width()-23, map_height()-38); 00716 00717 add_red_button(map_width()-37, map_height()-34); // second floor button 00718 add_green_button(map_width()-31, map_height()-34); 00719 add_blue_button(map_width()-23, map_height()-34); 00720 00721 add_red_button(map_width()-37, map_height()-31); // third floor button 00722 add_green_button(map_width()-31, map_height()-31); 00723 add_blue_button(map_width()-23, map_height()-31); 00724 00725 add_red_button(map_width()-37, map_height()-27); // fourth floor button 00726 add_green_button(map_width()-31, map_height()-27); 00727 add_blue_button(map_width()-23, map_height()-27); 00728 00729 add_toby_bone(map_width()-23, map_height()-21); 00730 add_door_locked(map_width()-35, map_height()-40); 00731 add_sign(map_width()-36, map_height()-41); 00732 00733 add_wall(0, map_height()-5, HORIZONTAL, 5); 00734 add_wall(5, map_height()-5, VERTICAL, 5); 00735 00736 add_door_locked(3, map_height()-5); 00737 add_chest(2, map_height()-2); 00738 00739 print_map(); 00740 } 00741 00742 /** 00743 * Program entry point! This is where it all begins. 00744 * This function orchestrates all the parts of the game. Most of your 00745 * implementation should be elsewhere - this holds the game loop, and should 00746 * read like a road map for the rest of the code. 00747 */ 00748 int main() 00749 { 00750 // First things first: initialize hardware 00751 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); 00752 init_start(); 00753 // Initialize the maps 00754 maps_init(); 00755 00756 init_main_map(); 00757 00758 // Initialize game state 00759 set_active_map(0); 00760 Player.x = 5; 00761 Player.y = 5; 00762 00763 // Initial drawing 00764 draw_game(true); 00765 00766 00767 GameInputs in; 00768 int action; 00769 int update; 00770 // Main game loop 00771 while(!game_over) 00772 { 00773 // Timer to measure game update speed 00774 Timer t; t.start(); 00775 00776 // Actuall do the game update: 00777 // 1. Read inputs 00778 in = read_inputs(); 00779 // 2. Determine action (get_action) 00780 action = get_action(in); 00781 // 3. Update game (update_game) 00782 update = update_game(action); 00783 check_health(); 00784 // 3b. Check for game over 00785 // 4. Draw frame (draw_game) 00786 draw_game(update); 00787 // 5. Frame delay 00788 t.stop(); 00789 int dt = t.read_ms(); 00790 if (dt < 100) wait_ms(100 - dt); 00791 } 00792 init_game_over(); 00793 } 00794 00795 void check_health() { 00796 if(health == 0) { 00797 uLCD.cls(); 00798 init_main_map(); 00799 Player.x = 5; 00800 Player.y = 5; 00801 menu = 0; 00802 health = 3; 00803 quest = 0; 00804 door = 0; 00805 draw_game(true); 00806 } 00807 } 00808 int go_up(int x, int y) { 00809 MapItem *item = get_north(x,y); 00810 if(item->walkable || omni_pressed) { 00811 if(run) { 00812 Player.y -= 2; 00813 } else { 00814 Player.y--; 00815 } 00816 return 1; 00817 } else { 00818 return 0; 00819 } 00820 } 00821 00822 int go_down(int x, int y) { 00823 MapItem *item = get_south(x,y); 00824 if(item->walkable || omni_pressed) { 00825 if(run) { 00826 Player.y += 2; 00827 } else { 00828 Player.y++; 00829 } 00830 return 1; 00831 } else { 00832 return 0; 00833 } 00834 } 00835 00836 int go_left(int x, int y) { 00837 MapItem *item = get_west(x,y); 00838 if(item->walkable || omni_pressed) { 00839 if(run==1) { 00840 Player.x -= 2; 00841 } else { 00842 Player.x--; 00843 } 00844 return 1; 00845 } else { 00846 return 0; 00847 } 00848 } 00849 00850 int go_right(int x, int y) { 00851 MapItem *item = get_east(x,y); 00852 if(item->walkable || omni_pressed) { 00853 if(run == 1) { 00854 Player.x += 2; 00855 } else { 00856 Player.x++; 00857 } 00858 return 1; 00859 } else { 00860 return 0; 00861 } 00862 }
Generated on Thu Jul 14 2022 01:43:17 by
1.7.2