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 Gamepad FLAPPY_BIRD
Bird.cpp
00001 00002 #include "Bird.h" 00003 00004 // parameter 00005 int _vb = 1; 00006 00007 // initialise the parameter 00008 void Bird::init(){ 00009 _birdy = 1; 00010 _x1 = 45; // 21 space interval 00011 _x2 = 66; 00012 _x3 = 88; 00013 _x4 = 109; 00014 _y1 =rand()%18+2; 00015 _y2 =rand()%18+2; 00016 _y3 =rand()%18+2; 00017 _y4 =rand()%18+2; 00018 _score = 0; 00019 } 00020 00021 // welcome screen - FLAPPY BRID 00022 void Bird::welcome(N5110 &lcd) 00023 { 00024 00025 // // F 2*6+7*2+2*5+9*2 00026 int F[20][6] = { 00027 { 1,1,1,1,1,1 }, 00028 { 1,1,1,1,1,1 }, 00029 { 1,1,0,0,0,0 }, 00030 { 1,1,0,0,0,0 }, 00031 { 1,1,0,0,0,0 }, 00032 { 1,1,0,0,0,0 }, 00033 { 1,1,0,0,0,0 }, 00034 { 1,1,0,0,0,0 }, 00035 { 1,1,0,0,0,0 }, 00036 { 1,1,1,1,1,0 }, 00037 { 1,1,1,1,1,0 }, 00038 { 1,1,0,0,0,0 }, 00039 { 1,1,0,0,0,0 }, 00040 { 1,1,0,0,0,0 }, 00041 { 1,1,0,0,0,0 }, 00042 { 1,1,0,0,0,0 }, 00043 { 1,1,0,0,0,0 }, 00044 { 1,1,0,0,0,0 }, 00045 { 1,1,0,0,0,0 }, 00046 { 1,1,0,0,0,0 }, 00047 00048 00049 }; 00050 00051 // L 18*2+2*6 00052 int L[20][6] = { 00053 { 1,1,0,0,0,0 }, 00054 { 1,1,0,0,0,0 }, 00055 { 1,1,0,0,0,0 }, 00056 { 1,1,0,0,0,0 }, 00057 { 1,1,0,0,0,0 }, 00058 { 1,1,0,0,0,0 }, 00059 { 1,1,0,0,0,0 }, 00060 { 1,1,0,0,0,0 }, 00061 { 1,1,0,0,0,0 }, 00062 { 1,1,0,0,0,0 }, 00063 { 1,1,0,0,0,0 }, 00064 { 1,1,0,0,0,0 }, 00065 { 1,1,0,0,0,0 }, 00066 { 1,1,0,0,0,0 }, 00067 { 1,1,0,0,0,0 }, 00068 { 1,1,0,0,0,0 }, 00069 { 1,1,0,0,0,0 }, 00070 { 1,1,0,0,0,0 }, 00071 { 1,1,1,1,1,1 }, 00072 { 1,1,1,1,1,1 }, 00073 }; 00074 00075 // A 2*6+7*4+2*6+9*4 00076 int A[20][6] = { 00077 { 1,1,1,1,1,1 }, 00078 { 1,1,1,1,1,1 }, 00079 { 1,1,0,0,1,1 }, 00080 { 1,1,0,0,1,1 }, 00081 { 1,1,0,0,1,1 }, 00082 { 1,1,0,0,1,1 }, 00083 { 1,1,0,0,1,1 }, 00084 { 1,1,0,0,1,1 }, 00085 { 1,1,0,0,1,1 }, 00086 { 1,1,1,1,1,1 }, 00087 { 1,1,1,1,1,1 }, 00088 { 1,1,0,0,1,1 }, 00089 { 1,1,0,0,1,1 }, 00090 { 1,1,0,0,1,1 }, 00091 { 1,1,0,0,1,1 }, 00092 { 1,1,0,0,1,1 }, 00093 { 1,1,0,0,1,1 }, 00094 { 1,1,0,0,1,1 }, 00095 { 1,1,0,0,1,1 }, 00096 { 1,1,0,0,1,1 }, 00097 }; 00098 00099 // P 2*6+7*4+2*6+9*2 00100 int P[20][6] = { 00101 { 1,1,1,1,1,1 }, 00102 { 1,1,1,1,1,1 }, 00103 { 1,1,0,0,1,1 }, 00104 { 1,1,0,0,1,1 }, 00105 { 1,1,0,0,1,1 }, 00106 { 1,1,0,0,1,1 }, 00107 { 1,1,0,0,1,1 }, 00108 { 1,1,0,0,1,1 }, 00109 { 1,1,0,0,1,1 }, 00110 { 1,1,1,1,1,1 }, 00111 { 1,1,1,1,1,1 }, 00112 { 1,1,0,0,0,0 }, 00113 { 1,1,0,0,0,0 }, 00114 { 1,1,0,0,0,0 }, 00115 { 1,1,0,0,0,0 }, 00116 { 1,1,0,0,0,0 }, 00117 { 1,1,0,0,0,0 }, 00118 { 1,1,0,0,0,0 }, 00119 { 1,1,0,0,0,0 }, 00120 { 1,1,0,0,0,0 }, 00121 }; 00122 00123 // Y 11*4+9*2 00124 int Y[20][6] = { 00125 { 1,1,0,0,1,1 }, 00126 { 1,1,0,0,1,1 }, 00127 { 1,1,0,0,1,1 }, 00128 { 1,1,0,0,1,1 }, 00129 { 1,1,0,0,1,1 }, 00130 { 1,1,0,0,1,1 }, 00131 { 1,1,0,0,1,1 }, 00132 { 1,1,0,0,1,1 }, 00133 { 1,1,0,0,1,1 }, 00134 { 1,1,0,0,1,1 }, 00135 { 1,1,0,0,1,1 }, 00136 { 0,0,1,1,0,0 }, 00137 { 0,0,1,1,0,0 }, 00138 { 0,0,1,1,0,0 }, 00139 { 0,0,1,1,0,0 }, 00140 { 0,0,1,1,0,0 }, 00141 { 0,0,1,1,0,0 }, 00142 { 0,0,1,1,0,0 }, 00143 { 0,0,1,1,0,0 }, 00144 { 0,0,1,1,0,0 }, 00145 }; 00146 00147 // B 1*4+1*5+1*5+ 5*4+1*5+2*5+1*5+5*4+1*5+1*5+1*4 00148 int B[20][6] = { 00149 { 1,1,1,1,0,0 }, 00150 { 1,1,1,1,1,0 }, 00151 { 1,1,0,1,1,1 }, 00152 { 1,1,0,0,1,1 }, 00153 { 1,1,0,0,1,1 }, 00154 { 1,1,0,0,1,1 }, 00155 { 1,1,0,0,1,1 }, 00156 { 1,1,0,0,1,1 }, 00157 { 1,1,0,1,1,1 }, 00158 { 1,1,1,1,1,0 }, 00159 { 1,1,1,1,1,0 }, 00160 { 1,1,0,1,1,1 }, 00161 { 1,1,0,0,1,1 }, 00162 { 1,1,0,0,1,1 }, 00163 { 1,1,0,0,1,1 }, 00164 { 1,1,0,0,1,1 }, 00165 { 1,1,0,0,1,1 }, 00166 { 1,1,0,1,1,1 }, 00167 { 1,1,1,1,1,0 }, 00168 { 1,1,1,1,0,0 }, 00169 }; 00170 00171 // I 20*2 00172 int I[20][2] = { 00173 { 1,1 }, 00174 { 1,1 }, 00175 { 1,1 }, 00176 { 1,1 }, 00177 { 1,1 }, 00178 { 1,1 }, 00179 { 1,1 }, 00180 { 1,1 }, 00181 { 1,1 }, 00182 { 1,1 }, 00183 { 1,1 }, 00184 { 1,1 }, 00185 { 1,1 }, 00186 { 1,1 }, 00187 { 1,1 }, 00188 { 1,1 }, 00189 { 1,1 }, 00190 { 1,1 }, 00191 { 1,1 }, 00192 { 1,1 }, 00193 }; 00194 00195 // R 2*6+7*4+2*6+1*4+1*5+1*5+6*4 00196 int R[20][6] = { 00197 { 1,1,1,1,1,1 }, 00198 { 1,1,1,1,1,1 }, 00199 { 1,1,0,0,1,1 }, 00200 { 1,1,0,0,1,1 }, 00201 { 1,1,0,0,1,1 }, 00202 { 1,1,0,0,1,1 }, 00203 { 1,1,0,0,1,1 }, 00204 { 1,1,0,0,1,1 }, 00205 { 1,1,0,0,1,1 }, 00206 { 1,1,1,1,1,1 }, 00207 { 1,1,1,1,1,1 }, 00208 { 1,1,1,1,0,0 }, 00209 { 1,1,1,1,1,0 }, 00210 { 1,1,0,1,1,1 }, 00211 { 1,1,0,0,1,1 }, 00212 { 1,1,0,0,1,1 }, 00213 { 1,1,0,0,1,1 }, 00214 { 1,1,0,0,1,1 }, 00215 { 1,1,0,0,1,1 }, 00216 { 1,1,0,0,1,1 }, 00217 }; 00218 00219 // D 1*4+1*5+1*5+14*4+1*5+1*5+1*4 00220 int D[20][6] = { 00221 { 1,1,1,1,0,0 }, 00222 { 1,1,1,1,1,0 }, 00223 { 1,1,0,1,1,1 }, 00224 { 1,1,0,0,1,1 }, 00225 { 1,1,0,0,1,1 }, 00226 { 1,1,0,0,1,1 }, 00227 { 1,1,0,0,1,1 }, 00228 { 1,1,0,0,1,1 }, 00229 { 1,1,0,0,1,1 }, 00230 { 1,1,0,0,1,1 }, 00231 { 1,1,0,0,1,1 }, 00232 { 1,1,0,0,1,1 }, 00233 { 1,1,0,0,1,1 }, 00234 { 1,1,0,0,1,1 }, 00235 { 1,1,0,0,1,1 }, 00236 { 1,1,0,0,1,1 }, 00237 { 1,1,0,0,1,1 }, 00238 { 1,1,0,1,1,1 }, 00239 { 1,1,1,1,1,0 }, 00240 { 1,1,1,1,0,0 }, 00241 }; 00242 00243 // // drawSprite(x_origin,y_origin,x_rows,y_coloums,sprite) 00244 lcd.drawSprite(4,2,20,6,(int *)F); 00245 lcd.drawSprite(12,2,20,6,(int *)L); 00246 lcd.drawSprite(20,2,20,6,(int *)A); 00247 lcd.drawSprite(28,2,20,6,(int *)P); 00248 lcd.drawSprite(36,2,20,6,(int *)P); 00249 lcd.drawSprite(44,2,20,6,(int *)Y); 00250 lcd.drawSprite(55,2,20,6,(int *)B); 00251 lcd.drawSprite(63,2,20,2,(int *)I); 00252 lcd.drawSprite(67,2,20,6,(int *)R); 00253 lcd.drawSprite(75,2,20,6,(int *)D); 00254 // draw a rectangular around the title 00255 lcd.drawRect(0,0,WIDTH,24,FILL_TRANSPARENT); 00256 lcd.printString(" Press Start ",0,4); 00257 lcd.printString(" To Play ",0,5); 00258 00259 } 00260 00261 // choosing difficulty 00262 void Bird::mode(N5110 &lcd, Gamepad &pad) 00263 { 00264 lcd.clear(); 00265 lcd.printString("Difficulty !",5,0); 00266 lcd.printString("Joy to tune,",0,1); 00267 lcd.printString("A to select. ",0,2); 00268 lcd.printString("Slow",30,3); 00269 lcd.printString("Medium",29,4); 00270 lcd.printString("Fast",30,5); 00271 int select[5][6] = { 00272 { 1,1,1,1,0,0 }, 00273 { 1,1,0,0,1,0 }, 00274 { 1,1,0,0,0,1 }, 00275 { 1,1,0,0,1,0 }, 00276 { 1,1,1,1,0,0 }, 00277 }; 00278 Direction d = pad.get_direction(); 00279 if (d == S){ 00280 _vb = _vb+1; 00281 } 00282 else if (d == N) { 00283 _vb = _vb-1; 00284 } 00285 if (_vb > 3) { 00286 _vb = 1; 00287 } 00288 if (_vb < 1){ 00289 _vb = 3; 00290 } 00291 lcd.drawSprite(20,_vb*8+17,5,6,(int *)select); 00292 lcd.refresh(); 00293 wait(0.3); 00294 } 00295 00296 // display score 00297 void Bird::score(N5110 &lcd){ 00298 if(_x1+4 > 14-_vb && _x1+4 < 15){ 00299 _score = _score +1; 00300 } 00301 if(_x2+4 > 14-_vb && _x2+4 < 15){ 00302 _score = _score +1; 00303 } 00304 if(_x3+4 > 14-_vb && _x3+4 < 15){ 00305 _score = _score +1; 00306 } 00307 if(_x4+4 > 14-_vb && _x4+4 < 15){ 00308 _score = _score +1; 00309 } 00310 char buffer1[14]; 00311 sprintf(buffer1,"%2d",_score); 00312 lcd.printString(buffer1,0,1); 00313 00314 } 00315 00316 // pause display 00317 void Bird::pause(N5110 &lcd, Gamepad &pad){ 00318 if ( pad.check_event(Gamepad::BACK_PRESSED) ){ 00319 while ( pad.check_event(Gamepad::BACK_PRESSED) == false ){ 00320 lcd.clear(); 00321 lcd.printString("PAUSE",25,3); 00322 lcd.refresh(); 00323 } 00324 lcd.clear(); 00325 lcd.printString("GO !",30,4); 00326 lcd.refresh(); 00327 wait(1.0); 00328 } 00329 } 00330 00331 // first figure 00332 void Bird::stru(N5110 &lcd){ 00333 00334 lcd.drawRect(0,0,WIDTH,HEIGHT-3,FILL_TRANSPARENT); 00335 lcd.drawRect(0,45,WIDTH,3,FILL_BLACK); 00336 for (int i=0; i<17; i++){ 00337 lcd.setPixel(0,i,false); 00338 lcd.setPixel(13,i,true); 00339 } 00340 for (int i=0; i<14; i++){ 00341 lcd.setPixel(i,0,false); 00342 lcd.setPixel(i,17,true); 00343 lcd.setPixel(13,0,true); 00344 } 00345 } 00346 00347 // Ready screen 00348 void Bird::ready(N5110 &lcd){ 00349 char buffer[14]; 00350 int number3 = 3; 00351 int number2 = 2; 00352 int number1 = 1; 00353 lcd.clear(); 00354 stru(lcd); 00355 sprintf(buffer,"%1d",number3); 00356 lcd.printString(buffer,40,4); 00357 lcd.refresh(); 00358 wait(1.0); 00359 lcd.clear(); 00360 stru(lcd); 00361 sprintf(buffer,"%1d",number2); 00362 lcd.printString(buffer,40,4); 00363 lcd.refresh(); 00364 wait(1.0); 00365 lcd.clear(); 00366 stru(lcd); 00367 sprintf(buffer,"%1d",number1); 00368 lcd.printString(buffer,40,4); 00369 lcd.refresh(); 00370 wait(1.0); 00371 lcd.clear(); 00372 stru(lcd); 00373 lcd.printString("GO !",30,4); 00374 lcd.refresh(); 00375 wait(0.5); 00376 00377 00378 00379 } 00380 00381 // Game play 00382 void Bird::barrier(N5110 &lcd){ 00383 stru(lcd); 00384 check(); 00385 // 1-th barrier 00386 lcd.drawRect(_x1,1,4,_y1,FILL_BLACK); 00387 lcd.drawRect(_x1-1,_y1+1,6,2,FILL_BLACK); // 20 pixel interval between upper 00388 lcd.drawRect(_x1-1,_y1+20,6,2,FILL_BLACK); // and lower side 00389 lcd.drawRect(_x1,_y1+22,4,22-_y1,FILL_BLACK); 00390 _x1 = _x1 - _vb; 00391 // 2-th barrier 00392 lcd.drawRect(_x2,1,4,_y2,FILL_BLACK); 00393 lcd.drawRect(_x2-1,_y2+1,6,2,FILL_BLACK); // 20 pixel interval between upper 00394 lcd.drawRect(_x2-1,_y2+20,6,2,FILL_BLACK); // and lower side 00395 lcd.drawRect(_x2,_y2+22,4,22-_y2,FILL_BLACK); 00396 _x2 = _x2 - _vb; 00397 // 3-th barrier 00398 lcd.drawRect(_x3,1,4,_y3,FILL_BLACK); 00399 lcd.drawRect(_x3-1,_y3+1,6,2,FILL_BLACK); // 20 pixel interval between upper 00400 lcd.drawRect(_x3-1,_y3+20,6,2,FILL_BLACK); // and lower side 00401 lcd.drawRect(_x3,_y3+22,4,22-_y3,FILL_BLACK); 00402 _x3 = _x3 - _vb; 00403 // 4-th barrier 00404 lcd.drawRect(_x4,1,4,_y4,FILL_BLACK); 00405 lcd.drawRect(_x4-1,_y4+1,6,2,FILL_BLACK); // 20 pixel interval between upper 00406 lcd.drawRect(_x4-1,_y4+20,6,2,FILL_BLACK); // and lower side 00407 lcd.drawRect(_x4,_y4+22,4,22-_y4,FILL_BLACK); 00408 _x4 = _x4 - _vb; 00409 00410 00411 } 00412 00413 // bird flyup 00414 void Bird::flyup(N5110 &lcd){ 00415 int bird[3][6] = { 00416 { 0,1,1,0,0,0 }, 00417 { 1,1,1,1,0,0 }, 00418 { 1,1,1,1,1,1 }, 00419 }; 00420 _birdy = _birdy - 2; 00421 _birdh = _birdy*3+9; 00422 lcd.drawSprite(15,_birdh,3,6,(int *)bird); 00423 } 00424 00425 // bird fly down 00426 void Bird::flydown(N5110 &lcd){ 00427 int bird[3][6] = { 00428 { 1,1,1,1,1,1 }, 00429 { 1,1,1,1,0,0 }, 00430 { 0,1,1,0,0,0 }, 00431 }; 00432 _birdy = _birdy + 1; 00433 _birdh = _birdy*3+10; 00434 lcd.drawSprite(15,_birdh,3,6,(int *)bird); 00435 } 00436 00437 // check if x is smaller than 0 00438 void Bird::check(){ 00439 if(_x1 < -2){ 00440 _x1 = 85; 00441 _y1 = rand()%18+2; 00442 } 00443 if(_x2 < -2){ 00444 _x2 = 85; 00445 _y2 = rand()%18+2; 00446 } 00447 if(_x3 < -2){ 00448 _x3 = 85; 00449 _y3 = rand()%18+2; 00450 } 00451 if(_x4 < -2){ 00452 _x4 = 85; 00453 _y4 = rand()%18+2; 00454 } 00455 } 00456 00457 // display failure screen if fail 00458 void Bird::fail(N5110 &lcd, Gamepad &pad){ 00459 if (coll()){ 00460 while ( pad.check_event(Gamepad::START_PRESSED ) == false ){ 00461 lcd.clear(); 00462 lcd.printString(" You've lost ! ",0,0); 00463 char buffer[14]; 00464 sprintf(buffer,"You scored: %d", _score); 00465 lcd.printString(buffer, 0,1); 00466 lcd.printString(" BACK: Author",0,3); 00467 lcd.printString(" Press Start",0,4); 00468 lcd.printString("to play again",0,5); 00469 lcd.refresh(); 00470 if ( pad.check_event(Gamepad::BACK_PRESSED )){ 00471 while ( pad.check_event(Gamepad::BACK_PRESSED ) == false ){ 00472 lcd.clear(); 00473 lcd.printString(" Author: ",0,1); 00474 lcd.printString(" Yang Junyao",10,2); 00475 lcd.printString(" ID: 201090191",0,4); 00476 lcd.refresh(); 00477 } 00478 } 00479 } 00480 lcd.init(); 00481 init(); 00482 pad.init(); 00483 } 00484 } 00485 00486 // return 1 if collided 00487 bool Bird::coll(){ 00488 if ( _x1 < 21 && _x1+4 > 14 ){ 00489 if( _y1+2 >= _birdh || _y1+20 <= _birdh+2 ){ 00490 return true; 00491 } 00492 else {return false; } 00493 } 00494 if ( _x2 < 21 && _x2+4 > 14 ){ 00495 if( _y2+2 >= _birdh || _y2+20 <= _birdh+2 ){ 00496 return true; 00497 } 00498 else {return false; } 00499 } 00500 if ( _x3 < 21 && _x3+4 > 14 ){ 00501 if( _y3+2 >= _birdh || _y3+20 <= _birdh+2 ){ 00502 return true; 00503 } 00504 else {return false; } 00505 } 00506 if ( _x4 < 21 && _x4+4 > 14 ){ 00507 if( _y4+2 >= _birdh || _y4+20 <= _birdh+2 ){ 00508 return true; 00509 } 00510 else {return false; } 00511 } 00512 if ( _birdh < 2 || _birdh > 42 ){ 00513 return true; 00514 } 00515 else { return false; } 00516 }
Generated on Thu Aug 4 2022 15:50:20 by
1.7.2