Mateo Gomez-Randulfe / Mbed 2 deprecated The_Children_of_Cronos

Dependencies:   mbed

Fork of The_Children_of_Cronos_el15mggr by ELEC2645 (2016/17)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "game.h"
00003 //serial object for checking with printf()
00004 Serial serial(USBTX, USBRX); 
00005 ////////////////////////////////////////////////////
00006 int main()
00007 {
00008     init();           //initialisats all games inits
00009     
00010     //pc.printf("intro done\n");
00011     
00012     intro();          //fancy intro
00013     introlights.detach(); //detach into switching on lights  function
00014     while(menuselection) {
00015         
00016         //pcd.printf("menu screen\n");
00017         
00018         pad.leds_on(); 
00019         float pott=pad.read_pot();
00020         lcd.setBrightness(pott);
00021         menuscreen();              //displays manu screen
00022         menuselect();              //allows to switch between positions
00023     }
00024     
00025 //pc.printf("out of the game\n");
00026 
00027 }
00028 /////////////////////////////////////////////////////////////////
00029 
00030 //if new game selected do this
00031 void newgame()
00032 {
00033     while(selection) {                                      //while the games selection variable is true,allow to choose game
00034     
00035     //pc.printf("games screen in\n");
00036     
00037         pad.leds_on();
00038         float pott=pad.read_pot();
00039         totalscore=scoreg1+scoreg2+scoreg3+scoreg4+scoreg5;//adds up all the individual games scores
00040         
00041         //pc.printf("score:%d\n",totalscore);
00042         
00043         game();                                            //peforms the game selection screen
00044         select();                                          //allows to select between screens
00045         if (r) {                                           //if back button(in this case r) is pressed,set the games selector running variable as false and the menu selecor variable to true
00046         
00047         //pc.printf("go back\n");
00048         
00049             selection=false;
00050             menuselection=true;
00051         }
00052     }
00053 }
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 
00057 //if menuselection is true,menu screen is running
00058 void menuscreen()
00059 {
00060     drawselectmenu();                           //allows o select options
00061     Direction d = pad.get_direction();
00062     if (d==S || a) {
00063         wait(0.1);                            //small button debouncing
00064         if (d==S || a) {
00065             statemenu = menu[statemenu].nextstate[1];  //swiches between options
00066         }
00067     }
00068     if (d==N || y) {
00069         wait(0.1);
00070         if(d==N || y) {
00071             statemenu = menu[statemenu].nextstate[0];
00072         }
00073     }
00074     outputmenu=menu[statemenu].position;
00075     //pc.printf("%d\n",statemenu);
00076 }
00077 
00078 //if selection is true, the games selector screen is running
00079 void game()
00080 {
00081     drawselectscreen();                //displays games to be chosen
00082     Direction d = pad.get_direction();
00083     if (d==S || a) {
00084         wait(0.1);                    //small debouncing
00085         if (d==S || a) {
00086             state = games[state].nextstate[1];
00087         }
00088     }
00089     if (d==N || y) {
00090         wait(0.1);
00091         if(d==N || y) {
00092             state = games[state].nextstate[0];//same as previous function
00093             wait(0.1);
00094         }
00095     }
00096     output=games[state].position;
00097     //pc.printf("%d\n",state);
00098 
00099 }
00100 //in the menu screen,the player selects the desired option screen by pressing l
00101 void menuselect()
00102 {
00103     if(l && outputmenu==0) {   //if one option is chosen
00104         menuselection=false;   //stop running menu screen, log off from this screen
00105         selection=true;       //start running the chosen option, log in in this screen
00106         newgame();            //do the chosen option functions
00107     }
00108 
00109     if(l && outputmenu==1) {
00110         menuselection=false;
00111         highscr=true;
00112         highscores();
00113     }
00114     
00115     if(l && outputmenu==2) {
00116         menuselection=false;
00117         tut=true;
00118         tutorial();
00119     }
00120     
00121     if(l && outputmenu==3) {
00122         menuselection=false;
00123         stor=true;
00124         story();
00125     }
00126 }
00127 
00128 //same as previous function but instead of wih menu options wih the different games
00129 
00130 void select()
00131 {
00132     if(start && output==0) {
00133         selection=false;
00134         game1=true;
00135         gameone();
00136     }
00137     if(start && output==1) {
00138         selection=false;
00139         game2=true;
00140         gametwo();
00141     }
00142     if(start && output==2) {
00143         selection=false;
00144         game3=true;
00145         gamethree();
00146     }
00147     if(start && output==3) {
00148         selection=false;
00149         game4=true;
00150         gamefour();
00151     }
00152     if(start && output==4) {
00153         selection=false;
00154         game5=true;
00155         gamefive();
00156     }
00157 }
00158 
00159 
00160 
00161 //////////////////////GAMES/////////////////////////////////////////////////////
00162 void gamefive()
00163 {
00164     while(game5) {                //if game 5 running
00165     
00166     //pc.printf("game05\n");
00167     
00168         float pott=pad.read_pot();
00169         lcd.setBrightness(pott);
00170         if(back) {
00171             if (tau.score ()>scoreg5) { //if back,get the score if bigger than the previous one
00172                 scoreg5=tau.score ();
00173             }
00174             game5=false;   //log off the game 5 function
00175             selection=true;  //go back to games selecor screen,log into it
00176         }
00177         tau.game(lcd,pad,a,start,b,x);  //performs game 5
00178 
00179     }
00180 }
00181 
00182 void gamefour()
00183 {
00184     
00185     //pc.printf("game04\n");
00186     
00187     while(game4) {
00188         float pott=pad.read_pot();
00189         lcd.setBrightness(pott);
00190         if(back) {                 //same as before
00191             game4=false;
00192             selection=true;
00193         }
00194         if(!cer.die ()) {              //if player hasnt died
00195             cer.commands(b,a,x,y,pad); //performs the game in the addecuate order of first commands(players input)
00196             cer.updatescr();          //then updates the movment of the blocks and check if they are dopped(refer to library for more info)
00197             cer.draw(lcd);           //draws the game and checks if player dies 
00198             cer.velocities();        //depending on the score increase or not the game velocity
00199         } else if(cer.die ()) {     //if died game over and allows to restart
00200             cer.gameover(lcd,start);
00201             if (cer.score ()>scoreg4) {
00202                 scoreg4=cer.score ();
00203             }
00204         }
00205     }
00206 }
00207 
00208 void gamethree()
00209 {
00210     
00211     //pc.printf("game03\n");
00212     
00213     while(game3) {
00214         float pott=pad.read_pot();
00215         lcd.setBrightness(pott);
00216         if(back) {      //same as before
00217             game3=false;
00218             selection=true;
00219         }
00220         if(aurig.touch()) { //if bird didnt touch the pipes
00221             srand( time( NULL ) ); //genrates random
00222             aurig.pipegenerator(pad,start); //generaes pipes (for more info refer to library)
00223             aurig.updatescr(a,b,x,y,pad);   //updates the screen movement and bird movement
00224             aurig.draw(lcd);
00225             wait(0.05);
00226         } else {                           //if bird touched the pipes
00227             aurig.gameover(lcd,pad,l,r); //gameover and allows to reset
00228             if (aurig.score ()>scoreg3) {
00229                 scoreg3=aurig.score ();
00230             }
00231         }
00232     }
00233 }
00234 
00235 void gametwo()
00236 {
00237     
00238     //pc.printf("game02\n");
00239     
00240     alienmove.attach(&aliens, 1.0);
00241     while(game2) {
00242         float pott=pad.read_pot();
00243         lcd.setBrightness(pott);
00244         if(back) {        //same as before
00245             game2=false;
00246             selection=true;
00247             alienmove.detach(); //detach the movement of the aliens if the player gets out of the game
00248         }
00249         if (!stym.die ()) {  //if player didnt die (refer to library for more info)
00250             stym.leds(pad); //lifes showed in the leds
00251             stym.commands(pad,l,b,x,r); //input
00252             stym.touch();  //checks if any bullet has hitted anything
00253             srand(time(NULL));
00254             stym.auxiliar(); //generates new alien bullets
00255             stym.updatescr(); //updates the movement
00256             stym.draw(lcd); //draws the screen
00257             wait(0.08);  //small delay
00258         } else if(stym.die ()) { //if dead
00259             stym.gameover(lcd,start,pad); //gameover allowing to restart
00260             if (stym.score ()>scoreg2) {
00261                 scoreg2=stym.score ();
00262             }
00263         }
00264     }
00265 }
00266 
00267 
00268 void gameone()
00269 {
00270 
00271     //pc.printf("game01\n");
00272 
00273     while(game1) {
00274         float pott=pad.read_pot();
00275         lcd.setBrightness(pott);
00276         if(back) { //same as before
00277             game1=false;
00278             selection=true;
00279         }
00280         if (hyd.border()) { //if player didnt hit anything
00281             hyd.leds(pad); //lifes
00282             hyd.commands(pad,a,b,x,y); //input(refer to library for more info)
00283             if (hyd.eat()) { //if snake eats food
00284                 pad.tone(NOTE_GS6,0.5);
00285                 srand( time( NULL ) );
00286                 hyd.auxiliar(); //generates new food
00287             }
00288             hyd.updatescr(); //updates movement
00289             hyd.pausegame(l,r,lcd,back); //checks if the game has been paused
00290             hyd.draw(lcd); //draws screen
00291             hyd.touch();  //checks if snake ates itself
00292             hyd.velocities(); //the higher score the faster the gam will go
00293         } else {
00294             hyd.gameover(lcd,start,pad);
00295             if (hyd.score()>scoreg1) {
00296                 scoreg1=hyd.score();
00297             }
00298         }
00299     }
00300 }
00301 
00302 ///////////////////////////////////////////////////////////////////////////////
00303 
00304 //draws the games selector screen
00305 void drawselectscreen()
00306 {
00307     lcd.clear();
00308 
00309     if(output==0) {
00310         lcd.drawRect(0,8,84,8,FILL_BLACK);
00311     }
00312     if(output==1) {
00313         lcd.drawRect(0,16,84,8,FILL_BLACK);
00314     }
00315     if(output==2) {
00316         lcd.drawRect(0,24,84,8,FILL_BLACK);
00317     }
00318     if(output==3) {
00319         lcd.drawRect(0,32,84,8,FILL_BLACK);
00320     }
00321     if(output==4) {
00322         lcd.drawRect(0,40,84,8,FILL_BLACK);
00323     }
00324     lcd.printString("DEMETER",0,1);
00325     lcd.printString("ZEUS",0,2);
00326     lcd.printString("HERA",0,3);
00327     lcd.printString("POSEIDON",0,4);
00328     lcd.printString("HADES",0,5);
00329     lcd.refresh();
00330 }
00331 //draws the men options selectors
00332 void drawselectmenu()
00333 {
00334     lcd.clear();
00335     lcd.printString("MENU",30,0);
00336     lcd.printString("GAME",0,2);
00337     lcd.printString("SCORES",0,3);
00338     lcd.printString("TUTORIAL",0,4);
00339     lcd.printString("STORY",0,5);
00340 
00341     if(outputmenu==0) {
00342         lcd.drawRect(0,16,84,8,FILL_BLACK);
00343         lcd.printString("GAME",0,2);
00344     }
00345     if(outputmenu==1) {
00346         lcd.drawRect(0,24,84,8,FILL_BLACK);
00347         lcd.printString("SCORES",0,3);
00348     }
00349     if(outputmenu==2) {
00350         lcd.drawRect(0,32,84,8,FILL_BLACK);
00351         lcd.printString("TUTORIAL",0,4);
00352     }
00353     if(outputmenu==3) {
00354         lcd.drawRect(0,40,84,8,FILL_BLACK);
00355         lcd.printString("STORY",0,5);
00356     }
00357 
00358     lcd.refresh();
00359 }
00360 
00361 //prints the intro
00362 void intro()
00363 {
00364     //pc.printf("intro\n");
00365     Bitmap sprite(randulfe,15,72);
00366     sprite.render(lcd,6,0);
00367     sprite.print();
00368     lcd.printString("presents",5,4);
00369     lcd.refresh();
00370     wait(1);
00371     lcd.printString(".",60,4);
00372     lcd.refresh();
00373     wait(1);
00374     lcd.printString(".",65,4);
00375     lcd.refresh();
00376     wait(1);
00377     lcd.printString(".",70,4);
00378     lcd.refresh();
00379     wait(1);
00380     lcd.clear();
00381     Bitmap sprite1(gamename,15,72);
00382     Bitmap sprite2(gamename1,17,72);
00383     Bitmap sprite3(gamename2,10,13);
00384     sprite1.render(lcd,0,0);
00385     sprite3.render(lcd,25,15);
00386     sprite2.render(lcd,4,26);
00387     sprite1.print();
00388     sprite3.print();
00389     sprite2.print();
00390     lcd.refresh();
00391     wait(8);
00392     lcd.clear();
00393     lcd.printString("In menu scr:",0,0);
00394     lcd.printString("select:l",0,1);
00395     lcd.printString("Back:r",0,2);
00396     lcd.printString("In game scr:",0,3);
00397     lcd.printString("select:start",0,4);
00398     lcd.printString("Back:back",0,5);
00399     lcd.refresh();
00400     wait(4);
00401     lcd.clear();
00402     lcd.printString("In the games",0,0);
00403     lcd.printString("the leds",0,1);
00404     lcd.printString("indicate",0,2);
00405     lcd.printString("your lifes",0,3);
00406     lcd.printString("left!",0,4);
00407     lcd.refresh();
00408     wait(4);
00409 }
00410 
00411 //performs story screen and the story selector algorithm as the one i showed previously
00412 void story()
00413 {
00414     //pc.printf("story\n");
00415     while(stor) {
00416         lcd.clear();
00417         lcd.printString("NEXT",60,5);
00418         if(outputstory==0) {
00419             lcd.printString("Hey there! :)",0,0);
00420             lcd.printString("Welcome to the",0,1);
00421             lcd.printString("Olympus.Hestia",0,2);
00422             lcd.printString("God of home,",0,3);
00423             lcd.printString("needs your hlp",0,4);
00424         }
00425         if(outputstory==1) {
00426             lcd.printString("for protecting",0,0);
00427             lcd.printString("the sacred",0,1);
00428             lcd.printString("fire.But first",0,2);
00429             lcd.printString("you need to",0,3);
00430             lcd.printString("prove to the",0,4);
00431         }
00432         if(outputstory==2) {
00433             lcd.printString("ancient gods",0,0);
00434             lcd.printString("(siblings of",0,1);
00435             lcd.printString("Hestia) that",0,2);
00436             lcd.printString("you're a brave",0,3);
00437             lcd.printString("soldier.",0,4);
00438         }
00439 
00440         if(outputstory==3) {
00441             lcd.printString("You must pass",0,0);
00442             lcd.printString("their challen-",0,1);
00443             lcd.printString("-ges!Get ",0,2);
00444             lcd.printString("ready for the",0,3);
00445             lcd.printString("adventure!!!",0,4);
00446         }
00447         lcd.refresh();
00448 
00449         Direction d = pad.get_direction();
00450         if (d==E || b) {
00451             wait(0.1);
00452             if (d==E || b) {
00453             statestory = games[statestory].nextstate[1];
00454             }
00455         }
00456         if (d==W || x) {
00457             wait(0.1);
00458             if (d==W || x) {
00459             statestory = games[statestory].nextstate[0];
00460          }   
00461         }
00462         outputstory=games[statestory].position;
00463         if(r) {
00464             stor=false;
00465             menuselection=true;
00466         }
00467     }
00468 }
00469 
00470 //shows scores for each game and a total score
00471 void highscores()
00472 {
00473     //pc.printf("highscores\n");
00474     while(highscr) {
00475         lcd.clear();
00476         sprintf(buffer0,"TOT score=%2d",totalscore);
00477         lcd.printString(buffer0,0,0);
00478         sprintf(buffer1,"Game 1=%2d",scoreg1);
00479         lcd.printString(buffer1,0,1);
00480         sprintf(buffer2,"Game 2=%2d",scoreg2);
00481         lcd.printString(buffer2,0,2);
00482         sprintf(buffer3,"Game 3=%2d",scoreg3);
00483         lcd.printString(buffer3,0,3);
00484         sprintf(buffer4,"Game 4=%2d",scoreg4);
00485         lcd.printString(buffer4,0,4);
00486         sprintf(buffer5,"Game 5=%2d",scoreg5);
00487         lcd.printString(buffer5,0,5);
00488         lcd.refresh();
00489         if(r) {
00490             highscr=false;
00491             menuselection=true;
00492         }
00493     }
00494 }
00495 
00496 //draws the tutorial screens and allows to switch between options as showed previousl
00497 void tutorial()
00498 {
00499     //pc.printf("tutorial\n");
00500     while(tut) {
00501         lcd.clear();
00502         lcd.printChar('1',0,0);
00503         lcd.printChar('2',16,0);
00504         lcd.printChar('3',32,0);
00505         lcd.printChar('4',48,0);
00506         lcd.printChar('5',64,0);
00507 
00508         if(outpututorial==0) {
00509             lcd.drawRect(0,0,9,9,FILL_BLACK);
00510             lcd.printString("Y,A,X,B:",0,2);
00511             lcd.printString("UP,DWN,LFT,RGT",0,3);
00512             lcd.printString("L,R     PAUSE",0,5);
00513         }
00514         if(outpututorial==1) {
00515             lcd.drawRect(16,0,9,9,FILL_BLACK);
00516             lcd.printString("A,B,JOY:",0,2);
00517             lcd.printString("MOVE RGT/LFT",0,3);
00518             lcd.printString("L,R     SHOOT",0,5);
00519         }
00520         if(outpututorial==2) {
00521             lcd.drawRect(32,0,9,9,FILL_BLACK);
00522             lcd.printString("A,B,X,Y:",0,2);
00523             lcd.printString("JUMP",0,3);
00524         }
00525         if(outpututorial==3) {
00526             lcd.drawRect(48,0,9,9,FILL_BLACK);
00527             lcd.printString("A/B/X/Y:",0,2);
00528             lcd.printString("LEAVE BLOCK",0,3);
00529         }
00530         if(outpututorial==4) {
00531             lcd.drawRect(64,0,9,9,FILL_BLACK);
00532             lcd.printString("JOY:",0,2);
00533             lcd.printString("SELECT",0,3);
00534             lcd.printString("A:CONTINUE",0,5);
00535         }
00536         lcd.refresh();
00537 
00538         Direction d = pad.get_direction();
00539         if (d==E || b) {
00540             wait(0.1);
00541             if (d==E || b) {
00542             statetutorial = games[statetutorial].nextstate[1];
00543             }
00544         }
00545         if (d==W || x) {
00546             wait(0.1);
00547             if (d==W || x) {
00548             statetutorial = games[statetutorial].nextstate[0];
00549             
00550         }}
00551         outpututorial=games[statetutorial].position;
00552         if(r) {
00553             tut=false;
00554             menuselection=true;
00555         }
00556 
00557 
00558     }
00559 }
00560 //ligh function for increasing one in the intro
00561 void lights()
00562 {
00563 
00564     if(k==1 && lit>6) {
00565         k=-1;
00566     }
00567     if(k==-1 && lit<0) {
00568         k=1;
00569     }
00570     lit+=k;
00571 
00572 
00573     for(int i=1; i<lit; i++) {
00574         pad.led(i,1);
00575     }
00576 }
00577 //function that allows the ticker to peform a library function
00578 void aliens()
00579 {
00580     stym.move();
00581 }
00582 //initialise objects
00583 void init()
00584 {
00585     //pc.printf("game starts\n");
00586     introlights.attach(&lights,1);
00587     tau.init();
00588     cer.init();
00589     aurig.init();
00590     stym.init();
00591     hyd.init();
00592     pad.init();
00593     lcd.init();
00594     lcd.normalMode();
00595     lcd.setBrightness(0.5);
00596 }