![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad FLAPPY_BIRD
Diff: Bird/Bird.cpp
- Revision:
- 0:bfd3317d7773
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bird/Bird.cpp Sun May 05 14:57:07 2019 +0000 @@ -0,0 +1,516 @@ + +#include "Bird.h" + +// parameter +int _vb = 1; + +// initialise the parameter +void Bird::init(){ +_birdy = 1; +_x1 = 45; // 21 space interval +_x2 = 66; +_x3 = 88; +_x4 = 109; +_y1 =rand()%18+2; +_y2 =rand()%18+2; +_y3 =rand()%18+2; +_y4 =rand()%18+2; +_score = 0; +} + +// welcome screen - FLAPPY BRID +void Bird::welcome(N5110 &lcd) +{ + +// // F 2*6+7*2+2*5+9*2 + int F[20][6] = { + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,1,1,1,0 }, + { 1,1,1,1,1,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + + +}; + + // L 18*2+2*6 + int L[20][6] = { + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, +}; + + // A 2*6+7*4+2*6+9*4 + int A[20][6] = { + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, +}; + + // P 2*6+7*4+2*6+9*2 + int P[20][6] = { + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, + { 1,1,0,0,0,0 }, +}; + + // Y 11*4+9*2 + int Y[20][6] = { + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, + { 0,0,1,1,0,0 }, +}; + + // B 1*4+1*5+1*5+ 5*4+1*5+2*5+1*5+5*4+1*5+1*5+1*4 + int B[20][6] = { + { 1,1,1,1,0,0 }, + { 1,1,1,1,1,0 }, + { 1,1,0,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,1,1,1 }, + { 1,1,1,1,1,0 }, + { 1,1,1,1,1,0 }, + { 1,1,0,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,1,1,1 }, + { 1,1,1,1,1,0 }, + { 1,1,1,1,0,0 }, +}; + + // I 20*2 + int I[20][2] = { + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, + { 1,1 }, +}; + + // R 2*6+7*4+2*6+1*4+1*5+1*5+6*4 + int R[20][6] = { + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,1,1,1,1 }, + { 1,1,1,1,0,0 }, + { 1,1,1,1,1,0 }, + { 1,1,0,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, +}; + + // D 1*4+1*5+1*5+14*4+1*5+1*5+1*4 + int D[20][6] = { + { 1,1,1,1,0,0 }, + { 1,1,1,1,1,0 }, + { 1,1,0,1,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,0,1,1 }, + { 1,1,0,1,1,1 }, + { 1,1,1,1,1,0 }, + { 1,1,1,1,0,0 }, +}; + +// // drawSprite(x_origin,y_origin,x_rows,y_coloums,sprite) + lcd.drawSprite(4,2,20,6,(int *)F); + lcd.drawSprite(12,2,20,6,(int *)L); + lcd.drawSprite(20,2,20,6,(int *)A); + lcd.drawSprite(28,2,20,6,(int *)P); + lcd.drawSprite(36,2,20,6,(int *)P); + lcd.drawSprite(44,2,20,6,(int *)Y); + lcd.drawSprite(55,2,20,6,(int *)B); + lcd.drawSprite(63,2,20,2,(int *)I); + lcd.drawSprite(67,2,20,6,(int *)R); + lcd.drawSprite(75,2,20,6,(int *)D); + // draw a rectangular around the title + lcd.drawRect(0,0,WIDTH,24,FILL_TRANSPARENT); + lcd.printString(" Press Start ",0,4); + lcd.printString(" To Play ",0,5); + +} + +// choosing difficulty +void Bird::mode(N5110 &lcd, Gamepad &pad) +{ + lcd.clear(); + lcd.printString("Difficulty !",5,0); + lcd.printString("Joy to tune,",0,1); + lcd.printString("A to select. ",0,2); + lcd.printString("Slow",30,3); + lcd.printString("Medium",29,4); + lcd.printString("Fast",30,5); + int select[5][6] = { + { 1,1,1,1,0,0 }, + { 1,1,0,0,1,0 }, + { 1,1,0,0,0,1 }, + { 1,1,0,0,1,0 }, + { 1,1,1,1,0,0 }, + }; + Direction d = pad.get_direction(); + if (d == S){ + _vb = _vb+1; + } + else if (d == N) { + _vb = _vb-1; + } + if (_vb > 3) { + _vb = 1; + } + if (_vb < 1){ + _vb = 3; + } + lcd.drawSprite(20,_vb*8+17,5,6,(int *)select); + lcd.refresh(); + wait(0.3); +} + +// display score +void Bird::score(N5110 &lcd){ + if(_x1+4 > 14-_vb && _x1+4 < 15){ + _score = _score +1; + } + if(_x2+4 > 14-_vb && _x2+4 < 15){ + _score = _score +1; + } + if(_x3+4 > 14-_vb && _x3+4 < 15){ + _score = _score +1; + } + if(_x4+4 > 14-_vb && _x4+4 < 15){ + _score = _score +1; + } + char buffer1[14]; + sprintf(buffer1,"%2d",_score); + lcd.printString(buffer1,0,1); + +} + +// pause display +void Bird::pause(N5110 &lcd, Gamepad &pad){ + if ( pad.check_event(Gamepad::BACK_PRESSED) ){ + while ( pad.check_event(Gamepad::BACK_PRESSED) == false ){ + lcd.clear(); + lcd.printString("PAUSE",25,3); + lcd.refresh(); + } + lcd.clear(); + lcd.printString("GO !",30,4); + lcd.refresh(); + wait(1.0); + } +} + +// first figure +void Bird::stru(N5110 &lcd){ + + lcd.drawRect(0,0,WIDTH,HEIGHT-3,FILL_TRANSPARENT); + lcd.drawRect(0,45,WIDTH,3,FILL_BLACK); + for (int i=0; i<17; i++){ + lcd.setPixel(0,i,false); + lcd.setPixel(13,i,true); + } + for (int i=0; i<14; i++){ + lcd.setPixel(i,0,false); + lcd.setPixel(i,17,true); + lcd.setPixel(13,0,true); + } +} + +// Ready screen +void Bird::ready(N5110 &lcd){ + char buffer[14]; + int number3 = 3; + int number2 = 2; + int number1 = 1; + lcd.clear(); + stru(lcd); + sprintf(buffer,"%1d",number3); + lcd.printString(buffer,40,4); + lcd.refresh(); + wait(1.0); + lcd.clear(); + stru(lcd); + sprintf(buffer,"%1d",number2); + lcd.printString(buffer,40,4); + lcd.refresh(); + wait(1.0); + lcd.clear(); + stru(lcd); + sprintf(buffer,"%1d",number1); + lcd.printString(buffer,40,4); + lcd.refresh(); + wait(1.0); + lcd.clear(); + stru(lcd); + lcd.printString("GO !",30,4); + lcd.refresh(); + wait(0.5); + + + +} + +// Game play +void Bird::barrier(N5110 &lcd){ + stru(lcd); + check(); + // 1-th barrier + lcd.drawRect(_x1,1,4,_y1,FILL_BLACK); + lcd.drawRect(_x1-1,_y1+1,6,2,FILL_BLACK); // 20 pixel interval between upper + lcd.drawRect(_x1-1,_y1+20,6,2,FILL_BLACK); // and lower side + lcd.drawRect(_x1,_y1+22,4,22-_y1,FILL_BLACK); + _x1 = _x1 - _vb; + // 2-th barrier + lcd.drawRect(_x2,1,4,_y2,FILL_BLACK); + lcd.drawRect(_x2-1,_y2+1,6,2,FILL_BLACK); // 20 pixel interval between upper + lcd.drawRect(_x2-1,_y2+20,6,2,FILL_BLACK); // and lower side + lcd.drawRect(_x2,_y2+22,4,22-_y2,FILL_BLACK); + _x2 = _x2 - _vb; + // 3-th barrier + lcd.drawRect(_x3,1,4,_y3,FILL_BLACK); + lcd.drawRect(_x3-1,_y3+1,6,2,FILL_BLACK); // 20 pixel interval between upper + lcd.drawRect(_x3-1,_y3+20,6,2,FILL_BLACK); // and lower side + lcd.drawRect(_x3,_y3+22,4,22-_y3,FILL_BLACK); + _x3 = _x3 - _vb; + // 4-th barrier + lcd.drawRect(_x4,1,4,_y4,FILL_BLACK); + lcd.drawRect(_x4-1,_y4+1,6,2,FILL_BLACK); // 20 pixel interval between upper + lcd.drawRect(_x4-1,_y4+20,6,2,FILL_BLACK); // and lower side + lcd.drawRect(_x4,_y4+22,4,22-_y4,FILL_BLACK); + _x4 = _x4 - _vb; + + +} + +// bird flyup +void Bird::flyup(N5110 &lcd){ + int bird[3][6] = { + { 0,1,1,0,0,0 }, + { 1,1,1,1,0,0 }, + { 1,1,1,1,1,1 }, + }; + _birdy = _birdy - 2; + _birdh = _birdy*3+9; + lcd.drawSprite(15,_birdh,3,6,(int *)bird); +} + +// bird fly down +void Bird::flydown(N5110 &lcd){ + int bird[3][6] = { + { 1,1,1,1,1,1 }, + { 1,1,1,1,0,0 }, + { 0,1,1,0,0,0 }, + }; + _birdy = _birdy + 1; + _birdh = _birdy*3+10; + lcd.drawSprite(15,_birdh,3,6,(int *)bird); +} + +// check if x is smaller than 0 +void Bird::check(){ + if(_x1 < -2){ + _x1 = 85; + _y1 = rand()%18+2; + } + if(_x2 < -2){ + _x2 = 85; + _y2 = rand()%18+2; + } + if(_x3 < -2){ + _x3 = 85; + _y3 = rand()%18+2; + } + if(_x4 < -2){ + _x4 = 85; + _y4 = rand()%18+2; + } +} + +// display failure screen if fail +void Bird::fail(N5110 &lcd, Gamepad &pad){ + if (coll()){ + while ( pad.check_event(Gamepad::START_PRESSED ) == false ){ + lcd.clear(); + lcd.printString(" You've lost ! ",0,0); + char buffer[14]; + sprintf(buffer,"You scored: %d", _score); + lcd.printString(buffer, 0,1); + lcd.printString(" BACK: Author",0,3); + lcd.printString(" Press Start",0,4); + lcd.printString("to play again",0,5); + lcd.refresh(); + if ( pad.check_event(Gamepad::BACK_PRESSED )){ + while ( pad.check_event(Gamepad::BACK_PRESSED ) == false ){ + lcd.clear(); + lcd.printString(" Author: ",0,1); + lcd.printString(" Yang Junyao",10,2); + lcd.printString(" ID: 201090191",0,4); + lcd.refresh(); + } + } + } + lcd.init(); + init(); + pad.init(); + } +} + +// return 1 if collided +bool Bird::coll(){ + if ( _x1 < 21 && _x1+4 > 14 ){ + if( _y1+2 >= _birdh || _y1+20 <= _birdh+2 ){ + return true; + } + else {return false; } + } + if ( _x2 < 21 && _x2+4 > 14 ){ + if( _y2+2 >= _birdh || _y2+20 <= _birdh+2 ){ + return true; + } + else {return false; } + } + if ( _x3 < 21 && _x3+4 > 14 ){ + if( _y3+2 >= _birdh || _y3+20 <= _birdh+2 ){ + return true; + } + else {return false; } + } + if ( _x4 < 21 && _x4+4 > 14 ){ + if( _y4+2 >= _birdh || _y4+20 <= _birdh+2 ){ + return true; + } + else {return false; } + } + if ( _birdh < 2 || _birdh > 42 ){ + return true; + } + else { return false; } +} \ No newline at end of file