ELEC2645 (2015/16) / Mbed 2 deprecated Flappyball

Dependencies:   N5110 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 // this game is similar to flappy bird
00003 // the ball will always drop so player should use joystick up 
00004 // if the ball touch the ground or half ball over the highest line, the game will finish
00005 // if the ball touch the line, the game will finish
00006 // press button could stop the game and press again to play 
00007 // when the game start, the led will fliker
00008 // when the game finish, the buzzer will sound
00009 int main()
00010 {
00011 // first need to initialise display+
00012     lcd.init();
00013     button.fall(&ting);
00014     button.mode(PullDown);
00015 
00016 //show the name of this game
00017     lcd.printString("Flappy ball",10,1); //show game name
00018     lcd.printString("YangQing",10,3);    //show desgin name
00019     lcd.printString("200986056",10,5);   //show student code
00020 
00021     wait(0.5);                           //keep screen 0.5s
00022     lcd.clear();                         //clear screen show another screen
00023 
00024 //just simulate the loading
00025     lcd.printString("LOADING...",8,2);
00026     char loading [14];
00027     for (int a = 0; a<=100; a = a+20) {
00028         sprintf(loading,"%d %%",a);
00029         lcd.printString(loading,18,4);
00030         wait(0.5);
00031     }
00032     lcd.clear();                        //clear screen show another screen
00033     ticker.attach(&yellow,0.1);         //make yellow light work, lip per 0.1s
00034 
00035     // normal colour mode back
00036     lcd.normalMode();
00037     // put LED backlight on 50%
00038     lcd.setBrightness(0.5);
00039 
00040 
00041 
00042     calibrateJoystick();  // get centred values of joystick
00043     pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
00044 
00045 
00046     while(1) {
00047 
00048 
00049         // can directly print strings at specified co-ordinates
00050         if(stop  == true) {
00051 
00052             if (printFlag ) {  // if flag set, clear flag and print joystick values to serial port
00053                 printFlag  = 0;
00054 
00055 //  use a random variable to change the height of vartical line.
00056                 if ( linex1 <0 ) {
00057                     linex1  = 80; // for first line the strat x-axis is 80
00058                     liney1  =rand()%48+30;//random value from 30 to 48
00059                 }
00060 
00061                 if ( linex2 <0 ) {
00062                     linex2  = 80; // for second line the strat x-axis is 80
00063                     liney2  =rand()%48+30;//random value from 30 to 48
00064                 }
00065 
00066                 if ( linex3 <0 ) {
00067                     linex3  = 80; // for third line the strat x-axis is 80
00068                     liney3  =rand()%48+20;//random value from 20 to 48
00069                 }
00070 
00071                 if ( linex4 <0 ) {
00072                     linex4  = 80; // for fourth line the strat x-axis is 80
00073                     liney4  =rand()%48+10; //random value from 10 to 48
00074                 }
00075                 if ( linex5 <0 ) {
00076                     linex5  = 81; // for fifth line the strat x-axis is 81
00077                     liney5  =rand()%48+10; //random value from 10 to 48
00078                 }
00079 
00080 // let x variable refuce to make these lines like moving to left
00081                 linex1 --;
00082                 linex2 --;
00083                 linex3 --;
00084                 linex4 --;
00085                 linex5 --;
00086                 lcd.clear();
00087 
00088 // use linex and liney to draw lines
00089                 lcd.drawLine(linex1 ,liney1 ,linex1 ,48,1);  // draw first line
00090                 lcd.drawLine(linex2 ,liney2 ,linex2 ,48,1);  // draw second line
00091                 lcd.drawLine(linex3 ,liney3 ,linex3 ,48,1);  // draw third line
00092                 lcd.drawLine(linex4 ,liney4 ,linex4 ,48,1);  // draw fourth line
00093                 lcd.drawLine(linex5 ,liney5 ,linex5 ,48,1);  // draw fifth line
00094 
00095 // check joystick direction
00096                 if (joystick.direction == UP ) {
00097                     d -=1;                                // let ball move to up
00098                     char score [14];                     // char score variable
00099                     sprintf(score,"%d %",s );             // let score show s vaule
00100                     s ++;                                 // s increase 1
00101                     touch();
00102                     if (flag ) {
00103                         lcd.clear();                     //clean screen
00104                         lcd.printString("Game over",5,1);//show game over
00105                         lcd.printString("Score:",5,4);   //show score on screen
00106                         lcd.printString(score,45,4);
00107                         buzzer.period(0.001);            //game over and buzzer work for 0.001 frequency
00108                         buzzer = 0.5;                    //the volume strong
00109                         wait(1.0);
00110                         break;                           //game break
00111                     }
00112                     lcd.drawCircle(c ,d ,4,0);             //draw circle
00113                 }
00114 
00115                 if (joystick.direction == DOWN /*&& d > 5*/) {
00116                     d +=1;                                // let ball move down
00117                     char score [14];                     // char score variable
00118                     sprintf(score,"%d %",s );             // let score show s vaule
00119                     s ++;                                 // s increase 1
00120                     touch();
00121                     if (flag ) {
00122                         lcd.clear();                     //clean screen
00123                         lcd.printString("Game over",5,1);//show game over
00124                         lcd.printString("Score:",5,4);   //show score on screen
00125                         lcd.printString(score,45,4);
00126                         buzzer.period(0.001);            //game over and buzzer work for 0.001 frequency
00127                         buzzer = 0.5;                    //the volume strong
00128                         wait(1.0);
00129                         break;                           //game break
00130                     }
00131                     lcd.drawCircle(c ,d ,4,0);             //draw circle
00132                 }
00133 
00134                 if (joystick.direction == LEFT ) {
00135                     c +=1;                                // let ball move to left
00136                     char score [14];                     // char score variable
00137                     sprintf(score,"%d %",s );             // let score show s vaule
00138                     s ++;                                 // s increase 1
00139                     touch();
00140                     if (flag ) {
00141                         lcd.clear();                     //clean screen
00142                         lcd.printString("Game over",5,1);//show game over
00143                         lcd.printString("Score:",5,4);   //show score on screen
00144                         lcd.printString(score,45,4);
00145                         buzzer.period(0.001);            //game over and buzzer work for 0.001 frequency
00146                         buzzer = 0.5;                    //the volume strong
00147                         wait(1.0);
00148                         break;                           //game break
00149                     }
00150                     lcd.drawCircle(c ,d ,4,0);             //draw circle
00151                 }
00152 
00153                 if (joystick.direction == RIGHT ) {
00154                     c -=1;                                // let ball move to right
00155                     char score [14];                     // char score variable
00156                     sprintf(score,"%d %",s );             // let score show s vaule
00157                     s ++;                                 // s increase 1
00158                     touch();
00159                     if (flag ) {
00160                         lcd.clear();                     //clean screen
00161                         lcd.printString("Game over",5,1);//show game over
00162                         lcd.printString("Score:",5,4);   //show score on screen
00163                         lcd.printString(score,45,4);
00164                         buzzer.period(0.001);            //game over and buzzer work for 0.001 frequency
00165                         buzzer = 0.5;                    //the volume strong
00166                         wait(1.0);
00167                         break;                           //game break
00168                     }
00169                     lcd.drawCircle(c ,d ,4,0);             //draw circle
00170                 }
00171 
00172                 if (joystick.direction == CENTRE ) {
00173                     d +=1;                                // make ball always fall down
00174                     char score [14];                     // char score variable
00175                     sprintf(score,"%d %",s );             // let score show s vaule
00176                     s ++;                                 // s increase 1
00177                     touch();
00178                     if (flag ) {
00179                         lcd.clear();                     //clean screen
00180                         lcd.printString("Game over",5,1);//show game over
00181                         lcd.printString("Score:",5,4);   //show score on screen
00182                         lcd.printString(score,45,4);
00183                         buzzer.period(0.001);            //game over and buzzer work for 0.001 frequency
00184                         buzzer = 0.5;                    //the volume strong
00185                         wait(1.0);
00186                         break;                           //game break
00187                     }
00188                     lcd.drawCircle(c ,d ,4,0);             //draw circle
00189                 }
00190 
00191             }
00192             lcd.refresh();
00193         } else {
00194             lcd.clear();
00195             lcd.printString("Press to play",0,3);        //when game stop to show
00196             lcd.refresh();
00197         }
00198         sleep();
00199     }
00200 }
00201 
00202 
00203 
00204 void calibrateJoystick()
00205 {
00206     button.mode(PullDown);
00207     // must not move during calibration
00208     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00209     joystick.y0 = yPot;
00210 }
00211 void updateJoystick()
00212 {
00213     // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00214     joystick.x = xPot - joystick.x0;
00215     joystick.y = yPot - joystick.y0;
00216     // read button state
00217     joystick.button = button;
00218 
00219     // calculate direction depending on x,y values
00220     // tolerance allows a little lee-way in case joystick not exactly in the stated direction
00221     if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00222         joystick.direction = CENTRE;
00223     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00224         joystick.direction = UP;
00225     } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00226         joystick.direction = DOWN;
00227     } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00228         joystick.direction = RIGHT;
00229     } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00230         joystick.direction = LEFT;
00231     } else {
00232         joystick.direction = UNKNOWN;
00233     }
00234 
00235     // set flag for printing
00236     printFlag  = 1;
00237 }
00238 
00239 // this function is to judge whether it touch or mot
00240 void touch()
00241 {
00242 // these information is the condition of judgment
00243     if (lcd.getPixel(c ,d +4)||d +4==47||d ==-4||lcd.getPixel(-4+c ,d )||lcd.getPixel(4+c ,d )||lcd.getPixel(c ,d -4)||lcd.getPixel(c -1,d -4)||lcd.getPixel(c -2,d -4)||lcd.getPixel(c +1,d -3)) {
00244 // flag = 1 means the conduction is achieved
00245         flag =1;
00246     } else {
00247         flag =0;
00248     }
00249 }
00250 
00251 void yellow() //the function judge yellow lights lip
00252 {
00253     yellow_led =! yellow_led;
00254 }
00255 
00256 void ting() // the function judge tp stop game
00257 {
00258     stop  = !stop ;
00259 }