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 taurolib.cpp Source File

taurolib.cpp

00001 #include "mbed.h"
00002 #include "N5110.h"
00003 #include "Gamepad.h"
00004 #include "taurolib.h"
00005 
00006 //constructor
00007 taurolib::taurolib()
00008 {
00009 
00010 }
00011 /**< structure for switching between answer options */
00012 struct State {
00013     int position;/**< actual state */
00014     int nextstate[2];/**< next state */
00015 };
00016 //state array for the answers
00017 State _answer[5]= {
00018     {0,{3,1}},
00019     {1,{0,2}},
00020     {2,{1,3}},
00021     {3,{2,0}}
00022 };
00023 
00024 //sets default output state and count to 0.Therefore,dafault option selected would be the first one
00025 //sets game started as false(_yes)
00026 void taurolib::init()
00027 {
00028     _state=0;
00029     _output=0;
00030     _count=0;
00031     _score=0;
00032     _yes=false;
00033 
00034 }
00035 //depending on the count the question will be the one corresponding the level
00036 void taurolib::drawquestion(N5110 &lcd)
00037 {
00038     if (_count==0) {
00039         lcd.printString("How many",15,0);
00040         lcd.printString("kingdoms",15,1);
00041         lcd.printString("are in",12,2);
00042         lcd.printString("nature?",11,3);
00043 
00044         lcd.printString("3",7,5);
00045         lcd.printString("2",25,5);
00046         lcd.printString("5",49,5);
00047         lcd.printString("6",70,5);
00048     }
00049     if(_count==1) {
00050         lcd.printString("In which",13,0);
00051         lcd.printString("century was",5,1);
00052         lcd.printString("Sheakspere",7,2);
00053         lcd.printString("born?",23,3);
00054         lcd.printString("16",4,5);
00055         lcd.printString("14",24,5);
00056         lcd.printString("17",47,5);
00057         lcd.printString("13",67,5);
00058     }
00059     if(_count==2) {
00060         lcd.printString("tan(45)?",18,1);
00061         lcd.printString("0.7",2,5);
00062         lcd.printString("0.5",23,5);
00063         lcd.printString("1",50,5);
00064         lcd.printString("1.7",64,5);
00065     }
00066     if(_count==3) {
00067         lcd.printString("How many",15,0);
00068         lcd.printString("kinds of",15,1);
00069         lcd.printString("persons con-",5,2);
00070         lcd.printString("-sider Platon?",0,3);
00071         lcd.printString("2",6,5);
00072         lcd.printString("5",28,5);
00073         lcd.printString("10",47,5);
00074         lcd.printString("3",69,5);
00075     }
00076     if(_count==4) {
00077         lcd.printString("How many",15,0);
00078         lcd.printString("electrons does",0,1);
00079         lcd.printString("a molecule",9,2);
00080         lcd.printString("of H20 have?",5,3);
00081         lcd.printString("8",7,5);
00082         lcd.printString("10",24,5);
00083         lcd.printString("6",49,5);
00084         lcd.printString("20",68,5);
00085     }
00086     if(_count==5) {
00087         lcd.printString("How many",15,0);
00088         lcd.printString("countries are a",0,1);
00089         lcd.printString("part of the?",18,2);
00090         lcd.printString("EU?",31,3);
00091         lcd.printString("26",4,5);
00092         lcd.printString("28",25,5);
00093         lcd.printString("18",47,5);
00094         lcd.printString("27",68,5);
00095     }
00096     if(_count==6) {
00097         lcd.printString("How many",15,0);
00098         lcd.printString("elementary",8,1);
00099         lcd.printString("particles",8,2);
00100         lcd.printString("are there?",8,3);
00101         lcd.printString("16",4,5);
00102         lcd.printString("12",25,5);
00103         lcd.printString("3",49,5);
00104         lcd.printString("8",70,5);
00105     }
00106     if(_count==7) {
00107         lcd.printString("When did the",6,0);
00108         lcd.printString("Roman Empire",0,1);
00109         lcd.printString("fall?",5,2);
00110         lcd.printString("476",1,5);
00111         lcd.printString("350",22,5);
00112         lcd.printString("512",41,5);
00113         lcd.printString("342ac",64,5);
00114     }
00115     if(_count==8) {
00116         lcd.printString("What year was",5,0);
00117         lcd.printString("the car",3,1);
00118         lcd.printString("invented in?",0,2);
00119         lcd.printString("(18..?)",18,3);
00120         lcd.printString("64",4,5);
00121         lcd.printString("21",26,5);
00122         lcd.printString("07",47,5);
00123         lcd.printString("48",68,5);
00124     }
00125     if(_count==9) {
00126         lcd.printString("How many",15,0);
00127         lcd.printString("children does",2,1);
00128         lcd.printString("Cronos have?",5,2);
00129         lcd.printString("5",7,5);
00130         lcd.printString("4",28,5);
00131         lcd.printString("6",49,5);
00132         lcd.printString("8",71,5);
00133     }
00134 
00135 }
00136 
00137 
00138 //draws the option selected
00139 void taurolib::drawselect(N5110 &lcd)
00140 {
00141     lcd.drawRect(0,32,20,15,FILL_TRANSPARENT);
00142     lcd.drawRect(21,32,20,15,FILL_TRANSPARENT);
00143     lcd.drawRect(42,32,20,15,FILL_TRANSPARENT);
00144     lcd.drawRect(63,32,20,15,FILL_TRANSPARENT);
00145     if(_output==0) {
00146         lcd.drawRect(0,32,20,15,FILL_BLACK);
00147     }
00148     if(_output==1) {
00149         lcd.drawRect(21,32,20,15,FILL_BLACK);
00150     }
00151     if(_output==2) {
00152         lcd.drawRect(42,32,20,15,FILL_BLACK);
00153     }
00154     if(_output==3) {
00155         lcd.drawRect(63,32,20,15,FILL_BLACK);
00156     }
00157 }
00158 
00159 //draw main function of the questions screen
00160 void taurolib::draw(N5110 &lcd,Gamepad &pad,DigitalOut &b,DigitalOut &x)
00161 {
00162     Direction d = pad.get_direction();
00163     if (d==E || b) {
00164         wait(0.1);    //small debouncing
00165         if (d==E || b) {
00166             _state = _answer[_state].nextstate[1];
00167             pad.tone(NOTE_A1,0.5);
00168         }
00169     }
00170     if (d==W || x) {
00171         wait(0.1);
00172         if (d==W || x) {
00173             _state = _answer[_state].nextstate[0];
00174             pad.tone(NOTE_A1,0.5);
00175         }
00176     }
00177 
00178     _output=_answer[_state].position;
00179     lcd.clear();
00180     taurolib::drawselect(lcd);
00181     taurolib::drawquestion(lcd);
00182     lcd.refresh();
00183 }
00184 
00185 //transition screens depending on output
00186 void taurolib::transition(N5110 &lcd)
00187 {
00188     if(_count==0) {
00189         lcd.clear();
00190         lcd.printString("Level 1 done!",10,0);
00191         lcd.printString("Get ready for:",3,1);
00192         lcd.printString("Level 2:",8,3);
00193         lcd.printString("Literature",8,4);
00194         sprintf(_buffer,"Score=%2d",_score);
00195         lcd.printString(_buffer,13,5);
00196         lcd.refresh();
00197         wait(2);
00198     }
00199     if(_count==1) {
00200         lcd.clear();
00201         lcd.printString("Level 2 done!",10,0);
00202         lcd.printString("Get ready for:",3,1);
00203         lcd.printString("Level 3:",8,3);
00204         lcd.printString("Maths",5,4);
00205         sprintf(_buffer,"Score=%2d",_score);
00206         lcd.printString(_buffer,13,5);
00207         lcd.refresh();
00208         wait(2);
00209     }
00210     if(_count==2) {
00211         lcd.clear();
00212         lcd.printString("Level 3 done!",10,0);
00213         lcd.printString("Get ready for:",3,1);
00214         lcd.printString("Level 4:",8,3);
00215         lcd.printString("Philosophy",5,4);
00216         sprintf(_buffer,"Score=%2d",_score);
00217         lcd.printString(_buffer,13,5);
00218         lcd.refresh();
00219         wait(2);
00220     }
00221     if(_count==3) {
00222         lcd.clear();
00223         lcd.printString("Level 4 done!",10,0);
00224         lcd.printString("Get ready for:",3,1);
00225         lcd.printString("Level 5:",8,3);
00226         lcd.printString("Chemistry",5,4);
00227         sprintf(_buffer,"Score=%2d",_score);
00228         lcd.printString(_buffer,13,5);
00229         lcd.refresh();
00230         wait(2);
00231     }
00232     if(_count==4) {
00233         lcd.clear();
00234         lcd.printString("Level 5 done!",10,0);
00235         lcd.printString("Get ready for:",3,1);
00236         lcd.printString("Level 6:",8,3);
00237         lcd.printString("Geography",5,4);
00238         sprintf(_buffer,"Score=%2d",_score);
00239         lcd.printString(_buffer,13,5);
00240         lcd.refresh();
00241         wait(2);
00242     }
00243     if(_count==5) {
00244         lcd.clear();
00245         lcd.printString("Level 6 done!",10,0);
00246         lcd.printString("Get ready for:",3,1);
00247         lcd.printString("Level 7:",8,3);
00248         lcd.printString("Physics",5,4);
00249         sprintf(_buffer,"Score=%2d",_score);
00250         lcd.printString(_buffer,13,5);
00251         lcd.refresh();
00252         wait(2);
00253     }
00254     if(_count==6) {
00255         lcd.clear();
00256         lcd.printString("Level 7 done!",10,0);
00257         lcd.printString("Get ready for:",3,1);
00258         lcd.printString("Level 8:",8,3);
00259         lcd.printString("History",5,4);
00260         sprintf(_buffer,"Score=%2d",_score);
00261         lcd.printString(_buffer,13,5);
00262         lcd.refresh();
00263         wait(2);
00264     }
00265     if(_count==7) {
00266         lcd.clear();
00267         lcd.printString("Level 8 done!",10,0);
00268         lcd.printString("Get ready for:",3,1);
00269         lcd.printString("Level 9:",8,3);
00270         lcd.printString("Technology",5,4);
00271         sprintf(_buffer,"Score=%2d",_score);
00272         lcd.printString(_buffer,13,5);
00273         lcd.refresh();
00274         wait(2);
00275     }
00276     if(_count==8) {
00277         lcd.clear();
00278         lcd.printString("Level 9 done!",10,0);
00279         lcd.printString("Get ready for:",3,1);
00280         lcd.printString("Level 10:",8,3);
00281         lcd.printString("You",15,4);
00282         sprintf(_buffer,"Score=%2d",_score);
00283         lcd.printString(_buffer,13,5);
00284         lcd.refresh();
00285         wait(2);
00286     }
00287 
00288     if(_count==9) {
00289         lcd.clear();
00290         lcd.printString("Your final",0,0);
00291         lcd.printString("score is:",0,1);
00292         sprintf(_buffer,"Score=%2d",_score);
00293         lcd.printString(_buffer,0,2);
00294         if(_score==100) {
00295             _score+=100;
00296             lcd.printString("100%,you just",0,4);
00297             lcd.printString("became a God",0,5);
00298         }
00299         lcd.refresh();
00300         wait(2);
00301     }
00302 }
00303 //intro screen
00304 void taurolib::intro(N5110 &lcd)
00305 {
00306     lcd.clear();
00307     lcd.printString("Welcome,",17,1);
00308     lcd.printString("press start",7,3);
00309     lcd.refresh();
00310 }
00311 //game over screen
00312 void taurolib::gameover(N5110 &lcd)
00313 {
00314     lcd.clear();
00315     lcd.printString("MECK!! WRONG",6,1);
00316     lcd.printString("-5 pts",20,4);
00317     lcd.refresh();
00318     _score-=5;
00319     wait(2);
00320 }
00321 //checks if answer was right and passes the transition and/or gameover screen
00322 void taurolib::update(N5110 &lcd,DigitalOut &a)
00323 {
00324     if(_output==2 && _count==0 && a) {
00325         _score+=10;
00326         taurolib::transition(lcd);
00327         _count++;
00328     } else if(_output!=2 && _count==0 && a) {
00329         taurolib::gameover(lcd);
00330         taurolib::transition(lcd);
00331         _count++;
00332     }
00333 
00334 
00335     if(_output==0 && _count==1 && a) {
00336         _score+=10;
00337         taurolib::transition(lcd);
00338         _count++;
00339     } else if(_output!=0 && _count==1 && a) {
00340         taurolib::gameover(lcd);
00341         taurolib::transition(lcd);
00342         _count++;
00343     }
00344 
00345 
00346     if(_output==2 && _count==2 && a) {
00347         _score+=10;
00348         taurolib::transition(lcd);
00349         _count++;
00350     } else if(_output!=2 && _count==2 && a) {
00351         taurolib::gameover(lcd);
00352         taurolib::transition(lcd);
00353         _count++;
00354     }
00355 
00356 
00357     if(_output==3 && _count==3 && a) {
00358         _score+=10;
00359         taurolib::transition(lcd);
00360         _count++;
00361     } else if(_output!=3 && _count==3 && a) {
00362         taurolib::gameover(lcd);
00363         taurolib::transition(lcd);
00364         _count++;
00365     }
00366 
00367 
00368     if(_output==1 && _count==4 && a) {
00369         _score+=10;
00370         taurolib::transition(lcd);
00371         _count++;
00372     } else if(_output!=1 && _count==4 && a) {
00373         taurolib::gameover(lcd);
00374         taurolib::transition(lcd);
00375         _count++;
00376     }
00377 
00378 
00379     if(_output==3 && _count==5 && a) {
00380         taurolib::transition(lcd);
00381         _score+=10;
00382         _count++;
00383     } else if(_output!=3 && _count==5 && a) {
00384         taurolib::gameover(lcd);
00385         taurolib::transition(lcd);
00386         _count++;
00387     }
00388 
00389 
00390     if(_output==1 && _count==6 && a) {
00391         _score+=10;
00392         taurolib::transition(lcd);
00393         _count++;
00394     } else if(_output!=1 && _count==6 && a) {
00395         taurolib::gameover(lcd);
00396         taurolib::transition(lcd);
00397         _count++;
00398     }
00399 
00400 
00401     if(_output==0 && _count==7 && a) {
00402         _score+=10;
00403         taurolib::transition(lcd);
00404         _count++;
00405     } else if(_output!=0 && _count==7 && a) {
00406         taurolib::gameover(lcd);
00407         taurolib::transition(lcd);
00408         _count++;
00409     }
00410 
00411 
00412     if(_output==2 && _count==8 && a) {
00413         _score+=10;
00414         taurolib::transition(lcd);
00415         _count++;
00416     } else if(_output!=2 && _count==8 && a) {
00417         taurolib::gameover(lcd);
00418         taurolib::transition(lcd);
00419         _count++;
00420     }
00421 
00422     if(_output==2 && _count==9 && a) {
00423         _score+=10;
00424         taurolib::transition(lcd);
00425         _count++;
00426     } else if(_output!=2 && _count==9 && a) {
00427         taurolib::gameover(lcd);
00428         taurolib::transition(lcd);
00429         _count++;
00430     }
00431     if(_count==10) {
00432         _yes=false;
00433         _count=0;
00434         _score=0;
00435     }
00436 
00437 }
00438 int taurolib::score ()
00439 {
00440     return _score;
00441 }
00442 
00443 void taurolib::game(N5110 &lcd,Gamepad &pad,DigitalOut &a,DigitalOut &start,DigitalOut &b,DigitalOut &x)
00444 {
00445     if(!_yes) { //while the game hasnt started
00446         taurolib::intro(lcd);
00447     }
00448     if(start) {
00449         _yes=true;
00450     }
00451     if(_yes) { //if it has started
00452         taurolib::draw(lcd,pad,b,x);
00453         taurolib::update(lcd,a);
00454         wait(0.1);
00455     }
00456 } //small delay