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