
Completed Snake Program
Dependencies: N5110 PinDetect PowerControl mbed
Fork of DocTest by
main.cpp@7:2942e97924f0, 2015-04-30 (annotated)
- Committer:
- MBirney
- Date:
- Thu Apr 30 15:39:20 2015 +0000
- Revision:
- 7:2942e97924f0
- Parent:
- 6:1de103a19681
- Child:
- 8:b857684a3983
Snake working with no boundaries 2x2 snake ; (next step boundary or classic menu)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MBirney | 6:1de103a19681 | 1 | #include "main.h" |
MBirney | 3:478b090b7e1b | 2 | |
MBirney | 3:478b090b7e1b | 3 | // create enumerated type (0,1,2,3 etc. for direction) |
MBirney | 3:478b090b7e1b | 4 | enum DirectionName { |
MBirney | 3:478b090b7e1b | 5 | UP, |
MBirney | 3:478b090b7e1b | 6 | DOWN, |
MBirney | 3:478b090b7e1b | 7 | LEFT, |
MBirney | 3:478b090b7e1b | 8 | RIGHT, |
MBirney | 3:478b090b7e1b | 9 | CENTRE, |
MBirney | 3:478b090b7e1b | 10 | UNKNOWN |
MBirney | 3:478b090b7e1b | 11 | }; |
MBirney | 3:478b090b7e1b | 12 | |
MBirney | 6:1de103a19681 | 13 | // create enumerated type (0,1,2 for difficulty) |
MBirney | 6:1de103a19681 | 14 | enum Difficulty { |
MBirney | 6:1de103a19681 | 15 | EASY, |
MBirney | 6:1de103a19681 | 16 | MEDIUM, |
MBirney | 6:1de103a19681 | 17 | HARD, |
MBirney | 6:1de103a19681 | 18 | }; |
MBirney | 6:1de103a19681 | 19 | |
MBirney | 7:2942e97924f0 | 20 | Difficulty currentDifficulty=EASY;// initialise to easy mode |
MBirney | 6:1de103a19681 | 21 | |
MBirney | 6:1de103a19681 | 22 | |
MBirney | 3:478b090b7e1b | 23 | // struct for Joystick |
MBirney | 3:478b090b7e1b | 24 | typedef struct JoyStick Joystick; |
MBirney | 3:478b090b7e1b | 25 | struct JoyStick { |
MBirney | 3:478b090b7e1b | 26 | float x; // current x value |
MBirney | 3:478b090b7e1b | 27 | float x0; // 'centred' x value |
MBirney | 3:478b090b7e1b | 28 | float y; // current y value |
MBirney | 3:478b090b7e1b | 29 | float y0; // 'centred' y value |
MBirney | 3:478b090b7e1b | 30 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
MBirney | 3:478b090b7e1b | 31 | DirectionName direction; // current direction |
MBirney | 3:478b090b7e1b | 32 | }; |
MBirney | 3:478b090b7e1b | 33 | // create struct variable |
MBirney | 3:478b090b7e1b | 34 | Joystick joystick; |
MBirney | 3:478b090b7e1b | 35 | |
MBirney | 7:2942e97924f0 | 36 | DirectionName previousDirection =RIGHT;//initial direction =Right |
MBirney | 3:478b090b7e1b | 37 | |
MBirney | 3:478b090b7e1b | 38 | // read default positions of the joystick to calibrate later readings |
MBirney | 3:478b090b7e1b | 39 | void calibrateJoystick() |
MBirney | 3:478b090b7e1b | 40 | { |
MBirney | 3:478b090b7e1b | 41 | button.mode(PullDown); |
MBirney | 3:478b090b7e1b | 42 | // must not move during calibration |
MBirney | 3:478b090b7e1b | 43 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
MBirney | 3:478b090b7e1b | 44 | joystick.y0 = yPot; |
MBirney | 3:478b090b7e1b | 45 | } |
MBirney | 3:478b090b7e1b | 46 | void updateJoystick() |
MBirney | 3:478b090b7e1b | 47 | { |
MBirney | 3:478b090b7e1b | 48 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
MBirney | 3:478b090b7e1b | 49 | joystick.x = xPot - joystick.x0; |
MBirney | 3:478b090b7e1b | 50 | joystick.y = yPot - joystick.y0; |
MBirney | 3:478b090b7e1b | 51 | // read button state |
MBirney | 3:478b090b7e1b | 52 | joystick.button = button; |
MBirney | 3:478b090b7e1b | 53 | |
MBirney | 3:478b090b7e1b | 54 | // calculate direction depending on x,y values |
MBirney | 3:478b090b7e1b | 55 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
MBirney | 3:478b090b7e1b | 56 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
MBirney | 3:478b090b7e1b | 57 | joystick.direction = CENTRE; |
MBirney | 3:478b090b7e1b | 58 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
MBirney | 6:1de103a19681 | 59 | joystick.direction = DOWN; |
MBirney | 3:478b090b7e1b | 60 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
MBirney | 6:1de103a19681 | 61 | joystick.direction = UP; |
MBirney | 3:478b090b7e1b | 62 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
MBirney | 6:1de103a19681 | 63 | joystick.direction = RIGHT; // remember switched this with right |
MBirney | 3:478b090b7e1b | 64 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
MBirney | 6:1de103a19681 | 65 | joystick.direction = LEFT; |
MBirney | 3:478b090b7e1b | 66 | } else { |
MBirney | 3:478b090b7e1b | 67 | joystick.direction = UNKNOWN; |
MBirney | 3:478b090b7e1b | 68 | } |
MBirney | 3:478b090b7e1b | 69 | } |
eencae | 0:b85460bc73b9 | 70 | |
MBirney | 6:1de103a19681 | 71 | //GAME FUNCTIONS |
MBirney | 4:551dea241d0a | 72 | |
MBirney | 6:1de103a19681 | 73 | //For start MENU |
MBirney | 4:551dea241d0a | 74 | |
MBirney | 2:deb61a34ac31 | 75 | void displaySplash() |
MBirney | 2:deb61a34ac31 | 76 | { |
MBirney | 7:2942e97924f0 | 77 | |
MBirney | 7:2942e97924f0 | 78 | lcd.inverseMode(); // change colour mode |
MBirney | 2:deb61a34ac31 | 79 | lcd.setBrightness(0.5); // put LED backlight on 50% |
MBirney | 2:deb61a34ac31 | 80 | |
MBirney | 2:deb61a34ac31 | 81 | //Draw S |
MBirney | 2:deb61a34ac31 | 82 | lcd.drawRect(28,10,2,5,1); |
MBirney | 2:deb61a34ac31 | 83 | lcd.drawRect(15,10,15,2,1); |
MBirney | 2:deb61a34ac31 | 84 | lcd.drawRect(15,10,2,10,1); |
MBirney | 2:deb61a34ac31 | 85 | lcd.drawRect(15,20,15,2,1); |
MBirney | 2:deb61a34ac31 | 86 | lcd.drawRect(28,20,2,10,1); |
MBirney | 2:deb61a34ac31 | 87 | lcd.drawRect(15,28,15,2,1); |
MBirney | 2:deb61a34ac31 | 88 | lcd.drawRect(15,25,2,3,1); |
MBirney | 2:deb61a34ac31 | 89 | |
MBirney | 2:deb61a34ac31 | 90 | lcd.printString("NAKE ",34,3); |
MBirney | 2:deb61a34ac31 | 91 | lcd.printString("By M.Birney",10,5); |
MBirney | 7:2942e97924f0 | 92 | lcd.drawRect(10,5,65,30,0); // outline for splash |
MBirney | 2:deb61a34ac31 | 93 | |
MBirney | 2:deb61a34ac31 | 94 | // need to refresh display after setting pixels |
MBirney | 2:deb61a34ac31 | 95 | |
MBirney | 2:deb61a34ac31 | 96 | lcd.refresh(); |
eencae | 0:b85460bc73b9 | 97 | } |
eencae | 0:b85460bc73b9 | 98 | |
eencae | 0:b85460bc73b9 | 99 | |
MBirney | 2:deb61a34ac31 | 100 | |
MBirney | 2:deb61a34ac31 | 101 | void easySelected() // display when easy is selected |
MBirney | 2:deb61a34ac31 | 102 | { |
MBirney | 3:478b090b7e1b | 103 | currentDifficulty=EASY; |
MBirney | 2:deb61a34ac31 | 104 | lcd.clear(); |
MBirney | 2:deb61a34ac31 | 105 | lcd.printString("Please Select",2,0); |
MBirney | 2:deb61a34ac31 | 106 | lcd.printString("Difficulty:",2,1); |
MBirney | 2:deb61a34ac31 | 107 | lcd.printString("Easy",20,3); |
MBirney | 2:deb61a34ac31 | 108 | lcd.printString("Medium",20,4); |
MBirney | 2:deb61a34ac31 | 109 | lcd.printString("Hard",20,5); |
MBirney | 2:deb61a34ac31 | 110 | lcd.refresh(); |
MBirney | 2:deb61a34ac31 | 111 | lcd.drawCircle(10,27,2,1); |
MBirney | 2:deb61a34ac31 | 112 | lcd.drawCircle(10,35,2,0); |
MBirney | 2:deb61a34ac31 | 113 | lcd.drawCircle(10,43,2,0); |
MBirney | 2:deb61a34ac31 | 114 | |
MBirney | 7:2942e97924f0 | 115 | gameSpeed= 1.0/5; // set easy game speed(for game time) |
MBirney | 2:deb61a34ac31 | 116 | } |
MBirney | 7:2942e97924f0 | 117 | void mediumSelected() // display when medium is selected |
MBirney | 2:deb61a34ac31 | 118 | { |
MBirney | 3:478b090b7e1b | 119 | currentDifficulty=MEDIUM; |
MBirney | 2:deb61a34ac31 | 120 | lcd.clear(); |
MBirney | 2:deb61a34ac31 | 121 | lcd.printString("Please Select",2,0); |
MBirney | 2:deb61a34ac31 | 122 | lcd.printString("Difficulty:",2,1); |
MBirney | 2:deb61a34ac31 | 123 | lcd.printString("Easy",20,3); |
MBirney | 2:deb61a34ac31 | 124 | lcd.printString("Medium",20,4); |
MBirney | 2:deb61a34ac31 | 125 | lcd.printString("Hard",20,5); |
MBirney | 2:deb61a34ac31 | 126 | lcd.refresh(); |
MBirney | 2:deb61a34ac31 | 127 | lcd.drawCircle(10,27,2,0); |
MBirney | 2:deb61a34ac31 | 128 | lcd.drawCircle(10,35,2,1); |
MBirney | 2:deb61a34ac31 | 129 | lcd.drawCircle(10,43,2,0); |
MBirney | 7:2942e97924f0 | 130 | gameSpeed=1.0/7; // set medium game speed |
MBirney | 2:deb61a34ac31 | 131 | } |
MBirney | 2:deb61a34ac31 | 132 | |
MBirney | 2:deb61a34ac31 | 133 | void hardSelected() // display when hard is selected |
MBirney | 2:deb61a34ac31 | 134 | { |
MBirney | 3:478b090b7e1b | 135 | currentDifficulty=HARD; |
MBirney | 2:deb61a34ac31 | 136 | lcd.clear(); |
MBirney | 2:deb61a34ac31 | 137 | lcd.printString("Please Select",2,0); |
MBirney | 2:deb61a34ac31 | 138 | lcd.printString("Difficulty:",2,1); |
MBirney | 2:deb61a34ac31 | 139 | lcd.printString("Easy",20,3); |
MBirney | 2:deb61a34ac31 | 140 | lcd.printString("Medium",20,4); |
MBirney | 2:deb61a34ac31 | 141 | lcd.printString("Hard",20,5); |
MBirney | 2:deb61a34ac31 | 142 | lcd.refresh(); |
MBirney | 2:deb61a34ac31 | 143 | lcd.drawCircle(10,27,2,0); |
MBirney | 2:deb61a34ac31 | 144 | lcd.drawCircle(10,35,2,0); |
MBirney | 2:deb61a34ac31 | 145 | lcd.drawCircle(10,43,2,1); |
MBirney | 7:2942e97924f0 | 146 | gameSpeed=1.0/12; // set hard game speed |
MBirney | 4:551dea241d0a | 147 | } |
MBirney | 4:551dea241d0a | 148 | |
MBirney | 7:2942e97924f0 | 149 | |
MBirney | 6:1de103a19681 | 150 | void checkSelectedDifficulty() |
MBirney | 6:1de103a19681 | 151 | { |
MBirney | 4:551dea241d0a | 152 | |
MBirney | 4:551dea241d0a | 153 | switch(currentDifficulty) { |
MBirney | 4:551dea241d0a | 154 | case EASY: |
MBirney | 4:551dea241d0a | 155 | |
MBirney | 4:551dea241d0a | 156 | switch (joystick.direction) { |
MBirney | 4:551dea241d0a | 157 | |
MBirney | 4:551dea241d0a | 158 | case UP: |
MBirney | 4:551dea241d0a | 159 | hardSelected(); |
MBirney | 4:551dea241d0a | 160 | break; |
MBirney | 4:551dea241d0a | 161 | |
MBirney | 4:551dea241d0a | 162 | |
MBirney | 4:551dea241d0a | 163 | case DOWN: |
MBirney | 4:551dea241d0a | 164 | mediumSelected(); |
MBirney | 4:551dea241d0a | 165 | break; |
MBirney | 4:551dea241d0a | 166 | } |
MBirney | 4:551dea241d0a | 167 | break; |
MBirney | 2:deb61a34ac31 | 168 | |
MBirney | 2:deb61a34ac31 | 169 | |
MBirney | 4:551dea241d0a | 170 | case MEDIUM: |
MBirney | 4:551dea241d0a | 171 | |
MBirney | 4:551dea241d0a | 172 | switch (joystick.direction) { |
MBirney | 4:551dea241d0a | 173 | |
MBirney | 4:551dea241d0a | 174 | case UP: |
MBirney | 4:551dea241d0a | 175 | easySelected(); |
MBirney | 4:551dea241d0a | 176 | break; |
MBirney | 4:551dea241d0a | 177 | |
MBirney | 4:551dea241d0a | 178 | |
MBirney | 4:551dea241d0a | 179 | case DOWN: |
MBirney | 4:551dea241d0a | 180 | hardSelected(); |
MBirney | 4:551dea241d0a | 181 | break; |
MBirney | 4:551dea241d0a | 182 | } |
MBirney | 4:551dea241d0a | 183 | |
MBirney | 4:551dea241d0a | 184 | |
MBirney | 4:551dea241d0a | 185 | |
MBirney | 4:551dea241d0a | 186 | break; |
MBirney | 4:551dea241d0a | 187 | case HARD: |
MBirney | 4:551dea241d0a | 188 | switch (joystick.direction) { |
MBirney | 4:551dea241d0a | 189 | |
MBirney | 4:551dea241d0a | 190 | case UP: |
MBirney | 4:551dea241d0a | 191 | mediumSelected(); |
MBirney | 4:551dea241d0a | 192 | break; |
MBirney | 4:551dea241d0a | 193 | |
MBirney | 4:551dea241d0a | 194 | |
MBirney | 4:551dea241d0a | 195 | case DOWN: |
MBirney | 4:551dea241d0a | 196 | easySelected(); |
MBirney | 4:551dea241d0a | 197 | break; |
MBirney | 4:551dea241d0a | 198 | } |
MBirney | 4:551dea241d0a | 199 | break; |
MBirney | 4:551dea241d0a | 200 | } |
MBirney | 4:551dea241d0a | 201 | wait(0.2); |
MBirney | 4:551dea241d0a | 202 | } |
MBirney | 4:551dea241d0a | 203 | |
MBirney | 6:1de103a19681 | 204 | |
MBirney | 6:1de103a19681 | 205 | |
MBirney | 6:1de103a19681 | 206 | |
MBirney | 6:1de103a19681 | 207 | //for gamePLAY |
MBirney | 6:1de103a19681 | 208 | |
MBirney | 6:1de103a19681 | 209 | |
MBirney | 6:1de103a19681 | 210 | |
MBirney | 6:1de103a19681 | 211 | void startingSnake() |
MBirney | 6:1de103a19681 | 212 | { |
MBirney | 7:2942e97924f0 | 213 | snakeX.resize(5); |
MBirney | 7:2942e97924f0 | 214 | snakeY.resize(5); |
MBirney | 6:1de103a19681 | 215 | snakeX[0]=20; |
MBirney | 7:2942e97924f0 | 216 | snakeX[1]=22; |
MBirney | 7:2942e97924f0 | 217 | snakeX[2]=24; |
MBirney | 7:2942e97924f0 | 218 | snakeX[3]=26; |
MBirney | 7:2942e97924f0 | 219 | snakeX[4]=28; |
MBirney | 7:2942e97924f0 | 220 | snakeY[0]=27; |
MBirney | 7:2942e97924f0 | 221 | snakeY[1]=27; |
MBirney | 7:2942e97924f0 | 222 | snakeY[2]=27; |
MBirney | 7:2942e97924f0 | 223 | snakeY[3]=27; |
MBirney | 7:2942e97924f0 | 224 | snakeY[4]=27; |
MBirney | 7:2942e97924f0 | 225 | |
MBirney | 6:1de103a19681 | 226 | |
MBirney | 7:2942e97924f0 | 227 | for (int i=0; i<5; i++) { |
MBirney | 7:2942e97924f0 | 228 | // lcd.setPixel(snakeX[i],snakeY[i]); |
MBirney | 7:2942e97924f0 | 229 | lcd.drawRect(snakeX[i],snakeY[i],1,1,1); |
MBirney | 4:551dea241d0a | 230 | } |
MBirney | 7:2942e97924f0 | 231 | |
MBirney | 6:1de103a19681 | 232 | } |
MBirney | 6:1de103a19681 | 233 | |
MBirney | 6:1de103a19681 | 234 | |
MBirney | 6:1de103a19681 | 235 | void randomiseFood() |
MBirney | 6:1de103a19681 | 236 | { |
MBirney | 7:2942e97924f0 | 237 | // http://stackoverflow.com/questions/3383286/create-a-random-even-number-between-range |
MBirney | 7:2942e97924f0 | 238 | |
MBirney | 6:1de103a19681 | 239 | |
MBirney | 6:1de103a19681 | 240 | srand(time(NULL)); |
MBirney | 6:1de103a19681 | 241 | |
MBirney | 7:2942e97924f0 | 242 | int randomX = 2 * (1 + rand() % (40 - 1 + 1)) ; //generate random even number between 2 and 80 |
MBirney | 6:1de103a19681 | 243 | |
MBirney | 7:2942e97924f0 | 244 | int randomY = (2 * (4 + rand() % (22 - 4 + 1))) +1; // generate random even number between 8 and 44 then plus 1 for odd number in range of 9-45 |
MBirney | 6:1de103a19681 | 245 | |
MBirney | 6:1de103a19681 | 246 | |
MBirney | 7:2942e97924f0 | 247 | while(lcd.getPixel(randomX,randomY)==1) { // if that pixel is already filled keep generating till a empty space is found |
MBirney | 6:1de103a19681 | 248 | |
MBirney | 7:2942e97924f0 | 249 | int randomX = 2 * (1 + rand() % (40 - 1 + 1)) ; |
MBirney | 7:2942e97924f0 | 250 | int randomY = (2 * (4 + rand() % (22 - 4 + 1))) +1; // generate random even number between 8 and 44 then plus 1 for odd number in range of 9-45 |
MBirney | 6:1de103a19681 | 251 | |
MBirney | 6:1de103a19681 | 252 | |
MBirney | 6:1de103a19681 | 253 | } |
MBirney | 7:2942e97924f0 | 254 | // lcd.setPixel(randomX,randomY) ;// set the food |
MBirney | 7:2942e97924f0 | 255 | lcd.drawRect(randomX,randomY,1,1,1); |
MBirney | 7:2942e97924f0 | 256 | //lcd.setPixel(randomX,randomY); |
MBirney | 7:2942e97924f0 | 257 | |
MBirney | 7:2942e97924f0 | 258 | foodX[0]=randomX; // update food position |
MBirney | 7:2942e97924f0 | 259 | foodY[0]=randomY;// update food position |
MBirney | 6:1de103a19681 | 260 | lcd.refresh(); |
MBirney | 7:2942e97924f0 | 261 | //serial.printf("%d",randomX); |
MBirney | 6:1de103a19681 | 262 | } |
MBirney | 6:1de103a19681 | 263 | |
MBirney | 6:1de103a19681 | 264 | void Boundary() |
MBirney | 6:1de103a19681 | 265 | { |
MBirney | 6:1de103a19681 | 266 | |
MBirney | 6:1de103a19681 | 267 | |
MBirney | 7:2942e97924f0 | 268 | for(int x = 1; x< 83; x++) { // draw 1 to 82 at y=8 |
MBirney | 6:1de103a19681 | 269 | lcd.setPixel(x,8); |
MBirney | 6:1de103a19681 | 270 | } |
MBirney | 7:2942e97924f0 | 271 | for(int x = 1; x< 83; x++) { // draw 1 to 82 at y=47 |
MBirney | 6:1de103a19681 | 272 | lcd.setPixel(x,47); |
MBirney | 6:1de103a19681 | 273 | } |
MBirney | 7:2942e97924f0 | 274 | for(int y = 8; y< 48; y++) {// draw 8 tp 47 at x=1 |
MBirney | 7:2942e97924f0 | 275 | lcd.setPixel(1,y); |
MBirney | 6:1de103a19681 | 276 | } |
MBirney | 7:2942e97924f0 | 277 | for(int y = 8; y< 48; y++) {// draw 8 to 47 at x =82 |
MBirney | 7:2942e97924f0 | 278 | lcd.setPixel(82,y); |
MBirney | 6:1de103a19681 | 279 | } |
MBirney | 6:1de103a19681 | 280 | |
MBirney | 6:1de103a19681 | 281 | lcd.refresh(); |
MBirney | 6:1de103a19681 | 282 | |
MBirney | 6:1de103a19681 | 283 | } |
MBirney | 6:1de103a19681 | 284 | |
MBirney | 6:1de103a19681 | 285 | |
MBirney | 6:1de103a19681 | 286 | |
MBirney | 6:1de103a19681 | 287 | |
MBirney | 6:1de103a19681 | 288 | |
MBirney | 6:1de103a19681 | 289 | void updateSnakeArray() |
MBirney | 6:1de103a19681 | 290 | { |
MBirney | 6:1de103a19681 | 291 | |
MBirney | 6:1de103a19681 | 292 | if (joystick.direction==LEFT && previousDirection==RIGHT) { |
MBirney | 6:1de103a19681 | 293 | joystick.direction=RIGHT; |
MBirney | 6:1de103a19681 | 294 | } |
MBirney | 6:1de103a19681 | 295 | |
MBirney | 6:1de103a19681 | 296 | if (joystick.direction==RIGHT && previousDirection==LEFT) { |
MBirney | 6:1de103a19681 | 297 | joystick.direction=LEFT; |
MBirney | 6:1de103a19681 | 298 | } |
MBirney | 6:1de103a19681 | 299 | |
MBirney | 6:1de103a19681 | 300 | if (joystick.direction==UP && previousDirection==DOWN) { |
MBirney | 6:1de103a19681 | 301 | joystick.direction=DOWN; |
MBirney | 6:1de103a19681 | 302 | } |
MBirney | 6:1de103a19681 | 303 | |
MBirney | 6:1de103a19681 | 304 | if (joystick.direction==DOWN && previousDirection==UP) { |
MBirney | 6:1de103a19681 | 305 | joystick.direction=UP; |
MBirney | 6:1de103a19681 | 306 | } |
MBirney | 6:1de103a19681 | 307 | |
MBirney | 6:1de103a19681 | 308 | if (joystick.direction==UNKNOWN || joystick.direction==CENTRE) { |
MBirney | 6:1de103a19681 | 309 | joystick.direction= previousDirection; |
MBirney | 6:1de103a19681 | 310 | } |
MBirney | 6:1de103a19681 | 311 | |
MBirney | 6:1de103a19681 | 312 | |
MBirney | 7:2942e97924f0 | 313 | // lcd.clearPixel(snakeX[0],snakeY[0]);//delete tail |
MBirney | 7:2942e97924f0 | 314 | lcd.drawRect(snakeX[0],snakeY[0],1,1,2); |
MBirney | 6:1de103a19681 | 315 | |
MBirney | 6:1de103a19681 | 316 | for (int i =0; i<snakeX.size(); i++) { // shift elements |
MBirney | 6:1de103a19681 | 317 | snakeX[i]=snakeX[i + 1]; // apart from head |
MBirney | 6:1de103a19681 | 318 | snakeY[i]=snakeY[i+ 1]; |
MBirney | 6:1de103a19681 | 319 | } |
MBirney | 6:1de103a19681 | 320 | |
MBirney | 6:1de103a19681 | 321 | switch(joystick.direction) { |
MBirney | 6:1de103a19681 | 322 | |
MBirney | 6:1de103a19681 | 323 | case UP: |
MBirney | 6:1de103a19681 | 324 | snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]; |
MBirney | 6:1de103a19681 | 325 | snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]-2; |
MBirney | 7:2942e97924f0 | 326 | |
MBirney | 7:2942e97924f0 | 327 | if(snakeY[snakeY.size()-1] <9) snakeY[snakeY.size()-1]=45; |
MBirney | 6:1de103a19681 | 328 | break; |
MBirney | 6:1de103a19681 | 329 | |
MBirney | 6:1de103a19681 | 330 | case DOWN: |
MBirney | 7:2942e97924f0 | 331 | snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]; |
MBirney | 6:1de103a19681 | 332 | snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]+2; |
MBirney | 7:2942e97924f0 | 333 | |
MBirney | 7:2942e97924f0 | 334 | if(snakeY[snakeY.size()-1] >45) snakeY[snakeY.size()-1]=9; |
MBirney | 6:1de103a19681 | 335 | break; |
MBirney | 6:1de103a19681 | 336 | |
MBirney | 6:1de103a19681 | 337 | case LEFT: |
MBirney | 6:1de103a19681 | 338 | snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]-2; |
MBirney | 7:2942e97924f0 | 339 | |
MBirney | 7:2942e97924f0 | 340 | if(snakeX[snakeX.size()-1] <2) snakeX[snakeX.size()-1]=80; |
MBirney | 7:2942e97924f0 | 341 | |
MBirney | 6:1de103a19681 | 342 | snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]; |
MBirney | 6:1de103a19681 | 343 | break; |
MBirney | 6:1de103a19681 | 344 | |
MBirney | 6:1de103a19681 | 345 | case RIGHT: |
MBirney | 7:2942e97924f0 | 346 | snakeX[snakeX.size()-1]= snakeX[snakeX.size()-2]+2; |
MBirney | 7:2942e97924f0 | 347 | |
MBirney | 7:2942e97924f0 | 348 | if(snakeX[snakeX.size()-1] >80) snakeX[snakeX.size()-1]=2; |
MBirney | 7:2942e97924f0 | 349 | |
MBirney | 7:2942e97924f0 | 350 | |
MBirney | 6:1de103a19681 | 351 | snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]; |
MBirney | 7:2942e97924f0 | 352 | |
MBirney | 6:1de103a19681 | 353 | break; |
MBirney | 6:1de103a19681 | 354 | |
MBirney | 6:1de103a19681 | 355 | case CENTRE: |
MBirney | 6:1de103a19681 | 356 | snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]+2; |
MBirney | 6:1de103a19681 | 357 | snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]; |
MBirney | 6:1de103a19681 | 358 | break; |
MBirney | 6:1de103a19681 | 359 | case UNKNOWN: |
MBirney | 6:1de103a19681 | 360 | snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]+2; |
MBirney | 6:1de103a19681 | 361 | snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]; |
MBirney | 6:1de103a19681 | 362 | break; |
MBirney | 6:1de103a19681 | 363 | |
MBirney | 6:1de103a19681 | 364 | } |
MBirney | 6:1de103a19681 | 365 | } |
MBirney | 7:2942e97924f0 | 366 | void playTune() |
MBirney | 7:2942e97924f0 | 367 | { |
MBirney | 7:2942e97924f0 | 368 | |
MBirney | 7:2942e97924f0 | 369 | //float frequency[]={440,659}; |
MBirney | 7:2942e97924f0 | 370 | // float beat[]={1,1,}; |
MBirney | 7:2942e97924f0 | 371 | // Buzzer=0.3; |
MBirney | 7:2942e97924f0 | 372 | // Buzzer.period(1/(440));// time =1/f |
MBirney | 7:2942e97924f0 | 373 | //wait(1); |
MBirney | 7:2942e97924f0 | 374 | //Buzzer.period(1/(659)); |
MBirney | 7:2942e97924f0 | 375 | |
MBirney | 7:2942e97924f0 | 376 | //Buzzer=0; |
MBirney | 7:2942e97924f0 | 377 | |
MBirney | 7:2942e97924f0 | 378 | // set PWM period |
MBirney | 7:2942e97924f0 | 379 | |
MBirney | 7:2942e97924f0 | 380 | // set duty cycle |
MBirney | 7:2942e97924f0 | 381 | //wait(0.5*beat[i]); |
MBirney | 7:2942e97924f0 | 382 | } |
MBirney | 6:1de103a19681 | 383 | void gameOver() |
MBirney | 6:1de103a19681 | 384 | { |
MBirney | 7:2942e97924f0 | 385 | leds=1; |
MBirney | 6:1de103a19681 | 386 | startGame.detach(); |
MBirney | 7:2942e97924f0 | 387 | lcd.drawRect(snakeX.back(),snakeY.back(),1,1,2); |
MBirney | 6:1de103a19681 | 388 | wait(0.2); |
MBirney | 6:1de103a19681 | 389 | lcd.refresh(); |
MBirney | 7:2942e97924f0 | 390 | lcd.drawRect(snakeX.back(),snakeY.back(),1,1,1); |
MBirney | 6:1de103a19681 | 391 | wait(0.3); |
MBirney | 6:1de103a19681 | 392 | lcd.refresh(); |
MBirney | 7:2942e97924f0 | 393 | lcd.drawRect(snakeX.back(),snakeY.back(),1,1,2); |
MBirney | 6:1de103a19681 | 394 | wait(0.2); |
MBirney | 6:1de103a19681 | 395 | lcd.refresh(); |
MBirney | 7:2942e97924f0 | 396 | lcd.drawRect(snakeX.back(),snakeY.back(),1,1,1); |
MBirney | 6:1de103a19681 | 397 | wait(0.2); |
MBirney | 6:1de103a19681 | 398 | lcd.refresh(); |
MBirney | 6:1de103a19681 | 399 | lcd.clear(); |
MBirney | 6:1de103a19681 | 400 | lcd.inverseMode(); |
MBirney | 6:1de103a19681 | 401 | |
MBirney | 7:2942e97924f0 | 402 | |
MBirney | 6:1de103a19681 | 403 | lcd.printString("Your Score" ,12,0); |
MBirney | 7:2942e97924f0 | 404 | lcd.printString("=" ,34,1); |
MBirney | 7:2942e97924f0 | 405 | |
MBirney | 7:2942e97924f0 | 406 | int updatedScore=score; |
MBirney | 7:2942e97924f0 | 407 | int length = sprintf(buffer,"%2d",updatedScore); |
MBirney | 7:2942e97924f0 | 408 | |
MBirney | 7:2942e97924f0 | 409 | if (length <= 14) // if string will fit on display |
MBirney | 7:2942e97924f0 | 410 | lcd.printString(buffer,40,1); |
MBirney | 6:1de103a19681 | 411 | |
MBirney | 7:2942e97924f0 | 412 | lcd.printString("Press Reset" ,2,3); |
MBirney | 7:2942e97924f0 | 413 | lcd.printString("Button To" ,10,4); |
MBirney | 7:2942e97924f0 | 414 | lcd.printString("Play Again" ,20,5); |
MBirney | 5:1bfc306466db | 415 | |
MBirney | 7:2942e97924f0 | 416 | // |
MBirney | 7:2942e97924f0 | 417 | lcd.refresh(); |
MBirney | 7:2942e97924f0 | 418 | playTune(); |
MBirney | 5:1bfc306466db | 419 | |
MBirney | 7:2942e97924f0 | 420 | //gamePlaying=0; |
MBirney | 7:2942e97924f0 | 421 | |
MBirney | 7:2942e97924f0 | 422 | |
MBirney | 6:1de103a19681 | 423 | |
MBirney | 6:1de103a19681 | 424 | } |
MBirney | 6:1de103a19681 | 425 | |
MBirney | 7:2942e97924f0 | 426 | |
MBirney | 7:2942e97924f0 | 427 | |
MBirney | 6:1de103a19681 | 428 | void checkForCollision() |
MBirney | 6:1de103a19681 | 429 | { |
MBirney | 6:1de103a19681 | 430 | |
MBirney | 7:2942e97924f0 | 431 | if (snakeX.back()==0|| snakeX.back()==82 || snakeY.back()==7 ||snakeY.back()>=47) { |
MBirney | 7:2942e97924f0 | 432 | myleds=15; |
MBirney | 7:2942e97924f0 | 433 | |
MBirney | 6:1de103a19681 | 434 | gameOver(); |
MBirney | 6:1de103a19681 | 435 | } |
MBirney | 6:1de103a19681 | 436 | } |
MBirney | 6:1de103a19681 | 437 | |
MBirney | 6:1de103a19681 | 438 | |
MBirney | 6:1de103a19681 | 439 | |
MBirney | 6:1de103a19681 | 440 | void checkForFood() |
MBirney | 6:1de103a19681 | 441 | { |
MBirney | 6:1de103a19681 | 442 | |
MBirney | 7:2942e97924f0 | 443 | if (snakeX.back()==foodX[0] && snakeY.back()==foodY[0]) { // if x and y of head match food |
MBirney | 6:1de103a19681 | 444 | |
MBirney | 6:1de103a19681 | 445 | |
MBirney | 6:1de103a19681 | 446 | switch(joystick.direction) { |
MBirney | 6:1de103a19681 | 447 | |
MBirney | 6:1de103a19681 | 448 | case RIGHT: |
MBirney | 7:2942e97924f0 | 449 | snakeX.insert (snakeX.begin() +0,foodX[0]-2 ); |
MBirney | 7:2942e97924f0 | 450 | snakeY.insert (snakeY.begin() ,foodY[0]); |
MBirney | 7:2942e97924f0 | 451 | //snakeX.push_back(foodX[0]+2); |
MBirney | 7:2942e97924f0 | 452 | // snakeY.push_back(foodY[0]); |
MBirney | 6:1de103a19681 | 453 | break; |
MBirney | 6:1de103a19681 | 454 | case LEFT: |
MBirney | 6:1de103a19681 | 455 | |
MBirney | 7:2942e97924f0 | 456 | snakeX.insert (snakeX.begin() +0,foodX[0]+2 ); |
MBirney | 7:2942e97924f0 | 457 | snakeY.insert (snakeY.begin() ,foodY[0]); |
MBirney | 7:2942e97924f0 | 458 | |
MBirney | 7:2942e97924f0 | 459 | // snakeX.push_back(foodX[0]-2); |
MBirney | 7:2942e97924f0 | 460 | // snakeY.push_back(foodY[0]); |
MBirney | 6:1de103a19681 | 461 | break; |
MBirney | 6:1de103a19681 | 462 | |
MBirney | 6:1de103a19681 | 463 | |
MBirney | 6:1de103a19681 | 464 | case UP: |
MBirney | 7:2942e97924f0 | 465 | |
MBirney | 7:2942e97924f0 | 466 | snakeX.insert (snakeX.begin() +0,foodX[0] ); |
MBirney | 7:2942e97924f0 | 467 | snakeY.insert (snakeY.begin() ,foodY[0]+2); |
MBirney | 7:2942e97924f0 | 468 | // snakeX.push_back(foodX[0]); |
MBirney | 7:2942e97924f0 | 469 | // snakeY.push_back(foodY[0]-2); |
MBirney | 6:1de103a19681 | 470 | break; |
MBirney | 6:1de103a19681 | 471 | |
MBirney | 6:1de103a19681 | 472 | case DOWN: |
MBirney | 7:2942e97924f0 | 473 | snakeX.insert (snakeX.begin() +0,foodX[0] ); |
MBirney | 7:2942e97924f0 | 474 | snakeY.insert (snakeY.begin() ,foodY[0]-2); |
MBirney | 7:2942e97924f0 | 475 | // snakeX.push_back(foodX[0]); |
MBirney | 7:2942e97924f0 | 476 | // snakeY.push_back(foodY[0]+2); |
MBirney | 6:1de103a19681 | 477 | break; |
MBirney | 6:1de103a19681 | 478 | } |
MBirney | 7:2942e97924f0 | 479 | lcd.drawRect(snakeX[0],snakeY[0],1,1,1); |
MBirney | 6:1de103a19681 | 480 | |
MBirney | 6:1de103a19681 | 481 | int updatedScore; |
MBirney | 6:1de103a19681 | 482 | // snakeX.insert (snakeX.begin() + 0, ); |
MBirney | 6:1de103a19681 | 483 | //snakeY.insert (snakeY.begin() + 0, ); |
MBirney | 6:1de103a19681 | 484 | |
MBirney | 6:1de103a19681 | 485 | updatedScore= score+5; |
MBirney | 6:1de103a19681 | 486 | score=updatedScore; |
MBirney | 6:1de103a19681 | 487 | int length = sprintf(buffer,"%2d",updatedScore); |
MBirney | 6:1de103a19681 | 488 | |
MBirney | 6:1de103a19681 | 489 | if (length <= 14) // if string will fit on display |
MBirney | 6:1de103a19681 | 490 | lcd.printString(buffer,0,0); |
MBirney | 6:1de103a19681 | 491 | // lcd.refresh(); |
MBirney | 6:1de103a19681 | 492 | |
MBirney | 6:1de103a19681 | 493 | randomiseFood(); |
MBirney | 6:1de103a19681 | 494 | |
MBirney | 5:1bfc306466db | 495 | } |
MBirney | 2:deb61a34ac31 | 496 | |
MBirney | 6:1de103a19681 | 497 | |
MBirney | 6:1de103a19681 | 498 | } |
MBirney | 6:1de103a19681 | 499 | |
MBirney | 6:1de103a19681 | 500 | void startButtonPressed() |
MBirney | 6:1de103a19681 | 501 | { |
MBirney | 6:1de103a19681 | 502 | gamePlaying=1; |
MBirney | 7:2942e97924f0 | 503 | // myleds=15; |
MBirney | 6:1de103a19681 | 504 | } |
MBirney | 6:1de103a19681 | 505 | |
MBirney | 6:1de103a19681 | 506 | |
MBirney | 6:1de103a19681 | 507 | |
MBirney | 7:2942e97924f0 | 508 | void updateBrightness() |
MBirney | 7:2942e97924f0 | 509 | { |
MBirney | 7:2942e97924f0 | 510 | float ain ; |
MBirney | 7:2942e97924f0 | 511 | ain=pot.read(); |
MBirney | 7:2942e97924f0 | 512 | lcd.setBrightness(1-ain); |
MBirney | 6:1de103a19681 | 513 | |
MBirney | 7:2942e97924f0 | 514 | } |
MBirney | 6:1de103a19681 | 515 | void updateGameISR() |
MBirney | 6:1de103a19681 | 516 | { |
MBirney | 6:1de103a19681 | 517 | updateGameFlag=1; |
MBirney | 6:1de103a19681 | 518 | } |
MBirney | 6:1de103a19681 | 519 | |
MBirney | 6:1de103a19681 | 520 | void printVectorContent() |
MBirney | 6:1de103a19681 | 521 | { |
MBirney | 6:1de103a19681 | 522 | |
MBirney | 7:2942e97924f0 | 523 | //for( int i=0; i<snakeX.size(); i++) |
MBirney | 7:2942e97924f0 | 524 | // serial.printf( "%d \n \r" ,snakeX[i]); |
MBirney | 6:1de103a19681 | 525 | |
MBirney | 6:1de103a19681 | 526 | } |
MBirney | 6:1de103a19681 | 527 | |
MBirney | 6:1de103a19681 | 528 | |
MBirney | 6:1de103a19681 | 529 | |
MBirney | 6:1de103a19681 | 530 | void pauseButtonPressed() |
MBirney | 6:1de103a19681 | 531 | { |
MBirney | 6:1de103a19681 | 532 | |
MBirney | 6:1de103a19681 | 533 | if (gamePaused==0) { |
MBirney | 6:1de103a19681 | 534 | |
MBirney | 6:1de103a19681 | 535 | startGame.detach(); |
MBirney | 6:1de103a19681 | 536 | |
MBirney | 6:1de103a19681 | 537 | gamePaused=1; |
MBirney | 7:2942e97924f0 | 538 | leds=2; |
MBirney | 7:2942e97924f0 | 539 | |
MBirney | 6:1de103a19681 | 540 | } |
MBirney | 6:1de103a19681 | 541 | |
MBirney | 6:1de103a19681 | 542 | else { |
MBirney | 6:1de103a19681 | 543 | |
MBirney | 6:1de103a19681 | 544 | |
MBirney | 6:1de103a19681 | 545 | startGame.attach(&updateGameISR,gameSpeed); |
MBirney | 6:1de103a19681 | 546 | |
MBirney | 6:1de103a19681 | 547 | gamePaused=0; |
MBirney | 6:1de103a19681 | 548 | } |
MBirney | 6:1de103a19681 | 549 | |
MBirney | 6:1de103a19681 | 550 | |
MBirney | 6:1de103a19681 | 551 | |
MBirney | 6:1de103a19681 | 552 | } |
MBirney | 6:1de103a19681 | 553 | |
MBirney | 6:1de103a19681 | 554 | void checkForCollisionWithSelf(int i) |
MBirney | 6:1de103a19681 | 555 | { |
MBirney | 7:2942e97924f0 | 556 | if(snakeX.back()==snakeX[i] && snakeY.back()==snakeY[i]) { |
MBirney | 7:2942e97924f0 | 557 | gameOver(); |
MBirney | 6:1de103a19681 | 558 | } |
MBirney | 6:1de103a19681 | 559 | |
MBirney | 6:1de103a19681 | 560 | |
MBirney | 6:1de103a19681 | 561 | } |
MBirney | 6:1de103a19681 | 562 | void startUpMenu() |
MBirney | 2:deb61a34ac31 | 563 | { |
MBirney | 7:2942e97924f0 | 564 | lcd.inverseMode(); |
MBirney | 2:deb61a34ac31 | 565 | easySelected(); |
MBirney | 3:478b090b7e1b | 566 | joystick.direction=UNKNOWN; |
MBirney | 3:478b090b7e1b | 567 | calibrateJoystick(); // get centred values of joystick |
MBirney | 7:2942e97924f0 | 568 | |
MBirney | 6:1de103a19681 | 569 | } |
MBirney | 6:1de103a19681 | 570 | |
MBirney | 6:1de103a19681 | 571 | void resetButtonPressed() |
MBirney | 6:1de103a19681 | 572 | { |
MBirney | 7:2942e97924f0 | 573 | gamePlaying=0; |
MBirney | 6:1de103a19681 | 574 | } |
MBirney | 6:1de103a19681 | 575 | |
MBirney | 6:1de103a19681 | 576 | |
MBirney | 6:1de103a19681 | 577 | int main() |
MBirney | 6:1de103a19681 | 578 | { |
MBirney | 7:2942e97924f0 | 579 | leds=1; |
MBirney | 6:1de103a19681 | 580 | lcd.init(); |
MBirney | 6:1de103a19681 | 581 | resetButton.mode(PullDown); |
MBirney | 6:1de103a19681 | 582 | startButton.mode(PullDown); |
MBirney | 6:1de103a19681 | 583 | button.mode(PullDown); |
MBirney | 4:551dea241d0a | 584 | startButton.rise(&startButtonPressed); |
MBirney | 4:551dea241d0a | 585 | resetButton.rise(&resetButtonPressed); |
MBirney | 6:1de103a19681 | 586 | button.rise(&pauseButtonPressed); |
MBirney | 7:2942e97924f0 | 587 | displaySplash(); |
MBirney | 7:2942e97924f0 | 588 | wait(4); |
MBirney | 7:2942e97924f0 | 589 | //serial.printf("starting"); |
MBirney | 4:551dea241d0a | 590 | while(1) { |
MBirney | 7:2942e97924f0 | 591 | startUpMenu(); |
MBirney | 7:2942e97924f0 | 592 | while (gamePlaying==0) { |
MBirney | 7:2942e97924f0 | 593 | updateJoystick(); |
MBirney | 6:1de103a19681 | 594 | checkSelectedDifficulty(); |
MBirney | 7:2942e97924f0 | 595 | updateBrightness(); |
MBirney | 7:2942e97924f0 | 596 | //serial.printf("check difficulty loop"); |
MBirney | 6:1de103a19681 | 597 | } |
MBirney | 6:1de103a19681 | 598 | |
MBirney | 7:2942e97924f0 | 599 | if (gamePlaying==1) { |
MBirney | 7:2942e97924f0 | 600 | |
MBirney | 6:1de103a19681 | 601 | lcd.clear(); |
MBirney | 6:1de103a19681 | 602 | lcd.normalMode(); // normal colour mode |
MBirney | 6:1de103a19681 | 603 | Boundary(); |
MBirney | 7:2942e97924f0 | 604 | previousDirection=RIGHT; |
MBirney | 7:2942e97924f0 | 605 | joystick.direction=RIGHT; // make sure when game reset that joystick is reset to right |
MBirney | 6:1de103a19681 | 606 | startingSnake(); |
MBirney | 6:1de103a19681 | 607 | randomiseFood(); |
MBirney | 7:2942e97924f0 | 608 | int score=5; |
MBirney | 7:2942e97924f0 | 609 | int length = sprintf(buffer,"%2d",score); |
MBirney | 6:1de103a19681 | 610 | if (length <= 14) // if string will fit on display |
MBirney | 6:1de103a19681 | 611 | lcd.printString(buffer,0,0); |
MBirney | 6:1de103a19681 | 612 | // lcd.drawRect(0,0,83,7,0); |
MBirney | 7:2942e97924f0 | 613 | lcd.refresh(); |
MBirney | 7:2942e97924f0 | 614 | startGame.attach(&updateGameISR,gameSpeed); |
MBirney | 7:2942e97924f0 | 615 | |
MBirney | 7:2942e97924f0 | 616 | while (gamePlaying==1) { |
MBirney | 6:1de103a19681 | 617 | |
MBirney | 7:2942e97924f0 | 618 | // serial.printf("enter game loop"); |
MBirney | 6:1de103a19681 | 619 | if(updateGameFlag==1) { |
MBirney | 7:2942e97924f0 | 620 | leds=4; |
MBirney | 6:1de103a19681 | 621 | //updateJoystick(); |
MBirney | 6:1de103a19681 | 622 | updateGameFlag=0; |
MBirney | 6:1de103a19681 | 623 | updateJoystick(); |
MBirney | 6:1de103a19681 | 624 | // lcd.clearPixel(snakeX[0],snakeY[0]); |
MBirney | 6:1de103a19681 | 625 | // updateSnakeDirection(); |
MBirney | 6:1de103a19681 | 626 | updateSnakeArray(); |
MBirney | 6:1de103a19681 | 627 | for (int i=0; i<snakeX.size(); i++) { |
MBirney | 6:1de103a19681 | 628 | lcd.drawRect(snakeX[i],snakeY[i],1,1,1); |
MBirney | 7:2942e97924f0 | 629 | // lcd.setPixel(snakeX[i],snakeY[i]); |
MBirney | 7:2942e97924f0 | 630 | |
MBirney | 6:1de103a19681 | 631 | |
MBirney | 6:1de103a19681 | 632 | } |
MBirney | 6:1de103a19681 | 633 | lcd.refresh(); |
MBirney | 6:1de103a19681 | 634 | previousDirection=joystick.direction; |
MBirney | 7:2942e97924f0 | 635 | |
MBirney | 7:2942e97924f0 | 636 | //checkForCollision(); |
MBirney | 6:1de103a19681 | 637 | checkForFood(); |
MBirney | 7:2942e97924f0 | 638 | |
MBirney | 7:2942e97924f0 | 639 | |
MBirney | 6:1de103a19681 | 640 | for (int i=0; i<snakeX.size()-1; i++) { |
MBirney | 6:1de103a19681 | 641 | checkForCollisionWithSelf(i); |
MBirney | 7:2942e97924f0 | 642 | updateBrightness(); |
MBirney | 7:2942e97924f0 | 643 | //serial.printf("%d",snakeX.size()); |
MBirney | 7:2942e97924f0 | 644 | // printVectorContent(); |
MBirney | 6:1de103a19681 | 645 | |
MBirney | 7:2942e97924f0 | 646 | } |
MBirney | 6:1de103a19681 | 647 | |
MBirney | 6:1de103a19681 | 648 | } |
MBirney | 6:1de103a19681 | 649 | |
MBirney | 6:1de103a19681 | 650 | } |
MBirney | 6:1de103a19681 | 651 | } |
MBirney | 6:1de103a19681 | 652 | |
MBirney | 6:1de103a19681 | 653 | |
MBirney | 2:deb61a34ac31 | 654 | |
MBirney | 7:2942e97924f0 | 655 | } |
eencae | 0:b85460bc73b9 | 656 | } |