renhao dai / Mbed 2 deprecated Joystick_snake_game

Dependencies:   N5110 mbed

Committer:
el14rd
Date:
Sun May 03 19:06:32 2015 +0000
Revision:
6:290173d4a22d
Parent:
5:d8a06e7c54fb
Child:
7:69ae0585d11a
with problem on button

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 0:026fa541af7a 1 #include "mbed.h"
el14rd 1:f682aeb462f1 2 #include "N5110.h"
eencae 0:026fa541af7a 3 #define DIRECTION_TOLERANCE 0.05
eencae 0:026fa541af7a 4
el14rd 1:f682aeb462f1 5
el14rd 1:f682aeb462f1 6 //pins in
el14rd 1:f682aeb462f1 7 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
el14rd 1:f682aeb462f1 8
eencae 0:026fa541af7a 9 // connections for joystick
el14rd 4:41acda9d68c7 10 DigitalIn button(p17);
el14rd 6:290173d4a22d 11 AnalogIn buttonA(p19);
el14rd 1:f682aeb462f1 12 AnalogIn xPot(p15);
el14rd 1:f682aeb462f1 13 AnalogIn yPot(p16);
el14rd 4:41acda9d68c7 14 PwmOut buzzer(p21);
el14rd 4:41acda9d68c7 15 PwmOut blacklight(p26);
el14rd 4:41acda9d68c7 16 PwmOut led1(p24);
el14rd 4:41acda9d68c7 17 PwmOut led2(p23);
el14rd 4:41acda9d68c7 18
el14rd 1:f682aeb462f1 19 int i;
el14rd 1:f682aeb462f1 20 int j;
el14rd 2:d7b17623ba26 21 int X;
el14rd 2:d7b17623ba26 22 int Y;
el14rd 5:d8a06e7c54fb 23 char buffer[14];
el14rd 1:f682aeb462f1 24
el14rd 1:f682aeb462f1 25 int snakeX[100];
el14rd 1:f682aeb462f1 26 int snakeY[100];
eencae 0:026fa541af7a 27
eencae 0:026fa541af7a 28 // timer to regularly read the joystick
eencae 0:026fa541af7a 29 Ticker pollJoystick;
eencae 0:026fa541af7a 30 // Serial for debug
eencae 0:026fa541af7a 31 Serial serial(USBTX,USBRX);
eencae 0:026fa541af7a 32
eencae 0:026fa541af7a 33 // create enumerated type (0,1,2,3 etc. for direction)
eencae 0:026fa541af7a 34 // could be extended for diagonals etc.
eencae 0:026fa541af7a 35 enum DirectionName {
eencae 0:026fa541af7a 36 UP,
eencae 0:026fa541af7a 37 DOWN,
eencae 0:026fa541af7a 38 LEFT,
eencae 0:026fa541af7a 39 RIGHT,
eencae 0:026fa541af7a 40 CENTRE,
eencae 0:026fa541af7a 41 UNKNOWN
eencae 0:026fa541af7a 42 };
eencae 0:026fa541af7a 43
eencae 0:026fa541af7a 44 // struct for Joystick
eencae 0:026fa541af7a 45 typedef struct JoyStick Joystick;
eencae 0:026fa541af7a 46 struct JoyStick {
eencae 0:026fa541af7a 47 float x; // current x value
eencae 0:026fa541af7a 48 float x0; // 'centred' x value
eencae 0:026fa541af7a 49 float y; // current y value
eencae 0:026fa541af7a 50 float y0; // 'centred' y value
eencae 0:026fa541af7a 51 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
eencae 0:026fa541af7a 52 DirectionName direction; // current direction
eencae 0:026fa541af7a 53 };
eencae 0:026fa541af7a 54 // create struct variable
eencae 0:026fa541af7a 55 Joystick joystick;
eencae 0:026fa541af7a 56
el14rd 6:290173d4a22d 57 int buttonAFlag = 0;
eencae 0:026fa541af7a 58 int printFlag = 0;
el14rd 3:04ff8cd27dd1 59 int nutFlag =1;
el14rd 5:d8a06e7c54fb 60 int lifeFlag =0;
el14rd 5:d8a06e7c54fb 61 float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440};
el14rd 5:d8a06e7c54fb 62 float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2};
el14rd 2:d7b17623ba26 63 //int buttonFlag = 0;
eencae 0:026fa541af7a 64 // function prototypes
el14rd 1:f682aeb462f1 65 //void setPixel(int x, int y);
el14rd 1:f682aeb462f1 66 //void clearPixel(int x, int y);
el14rd 1:f682aeb462f1 67
eencae 0:026fa541af7a 68
el14rd 1:f682aeb462f1 69 typedef struct Nut Nut;
el14rd 1:f682aeb462f1 70 struct Nut {
el14rd 1:f682aeb462f1 71 int X;
el14rd 1:f682aeb462f1 72 int Y;
el14rd 1:f682aeb462f1 73 int yes;
el14rd 1:f682aeb462f1 74 };
el14rd 1:f682aeb462f1 75 Nut nut;
eencae 0:026fa541af7a 76
el14rd 1:f682aeb462f1 77 typedef struct Snake Snake;
el14rd 1:f682aeb462f1 78 struct Snake {
el14rd 1:f682aeb462f1 79 int snakeX[100];
el14rd 1:f682aeb462f1 80 int snakeY[100];
el14rd 1:f682aeb462f1 81 int node;
el14rd 1:f682aeb462f1 82 int life;
el14rd 1:f682aeb462f1 83 DirectionName direction;
el14rd 1:f682aeb462f1 84 };
el14rd 1:f682aeb462f1 85 Snake snake;
eencae 0:026fa541af7a 86
el14rd 1:f682aeb462f1 87 DirectionName snakeDirection;
el14rd 1:f682aeb462f1 88
el14rd 2:d7b17623ba26 89 void initNut()
el14rd 2:d7b17623ba26 90 {
el14rd 3:04ff8cd27dd1 91 nut.X=rand()%84;
el14rd 3:04ff8cd27dd1 92 nut.Y=rand()%48;
el14rd 3:04ff8cd27dd1 93 //nutFlag=1;
el14rd 2:d7b17623ba26 94 }
el14rd 3:04ff8cd27dd1 95
el14rd 2:d7b17623ba26 96 void drawNut()
el14rd 2:d7b17623ba26 97 {
el14rd 3:04ff8cd27dd1 98 //int X = rand()%84;
el14rd 3:04ff8cd27dd1 99 //int Y = rand()%48;
el14rd 3:04ff8cd27dd1 100 if(nutFlag==1){
el14rd 4:41acda9d68c7 101 lcd.drawCircle(nut.X,nut.Y,1,1);
el14rd 3:04ff8cd27dd1 102 }
el14rd 2:d7b17623ba26 103 }
eencae 0:026fa541af7a 104
el14rd 1:f682aeb462f1 105 void moveSnake()
el14rd 1:f682aeb462f1 106 {
el14rd 3:04ff8cd27dd1 107
el14rd 4:41acda9d68c7 108 if(joystick.direction == CENTRE){
el14rd 4:41acda9d68c7 109 //snake.snakeX[0]+=1;
el14rd 4:41acda9d68c7 110 joystick.direction = CENTRE;
el14rd 3:04ff8cd27dd1 111 }
el14rd 4:41acda9d68c7 112 //else if(joystick.button == button){
el14rd 4:41acda9d68c7 113 // snake.snakeX[i]=snake.snakeX[i];
el14rd 4:41acda9d68c7 114 // snake.snakeY[i]=snake.snakeY[i];
el14rd 4:41acda9d68c7 115 // }
el14rd 5:d8a06e7c54fb 116 else if(joystick.direction == UNKNOWN){
el14rd 5:d8a06e7c54fb 117 joystick.direction = UNKNOWN;
el14rd 5:d8a06e7c54fb 118 }
el14rd 3:04ff8cd27dd1 119 else if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) {
el14rd 2:d7b17623ba26 120
el14rd 4:41acda9d68c7 121 if((joystick.direction == DOWN)&&(snake.direction!=LEFT)) {
el14rd 2:d7b17623ba26 122 snake.direction= RIGHT;
el14rd 4:41acda9d68c7 123 //snake.snakeX[0]+=1;
el14rd 3:04ff8cd27dd1 124
el14rd 4:41acda9d68c7 125 } else if((joystick.direction == UP)&&(snake.direction!=RIGHT)) {
el14rd 2:d7b17623ba26 126 snake.direction=LEFT;
el14rd 4:41acda9d68c7 127 //snake.snakeX[0]-=1;
el14rd 4:41acda9d68c7 128 } else if((joystick.direction == LEFT)&&(snake.direction!=DOWN)) {
el14rd 5:d8a06e7c54fb 129 snake.direction=UP;
el14rd 4:41acda9d68c7 130 //snake.snakeY[0]-=1;
el14rd 4:41acda9d68c7 131 } else if((joystick.direction == RIGHT)&&(snake.direction!=UP)) {
el14rd 5:d8a06e7c54fb 132 snake.direction=DOWN;
el14rd 4:41acda9d68c7 133 //snake.snakeY[0]+=1;
eencae 0:026fa541af7a 134 }
eencae 0:026fa541af7a 135
eencae 0:026fa541af7a 136 }
el14rd 3:04ff8cd27dd1 137
el14rd 2:d7b17623ba26 138 }
el14rd 2:d7b17623ba26 139
el14rd 2:d7b17623ba26 140 void initSnake()
el14rd 2:d7b17623ba26 141 {
el14rd 5:d8a06e7c54fb 142
el14rd 5:d8a06e7c54fb 143 snake.snakeX[0] = 16; //coordinate of head
el14rd 2:d7b17623ba26 144 snake.snakeY[0] = 10;
el14rd 5:d8a06e7c54fb 145 snake.snakeX[1] = 14;
el14rd 2:d7b17623ba26 146 snake.snakeY[1] = 10;
el14rd 4:41acda9d68c7 147 snake.snakeX[2] = 12;
el14rd 2:d7b17623ba26 148 snake.snakeY[2] = 10;
el14rd 5:d8a06e7c54fb 149 snake.snakeX[3] = 10;
el14rd 2:d7b17623ba26 150 snake.snakeY[3] = 10;
el14rd 5:d8a06e7c54fb 151 snake.node = 6; //node of snake
el14rd 4:41acda9d68c7 152 snake.direction = RIGHT;
el14rd 5:d8a06e7c54fb 153
el14rd 5:d8a06e7c54fb 154 }
el14rd 2:d7b17623ba26 155
eencae 0:026fa541af7a 156
el14rd 1:f682aeb462f1 157 void drawSnake()
el14rd 5:d8a06e7c54fb 158 {
el14rd 5:d8a06e7c54fb 159
el14rd 5:d8a06e7c54fb 160 for(i=0; i<snake.node-1; i++) {
el14rd 5:d8a06e7c54fb 161 lcd.drawCircle(snake.snakeX[i],snake.snakeY[i],1,1);
el14rd 5:d8a06e7c54fb 162 //lcd.setPixel(snake.snakeX[i],snake.snakeY[i]);
el14rd 5:d8a06e7c54fb 163 }
el14rd 5:d8a06e7c54fb 164
el14rd 1:f682aeb462f1 165 }
el14rd 1:f682aeb462f1 166
el14rd 1:f682aeb462f1 167 void startGmae()
el14rd 1:f682aeb462f1 168 {
el14rd 2:d7b17623ba26 169 //lcd.drawRect(snake.snakeX[0]+10,snake.snakeY[0]+10,10,10,1);
el14rd 3:04ff8cd27dd1 170 //nutFlag = 1;
el14rd 5:d8a06e7c54fb 171 //lifeFlag = 0;
el14rd 2:d7b17623ba26 172 //snake.direction=LEFT;
el14rd 1:f682aeb462f1 173 //if(joystick.direction == UNKNOWN)
el14rd 4:41acda9d68c7 174
el14rd 6:290173d4a22d 175 if((snake.snakeX[0]==nut.X)&&(snake.snakeY[0]==nut.Y)) { // if snake got nut ||(snake.snakeX[0]==nut.X-1)&&(snake.snakeY[0]==nut.Y-1)||(snake.snakeY[0]==nut.Y+1)&&(snake.snakeY[0]==nut.Y+1
el14rd 3:04ff8cd27dd1 176 snake.node++;
el14rd 5:d8a06e7c54fb 177
el14rd 5:d8a06e7c54fb 178 //buzzer.period(1/659); // set PWM period
el14rd 5:d8a06e7c54fb 179 //buzzer=0.1; // set duty cycle
el14rd 5:d8a06e7c54fb 180 //wait(0.1);
el14rd 3:04ff8cd27dd1 181 nutFlag=0; // new nut appear
el14rd 3:04ff8cd27dd1 182 }
el14rd 3:04ff8cd27dd1 183
el14rd 3:04ff8cd27dd1 184 if(nutFlag==0) { //set nut randomly
el14rd 1:f682aeb462f1 185 nut.X = rand()%83;
el14rd 1:f682aeb462f1 186 nut.Y = rand()%47;
el14rd 3:04ff8cd27dd1 187 nutFlag = 1;
el14rd 1:f682aeb462f1 188 }
el14rd 2:d7b17623ba26 189
el14rd 5:d8a06e7c54fb 190
el14rd 5:d8a06e7c54fb 191 for(i=3; i<snake.node; i++) { //if snake collide itself snake die
el14rd 5:d8a06e7c54fb 192 if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) {
el14rd 5:d8a06e7c54fb 193 lifeFlag=1;
el14rd 5:d8a06e7c54fb 194 // break;
el14rd 5:d8a06e7c54fb 195 }
el14rd 5:d8a06e7c54fb 196 }
el14rd 4:41acda9d68c7 197
el14rd 4:41acda9d68c7 198 for(i=snake.node-1; i>0; i--) { //move snake
el14rd 4:41acda9d68c7 199 if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47){
el14rd 4:41acda9d68c7 200 snake.snakeX[i]=snake.snakeX[i-1];
el14rd 4:41acda9d68c7 201 snake.snakeY[i]=snake.snakeY[i-1];
el14rd 4:41acda9d68c7 202 }
el14rd 4:41acda9d68c7 203 else{
el14rd 4:41acda9d68c7 204 snake.snakeX[i]=snake.snakeX[i];
el14rd 4:41acda9d68c7 205 snake.snakeY[i]=snake.snakeY[i];
el14rd 4:41acda9d68c7 206 }
el14rd 4:41acda9d68c7 207 }
el14rd 4:41acda9d68c7 208
el14rd 1:f682aeb462f1 209
el14rd 1:f682aeb462f1 210 if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) {
el14rd 5:d8a06e7c54fb 211 lifeFlag=1; // snake collide the corrider snake die
el14rd 1:f682aeb462f1 212 //break;
el14rd 1:f682aeb462f1 213 }
el14rd 1:f682aeb462f1 214
el14rd 1:f682aeb462f1 215
el14rd 5:d8a06e7c54fb 216 //if(snake.life==1) {
el14rd 1:f682aeb462f1 217 //break;
el14rd 5:d8a06e7c54fb 218 //}
el14rd 3:04ff8cd27dd1 219
el14rd 4:41acda9d68c7 220
el14rd 1:f682aeb462f1 221
el14rd 1:f682aeb462f1 222 }
el14rd 1:f682aeb462f1 223
el14rd 5:d8a06e7c54fb 224 void printScore(){
el14rd 6:290173d4a22d 225 lcd.printString("Your Score is",0,0);
el14rd 6:290173d4a22d 226 int score=snake.node-6;
el14rd 6:290173d4a22d 227 int length = sprintf(buffer," SCORE=%2d ",score);
el14rd 6:290173d4a22d 228 lcd.printString(buffer,0,2 );
el14rd 6:290173d4a22d 229 lcd.printString("Press to start",0,4);
el14rd 5:d8a06e7c54fb 230 }
el14rd 6:290173d4a22d 231
el14rd 6:290173d4a22d 232 void buttonAPressed(){
el14rd 6:290173d4a22d 233 buttonAFlag=1;
el14rd 5:d8a06e7c54fb 234 }
el14rd 6:290173d4a22d 235
el14rd 1:f682aeb462f1 236 //while(joystick.direction == UNKNOWN) {
el14rd 1:f682aeb462f1 237
el14rd 1:f682aeb462f1 238
el14rd 1:f682aeb462f1 239 //switch(snake.direction)
el14rd 1:f682aeb462f1 240 //{
el14rd 1:f682aeb462f1 241 // case RIGHT:snake.X[0]+=1;
el14rd 1:f682aeb462f1 242 // break;    
el14rd 1:f682aeb462f1 243 // case LEFT:snake.X[0]-=1;
el14rd 1:f682aeb462f1 244 // break;     
el14rd 1:f682aeb462f1 245 // case UP:snake.Y[0]-=1;
el14rd 1:f682aeb462f1 246 // break;
el14rd 1:f682aeb462f1 247 // case DOWN:snake.Y[0]+=1;
el14rd 1:f682aeb462f1 248 // break;
el14rd 1:f682aeb462f1 249 // }
el14rd 1:f682aeb462f1 250
el14rd 1:f682aeb462f1 251 //end of while(joystick.direction = UNKNOWN)
el14rd 1:f682aeb462f1 252
el14rd 1:f682aeb462f1 253
el14rd 1:f682aeb462f1 254 // if(joystick.direction == UP&&snake.direction!=DOWN) {
el14rd 1:f682aeb462f1 255 // snake.direction=UP;
el14rd 1:f682aeb462f1 256 // } else if(joystick.direction == RIGHT&&snake.direction!=LEFT) {
el14rd 1:f682aeb462f1 257 // snake.direction= RIGHT;
el14rd 1:f682aeb462f1 258 // } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) {
el14rd 1:f682aeb462f1 259 // snake.direction=LEFT;
el14rd 1:f682aeb462f1 260 // } else(joystick.direction == DOWN&&snake.direction!=UP) {
el14rd 1:f682aeb462f1 261 // snake.direction=DOWN;
el14rd 1:f682aeb462f1 262 // }
el14rd 1:f682aeb462f1 263
el14rd 1:f682aeb462f1 264
el14rd 1:f682aeb462f1 265
eencae 0:026fa541af7a 266 // read default positions of the joystick to calibrate later readings
eencae 0:026fa541af7a 267 void calibrateJoystick()
eencae 0:026fa541af7a 268 {
eencae 0:026fa541af7a 269 button.mode(PullDown);
eencae 0:026fa541af7a 270 // must not move during calibration
eencae 0:026fa541af7a 271 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
eencae 0:026fa541af7a 272 joystick.y0 = yPot;
eencae 0:026fa541af7a 273 }
el14rd 1:f682aeb462f1 274
el14rd 2:d7b17623ba26 275
eencae 0:026fa541af7a 276 void updateJoystick()
eencae 0:026fa541af7a 277 {
eencae 0:026fa541af7a 278 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
eencae 0:026fa541af7a 279 joystick.x = xPot - joystick.x0;
eencae 0:026fa541af7a 280 joystick.y = yPot - joystick.y0;
eencae 0:026fa541af7a 281 // read button state
eencae 0:026fa541af7a 282 joystick.button = button;
eencae 0:026fa541af7a 283
eencae 0:026fa541af7a 284 // calculate direction depending on x,y values
eencae 0:026fa541af7a 285 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
eencae 0:026fa541af7a 286 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 287 joystick.direction = CENTRE;
el14rd 4:41acda9d68c7 288 //LED OFF
eencae 0:026fa541af7a 289 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 290 joystick.direction = UP;
el14rd 4:41acda9d68c7 291 //LIGHT LED1
el14rd 1:f682aeb462f1 292 //snake.direction = UP;
eencae 0:026fa541af7a 293 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 294 joystick.direction = DOWN;
el14rd 4:41acda9d68c7 295 //LIGHT LED2
eencae 0:026fa541af7a 296 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 297 joystick.direction = RIGHT;
el14rd 4:41acda9d68c7 298 //LIGHT LED3
eencae 0:026fa541af7a 299 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 300 joystick.direction = LEFT;
eencae 0:026fa541af7a 301 } else {
eencae 0:026fa541af7a 302 joystick.direction = UNKNOWN;
eencae 0:026fa541af7a 303 }
eencae 0:026fa541af7a 304 // set flag for printing
eencae 0:026fa541af7a 305 printFlag = 1;
el14rd 1:f682aeb462f1 306 }
el14rd 1:f682aeb462f1 307
el14rd 1:f682aeb462f1 308 void updateSnake()
el14rd 1:f682aeb462f1 309 {
el14rd 1:f682aeb462f1 310 // get direction of joystick and update snake
el14rd 4:41acda9d68c7 311 if (snake.direction == LEFT) {
el14rd 2:d7b17623ba26 312 snake.snakeX[0]--;
el14rd 4:41acda9d68c7 313 //LED1
el14rd 4:41acda9d68c7 314 if(snake.snakeX[0]<1) {
el14rd 4:41acda9d68c7 315 snake.snakeX[0]=1;
el14rd 4:41acda9d68c7 316
el14rd 1:f682aeb462f1 317 }//will stop at the top edge
el14rd 1:f682aeb462f1 318
el14rd 1:f682aeb462f1 319 }
el14rd 4:41acda9d68c7 320 if (snake.direction == RIGHT) {
el14rd 2:d7b17623ba26 321 snake.snakeX[0]++;
el14rd 4:41acda9d68c7 322 //LED2
el14rd 4:41acda9d68c7 323 if(snake.snakeX[0]>83) {
el14rd 4:41acda9d68c7 324 snake.snakeX[0]=83;
el14rd 1:f682aeb462f1 325 }//will stop at the bottom edge
el14rd 1:f682aeb462f1 326 }
el14rd 4:41acda9d68c7 327 if (snake.direction == UP) {
el14rd 5:d8a06e7c54fb 328 snake.snakeY[0]--;
el14rd 4:41acda9d68c7 329 if(snake.snakeY[0]>47) {
el14rd 4:41acda9d68c7 330 snake.snakeY[0]=47;
el14rd 1:f682aeb462f1 331 }//will stop at the left edge
el14rd 1:f682aeb462f1 332
el14rd 1:f682aeb462f1 333 }
el14rd 4:41acda9d68c7 334 if (snake.direction == DOWN) {
el14rd 5:d8a06e7c54fb 335 snake.snakeY[0]++;
el14rd 4:41acda9d68c7 336 if(snake.snakeY[0]<1) {
el14rd 4:41acda9d68c7 337 snake.snakeY[0]=1;
el14rd 1:f682aeb462f1 338 }//will stop at the right edge
el14rd 1:f682aeb462f1 339
el14rd 1:f682aeb462f1 340 }
el14rd 4:41acda9d68c7 341 if(snake.direction == CENTRE){
el14rd 4:41acda9d68c7 342 snake.snakeX[0]+=1;
el14rd 4:41acda9d68c7 343 }
el14rd 5:d8a06e7c54fb 344 if(snake.direction == UNKNOWN){
el14rd 5:d8a06e7c54fb 345 snake.snakeX[0]+=1;
el14rd 5:d8a06e7c54fb 346 }
el14rd 1:f682aeb462f1 347 }
el14rd 1:f682aeb462f1 348
el14rd 1:f682aeb462f1 349
el14rd 2:d7b17623ba26 350
el14rd 1:f682aeb462f1 351 //main code
el14rd 1:f682aeb462f1 352 int main()
el14rd 1:f682aeb462f1 353 {
el14rd 1:f682aeb462f1 354 lcd.init();
el14rd 1:f682aeb462f1 355 lcd.setBrightness(0.5); // put LED backlight on 50%
el14rd 5:d8a06e7c54fb 356 //button.rise(&buttonPressed);
el14rd 1:f682aeb462f1 357 calibrateJoystick(); // get centred values of joystick
el14rd 1:f682aeb462f1 358 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
el14rd 1:f682aeb462f1 359 lcd.refresh();
el14rd 2:d7b17623ba26 360 initNut();
el14rd 2:d7b17623ba26 361 initSnake();
el14rd 5:d8a06e7c54fb 362 //buzzer.period(0.020); // a 20ms period
el14rd 6:290173d4a22d 363 //buttonA.rise(&buttonAPressed);
el14rd 5:d8a06e7c54fb 364
el14rd 4:41acda9d68c7 365 //printScore();
el14rd 1:f682aeb462f1 366
el14rd 1:f682aeb462f1 367 //infinite while loop
el14rd 1:f682aeb462f1 368 while(1) {
el14rd 5:d8a06e7c54fb 369
el14rd 6:290173d4a22d 370 if(buttonAFlag==1){
el14rd 6:290173d4a22d 371 buttonAFlag=0;
el14rd 1:f682aeb462f1 372 lcd.clear();
el14rd 5:d8a06e7c54fb 373 lifeFlag=0;
el14rd 1:f682aeb462f1 374 updateSnake();
el14rd 1:f682aeb462f1 375 drawSnake();
el14rd 2:d7b17623ba26 376 drawNut();
el14rd 1:f682aeb462f1 377 moveSnake();
el14rd 1:f682aeb462f1 378 startGmae();
el14rd 5:d8a06e7c54fb 379 if(lifeFlag==1){
el14rd 6:290173d4a22d 380 lcd.clear();
el14rd 6:290173d4a22d 381 lcd.refresh();
el14rd 6:290173d4a22d 382 printScore();
el14rd 6:290173d4a22d 383 for(int i=0; i<=11; i++){
el14rd 6:290173d4a22d 384 buzzer.period(1/(frequency[i])); // set PWM period
el14rd 6:290173d4a22d 385 buzzer=0.1; // set duty cycle
el14rd 6:290173d4a22d 386 wait(0.5*beat[i]);
el14rd 6:290173d4a22d 387 }
el14rd 6:290173d4a22d 388 lifeFlag=0;
el14rd 5:d8a06e7c54fb 389 }
el14rd 5:d8a06e7c54fb 390 lcd.refresh();
el14rd 6:290173d4a22d 391 wait(0.02);
el14rd 6:290173d4a22d 392 }
el14rd 6:290173d4a22d 393
el14rd 6:290173d4a22d 394 else
el14rd 6:290173d4a22d 395 lcd.printString("Press to start",1,1);
el14rd 5:d8a06e7c54fb 396 }
el14rd 2:d7b17623ba26 397 //wait(0.5);
el14rd 2:d7b17623ba26 398 //if(snake.life==1) {
el14rd 2:d7b17623ba26 399 // lcd.printString("lost",24,42);
el14rd 2:d7b17623ba26 400 // break;
el14rd 6:290173d4a22d 401
el14rd 6:290173d4a22d 402
el14rd 1:f682aeb462f1 403
el14rd 1:f682aeb462f1 404 }
el14rd 1:f682aeb462f1 405