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