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