ZIWEI LIU / Mbed 2 deprecated ELEC2645_Project_el19z2l

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DodgeEngine.cpp Source File

DodgeEngine.cpp

00001 #include "DodgeEngine.h"
00002 
00003 
00004 DodgeEngine::DodgeEngine(){}
00005 DodgeEngine::~DodgeEngine(){}
00006 
00007 void DodgeEngine::init(int x1up, int y1up, int x1down, int y1down, int x2up, int y2up, int x2down, int y2down, int manx, int many){
00008     
00009     //initiate the block and character
00010     block.blockinit(x1up, y1up, x1down, y1down, x2up, y2up,x2down,y2down);
00011     character.charinit(manx, many);
00012 }
00013 
00014 void DodgeEngine::charactermove(N5110 &lcd,Gamepad &pad){
00015     
00016     //control the movement and check for boundry
00017     character.move_up(pad);
00018     character.move_down();
00019     character.boundry();
00020     character.draw(lcd);
00021     
00022 }
00023 
00024 void DodgeEngine::blockmove(N5110 &lcd){
00025     
00026     //control the movement of the block
00027     block.draw_block(lcd);
00028        
00029 }
00030 
00031 void DodgeEngine::dodgeupdate(N5110 &lcd, Gamepad &pad,int x1up, int y1up, int x1down, int y1down, int x2up, int y2up, int x2down, int y2down, int manx, int many){
00032     
00033     
00034     //check if the character has collide with the walls first
00035     check_crash(lcd,pad,x1up, y1up, x1down, y1down, x2up, y2up,x2down,y2down,manx, many);
00036     show_score(lcd);
00037     
00038     int updatescore;
00039     updatescore = block.get_score();
00040     
00041     //check if the player has pass the game
00042     if(updatescore > 39){
00043         pass_game(lcd,pad);
00044     }
00045         
00046     //update levels by score
00047     if(updatescore > 9 && updatescore < 20){
00048         //int test1 = 1;
00049         block.blockupgrate2();
00050     }
00051     else if(updatescore > 19 &&updatescore <30){
00052         //int test1 = 2;
00053         block.blockupgrate3();
00054     }
00055     else if(updatescore > 29 &&updatescore <40){
00056         //int test1 = 3;
00057         block.blockupgrate4();
00058     }
00059     else{
00060         //int test1 =4;
00061         block.blockupgrate();
00062     }
00063     //printf("the level is at = %d\n",test1);
00064     
00065     block.y_update();
00066   
00067      
00068     
00069 }
00070 
00071 void DodgeEngine::check_crash(N5110 &lcd, Gamepad &pad,int x1up, int y1up, int x1down, int y1down, int x2up, int y2up, int x2down, int y2down, int manx, int many){
00072     Vector2D characterposition = character.get_char_position();
00073     Vector2D upperlimit = block.getposition2();
00074     Vector2D lowerlimit = block.getposition3();
00075     
00076     //check if character has fall on the ground
00077     if(characterposition.y > 35){
00078       
00079         //audio and led feedback
00080         pad.tone(500.0,0.5);
00081         pad.leds_on();
00082         wait(0.1);
00083         pad.leds_off();
00084         print_score(lcd,pad,x1up, y1up, x1down, y1down, x2up, y2up,x2down,y2down,manx, many);
00085         
00086     }
00087     //check if character has crash on the wall
00088     if(upperlimit.x > 15 && upperlimit.x < 26){
00089         
00090         if(characterposition.y < upperlimit.y || characterposition.y + 11 > lowerlimit.y ){
00091             
00092             pad.leds_on();
00093             pad.tone(500.0,0.5);
00094             print_score(lcd,pad,x1up, y1up, x1down, y1down, x2up, y2up,x2down,y2down,manx, many);
00095         }
00096     }
00097     
00098     int check_crash_score;
00099     check_crash_score = block.get_score();
00100     
00101     //printf("score = %2d\n", check_crash_score)
00102     
00103     //check the score and update the level, each level has different coordinates
00104     //for adding the score becasue of the velocity different
00105     if(check_crash_score < 10){
00106         //int test2 = 1;
00107         if(upperlimit.x > 13 && upperlimit.x < 15){
00108             block.add_score();
00109         }
00110     }
00111     else if(check_crash_score >9 && check_crash_score < 20){
00112         //int test2 = 2;
00113         if(upperlimit.x > 12 && upperlimit.x < 15){
00114             block.add_score();
00115         }
00116     }
00117     else if(check_crash_score >19 && check_crash_score < 30){
00118         //int test2 = 3;
00119         if(upperlimit.x > 11 && upperlimit.x < 15){
00120             block.add_score();
00121         }
00122     }
00123     else{ 
00124         //int test2 = 4;
00125         if(upperlimit.x > 10 && upperlimit.x < 15){
00126             block.add_score();
00127         }
00128     
00129     }
00130     //printf("the level2 is at = %d\n",test2);
00131  }   
00132  
00133  void DodgeEngine::print_score(N5110 &lcd, Gamepad &pad,int x1up, int y1up, int x1down, int y1down, int x2up, int y2up, int x2down, int y2down, int manx, int many){
00134     
00135     
00136     int finalscore = block.get_score();
00137     lcd.clear();
00138     char score[14];
00139     while(pad.B_pressed() == false){
00140         sprintf(score, "%2d score!", finalscore);
00141         lcd.printString("Finally...",1,0);
00142         lcd.printString("You achieved",1,1);
00143         lcd.printString(score,1,2);
00144         lcd.printString("Try again?",1,3);
00145         lcd.printString("press B",1,4);
00146         pad.leds_on();
00147         wait(0.1);
00148         pad.leds_off();
00149         wait(0.1);
00150         lcd.refresh();
00151     }
00152     lcd.clear();
00153     init(x1up, y1up, x1down, y1down, x2up, y2up,x2down,y2down,manx, many);
00154     
00155 }
00156 
00157 //show the score on the top left corner on the lcd screen    
00158 void DodgeEngine::show_score(N5110 &lcd){
00159     
00160     int showscore = block.get_score();
00161     char score2[10];
00162     sprintf(score2, "%2d", showscore);
00163     lcd.printString(score2,0,0);
00164     lcd.refresh();
00165     
00166 }
00167 
00168 void DodgeEngine::pass_game(N5110 &lcd, Gamepad &pad){
00169     
00170     lcd.clear();
00171     while(pad.B_pressed() == false){
00172         lcd.printString("Congratulation",1,1);
00173         lcd.printString("All level pass",1,2);
00174         lcd.printString("  Try Again?  ",1,4);
00175         lcd.printString("    Press B   ",1,5);
00176         pad.leds_on();
00177         wait(0.1);
00178         pad.leds_off();
00179         wait(0.1);
00180         lcd.refresh();
00181     }
00182 }
00183