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.
main.cpp@2:748d7682d43c, 2016-05-05 (annotated)
- Committer:
- JasonYang
- Date:
- Thu May 05 09:51:40 2016 +0000
- Revision:
- 2:748d7682d43c
- Parent:
- 1:e28c7063fcd6
el15qy; SID:200986056
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| JasonYang | 0:e27445dfcf3f | 1 | #include "main.h" |
| JasonYang | 2:748d7682d43c | 2 | // this game is similar to flappy bird |
| JasonYang | 2:748d7682d43c | 3 | // the ball will always drop so player should use joystick up |
| JasonYang | 2:748d7682d43c | 4 | // if the ball touch the ground or half ball over the highest line, the game will finish |
| JasonYang | 2:748d7682d43c | 5 | // if the ball touch the line, the game will finish |
| JasonYang | 2:748d7682d43c | 6 | // press button could stop the game and press again to play |
| JasonYang | 2:748d7682d43c | 7 | // when the game start, the led will fliker |
| JasonYang | 2:748d7682d43c | 8 | // when the game finish, the buzzer will sound |
| JasonYang | 0:e27445dfcf3f | 9 | int main() |
| JasonYang | 0:e27445dfcf3f | 10 | { |
| JasonYang | 0:e27445dfcf3f | 11 | // first need to initialise display+ |
| JasonYang | 0:e27445dfcf3f | 12 | lcd.init(); |
| JasonYang | 0:e27445dfcf3f | 13 | button.fall(&ting); |
| JasonYang | 0:e27445dfcf3f | 14 | button.mode(PullDown); |
| JasonYang | 0:e27445dfcf3f | 15 | |
| JasonYang | 0:e27445dfcf3f | 16 | //show the name of this game |
| JasonYang | 0:e27445dfcf3f | 17 | lcd.printString("Flappy ball",10,1); //show game name |
| JasonYang | 0:e27445dfcf3f | 18 | lcd.printString("YangQing",10,3); //show desgin name |
| JasonYang | 0:e27445dfcf3f | 19 | lcd.printString("200986056",10,5); //show student code |
| JasonYang | 0:e27445dfcf3f | 20 | |
| JasonYang | 0:e27445dfcf3f | 21 | wait(0.5); //keep screen 0.5s |
| JasonYang | 0:e27445dfcf3f | 22 | lcd.clear(); //clear screen show another screen |
| JasonYang | 0:e27445dfcf3f | 23 | |
| JasonYang | 0:e27445dfcf3f | 24 | //just simulate the loading |
| JasonYang | 0:e27445dfcf3f | 25 | lcd.printString("LOADING...",8,2); |
| JasonYang | 0:e27445dfcf3f | 26 | char loading [14]; |
| JasonYang | 0:e27445dfcf3f | 27 | for (int a = 0; a<=100; a = a+20) { |
| JasonYang | 0:e27445dfcf3f | 28 | sprintf(loading,"%d %%",a); |
| JasonYang | 0:e27445dfcf3f | 29 | lcd.printString(loading,18,4); |
| JasonYang | 0:e27445dfcf3f | 30 | wait(0.5); |
| JasonYang | 0:e27445dfcf3f | 31 | } |
| JasonYang | 0:e27445dfcf3f | 32 | lcd.clear(); //clear screen show another screen |
| JasonYang | 0:e27445dfcf3f | 33 | ticker.attach(&yellow,0.1); //make yellow light work, lip per 0.1s |
| JasonYang | 0:e27445dfcf3f | 34 | |
| JasonYang | 0:e27445dfcf3f | 35 | // normal colour mode back |
| JasonYang | 0:e27445dfcf3f | 36 | lcd.normalMode(); |
| JasonYang | 0:e27445dfcf3f | 37 | // put LED backlight on 50% |
| JasonYang | 0:e27445dfcf3f | 38 | lcd.setBrightness(0.5); |
| JasonYang | 0:e27445dfcf3f | 39 | |
| JasonYang | 0:e27445dfcf3f | 40 | |
| JasonYang | 0:e27445dfcf3f | 41 | |
| JasonYang | 0:e27445dfcf3f | 42 | calibrateJoystick(); // get centred values of joystick |
| JasonYang | 0:e27445dfcf3f | 43 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
| JasonYang | 0:e27445dfcf3f | 44 | |
| JasonYang | 0:e27445dfcf3f | 45 | |
| JasonYang | 0:e27445dfcf3f | 46 | while(1) { |
| JasonYang | 0:e27445dfcf3f | 47 | |
| JasonYang | 0:e27445dfcf3f | 48 | |
| JasonYang | 0:e27445dfcf3f | 49 | // can directly print strings at specified co-ordinates |
| JasonYang | 0:e27445dfcf3f | 50 | if(stop == true) { |
| JasonYang | 0:e27445dfcf3f | 51 | |
| JasonYang | 0:e27445dfcf3f | 52 | if (printFlag) { // if flag set, clear flag and print joystick values to serial port |
| JasonYang | 0:e27445dfcf3f | 53 | printFlag = 0; |
| JasonYang | 0:e27445dfcf3f | 54 | |
| JasonYang | 0:e27445dfcf3f | 55 | // use a random variable to change the height of vartical line. |
| JasonYang | 0:e27445dfcf3f | 56 | if ( linex1<0 ) { |
| JasonYang | 0:e27445dfcf3f | 57 | linex1 = 80; // for first line the strat x-axis is 80 |
| JasonYang | 0:e27445dfcf3f | 58 | liney1 =rand()%48+30;//random value from 30 to 48 |
| JasonYang | 0:e27445dfcf3f | 59 | } |
| JasonYang | 0:e27445dfcf3f | 60 | |
| JasonYang | 0:e27445dfcf3f | 61 | if ( linex2<0 ) { |
| JasonYang | 0:e27445dfcf3f | 62 | linex2 = 80; // for second line the strat x-axis is 80 |
| JasonYang | 0:e27445dfcf3f | 63 | liney2 =rand()%48+30;//random value from 30 to 48 |
| JasonYang | 0:e27445dfcf3f | 64 | } |
| JasonYang | 0:e27445dfcf3f | 65 | |
| JasonYang | 0:e27445dfcf3f | 66 | if ( linex3<0 ) { |
| JasonYang | 0:e27445dfcf3f | 67 | linex3 = 80; // for third line the strat x-axis is 80 |
| JasonYang | 0:e27445dfcf3f | 68 | liney3 =rand()%48+20;//random value from 20 to 48 |
| JasonYang | 0:e27445dfcf3f | 69 | } |
| JasonYang | 0:e27445dfcf3f | 70 | |
| JasonYang | 0:e27445dfcf3f | 71 | if ( linex4<0 ) { |
| JasonYang | 0:e27445dfcf3f | 72 | linex4 = 80; // for fourth line the strat x-axis is 80 |
| JasonYang | 0:e27445dfcf3f | 73 | liney4 =rand()%48+10; //random value from 10 to 48 |
| JasonYang | 0:e27445dfcf3f | 74 | } |
| JasonYang | 0:e27445dfcf3f | 75 | if ( linex5<0 ) { |
| JasonYang | 0:e27445dfcf3f | 76 | linex5 = 81; // for fifth line the strat x-axis is 81 |
| JasonYang | 0:e27445dfcf3f | 77 | liney5 =rand()%48+10; //random value from 10 to 48 |
| JasonYang | 0:e27445dfcf3f | 78 | } |
| JasonYang | 0:e27445dfcf3f | 79 | |
| JasonYang | 0:e27445dfcf3f | 80 | // let x variable refuce to make these lines like moving to left |
| JasonYang | 0:e27445dfcf3f | 81 | linex1--; |
| JasonYang | 0:e27445dfcf3f | 82 | linex2--; |
| JasonYang | 0:e27445dfcf3f | 83 | linex3--; |
| JasonYang | 0:e27445dfcf3f | 84 | linex4--; |
| JasonYang | 0:e27445dfcf3f | 85 | linex5--; |
| JasonYang | 0:e27445dfcf3f | 86 | lcd.clear(); |
| JasonYang | 0:e27445dfcf3f | 87 | |
| JasonYang | 0:e27445dfcf3f | 88 | // use linex and liney to draw lines |
| JasonYang | 0:e27445dfcf3f | 89 | lcd.drawLine(linex1,liney1,linex1,48,1); // draw first line |
| JasonYang | 0:e27445dfcf3f | 90 | lcd.drawLine(linex2,liney2,linex2,48,1); // draw second line |
| JasonYang | 0:e27445dfcf3f | 91 | lcd.drawLine(linex3,liney3,linex3,48,1); // draw third line |
| JasonYang | 0:e27445dfcf3f | 92 | lcd.drawLine(linex4,liney4,linex4,48,1); // draw fourth line |
| JasonYang | 0:e27445dfcf3f | 93 | lcd.drawLine(linex5,liney5,linex5,48,1); // draw fifth line |
| JasonYang | 0:e27445dfcf3f | 94 | |
| JasonYang | 0:e27445dfcf3f | 95 | // check joystick direction |
| JasonYang | 0:e27445dfcf3f | 96 | if (joystick.direction == UP ) { |
| JasonYang | 0:e27445dfcf3f | 97 | d-=1; // let ball move to up |
| JasonYang | 0:e27445dfcf3f | 98 | char score [14]; // char score variable |
| JasonYang | 0:e27445dfcf3f | 99 | sprintf(score,"%d %",s); // let score show s vaule |
| JasonYang | 0:e27445dfcf3f | 100 | s++; // s increase 1 |
| JasonYang | 0:e27445dfcf3f | 101 | touch(); |
| JasonYang | 0:e27445dfcf3f | 102 | if (flag) { |
| JasonYang | 0:e27445dfcf3f | 103 | lcd.clear(); //clean screen |
| JasonYang | 0:e27445dfcf3f | 104 | lcd.printString("Game over",5,1);//show game over |
| JasonYang | 0:e27445dfcf3f | 105 | lcd.printString("Score:",5,4); //show score on screen |
| JasonYang | 0:e27445dfcf3f | 106 | lcd.printString(score,45,4); |
| JasonYang | 0:e27445dfcf3f | 107 | buzzer.period(0.001); //game over and buzzer work for 0.001 frequency |
| JasonYang | 0:e27445dfcf3f | 108 | buzzer = 0.5; //the volume strong |
| JasonYang | 0:e27445dfcf3f | 109 | wait(1.0); |
| JasonYang | 0:e27445dfcf3f | 110 | break; //game break |
| JasonYang | 0:e27445dfcf3f | 111 | } |
| JasonYang | 0:e27445dfcf3f | 112 | lcd.drawCircle(c,d,4,0); //draw circle |
| JasonYang | 0:e27445dfcf3f | 113 | } |
| JasonYang | 0:e27445dfcf3f | 114 | |
| JasonYang | 0:e27445dfcf3f | 115 | if (joystick.direction == DOWN /*&& d > 5*/) { |
| JasonYang | 0:e27445dfcf3f | 116 | d+=1; // let ball move down |
| JasonYang | 0:e27445dfcf3f | 117 | char score [14]; // char score variable |
| JasonYang | 0:e27445dfcf3f | 118 | sprintf(score,"%d %",s); // let score show s vaule |
| JasonYang | 0:e27445dfcf3f | 119 | s++; // s increase 1 |
| JasonYang | 0:e27445dfcf3f | 120 | touch(); |
| JasonYang | 0:e27445dfcf3f | 121 | if (flag) { |
| JasonYang | 0:e27445dfcf3f | 122 | lcd.clear(); //clean screen |
| JasonYang | 0:e27445dfcf3f | 123 | lcd.printString("Game over",5,1);//show game over |
| JasonYang | 0:e27445dfcf3f | 124 | lcd.printString("Score:",5,4); //show score on screen |
| JasonYang | 0:e27445dfcf3f | 125 | lcd.printString(score,45,4); |
| JasonYang | 0:e27445dfcf3f | 126 | buzzer.period(0.001); //game over and buzzer work for 0.001 frequency |
| JasonYang | 0:e27445dfcf3f | 127 | buzzer = 0.5; //the volume strong |
| JasonYang | 0:e27445dfcf3f | 128 | wait(1.0); |
| JasonYang | 0:e27445dfcf3f | 129 | break; //game break |
| JasonYang | 0:e27445dfcf3f | 130 | } |
| JasonYang | 0:e27445dfcf3f | 131 | lcd.drawCircle(c,d,4,0); //draw circle |
| JasonYang | 0:e27445dfcf3f | 132 | } |
| JasonYang | 0:e27445dfcf3f | 133 | |
| JasonYang | 0:e27445dfcf3f | 134 | if (joystick.direction == LEFT ) { |
| JasonYang | 0:e27445dfcf3f | 135 | c+=1; // let ball move to left |
| JasonYang | 0:e27445dfcf3f | 136 | char score [14]; // char score variable |
| JasonYang | 0:e27445dfcf3f | 137 | sprintf(score,"%d %",s); // let score show s vaule |
| JasonYang | 0:e27445dfcf3f | 138 | s++; // s increase 1 |
| JasonYang | 0:e27445dfcf3f | 139 | touch(); |
| JasonYang | 0:e27445dfcf3f | 140 | if (flag) { |
| JasonYang | 0:e27445dfcf3f | 141 | lcd.clear(); //clean screen |
| JasonYang | 0:e27445dfcf3f | 142 | lcd.printString("Game over",5,1);//show game over |
| JasonYang | 0:e27445dfcf3f | 143 | lcd.printString("Score:",5,4); //show score on screen |
| JasonYang | 0:e27445dfcf3f | 144 | lcd.printString(score,45,4); |
| JasonYang | 0:e27445dfcf3f | 145 | buzzer.period(0.001); //game over and buzzer work for 0.001 frequency |
| JasonYang | 0:e27445dfcf3f | 146 | buzzer = 0.5; //the volume strong |
| JasonYang | 0:e27445dfcf3f | 147 | wait(1.0); |
| JasonYang | 0:e27445dfcf3f | 148 | break; //game break |
| JasonYang | 0:e27445dfcf3f | 149 | } |
| JasonYang | 0:e27445dfcf3f | 150 | lcd.drawCircle(c,d,4,0); //draw circle |
| JasonYang | 0:e27445dfcf3f | 151 | } |
| JasonYang | 0:e27445dfcf3f | 152 | |
| JasonYang | 0:e27445dfcf3f | 153 | if (joystick.direction == RIGHT ) { |
| JasonYang | 0:e27445dfcf3f | 154 | c-=1; // let ball move to right |
| JasonYang | 0:e27445dfcf3f | 155 | char score [14]; // char score variable |
| JasonYang | 0:e27445dfcf3f | 156 | sprintf(score,"%d %",s); // let score show s vaule |
| JasonYang | 0:e27445dfcf3f | 157 | s++; // s increase 1 |
| JasonYang | 0:e27445dfcf3f | 158 | touch(); |
| JasonYang | 0:e27445dfcf3f | 159 | if (flag) { |
| JasonYang | 0:e27445dfcf3f | 160 | lcd.clear(); //clean screen |
| JasonYang | 0:e27445dfcf3f | 161 | lcd.printString("Game over",5,1);//show game over |
| JasonYang | 0:e27445dfcf3f | 162 | lcd.printString("Score:",5,4); //show score on screen |
| JasonYang | 0:e27445dfcf3f | 163 | lcd.printString(score,45,4); |
| JasonYang | 0:e27445dfcf3f | 164 | buzzer.period(0.001); //game over and buzzer work for 0.001 frequency |
| JasonYang | 0:e27445dfcf3f | 165 | buzzer = 0.5; //the volume strong |
| JasonYang | 0:e27445dfcf3f | 166 | wait(1.0); |
| JasonYang | 0:e27445dfcf3f | 167 | break; //game break |
| JasonYang | 0:e27445dfcf3f | 168 | } |
| JasonYang | 0:e27445dfcf3f | 169 | lcd.drawCircle(c,d,4,0); //draw circle |
| JasonYang | 0:e27445dfcf3f | 170 | } |
| JasonYang | 0:e27445dfcf3f | 171 | |
| JasonYang | 0:e27445dfcf3f | 172 | if (joystick.direction == CENTRE ) { |
| JasonYang | 0:e27445dfcf3f | 173 | d+=1; // make ball always fall down |
| JasonYang | 0:e27445dfcf3f | 174 | char score [14]; // char score variable |
| JasonYang | 0:e27445dfcf3f | 175 | sprintf(score,"%d %",s); // let score show s vaule |
| JasonYang | 0:e27445dfcf3f | 176 | s++; // s increase 1 |
| JasonYang | 0:e27445dfcf3f | 177 | touch(); |
| JasonYang | 0:e27445dfcf3f | 178 | if (flag) { |
| JasonYang | 0:e27445dfcf3f | 179 | lcd.clear(); //clean screen |
| JasonYang | 0:e27445dfcf3f | 180 | lcd.printString("Game over",5,1);//show game over |
| JasonYang | 0:e27445dfcf3f | 181 | lcd.printString("Score:",5,4); //show score on screen |
| JasonYang | 0:e27445dfcf3f | 182 | lcd.printString(score,45,4); |
| JasonYang | 0:e27445dfcf3f | 183 | buzzer.period(0.001); //game over and buzzer work for 0.001 frequency |
| JasonYang | 0:e27445dfcf3f | 184 | buzzer = 0.5; //the volume strong |
| JasonYang | 0:e27445dfcf3f | 185 | wait(1.0); |
| JasonYang | 0:e27445dfcf3f | 186 | break; //game break |
| JasonYang | 0:e27445dfcf3f | 187 | } |
| JasonYang | 0:e27445dfcf3f | 188 | lcd.drawCircle(c,d,4,0); //draw circle |
| JasonYang | 0:e27445dfcf3f | 189 | } |
| JasonYang | 0:e27445dfcf3f | 190 | |
| JasonYang | 0:e27445dfcf3f | 191 | } |
| JasonYang | 0:e27445dfcf3f | 192 | lcd.refresh(); |
| JasonYang | 0:e27445dfcf3f | 193 | } else { |
| JasonYang | 0:e27445dfcf3f | 194 | lcd.clear(); |
| JasonYang | 0:e27445dfcf3f | 195 | lcd.printString("Press to play",0,3); //when game stop to show |
| JasonYang | 0:e27445dfcf3f | 196 | lcd.refresh(); |
| JasonYang | 0:e27445dfcf3f | 197 | } |
| JasonYang | 0:e27445dfcf3f | 198 | sleep(); |
| JasonYang | 0:e27445dfcf3f | 199 | } |
| JasonYang | 0:e27445dfcf3f | 200 | } |
| JasonYang | 0:e27445dfcf3f | 201 | |
| JasonYang | 0:e27445dfcf3f | 202 | |
| JasonYang | 0:e27445dfcf3f | 203 | |
| JasonYang | 0:e27445dfcf3f | 204 | void calibrateJoystick() |
| JasonYang | 0:e27445dfcf3f | 205 | { |
| JasonYang | 0:e27445dfcf3f | 206 | button.mode(PullDown); |
| JasonYang | 0:e27445dfcf3f | 207 | // must not move during calibration |
| JasonYang | 0:e27445dfcf3f | 208 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
| JasonYang | 0:e27445dfcf3f | 209 | joystick.y0 = yPot; |
| JasonYang | 0:e27445dfcf3f | 210 | } |
| JasonYang | 0:e27445dfcf3f | 211 | void updateJoystick() |
| JasonYang | 0:e27445dfcf3f | 212 | { |
| JasonYang | 0:e27445dfcf3f | 213 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
| JasonYang | 0:e27445dfcf3f | 214 | joystick.x = xPot - joystick.x0; |
| JasonYang | 0:e27445dfcf3f | 215 | joystick.y = yPot - joystick.y0; |
| JasonYang | 0:e27445dfcf3f | 216 | // read button state |
| JasonYang | 0:e27445dfcf3f | 217 | joystick.button = button; |
| JasonYang | 0:e27445dfcf3f | 218 | |
| JasonYang | 0:e27445dfcf3f | 219 | // calculate direction depending on x,y values |
| JasonYang | 0:e27445dfcf3f | 220 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
| JasonYang | 0:e27445dfcf3f | 221 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| JasonYang | 0:e27445dfcf3f | 222 | joystick.direction = CENTRE; |
| JasonYang | 0:e27445dfcf3f | 223 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| JasonYang | 0:e27445dfcf3f | 224 | joystick.direction = UP; |
| JasonYang | 0:e27445dfcf3f | 225 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| JasonYang | 0:e27445dfcf3f | 226 | joystick.direction = DOWN; |
| JasonYang | 0:e27445dfcf3f | 227 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| JasonYang | 0:e27445dfcf3f | 228 | joystick.direction = RIGHT; |
| JasonYang | 0:e27445dfcf3f | 229 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| JasonYang | 0:e27445dfcf3f | 230 | joystick.direction = LEFT; |
| JasonYang | 0:e27445dfcf3f | 231 | } else { |
| JasonYang | 0:e27445dfcf3f | 232 | joystick.direction = UNKNOWN; |
| JasonYang | 0:e27445dfcf3f | 233 | } |
| JasonYang | 0:e27445dfcf3f | 234 | |
| JasonYang | 0:e27445dfcf3f | 235 | // set flag for printing |
| JasonYang | 0:e27445dfcf3f | 236 | printFlag = 1; |
| JasonYang | 0:e27445dfcf3f | 237 | } |
| JasonYang | 0:e27445dfcf3f | 238 | |
| JasonYang | 0:e27445dfcf3f | 239 | // this function is to judge whether it touch or mot |
| JasonYang | 0:e27445dfcf3f | 240 | void touch() |
| JasonYang | 0:e27445dfcf3f | 241 | { |
| JasonYang | 0:e27445dfcf3f | 242 | // these information is the condition of judgment |
| JasonYang | 0:e27445dfcf3f | 243 | 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)) { |
| JasonYang | 0:e27445dfcf3f | 244 | // flag = 1 means the conduction is achieved |
| JasonYang | 0:e27445dfcf3f | 245 | flag=1; |
| JasonYang | 0:e27445dfcf3f | 246 | } else { |
| JasonYang | 0:e27445dfcf3f | 247 | flag=0; |
| JasonYang | 0:e27445dfcf3f | 248 | } |
| JasonYang | 0:e27445dfcf3f | 249 | } |
| JasonYang | 0:e27445dfcf3f | 250 | |
| JasonYang | 0:e27445dfcf3f | 251 | void yellow() //the function judge yellow lights lip |
| JasonYang | 0:e27445dfcf3f | 252 | { |
| JasonYang | 0:e27445dfcf3f | 253 | yellow_led =! yellow_led; |
| JasonYang | 0:e27445dfcf3f | 254 | } |
| JasonYang | 0:e27445dfcf3f | 255 | |
| JasonYang | 0:e27445dfcf3f | 256 | void ting() // the function judge tp stop game |
| JasonYang | 0:e27445dfcf3f | 257 | { |
| JasonYang | 0:e27445dfcf3f | 258 | stop = !stop; |
| JasonYang | 0:e27445dfcf3f | 259 | } |