renhao dai / Mbed 2 deprecated Joystick_snake_game

Dependencies:   N5110 mbed

Committer:
el14rd
Date:
Tue May 05 14:34:44 2015 +0000
Revision:
7:69ae0585d11a
Parent:
6:290173d4a22d
Child:
8:b01f638d23b4
button and buzzer problem still

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 7:69ae0585d11a 57 Ticker timer;
el14rd 7:69ae0585d11a 58 int timerFlag=0;
el14rd 7:69ae0585d11a 59 int buttonFlag = 0;
eencae 0:026fa541af7a 60 int printFlag = 0;
el14rd 3:04ff8cd27dd1 61 int nutFlag =1;
el14rd 5:d8a06e7c54fb 62 int lifeFlag =0;
el14rd 7:69ae0585d11a 63 //float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440};
el14rd 7:69ae0585d11a 64 //float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2};
el14rd 2:d7b17623ba26 65 //int buttonFlag = 0;
eencae 0:026fa541af7a 66 // function prototypes
el14rd 1:f682aeb462f1 67 //void setPixel(int x, int y);
el14rd 1:f682aeb462f1 68 //void clearPixel(int x, int y);
el14rd 1:f682aeb462f1 69
eencae 0:026fa541af7a 70
el14rd 1:f682aeb462f1 71 typedef struct Nut Nut;
el14rd 1:f682aeb462f1 72 struct Nut {
el14rd 1:f682aeb462f1 73 int X;
el14rd 1:f682aeb462f1 74 int Y;
el14rd 1:f682aeb462f1 75 };
el14rd 1:f682aeb462f1 76 Nut nut;
eencae 0:026fa541af7a 77
el14rd 1:f682aeb462f1 78 typedef struct Snake Snake;
el14rd 1:f682aeb462f1 79 struct Snake {
el14rd 1:f682aeb462f1 80 int snakeX[100];
el14rd 1:f682aeb462f1 81 int snakeY[100];
el14rd 1:f682aeb462f1 82 int node;
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 7:69ae0585d11a 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 7:69ae0585d11a 155
el14rd 7:69ae0585d11a 156
el14rd 1:f682aeb462f1 157 void drawSnake()
el14rd 5:d8a06e7c54fb 158 {
el14rd 7:69ae0585d11a 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 7:69ae0585d11a 163
el14rd 5:d8a06e7c54fb 164 }
el14rd 5:d8a06e7c54fb 165
el14rd 1:f682aeb462f1 166 }
el14rd 1:f682aeb462f1 167
el14rd 1:f682aeb462f1 168 void startGmae()
el14rd 1:f682aeb462f1 169 {
el14rd 7:69ae0585d11a 170 //nutFlag = 1;
el14rd 5:d8a06e7c54fb 171 //lifeFlag = 0;
el14rd 2:d7b17623ba26 172 //snake.direction=LEFT;
el14rd 7:69ae0585d11a 173
el14rd 6:290173d4a22d 174 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 7:69ae0585d11a 175 snake.node+=10;
el14rd 7:69ae0585d11a 176 nutFlag=0;
el14rd 7:69ae0585d11a 177 //if(timerFlag){
el14rd 7:69ae0585d11a 178 //timerFlag=0;
el14rd 7:69ae0585d11a 179 // new nut appear
el14rd 3:04ff8cd27dd1 180 }
el14rd 3:04ff8cd27dd1 181
el14rd 3:04ff8cd27dd1 182 if(nutFlag==0) { //set nut randomly
el14rd 1:f682aeb462f1 183 nut.X = rand()%83;
el14rd 1:f682aeb462f1 184 nut.Y = rand()%47;
el14rd 3:04ff8cd27dd1 185 nutFlag = 1;
el14rd 1:f682aeb462f1 186 }
el14rd 2:d7b17623ba26 187
el14rd 5:d8a06e7c54fb 188
el14rd 5:d8a06e7c54fb 189 for(i=3; i<snake.node; i++) { //if snake collide itself snake die
el14rd 5:d8a06e7c54fb 190 if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) {
el14rd 5:d8a06e7c54fb 191 lifeFlag=1;
el14rd 5:d8a06e7c54fb 192 }
el14rd 5:d8a06e7c54fb 193 }
el14rd 7:69ae0585d11a 194
el14rd 7:69ae0585d11a 195
el14rd 4:41acda9d68c7 196 for(i=snake.node-1; i>0; i--) { //move snake
el14rd 4:41acda9d68c7 197 if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47){
el14rd 4:41acda9d68c7 198 snake.snakeX[i]=snake.snakeX[i-1];
el14rd 4:41acda9d68c7 199 snake.snakeY[i]=snake.snakeY[i-1];
el14rd 4:41acda9d68c7 200 }
el14rd 4:41acda9d68c7 201 else{
el14rd 4:41acda9d68c7 202 snake.snakeX[i]=snake.snakeX[i];
el14rd 4:41acda9d68c7 203 snake.snakeY[i]=snake.snakeY[i];
el14rd 4:41acda9d68c7 204 }
el14rd 7:69ae0585d11a 205 }
el14rd 4:41acda9d68c7 206
el14rd 1:f682aeb462f1 207
el14rd 1:f682aeb462f1 208 if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) {
el14rd 5:d8a06e7c54fb 209 lifeFlag=1; // snake collide the corrider snake die
el14rd 1:f682aeb462f1 210 }
el14rd 3:04ff8cd27dd1 211
el14rd 1:f682aeb462f1 212 }
el14rd 1:f682aeb462f1 213
el14rd 5:d8a06e7c54fb 214 void printScore(){
el14rd 6:290173d4a22d 215 lcd.printString("Your Score is",0,0);
el14rd 6:290173d4a22d 216 int score=snake.node-6;
el14rd 6:290173d4a22d 217 int length = sprintf(buffer," SCORE=%2d ",score);
el14rd 6:290173d4a22d 218 lcd.printString(buffer,0,2 );
el14rd 6:290173d4a22d 219 lcd.printString("Press to start",0,4);
el14rd 5:d8a06e7c54fb 220 }
el14rd 6:290173d4a22d 221
el14rd 7:69ae0585d11a 222 //void buttonAPressed(){
el14rd 7:69ae0585d11a 223 //buttonAFlag=1;
el14rd 7:69ae0585d11a 224 //}
el14rd 7:69ae0585d11a 225
el14rd 7:69ae0585d11a 226 void pauseGame(){
el14rd 7:69ae0585d11a 227 if(button==1){
el14rd 7:69ae0585d11a 228 buttonFlag = 1;
el14rd 7:69ae0585d11a 229 }
el14rd 7:69ae0585d11a 230 }
el14rd 7:69ae0585d11a 231
el14rd 7:69ae0585d11a 232 void timerExpired(){
el14rd 7:69ae0585d11a 233 timerFlag = 1;
el14rd 7:69ae0585d11a 234 }
el14rd 7:69ae0585d11a 235
el14rd 7:69ae0585d11a 236 void promptVoice(){
el14rd 6:290173d4a22d 237
el14rd 7:69ae0585d11a 238 buzzer.period(0.0020); // set PWM period
el14rd 7:69ae0585d11a 239 buzzer.pulsewidth(0.001); // set duty cycle
el14rd 7:69ae0585d11a 240 wait(0.5);
el14rd 7:69ae0585d11a 241 lcd.printString("Your Score is",0,0);
el14rd 7:69ae0585d11a 242
el14rd 7:69ae0585d11a 243 }
el14rd 7:69ae0585d11a 244
el14rd 7:69ae0585d11a 245 void snakeModel(){
el14rd 7:69ae0585d11a 246 lcd.drawRect(24,21,1,1,1);
el14rd 7:69ae0585d11a 247 lcd.drawRect(26,21,1,1,1);
el14rd 7:69ae0585d11a 248 lcd.drawRect(28,21,1,1,1);
el14rd 7:69ae0585d11a 249
el14rd 7:69ae0585d11a 250 lcd.drawRect(22,23,1,1,1);
el14rd 7:69ae0585d11a 251 lcd.drawRect(24,23,1,1,1);
el14rd 7:69ae0585d11a 252 lcd.drawRect(26,23,1,1,1);
el14rd 7:69ae0585d11a 253 lcd.drawRect(28,23,1,1,1);
el14rd 7:69ae0585d11a 254
el14rd 7:69ae0585d11a 255 lcd.drawRect(20,25,1,1,1);
el14rd 7:69ae0585d11a 256 lcd.drawRect(22,25,1,1,1);
el14rd 7:69ae0585d11a 257 lcd.drawRect(24,25,1,1,1);
el14rd 7:69ae0585d11a 258 lcd.drawRect(26,25,1,1,1);
el14rd 7:69ae0585d11a 259 lcd.drawRect(28,25,1,1,1);
el14rd 7:69ae0585d11a 260 lcd.drawRect(30,25,1,1,1);
el14rd 7:69ae0585d11a 261
el14rd 7:69ae0585d11a 262 lcd.drawRect(20,27,1,1,1);
el14rd 7:69ae0585d11a 263 lcd.drawRect(22,27,1,1,1);
el14rd 7:69ae0585d11a 264 lcd.drawRect(24,27,1,1,1);
el14rd 7:69ae0585d11a 265 lcd.drawRect(26,27,1,1,1);
el14rd 7:69ae0585d11a 266 lcd.drawRect(28,27,1,1,1);
el14rd 7:69ae0585d11a 267 lcd.drawRect(30,27,1,1,1);//head
el14rd 7:69ae0585d11a 268
el14rd 7:69ae0585d11a 269 lcd.drawRect(18,29,1,1,0); //tougue
el14rd 7:69ae0585d11a 270 lcd.drawRect(20,31,1,1,0);
el14rd 7:69ae0585d11a 271
el14rd 7:69ae0585d11a 272 lcd.drawRect(26,29,1,1,1);
el14rd 7:69ae0585d11a 273 lcd.drawRect(28,29,1,1,1);
el14rd 7:69ae0585d11a 274 lcd.drawRect(30,29,1,1,1);
el14rd 7:69ae0585d11a 275
el14rd 7:69ae0585d11a 276 lcd.drawRect(26,31,1,1,1);
el14rd 7:69ae0585d11a 277 lcd.drawRect(28,31,1,1,1);
el14rd 7:69ae0585d11a 278 lcd.drawRect(30,31,1,1,1);
el14rd 7:69ae0585d11a 279
el14rd 7:69ae0585d11a 280 lcd.drawRect(24,33,1,1,1);
el14rd 7:69ae0585d11a 281 lcd.drawRect(26,33,1,1,1);
el14rd 7:69ae0585d11a 282 lcd.drawRect(28,33,1,1,1);
el14rd 7:69ae0585d11a 283
el14rd 7:69ae0585d11a 284 lcd.drawRect(24,35,1,1,1);
el14rd 7:69ae0585d11a 285 lcd.drawRect(26,35,1,1,1);
el14rd 7:69ae0585d11a 286 lcd.drawRect(28,35,1,1,1);
el14rd 7:69ae0585d11a 287
el14rd 7:69ae0585d11a 288 lcd.drawRect(22,37,1,1,1);
el14rd 7:69ae0585d11a 289 lcd.drawRect(24,37,1,1,1);
el14rd 7:69ae0585d11a 290 lcd.drawRect(26,37,1,1,1);
el14rd 7:69ae0585d11a 291
el14rd 7:69ae0585d11a 292 lcd.drawRect(22,39,1,1,1);
el14rd 7:69ae0585d11a 293 lcd.drawRect(24,39,1,1,1);
el14rd 7:69ae0585d11a 294 lcd.drawRect(26,39,1,1,1);
el14rd 7:69ae0585d11a 295
el14rd 7:69ae0585d11a 296 lcd.drawRect(24,41,1,1,1);
el14rd 7:69ae0585d11a 297 lcd.drawRect(26,41,1,1,1);
el14rd 7:69ae0585d11a 298 lcd.drawRect(28,41,1,1,1);
el14rd 7:69ae0585d11a 299 lcd.drawRect(30,41,1,1,1);//neck
el14rd 7:69ae0585d11a 300
el14rd 7:69ae0585d11a 301 lcd.drawRect(60,41,1,1,1);//tail
el14rd 7:69ae0585d11a 302 lcd.drawRect(62,41,1,1,1);
el14rd 7:69ae0585d11a 303 lcd.drawRect(64,41,1,1,1);
el14rd 7:69ae0585d11a 304
el14rd 7:69ae0585d11a 305 lcd.drawRect(62,39,1,1,1);
el14rd 7:69ae0585d11a 306 lcd.drawRect(64,39,1,1,1);
el14rd 7:69ae0585d11a 307
el14rd 7:69ae0585d11a 308 lcd.drawRect(62,37,1,1,1);
el14rd 7:69ae0585d11a 309
el14rd 7:69ae0585d11a 310 lcd.drawRect(60,35,1,1,1);//tail
el14rd 7:69ae0585d11a 311
el14rd 7:69ae0585d11a 312 lcd.drawRect(26,43,1,1,1);
el14rd 7:69ae0585d11a 313 lcd.drawRect(28,43,1,1,1);
el14rd 7:69ae0585d11a 314 lcd.drawRect(30,43,1,1,1);
el14rd 7:69ae0585d11a 315 lcd.drawRect(32,43,1,1,1);
el14rd 7:69ae0585d11a 316 lcd.drawRect(34,43,1,1,1);
el14rd 7:69ae0585d11a 317 lcd.drawRect(36,43,1,1,1);
el14rd 7:69ae0585d11a 318 lcd.drawRect(38,43,1,1,1);
el14rd 7:69ae0585d11a 319 lcd.drawRect(40,43,1,1,1);
el14rd 7:69ae0585d11a 320 lcd.drawRect(42,43,1,1,1);
el14rd 7:69ae0585d11a 321 lcd.drawRect(44,43,1,1,1);
el14rd 7:69ae0585d11a 322 lcd.drawRect(46,43,1,1,1);
el14rd 7:69ae0585d11a 323 lcd.drawRect(48,43,1,1,1);
el14rd 7:69ae0585d11a 324 lcd.drawRect(50,43,1,1,1);
el14rd 7:69ae0585d11a 325 lcd.drawRect(52,43,1,1,1);
el14rd 7:69ae0585d11a 326 lcd.drawRect(54,43,1,1,1);
el14rd 7:69ae0585d11a 327 lcd.drawRect(56,43,1,1,1);
el14rd 7:69ae0585d11a 328 lcd.drawRect(58,43,1,1,1);
el14rd 7:69ae0585d11a 329 lcd.drawRect(60,43,1,1,1);
el14rd 7:69ae0585d11a 330 lcd.drawRect(62,43,1,1,1);
el14rd 7:69ae0585d11a 331
el14rd 7:69ae0585d11a 332 lcd.drawRect(28,45,1,1,1);
el14rd 7:69ae0585d11a 333 lcd.drawRect(30,45,1,1,1);
el14rd 7:69ae0585d11a 334 lcd.drawRect(32,45,1,1,1);
el14rd 7:69ae0585d11a 335 lcd.drawRect(34,45,1,1,1);
el14rd 7:69ae0585d11a 336 lcd.drawRect(36,45,1,1,1);
el14rd 7:69ae0585d11a 337 lcd.drawRect(38,45,1,1,1);
el14rd 7:69ae0585d11a 338 lcd.drawRect(40,45,1,1,1);
el14rd 7:69ae0585d11a 339 lcd.drawRect(42,45,1,1,1);
el14rd 7:69ae0585d11a 340 lcd.drawRect(44,45,1,1,1);
el14rd 7:69ae0585d11a 341 lcd.drawRect(46,45,1,1,1);
el14rd 7:69ae0585d11a 342 lcd.drawRect(48,45,1,1,1);
el14rd 7:69ae0585d11a 343 lcd.drawRect(50,45,1,1,1);
el14rd 7:69ae0585d11a 344 lcd.drawRect(52,45,1,1,1);
el14rd 7:69ae0585d11a 345 lcd.drawRect(54,45,1,1,1);
el14rd 7:69ae0585d11a 346 lcd.drawRect(56,45,1,1,1);
el14rd 7:69ae0585d11a 347 lcd.drawRect(58,45,1,1,1);
el14rd 7:69ae0585d11a 348 lcd.drawRect(60,45,1,1,1);
el14rd 7:69ae0585d11a 349
el14rd 7:69ae0585d11a 350 }
el14rd 1:f682aeb462f1 351 //switch(snake.direction)
el14rd 1:f682aeb462f1 352 //{
el14rd 1:f682aeb462f1 353 // case RIGHT:snake.X[0]+=1;
el14rd 1:f682aeb462f1 354 // break;    
el14rd 1:f682aeb462f1 355 // case LEFT:snake.X[0]-=1;
el14rd 1:f682aeb462f1 356 // break;     
el14rd 1:f682aeb462f1 357 // case UP:snake.Y[0]-=1;
el14rd 1:f682aeb462f1 358 // break;
el14rd 1:f682aeb462f1 359 // case DOWN:snake.Y[0]+=1;
el14rd 1:f682aeb462f1 360 // break;
el14rd 1:f682aeb462f1 361 // }
el14rd 1:f682aeb462f1 362
el14rd 1:f682aeb462f1 363 //end of while(joystick.direction = UNKNOWN)
el14rd 1:f682aeb462f1 364
el14rd 1:f682aeb462f1 365
eencae 0:026fa541af7a 366 // read default positions of the joystick to calibrate later readings
eencae 0:026fa541af7a 367 void calibrateJoystick()
eencae 0:026fa541af7a 368 {
eencae 0:026fa541af7a 369 button.mode(PullDown);
eencae 0:026fa541af7a 370 // must not move during calibration
eencae 0:026fa541af7a 371 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
eencae 0:026fa541af7a 372 joystick.y0 = yPot;
eencae 0:026fa541af7a 373 }
el14rd 1:f682aeb462f1 374
el14rd 2:d7b17623ba26 375
eencae 0:026fa541af7a 376 void updateJoystick()
eencae 0:026fa541af7a 377 {
eencae 0:026fa541af7a 378 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
eencae 0:026fa541af7a 379 joystick.x = xPot - joystick.x0;
eencae 0:026fa541af7a 380 joystick.y = yPot - joystick.y0;
eencae 0:026fa541af7a 381 // read button state
eencae 0:026fa541af7a 382 joystick.button = button;
eencae 0:026fa541af7a 383
eencae 0:026fa541af7a 384 // calculate direction depending on x,y values
eencae 0:026fa541af7a 385 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
eencae 0:026fa541af7a 386 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 387 joystick.direction = CENTRE;
el14rd 4:41acda9d68c7 388 //LED OFF
eencae 0:026fa541af7a 389 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 390 joystick.direction = UP;
el14rd 4:41acda9d68c7 391 //LIGHT LED1
el14rd 1:f682aeb462f1 392 //snake.direction = UP;
eencae 0:026fa541af7a 393 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 394 joystick.direction = DOWN;
el14rd 4:41acda9d68c7 395 //LIGHT LED2
eencae 0:026fa541af7a 396 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 397 joystick.direction = RIGHT;
el14rd 4:41acda9d68c7 398 //LIGHT LED3
eencae 0:026fa541af7a 399 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
eencae 0:026fa541af7a 400 joystick.direction = LEFT;
eencae 0:026fa541af7a 401 } else {
eencae 0:026fa541af7a 402 joystick.direction = UNKNOWN;
eencae 0:026fa541af7a 403 }
eencae 0:026fa541af7a 404 // set flag for printing
eencae 0:026fa541af7a 405 printFlag = 1;
el14rd 1:f682aeb462f1 406 }
el14rd 1:f682aeb462f1 407
el14rd 1:f682aeb462f1 408 void updateSnake()
el14rd 1:f682aeb462f1 409 {
el14rd 1:f682aeb462f1 410 // get direction of joystick and update snake
el14rd 4:41acda9d68c7 411 if (snake.direction == LEFT) {
el14rd 7:69ae0585d11a 412 snake.snakeX[0]-=1;
el14rd 4:41acda9d68c7 413 //LED1
el14rd 4:41acda9d68c7 414 if(snake.snakeX[0]<1) {
el14rd 4:41acda9d68c7 415 snake.snakeX[0]=1;
el14rd 4:41acda9d68c7 416
el14rd 1:f682aeb462f1 417 }//will stop at the top edge
el14rd 1:f682aeb462f1 418
el14rd 1:f682aeb462f1 419 }
el14rd 4:41acda9d68c7 420 if (snake.direction == RIGHT) {
el14rd 7:69ae0585d11a 421 snake.snakeX[0]+=1;
el14rd 4:41acda9d68c7 422 //LED2
el14rd 4:41acda9d68c7 423 if(snake.snakeX[0]>83) {
el14rd 4:41acda9d68c7 424 snake.snakeX[0]=83;
el14rd 1:f682aeb462f1 425 }//will stop at the bottom edge
el14rd 1:f682aeb462f1 426 }
el14rd 4:41acda9d68c7 427 if (snake.direction == UP) {
el14rd 7:69ae0585d11a 428 snake.snakeY[0]-=1;
el14rd 4:41acda9d68c7 429 if(snake.snakeY[0]>47) {
el14rd 4:41acda9d68c7 430 snake.snakeY[0]=47;
el14rd 1:f682aeb462f1 431 }//will stop at the left edge
el14rd 1:f682aeb462f1 432
el14rd 1:f682aeb462f1 433 }
el14rd 4:41acda9d68c7 434 if (snake.direction == DOWN) {
el14rd 7:69ae0585d11a 435 snake.snakeY[0]+=1;
el14rd 4:41acda9d68c7 436 if(snake.snakeY[0]<1) {
el14rd 4:41acda9d68c7 437 snake.snakeY[0]=1;
el14rd 1:f682aeb462f1 438 }//will stop at the right edge
el14rd 1:f682aeb462f1 439
el14rd 1:f682aeb462f1 440 }
el14rd 4:41acda9d68c7 441 if(snake.direction == CENTRE){
el14rd 4:41acda9d68c7 442 snake.snakeX[0]+=1;
el14rd 4:41acda9d68c7 443 }
el14rd 5:d8a06e7c54fb 444 if(snake.direction == UNKNOWN){
el14rd 5:d8a06e7c54fb 445 snake.snakeX[0]+=1;
el14rd 5:d8a06e7c54fb 446 }
el14rd 1:f682aeb462f1 447 }
el14rd 1:f682aeb462f1 448
el14rd 1:f682aeb462f1 449
el14rd 2:d7b17623ba26 450
el14rd 1:f682aeb462f1 451 //main code
el14rd 1:f682aeb462f1 452 int main()
el14rd 1:f682aeb462f1 453 {
el14rd 1:f682aeb462f1 454 lcd.init();
el14rd 1:f682aeb462f1 455 lcd.setBrightness(0.5); // put LED backlight on 50%
el14rd 1:f682aeb462f1 456 calibrateJoystick(); // get centred values of joystick
el14rd 1:f682aeb462f1 457 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
el14rd 1:f682aeb462f1 458 lcd.refresh();
el14rd 2:d7b17623ba26 459 initNut();
el14rd 2:d7b17623ba26 460 initSnake();
el14rd 7:69ae0585d11a 461 timer.attach(&timerExpired,0.05);
el14rd 5:d8a06e7c54fb 462 //buzzer.period(0.020); // a 20ms period
el14rd 6:290173d4a22d 463 //buttonA.rise(&buttonAPressed);
el14rd 5:d8a06e7c54fb 464
el14rd 4:41acda9d68c7 465 //printScore();
el14rd 1:f682aeb462f1 466
el14rd 1:f682aeb462f1 467 //infinite while loop
el14rd 1:f682aeb462f1 468 while(1) {
el14rd 5:d8a06e7c54fb 469
el14rd 7:69ae0585d11a 470 if(buttonFlag==1){
el14rd 1:f682aeb462f1 471 lcd.clear();
el14rd 7:69ae0585d11a 472 buttonFlag=0;
el14rd 5:d8a06e7c54fb 473 lifeFlag=0;
el14rd 1:f682aeb462f1 474 updateSnake();
el14rd 1:f682aeb462f1 475 drawSnake();
el14rd 2:d7b17623ba26 476 drawNut();
el14rd 1:f682aeb462f1 477 moveSnake();
el14rd 1:f682aeb462f1 478 startGmae();
el14rd 7:69ae0585d11a 479 pauseGame();
el14rd 7:69ae0585d11a 480 if (buttonFlag==1){
el14rd 7:69ae0585d11a 481 while(1){
el14rd 7:69ae0585d11a 482 buttonFlag =0;
el14rd 7:69ae0585d11a 483 if(buttonFlag ==1)
el14rd 7:69ae0585d11a 484 break;
el14rd 7:69ae0585d11a 485 }
el14rd 7:69ae0585d11a 486 }
el14rd 5:d8a06e7c54fb 487 if(lifeFlag==1){
el14rd 6:290173d4a22d 488 lcd.clear();
el14rd 6:290173d4a22d 489 lcd.refresh();
el14rd 6:290173d4a22d 490 printScore();
el14rd 7:69ae0585d11a 491 //for(int i=0; i<=11; i++){
el14rd 7:69ae0585d11a 492 // buzzer.period(1/(frequency[i])); // set PWM period
el14rd 7:69ae0585d11a 493 // buzzer=0.1; // set duty cycle
el14rd 7:69ae0585d11a 494 // wait(0.5*beat[i]);
el14rd 7:69ae0585d11a 495 //}
el14rd 6:290173d4a22d 496 lifeFlag=0;
el14rd 7:69ae0585d11a 497 wait(1);
el14rd 5:d8a06e7c54fb 498 }
el14rd 7:69ae0585d11a 499 // if (buttonFlag==1){
el14rd 7:69ae0585d11a 500 // while(1){
el14rd 7:69ae0585d11a 501 // buttonFlag =0;
el14rd 7:69ae0585d11a 502 // if(buttonFlag ==1)
el14rd 7:69ae0585d11a 503 // break;
el14rd 7:69ae0585d11a 504 // }
el14rd 7:69ae0585d11a 505 // }
el14rd 7:69ae0585d11a 506 lcd.refresh();
el14rd 7:69ae0585d11a 507 wait(0.02);
el14rd 7:69ae0585d11a 508 }
el14rd 6:290173d4a22d 509 else
el14rd 6:290173d4a22d 510 lcd.printString("Press to start",1,1);
el14rd 7:69ae0585d11a 511 snakeModel();
el14rd 5:d8a06e7c54fb 512 }
el14rd 1:f682aeb462f1 513 }
el14rd 1:f682aeb462f1 514