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@5:d8a06e7c54fb, 2015-05-02 (annotated)
- Committer:
- el14rd
- Date:
- Sat May 02 17:12:03 2015 +0000
- Revision:
- 5:d8a06e7c54fb
- Parent:
- 4:41acda9d68c7
- Child:
- 6:290173d4a22d
snake with printscore and button unfinished
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:d8a06e7c54fb | 11 | //DigitalIn button1(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 | 5:d8a06e7c54fb | 57 | int buttonFlag = 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 | /* |
el14rd | 1:f682aeb462f1 | 168 | */ |
el14rd | 1:f682aeb462f1 | 169 | |
el14rd | 1:f682aeb462f1 | 170 | void startGmae() |
el14rd | 1:f682aeb462f1 | 171 | { |
el14rd | 2:d7b17623ba26 | 172 | //lcd.drawRect(snake.snakeX[0]+10,snake.snakeY[0]+10,10,10,1); |
el14rd | 3:04ff8cd27dd1 | 173 | //nutFlag = 1; |
el14rd | 5:d8a06e7c54fb | 174 | //lifeFlag = 0; |
el14rd | 2:d7b17623ba26 | 175 | //snake.direction=LEFT; |
el14rd | 1:f682aeb462f1 | 176 | //if(joystick.direction == UNKNOWN) |
el14rd | 4:41acda9d68c7 | 177 | |
el14rd | 4:41acda9d68c7 | 178 | if((snake.snakeX[0]==nut.X)&&(snake.snakeY[0]==nut.Y)||(snake.snakeX[0]==nut.X-1)&&(snake.snakeY[0]==nut.Y-1)||(snake.snakeY[0]==nut.Y+1)&&(snake.snakeY[0]==nut.Y+1)) { // if snake got nut |
el14rd | 3:04ff8cd27dd1 | 179 | snake.node++; |
el14rd | 5:d8a06e7c54fb | 180 | |
el14rd | 5:d8a06e7c54fb | 181 | //buzzer.period(1/659); // set PWM period |
el14rd | 5:d8a06e7c54fb | 182 | //buzzer=0.1; // set duty cycle |
el14rd | 5:d8a06e7c54fb | 183 | //wait(0.1); |
el14rd | 3:04ff8cd27dd1 | 184 | nutFlag=0; // new nut appear |
el14rd | 3:04ff8cd27dd1 | 185 | } |
el14rd | 3:04ff8cd27dd1 | 186 | |
el14rd | 3:04ff8cd27dd1 | 187 | if(nutFlag==0) { //set nut randomly |
el14rd | 1:f682aeb462f1 | 188 | nut.X = rand()%83; |
el14rd | 1:f682aeb462f1 | 189 | nut.Y = rand()%47; |
el14rd | 3:04ff8cd27dd1 | 190 | nutFlag = 1; |
el14rd | 1:f682aeb462f1 | 191 | } |
el14rd | 2:d7b17623ba26 | 192 | |
el14rd | 5:d8a06e7c54fb | 193 | |
el14rd | 5:d8a06e7c54fb | 194 | for(i=3; i<snake.node; i++) { //if snake collide itself snake die |
el14rd | 5:d8a06e7c54fb | 195 | if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) { |
el14rd | 5:d8a06e7c54fb | 196 | lifeFlag=1; |
el14rd | 5:d8a06e7c54fb | 197 | // break; |
el14rd | 5:d8a06e7c54fb | 198 | } |
el14rd | 5:d8a06e7c54fb | 199 | } |
el14rd | 4:41acda9d68c7 | 200 | |
el14rd | 4:41acda9d68c7 | 201 | for(i=snake.node-1; i>0; i--) { //move snake |
el14rd | 4:41acda9d68c7 | 202 | if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47){ |
el14rd | 4:41acda9d68c7 | 203 | snake.snakeX[i]=snake.snakeX[i-1]; |
el14rd | 4:41acda9d68c7 | 204 | snake.snakeY[i]=snake.snakeY[i-1]; |
el14rd | 4:41acda9d68c7 | 205 | } |
el14rd | 4:41acda9d68c7 | 206 | else{ |
el14rd | 4:41acda9d68c7 | 207 | snake.snakeX[i]=snake.snakeX[i]; |
el14rd | 4:41acda9d68c7 | 208 | snake.snakeY[i]=snake.snakeY[i]; |
el14rd | 4:41acda9d68c7 | 209 | } |
el14rd | 4:41acda9d68c7 | 210 | } |
el14rd | 4:41acda9d68c7 | 211 | |
el14rd | 1:f682aeb462f1 | 212 | |
el14rd | 1:f682aeb462f1 | 213 | if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) { |
el14rd | 5:d8a06e7c54fb | 214 | lifeFlag=1; // snake collide the corrider snake die |
el14rd | 1:f682aeb462f1 | 215 | //break; |
el14rd | 1:f682aeb462f1 | 216 | } |
el14rd | 1:f682aeb462f1 | 217 | |
el14rd | 1:f682aeb462f1 | 218 | |
el14rd | 5:d8a06e7c54fb | 219 | //if(snake.life==1) { |
el14rd | 1:f682aeb462f1 | 220 | //break; |
el14rd | 5:d8a06e7c54fb | 221 | //} |
el14rd | 3:04ff8cd27dd1 | 222 | |
el14rd | 4:41acda9d68c7 | 223 | |
el14rd | 1:f682aeb462f1 | 224 | |
el14rd | 1:f682aeb462f1 | 225 | } |
el14rd | 1:f682aeb462f1 | 226 | |
el14rd | 5:d8a06e7c54fb | 227 | void printScore(){ |
el14rd | 5:d8a06e7c54fb | 228 | lcd.printString("YourScoreis",0,0); |
el14rd | 5:d8a06e7c54fb | 229 | int score=snake.node-5; |
el14rd | 5:d8a06e7c54fb | 230 | int length = sprintf(buffer,"\n SCORE=%2d \n",score); |
el14rd | 5:d8a06e7c54fb | 231 | } |
el14rd | 5:d8a06e7c54fb | 232 | /* |
el14rd | 5:d8a06e7c54fb | 233 | void buttonPressed(){ |
el14rd | 5:d8a06e7c54fb | 234 | buttonFlag=1; |
el14rd | 5:d8a06e7c54fb | 235 | } |
el14rd | 5:d8a06e7c54fb | 236 | */ |
el14rd | 1:f682aeb462f1 | 237 | //while(joystick.direction == UNKNOWN) { |
el14rd | 1:f682aeb462f1 | 238 | |
el14rd | 1:f682aeb462f1 | 239 | |
el14rd | 1:f682aeb462f1 | 240 | //switch(snake.direction) |
el14rd | 1:f682aeb462f1 | 241 | //{ |
el14rd | 1:f682aeb462f1 | 242 | // case RIGHT:snake.X[0]+=1; |
el14rd | 1:f682aeb462f1 | 243 | // break; |
el14rd | 1:f682aeb462f1 | 244 | // case LEFT:snake.X[0]-=1; |
el14rd | 1:f682aeb462f1 | 245 | // break; |
el14rd | 1:f682aeb462f1 | 246 | // case UP:snake.Y[0]-=1; |
el14rd | 1:f682aeb462f1 | 247 | // break; |
el14rd | 1:f682aeb462f1 | 248 | // case DOWN:snake.Y[0]+=1; |
el14rd | 1:f682aeb462f1 | 249 | // break; |
el14rd | 1:f682aeb462f1 | 250 | // } |
el14rd | 1:f682aeb462f1 | 251 | |
el14rd | 1:f682aeb462f1 | 252 | //end of while(joystick.direction = UNKNOWN) |
el14rd | 1:f682aeb462f1 | 253 | |
el14rd | 1:f682aeb462f1 | 254 | |
el14rd | 1:f682aeb462f1 | 255 | // if(joystick.direction == UP&&snake.direction!=DOWN) { |
el14rd | 1:f682aeb462f1 | 256 | // snake.direction=UP; |
el14rd | 1:f682aeb462f1 | 257 | // } else if(joystick.direction == RIGHT&&snake.direction!=LEFT) { |
el14rd | 1:f682aeb462f1 | 258 | // snake.direction= RIGHT; |
el14rd | 1:f682aeb462f1 | 259 | // } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) { |
el14rd | 1:f682aeb462f1 | 260 | // snake.direction=LEFT; |
el14rd | 1:f682aeb462f1 | 261 | // } else(joystick.direction == DOWN&&snake.direction!=UP) { |
el14rd | 1:f682aeb462f1 | 262 | // snake.direction=DOWN; |
el14rd | 1:f682aeb462f1 | 263 | // } |
el14rd | 1:f682aeb462f1 | 264 | |
el14rd | 1:f682aeb462f1 | 265 | |
el14rd | 1:f682aeb462f1 | 266 | |
eencae | 0:026fa541af7a | 267 | // read default positions of the joystick to calibrate later readings |
eencae | 0:026fa541af7a | 268 | void calibrateJoystick() |
eencae | 0:026fa541af7a | 269 | { |
eencae | 0:026fa541af7a | 270 | button.mode(PullDown); |
eencae | 0:026fa541af7a | 271 | // must not move during calibration |
eencae | 0:026fa541af7a | 272 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
eencae | 0:026fa541af7a | 273 | joystick.y0 = yPot; |
eencae | 0:026fa541af7a | 274 | } |
el14rd | 1:f682aeb462f1 | 275 | |
el14rd | 2:d7b17623ba26 | 276 | |
eencae | 0:026fa541af7a | 277 | void updateJoystick() |
eencae | 0:026fa541af7a | 278 | { |
eencae | 0:026fa541af7a | 279 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
eencae | 0:026fa541af7a | 280 | joystick.x = xPot - joystick.x0; |
eencae | 0:026fa541af7a | 281 | joystick.y = yPot - joystick.y0; |
eencae | 0:026fa541af7a | 282 | // read button state |
eencae | 0:026fa541af7a | 283 | joystick.button = button; |
eencae | 0:026fa541af7a | 284 | |
eencae | 0:026fa541af7a | 285 | // calculate direction depending on x,y values |
eencae | 0:026fa541af7a | 286 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
eencae | 0:026fa541af7a | 287 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 288 | joystick.direction = CENTRE; |
el14rd | 4:41acda9d68c7 | 289 | //LED OFF |
eencae | 0:026fa541af7a | 290 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 291 | joystick.direction = UP; |
el14rd | 4:41acda9d68c7 | 292 | //LIGHT LED1 |
el14rd | 1:f682aeb462f1 | 293 | //snake.direction = UP; |
eencae | 0:026fa541af7a | 294 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 295 | joystick.direction = DOWN; |
el14rd | 4:41acda9d68c7 | 296 | //LIGHT LED2 |
eencae | 0:026fa541af7a | 297 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 298 | joystick.direction = RIGHT; |
el14rd | 4:41acda9d68c7 | 299 | //LIGHT LED3 |
eencae | 0:026fa541af7a | 300 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 301 | joystick.direction = LEFT; |
eencae | 0:026fa541af7a | 302 | } else { |
eencae | 0:026fa541af7a | 303 | joystick.direction = UNKNOWN; |
eencae | 0:026fa541af7a | 304 | } |
eencae | 0:026fa541af7a | 305 | // set flag for printing |
eencae | 0:026fa541af7a | 306 | printFlag = 1; |
el14rd | 1:f682aeb462f1 | 307 | } |
el14rd | 1:f682aeb462f1 | 308 | |
el14rd | 1:f682aeb462f1 | 309 | void updateSnake() |
el14rd | 1:f682aeb462f1 | 310 | { |
el14rd | 1:f682aeb462f1 | 311 | // get direction of joystick and update snake |
el14rd | 4:41acda9d68c7 | 312 | if (snake.direction == LEFT) { |
el14rd | 2:d7b17623ba26 | 313 | snake.snakeX[0]--; |
el14rd | 4:41acda9d68c7 | 314 | //LED1 |
el14rd | 4:41acda9d68c7 | 315 | if(snake.snakeX[0]<1) { |
el14rd | 4:41acda9d68c7 | 316 | snake.snakeX[0]=1; |
el14rd | 4:41acda9d68c7 | 317 | |
el14rd | 1:f682aeb462f1 | 318 | }//will stop at the top edge |
el14rd | 1:f682aeb462f1 | 319 | |
el14rd | 1:f682aeb462f1 | 320 | } |
el14rd | 4:41acda9d68c7 | 321 | if (snake.direction == RIGHT) { |
el14rd | 2:d7b17623ba26 | 322 | snake.snakeX[0]++; |
el14rd | 4:41acda9d68c7 | 323 | //LED2 |
el14rd | 4:41acda9d68c7 | 324 | if(snake.snakeX[0]>83) { |
el14rd | 4:41acda9d68c7 | 325 | snake.snakeX[0]=83; |
el14rd | 1:f682aeb462f1 | 326 | }//will stop at the bottom edge |
el14rd | 1:f682aeb462f1 | 327 | } |
el14rd | 4:41acda9d68c7 | 328 | if (snake.direction == UP) { |
el14rd | 5:d8a06e7c54fb | 329 | snake.snakeY[0]--; |
el14rd | 4:41acda9d68c7 | 330 | if(snake.snakeY[0]>47) { |
el14rd | 4:41acda9d68c7 | 331 | snake.snakeY[0]=47; |
el14rd | 1:f682aeb462f1 | 332 | }//will stop at the left edge |
el14rd | 1:f682aeb462f1 | 333 | |
el14rd | 1:f682aeb462f1 | 334 | } |
el14rd | 4:41acda9d68c7 | 335 | if (snake.direction == DOWN) { |
el14rd | 5:d8a06e7c54fb | 336 | snake.snakeY[0]++; |
el14rd | 4:41acda9d68c7 | 337 | if(snake.snakeY[0]<1) { |
el14rd | 4:41acda9d68c7 | 338 | snake.snakeY[0]=1; |
el14rd | 1:f682aeb462f1 | 339 | }//will stop at the right edge |
el14rd | 1:f682aeb462f1 | 340 | |
el14rd | 1:f682aeb462f1 | 341 | } |
el14rd | 4:41acda9d68c7 | 342 | if(snake.direction == CENTRE){ |
el14rd | 4:41acda9d68c7 | 343 | snake.snakeX[0]+=1; |
el14rd | 4:41acda9d68c7 | 344 | } |
el14rd | 5:d8a06e7c54fb | 345 | if(snake.direction == UNKNOWN){ |
el14rd | 5:d8a06e7c54fb | 346 | snake.snakeX[0]+=1; |
el14rd | 5:d8a06e7c54fb | 347 | } |
el14rd | 1:f682aeb462f1 | 348 | } |
el14rd | 1:f682aeb462f1 | 349 | |
el14rd | 1:f682aeb462f1 | 350 | |
el14rd | 2:d7b17623ba26 | 351 | |
el14rd | 1:f682aeb462f1 | 352 | //main code |
el14rd | 1:f682aeb462f1 | 353 | int main() |
el14rd | 1:f682aeb462f1 | 354 | { |
el14rd | 1:f682aeb462f1 | 355 | lcd.init(); |
el14rd | 1:f682aeb462f1 | 356 | lcd.setBrightness(0.5); // put LED backlight on 50% |
el14rd | 5:d8a06e7c54fb | 357 | //button.rise(&buttonPressed); |
el14rd | 1:f682aeb462f1 | 358 | calibrateJoystick(); // get centred values of joystick |
el14rd | 1:f682aeb462f1 | 359 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el14rd | 1:f682aeb462f1 | 360 | lcd.refresh(); |
el14rd | 2:d7b17623ba26 | 361 | initNut(); |
el14rd | 2:d7b17623ba26 | 362 | initSnake(); |
el14rd | 5:d8a06e7c54fb | 363 | //buzzer.period(0.020); // a 20ms period |
el14rd | 5:d8a06e7c54fb | 364 | lcd.printString("press to start",0,0); |
el14rd | 5:d8a06e7c54fb | 365 | |
el14rd | 5:d8a06e7c54fb | 366 | |
el14rd | 4:41acda9d68c7 | 367 | //printScore(); |
el14rd | 1:f682aeb462f1 | 368 | |
el14rd | 1:f682aeb462f1 | 369 | //infinite while loop |
el14rd | 1:f682aeb462f1 | 370 | while(1) { |
el14rd | 5:d8a06e7c54fb | 371 | |
el14rd | 5:d8a06e7c54fb | 372 | //if(button){ |
el14rd | 1:f682aeb462f1 | 373 | lcd.clear(); |
el14rd | 5:d8a06e7c54fb | 374 | lifeFlag=0; |
el14rd | 1:f682aeb462f1 | 375 | updateSnake(); |
el14rd | 1:f682aeb462f1 | 376 | drawSnake(); |
el14rd | 2:d7b17623ba26 | 377 | drawNut(); |
el14rd | 1:f682aeb462f1 | 378 | moveSnake(); |
el14rd | 1:f682aeb462f1 | 379 | startGmae(); |
el14rd | 5:d8a06e7c54fb | 380 | if(lifeFlag==1){ |
el14rd | 5:d8a06e7c54fb | 381 | lcd.clear(); |
el14rd | 5:d8a06e7c54fb | 382 | lcd.refresh(); |
el14rd | 5:d8a06e7c54fb | 383 | //lcd.printString("press to start",0,0); |
el14rd | 5:d8a06e7c54fb | 384 | printScore(); |
el14rd | 5:d8a06e7c54fb | 385 | lifeFlag=0; |
el14rd | 5:d8a06e7c54fb | 386 | } |
el14rd | 5:d8a06e7c54fb | 387 | lcd.refresh(); |
el14rd | 5:d8a06e7c54fb | 388 | wait(0.1); |
el14rd | 5:d8a06e7c54fb | 389 | } |
el14rd | 5:d8a06e7c54fb | 390 | //else |
el14rd | 5:d8a06e7c54fb | 391 | //lcd.printString("press to start",20,20); |
el14rd | 5:d8a06e7c54fb | 392 | // } |
el14rd | 2:d7b17623ba26 | 393 | //wait(0.5); |
el14rd | 2:d7b17623ba26 | 394 | //if(snake.life==1) { |
el14rd | 2:d7b17623ba26 | 395 | // lcd.printString("lost",24,42); |
el14rd | 2:d7b17623ba26 | 396 | // break; |
el14rd | 2:d7b17623ba26 | 397 | |
el14rd | 1:f682aeb462f1 | 398 | |
el14rd | 1:f682aeb462f1 | 399 | } |
el14rd | 1:f682aeb462f1 | 400 |