QINGXIANG KONG / Mbed 2 deprecated FlappyBird

Dependencies:   N5110 mbed

Committer:
240742751
Date:
Sat May 09 14:47:26 2015 +0000
Revision:
0:07193f134d3b
mbed NXP LPC1768, Nokia N5110, Joystick, Flappy bird

Who changed what in which revision?

UserRevisionLine numberNew contents of line
240742751 0:07193f134d3b 1 #include "mbed.h"
240742751 0:07193f134d3b 2 #include "N5110.h"
240742751 0:07193f134d3b 3
240742751 0:07193f134d3b 4 //change this to alter tolerance of joystick direction
240742751 0:07193f134d3b 5 #define DIRECTION_TOLERANCE 0.05
240742751 0:07193f134d3b 6
240742751 0:07193f134d3b 7 Serial serial (USBTX,USBRX); // TEST OF JOYSTICK
240742751 0:07193f134d3b 8 N5110 lcd(p7, p8, p9, p10, p11, p13, p26); // N5110
240742751 0:07193f134d3b 9
240742751 0:07193f134d3b 10 int printFlag = 0;
240742751 0:07193f134d3b 11
240742751 0:07193f134d3b 12 int Bird_X=5; // define bird-x
240742751 0:07193f134d3b 13 int Bird_Y=20; // define bird-y
240742751 0:07193f134d3b 14
240742751 0:07193f134d3b 15 int Wallup_x = 20; //define UP wall-x
240742751 0:07193f134d3b 16 int Wallup_y = 1; //define UP wall-Y
240742751 0:07193f134d3b 17 int Walldown_x = 60; //define DOWN wall-x
240742751 0:07193f134d3b 18 int Walldown_y = 20; //define DOWN wall-Y
240742751 0:07193f134d3b 19
240742751 0:07193f134d3b 20 //connections for button A nad button B
240742751 0:07193f134d3b 21 InterruptIn buttonA(p29);
240742751 0:07193f134d3b 22 InterruptIn buttonB(p28);
240742751 0:07193f134d3b 23
240742751 0:07193f134d3b 24 //connections for buzzer
240742751 0:07193f134d3b 25 PwmOut buzzer(p21);
240742751 0:07193f134d3b 26 //Jigglypuff's Song
240742751 0:07193f134d3b 27 float frequency[]= {440,659,554,440,492,554,600,554,494,554,440};
240742751 0:07193f134d3b 28 float beat[]= {1,3,3,3,1,3,1.5,1,3,1.5,0.5}; // speed x 1 (just choose one)
240742751 0:07193f134d3b 29 //float beat[]={2,6,6,6,2,6,3,2,6,3,1}; // speed x 2 (just choose one)
240742751 0:07193f134d3b 30 //float beat[]={3,9,9,9,3,9,4.5,3,9,4.5,1.5}; // speed x 3 (just choose one)
240742751 0:07193f134d3b 31
240742751 0:07193f134d3b 32 //connections for joystick
240742751 0:07193f134d3b 33 AnalogIn xPot(p15);
240742751 0:07193f134d3b 34 AnalogIn yPot(p16);
240742751 0:07193f134d3b 35
240742751 0:07193f134d3b 36 //timer to regularly read the joystick
240742751 0:07193f134d3b 37 Ticker pollJoystick;
240742751 0:07193f134d3b 38
240742751 0:07193f134d3b 39 // create enumerated type (0,1,2,3 etc. for direction)
240742751 0:07193f134d3b 40 // could be extended for diagonals etc.
240742751 0:07193f134d3b 41 enum DirectionName {
240742751 0:07193f134d3b 42 UP,
240742751 0:07193f134d3b 43 DOWN,
240742751 0:07193f134d3b 44 LEFT,
240742751 0:07193f134d3b 45 RIGHT,
240742751 0:07193f134d3b 46 CENTRE,
240742751 0:07193f134d3b 47 UNKNOWN
240742751 0:07193f134d3b 48 };
240742751 0:07193f134d3b 49
240742751 0:07193f134d3b 50 // struct for Joystick
240742751 0:07193f134d3b 51 typedef struct JoyStick Joystick;
240742751 0:07193f134d3b 52 struct JoyStick {
240742751 0:07193f134d3b 53 float x; // current x value
240742751 0:07193f134d3b 54 float x0; // 'centred' x value
240742751 0:07193f134d3b 55 float y; // current y value
240742751 0:07193f134d3b 56 float y0; // 'centred' y value
240742751 0:07193f134d3b 57 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
240742751 0:07193f134d3b 58 DirectionName direction; // current direction
240742751 0:07193f134d3b 59 };
240742751 0:07193f134d3b 60 // create struct variable
240742751 0:07193f134d3b 61 Joystick joystick;
240742751 0:07193f134d3b 62
240742751 0:07193f134d3b 63 // function prototypes
240742751 0:07193f134d3b 64 void calibrateJoystick();
240742751 0:07193f134d3b 65 void updateJoystick();
240742751 0:07193f134d3b 66
240742751 0:07193f134d3b 67 // read default positions of the joystick to calibrate later readings
240742751 0:07193f134d3b 68 void calibrateJoystick()
240742751 0:07193f134d3b 69 {
240742751 0:07193f134d3b 70 //button.mode(PullDown);
240742751 0:07193f134d3b 71 // must not move during calibration
240742751 0:07193f134d3b 72 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
240742751 0:07193f134d3b 73 joystick.y0 = yPot;
240742751 0:07193f134d3b 74 }
240742751 0:07193f134d3b 75 void updateJoystick()
240742751 0:07193f134d3b 76 {
240742751 0:07193f134d3b 77 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
240742751 0:07193f134d3b 78 joystick.x = xPot - joystick.x0;
240742751 0:07193f134d3b 79 joystick.y = yPot - joystick.y0;
240742751 0:07193f134d3b 80 // read button state
240742751 0:07193f134d3b 81 //joystick.button = button;
240742751 0:07193f134d3b 82 // calculate direction depending on x,y values
240742751 0:07193f134d3b 83 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
240742751 0:07193f134d3b 84 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
240742751 0:07193f134d3b 85 joystick.direction = CENTRE;
240742751 0:07193f134d3b 86
240742751 0:07193f134d3b 87 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
240742751 0:07193f134d3b 88 joystick.direction = DOWN;
240742751 0:07193f134d3b 89 Bird_Y=Bird_Y+3; // The bird's down movement of Y-axis is 3 for each refresh time [change the value here to change the value of joystick]
240742751 0:07193f134d3b 90
240742751 0:07193f134d3b 91 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
240742751 0:07193f134d3b 92 joystick.direction = UP;
240742751 0:07193f134d3b 93 Bird_Y = Bird_Y-4; // The bird's up movement of Y-axis is 4 for each refresh time [change the value here to change the value of joystick]
240742751 0:07193f134d3b 94
240742751 0:07193f134d3b 95 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
240742751 0:07193f134d3b 96 joystick.direction = RIGHT;
240742751 0:07193f134d3b 97
240742751 0:07193f134d3b 98 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
240742751 0:07193f134d3b 99 joystick.direction = LEFT;
240742751 0:07193f134d3b 100
240742751 0:07193f134d3b 101 } else {
240742751 0:07193f134d3b 102 joystick.direction = UNKNOWN;
240742751 0:07193f134d3b 103 }
240742751 0:07193f134d3b 104 printFlag = 1;
240742751 0:07193f134d3b 105 }
240742751 0:07193f134d3b 106
240742751 0:07193f134d3b 107 void Bird(int bird_x, int bird_y) // Draw my bird
240742751 0:07193f134d3b 108 {
240742751 0:07193f134d3b 109 for (int i=bird_y; i<=bird_y+5; i++) {
240742751 0:07193f134d3b 110 lcd.setPixel(bird_x,i); // left line of bird's body
240742751 0:07193f134d3b 111 lcd.setPixel(bird_x+5,i); // right line of bird's body
240742751 0:07193f134d3b 112 }
240742751 0:07193f134d3b 113
240742751 0:07193f134d3b 114 for (int j=bird_x+1; j<=bird_x+5; j++)
240742751 0:07193f134d3b 115
240742751 0:07193f134d3b 116 {
240742751 0:07193f134d3b 117 lcd.setPixel(j,bird_y); // up line of bird's body
240742751 0:07193f134d3b 118 lcd.setPixel(j,bird_y+5); // down line of bird's body
240742751 0:07193f134d3b 119 }
240742751 0:07193f134d3b 120
240742751 0:07193f134d3b 121 lcd.setPixel(bird_x+3,bird_y+2);// eye of bird
240742751 0:07193f134d3b 122
240742751 0:07193f134d3b 123 lcd.setPixel(bird_x-2,bird_y+3);// tail of bird
240742751 0:07193f134d3b 124 lcd.setPixel(bird_x-2,bird_y+4);
240742751 0:07193f134d3b 125 lcd.setPixel(bird_x-1,bird_y+4);
240742751 0:07193f134d3b 126
240742751 0:07193f134d3b 127 lcd.setPixel(bird_x+6,bird_y+3);// mouth of bird
240742751 0:07193f134d3b 128 lcd.setPixel(bird_x+6,bird_y+4);
240742751 0:07193f134d3b 129
240742751 0:07193f134d3b 130 lcd.refresh();
240742751 0:07193f134d3b 131 }
240742751 0:07193f134d3b 132
240742751 0:07193f134d3b 133
240742751 0:07193f134d3b 134 void Wall(int x, int y) // Drow the wall
240742751 0:07193f134d3b 135 {
240742751 0:07193f134d3b 136
240742751 0:07193f134d3b 137 for (int k=1; k<=100; k++) { // set the time of wall
240742751 0:07193f134d3b 138 for (int i=x; i<=x+8; i++) { // set teh width of wall
240742751 0:07193f134d3b 139 for (int j=y; j<=y+28; j++) { // set the high of wall
240742751 0:07193f134d3b 140 lcd.setPixel(80*k+i, j); // present wall
240742751 0:07193f134d3b 141 //-------------------------when bird hit on the wall-----------------------
240742751 0:07193f134d3b 142 if (Bird_X == 80*k+i-6 && Bird_Y == j-3 ) {
240742751 0:07193f134d3b 143 lcd.clear();
240742751 0:07193f134d3b 144 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 145 lcd.refresh();
240742751 0:07193f134d3b 146 wait(5.0);
240742751 0:07193f134d3b 147 }
240742751 0:07193f134d3b 148
240742751 0:07193f134d3b 149 else if (Bird_X == 80*k+i-5 && Bird_Y == j ) {
240742751 0:07193f134d3b 150 lcd.clear();
240742751 0:07193f134d3b 151 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 152 lcd.refresh();
240742751 0:07193f134d3b 153 wait(5.0);
240742751 0:07193f134d3b 154 }
240742751 0:07193f134d3b 155
240742751 0:07193f134d3b 156 else if (Bird_X == 80*k+i-4 && Bird_Y == j ) {
240742751 0:07193f134d3b 157 lcd.clear();
240742751 0:07193f134d3b 158 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 159 lcd.refresh();
240742751 0:07193f134d3b 160 wait(5.0);
240742751 0:07193f134d3b 161 }
240742751 0:07193f134d3b 162
240742751 0:07193f134d3b 163 else if (Bird_X == 80*k+i-3 && Bird_Y == j ) {
240742751 0:07193f134d3b 164 lcd.clear();
240742751 0:07193f134d3b 165 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 166 lcd.refresh();
240742751 0:07193f134d3b 167 wait(5.0);
240742751 0:07193f134d3b 168 }
240742751 0:07193f134d3b 169
240742751 0:07193f134d3b 170 else if (Bird_X == 80*k+i-2 && Bird_Y == j ) {
240742751 0:07193f134d3b 171 lcd.clear();
240742751 0:07193f134d3b 172 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 173 lcd.refresh();
240742751 0:07193f134d3b 174 wait(5.0);
240742751 0:07193f134d3b 175 }
240742751 0:07193f134d3b 176
240742751 0:07193f134d3b 177 else if (Bird_X == 80*k+i-1 && Bird_Y == j ) {
240742751 0:07193f134d3b 178 lcd.clear();
240742751 0:07193f134d3b 179 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 180 lcd.refresh();
240742751 0:07193f134d3b 181 wait(5.0);
240742751 0:07193f134d3b 182 }
240742751 0:07193f134d3b 183
240742751 0:07193f134d3b 184 else if (Bird_X == 80*k+i && Bird_Y == j ) {
240742751 0:07193f134d3b 185 lcd.clear();
240742751 0:07193f134d3b 186 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 187 lcd.refresh();
240742751 0:07193f134d3b 188 wait(5.0);
240742751 0:07193f134d3b 189 }
240742751 0:07193f134d3b 190 //-------------------------when bird hit on the wall-----------------------
240742751 0:07193f134d3b 191 }
240742751 0:07193f134d3b 192 }
240742751 0:07193f134d3b 193 }
240742751 0:07193f134d3b 194 lcd.refresh();
240742751 0:07193f134d3b 195 }
240742751 0:07193f134d3b 196
240742751 0:07193f134d3b 197 //------------------define button----------------------------------------------
240742751 0:07193f134d3b 198
240742751 0:07193f134d3b 199 //Button A
240742751 0:07193f134d3b 200 int buttonFlagA = 0;
240742751 0:07193f134d3b 201 //Button B
240742751 0:07193f134d3b 202 int buttonFlagB = 0;
240742751 0:07193f134d3b 203
240742751 0:07193f134d3b 204
240742751 0:07193f134d3b 205 void buttonAPressed()
240742751 0:07193f134d3b 206 {
240742751 0:07193f134d3b 207 buttonFlagA = 1;
240742751 0:07193f134d3b 208 }
240742751 0:07193f134d3b 209
240742751 0:07193f134d3b 210
240742751 0:07193f134d3b 211 void buttonBPressed()
240742751 0:07193f134d3b 212 {
240742751 0:07193f134d3b 213 buttonFlagB = 1;
240742751 0:07193f134d3b 214 }
240742751 0:07193f134d3b 215
240742751 0:07193f134d3b 216 //------------------define button----------------------------------------------
240742751 0:07193f134d3b 217
240742751 0:07193f134d3b 218 int main()
240742751 0:07193f134d3b 219 {
240742751 0:07193f134d3b 220 lcd.init();
240742751 0:07193f134d3b 221
240742751 0:07193f134d3b 222 //present the bird
240742751 0:07193f134d3b 223 Bird(Bird_X , Bird_Y);
240742751 0:07193f134d3b 224
240742751 0:07193f134d3b 225 // square of the screen
240742751 0:07193f134d3b 226 lcd.drawLine(0,0,83,0,1);
240742751 0:07193f134d3b 227 lcd.drawLine(0,47,83,47,1);
240742751 0:07193f134d3b 228 lcd.drawLine(0,1,0,46,1);
240742751 0:07193f134d3b 229 lcd.drawLine(83,1,83,46,1);
240742751 0:07193f134d3b 230
240742751 0:07193f134d3b 231 // show the words of "Flappy Bird"
240742751 0:07193f134d3b 232 lcd.printString("Flappy",26,2);
240742751 0:07193f134d3b 233 lcd.printString("Bird",32,4);
240742751 0:07193f134d3b 234
240742751 0:07193f134d3b 235 lcd.refresh();
240742751 0:07193f134d3b 236
240742751 0:07193f134d3b 237 // read the buzzer
240742751 0:07193f134d3b 238 for (int i= 0; i< (sizeof(frequency) / sizeof(int)); i++) {
240742751 0:07193f134d3b 239 buzzer.period(1.0 / frequency[i]);
240742751 0:07193f134d3b 240 buzzer.write(0.5);
240742751 0:07193f134d3b 241 wait(1.0 / beat[i]);
240742751 0:07193f134d3b 242 buzzer.write(0);
240742751 0:07193f134d3b 243 wait(0.05);
240742751 0:07193f134d3b 244 }
240742751 0:07193f134d3b 245
240742751 0:07193f134d3b 246 calibrateJoystick(); // get centred values of joystick
240742751 0:07193f134d3b 247 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
240742751 0:07193f134d3b 248
240742751 0:07193f134d3b 249 int speed = 0; //change speed of wall down
240742751 0:07193f134d3b 250
240742751 0:07193f134d3b 251 buttonA.rise(&buttonAPressed); //buttonA rise
240742751 0:07193f134d3b 252 buttonB.rise(&buttonBPressed); //buttonB rise
240742751 0:07193f134d3b 253
240742751 0:07193f134d3b 254 while(1) {
240742751 0:07193f134d3b 255
240742751 0:07193f134d3b 256 lcd.clear();
240742751 0:07193f134d3b 257 //---------------------------------------bird hit on top and bottom--------------------------
240742751 0:07193f134d3b 258
240742751 0:07193f134d3b 259 if (Bird_Y <= -4) {
240742751 0:07193f134d3b 260 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 261 lcd.refresh();
240742751 0:07193f134d3b 262 } else if (Bird_Y >= 42) {
240742751 0:07193f134d3b 263 lcd.printString("Game Over",15,2);
240742751 0:07193f134d3b 264 lcd.refresh();
240742751 0:07193f134d3b 265 wait(5);
240742751 0:07193f134d3b 266 }
240742751 0:07193f134d3b 267 else
240742751 0:07193f134d3b 268 {
240742751 0:07193f134d3b 269 //---------------------------------------bird hit on top and bottom--------------------------
240742751 0:07193f134d3b 270
240742751 0:07193f134d3b 271 //---------------------button to change the speed of the wall--------------------------------
240742751 0:07193f134d3b 272
240742751 0:07193f134d3b 273
240742751 0:07193f134d3b 274 if (buttonFlagA==1) {
240742751 0:07193f134d3b 275 buttonFlagA=0;
240742751 0:07193f134d3b 276 speed--;
240742751 0:07193f134d3b 277 lcd.refresh();
240742751 0:07193f134d3b 278 }
240742751 0:07193f134d3b 279
240742751 0:07193f134d3b 280 if (buttonFlagB==1) {
240742751 0:07193f134d3b 281 buttonFlagB=0;
240742751 0:07193f134d3b 282 speed++;
240742751 0:07193f134d3b 283 lcd.refresh();
240742751 0:07193f134d3b 284 }
240742751 0:07193f134d3b 285
240742751 0:07193f134d3b 286 //---------------------button to change the speed of the wall---------------------------------
240742751 0:07193f134d3b 287
240742751 0:07193f134d3b 288 lcd.drawLine(0,0,83,0,1); //present the top line
240742751 0:07193f134d3b 289 lcd.drawLine(0,47,83,47,1); //present the botton line
240742751 0:07193f134d3b 290
240742751 0:07193f134d3b 291 Bird_Y = Bird_Y + 2;
240742751 0:07193f134d3b 292 Bird(Bird_X,Bird_Y); //present the bird
240742751 0:07193f134d3b 293
240742751 0:07193f134d3b 294 Wallup_x = Wallup_x + speed -1; //up wall moving function
240742751 0:07193f134d3b 295 Walldown_x = Walldown_x + speed -1; //down wall moving function
240742751 0:07193f134d3b 296
240742751 0:07193f134d3b 297 Wall(Wallup_x, Wallup_y); //draw the wall
240742751 0:07193f134d3b 298 Wall(Walldown_x, Walldown_y); //draw the wall
240742751 0:07193f134d3b 299
240742751 0:07193f134d3b 300 wait(0.1);
240742751 0:07193f134d3b 301 }
240742751 0:07193f134d3b 302 }
240742751 0:07193f134d3b 303 }