Kyle Sturgess / Mbed 2 deprecated GameProject

Dependencies:   N5110 mbed

Fork of DocTest by Craig Evans

Committer:
200784123
Date:
Sat May 09 09:38:26 2015 +0000
Revision:
2:5446154a4028
Parent:
1:6fc14cd8ccf1
Child:
3:b78d65300f24
Final version or project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 0:b85460bc73b9 1 /**
eencae 0:b85460bc73b9 2 @file main.cpp
eencae 0:b85460bc73b9 3
eencae 0:b85460bc73b9 4 @brief Program implementation
eencae 0:b85460bc73b9 5
eencae 0:b85460bc73b9 6 */
eencae 0:b85460bc73b9 7 #include "main.h"
eencae 0:b85460bc73b9 8
200784123 2:5446154a4028 9 int main(){
200784123 2:5446154a4028 10 ///minimal amount of code in the main, see All() for more
200784123 2:5446154a4028 11 All();
200784123 2:5446154a4028 12 }
200784123 2:5446154a4028 13
200784123 2:5446154a4028 14 void All(){
200784123 2:5446154a4028 15 calibrateJoystick(); ///get the centred values of the joystick
200784123 2:5446154a4028 16 bright=5;
eencae 0:b85460bc73b9 17 while(1) {
200784123 2:5446154a4028 18 if(screen)
200784123 2:5446154a4028 19 lcd.inverseMode();
200784123 2:5446154a4028 20 if(!screen)
200784123 2:5446154a4028 21 lcd.normalMode();
200784123 2:5446154a4028 22 lcd.refresh();
200784123 2:5446154a4028 23 Initialise();
200784123 2:5446154a4028 24 start();
200784123 2:5446154a4028 25 }
200784123 2:5446154a4028 26 }
200784123 2:5446154a4028 27
200784123 2:5446154a4028 28 ///all of the variable, flags, tickers, and anything else that needs to set each time it's powered up
200784123 2:5446154a4028 29 void Initialise(){
200784123 2:5446154a4028 30 lcd.init();
200784123 2:5446154a4028 31 Power.attach(&powerDown,1.0);///increases the power counter every second
200784123 2:5446154a4028 32 backlight.attach(&brightness,1.0/10.0);///updates the brightness 10 times a second
200784123 2:5446154a4028 33 pollJoystick.attach(&updateJoystick,1.0/10.0); /// read joystick 10 times per second
200784123 2:5446154a4028 34 i=1;
200784123 2:5446154a4028 35 j=24;
200784123 2:5446154a4028 36 life=3;
200784123 2:5446154a4028 37 winFlag=0;
200784123 2:5446154a4028 38 startFlag=1;
200784123 2:5446154a4028 39 buzzer.period(0.002);
200784123 2:5446154a4028 40 c=1;
200784123 2:5446154a4028 41 }
200784123 2:5446154a4028 42
200784123 2:5446154a4028 43
200784123 2:5446154a4028 44 void powerDown(){///goes into deep power down if nothing happens for a minute
200784123 2:5446154a4028 45 power++;
200784123 2:5446154a4028 46 if(power==55){
200784123 2:5446154a4028 47 red1=0;
200784123 2:5446154a4028 48 red2=0.1*bright;
200784123 2:5446154a4028 49 red3=0;
200784123 2:5446154a4028 50 yel=0.1*bright;
200784123 2:5446154a4028 51 buzzer=0.4;
200784123 2:5446154a4028 52 }
200784123 2:5446154a4028 53 if(power==56){
200784123 2:5446154a4028 54 red1=0.1*bright;
200784123 2:5446154a4028 55 red2=0;
200784123 2:5446154a4028 56 red3=0.1*bright;
200784123 2:5446154a4028 57 yel=0;
200784123 2:5446154a4028 58 buzzer=0.9;
200784123 2:5446154a4028 59 }
200784123 2:5446154a4028 60 if(power==57){
200784123 2:5446154a4028 61 red1=0;
200784123 2:5446154a4028 62 red2=0.1*bright;
200784123 2:5446154a4028 63 red3=0;
200784123 2:5446154a4028 64 yel=0.1*bright;
200784123 2:5446154a4028 65 buzzer=0.4;
200784123 2:5446154a4028 66 }
200784123 2:5446154a4028 67 if(power==58){
200784123 2:5446154a4028 68 red1=0.1*bright;
200784123 2:5446154a4028 69 red2=0;
200784123 2:5446154a4028 70 red3=0.1*bright;
200784123 2:5446154a4028 71 yel=0;
200784123 2:5446154a4028 72 buzzer=0.9;
200784123 2:5446154a4028 73 }
200784123 2:5446154a4028 74 if(power==59){
200784123 2:5446154a4028 75 red1=0;
200784123 2:5446154a4028 76 red2=0.1*bright;
200784123 2:5446154a4028 77 red3=0;
200784123 2:5446154a4028 78 yel=0.1*bright;
200784123 2:5446154a4028 79 buzzer=0.4;
200784123 2:5446154a4028 80 }
200784123 2:5446154a4028 81 if(power>60){
200784123 2:5446154a4028 82 red1=0;
200784123 2:5446154a4028 83 red2=0.1*bright;
200784123 2:5446154a4028 84 red3=0;
200784123 2:5446154a4028 85 yel=0.1*bright;
200784123 2:5446154a4028 86 buzzer=0.9;
200784123 2:5446154a4028 87 DeepPowerDown();
eencae 0:b85460bc73b9 88 }
eencae 0:b85460bc73b9 89 }
eencae 0:b85460bc73b9 90
eencae 0:b85460bc73b9 91
200784123 2:5446154a4028 92 ///the start screen, shows up at the begining and when the player runs out of lives
200784123 2:5446154a4028 93 ///links to the options menu, the highscores, the level select, and the game
200784123 2:5446154a4028 94 void start(){
200784123 2:5446154a4028 95 power=0;
200784123 2:5446154a4028 96 clear();
200784123 2:5446154a4028 97 lcd.refresh();
200784123 2:5446154a4028 98 butt.rise(&menus);
200784123 2:5446154a4028 99 push.rise(&menus);
200784123 2:5446154a4028 100 startFlag=1;
200784123 2:5446154a4028 101 while(c) {
200784123 2:5446154a4028 102 lcd.clearPixel(i,j);
200784123 2:5446154a4028 103 lcd.printString("Click",25,2);
200784123 2:5446154a4028 104 lcd.refresh();
200784123 2:5446154a4028 105 }
200784123 2:5446154a4028 106 clear();
200784123 2:5446154a4028 107 while(startFlag) {
200784123 2:5446154a4028 108 lcd.clearPixel(i,j);
200784123 2:5446154a4028 109 while(joystick.direction!=CENTRE) {
200784123 2:5446154a4028 110 lcd.clearPixel(i,j);
200784123 2:5446154a4028 111 ///do nothing, stay on current screen
200784123 2:5446154a4028 112 lcd.printString("^ Options ^",10,0);
200784123 2:5446154a4028 113 lcd.printString("< Score Inv. >",0,2);
200784123 2:5446154a4028 114 lcd.printString("v Levels v",12,5);
200784123 2:5446154a4028 115 lcd.refresh();
200784123 2:5446154a4028 116 }
200784123 2:5446154a4028 117 if(joystick.direction==CENTRE) {
200784123 2:5446154a4028 118 lcd.clearPixel(i,j);
200784123 2:5446154a4028 119 lcd.printString("^ Options ^",10,0);
200784123 2:5446154a4028 120 lcd.printString("< Score Inv. >",0,2);
200784123 2:5446154a4028 121 lcd.printString("v Levels v",12,5);
200784123 2:5446154a4028 122 lcd.refresh();
200784123 2:5446154a4028 123 ///for some reason if UP is used to change the flag then it goes straight to the maze
200784123 2:5446154a4028 124 }
200784123 2:5446154a4028 125 if(joystick.direction==UP) {
200784123 2:5446154a4028 126 power=0;
200784123 2:5446154a4028 127 lcd.clearPixel(i,j);
200784123 2:5446154a4028 128 toggle();
200784123 2:5446154a4028 129 Options();
200784123 2:5446154a4028 130 }
200784123 2:5446154a4028 131 if (joystick.direction==DOWN) {
200784123 2:5446154a4028 132 power=0;
200784123 2:5446154a4028 133 lcd.clearPixel(i,j);
200784123 2:5446154a4028 134 toggle();
200784123 2:5446154a4028 135 Levels();
200784123 2:5446154a4028 136 }
200784123 2:5446154a4028 137 if(joystick.direction==LEFT) {
200784123 2:5446154a4028 138 power=0;
200784123 2:5446154a4028 139 lcd.clearPixel(i,j);
200784123 2:5446154a4028 140 toggle();
200784123 2:5446154a4028 141 Score();
200784123 2:5446154a4028 142 }
200784123 2:5446154a4028 143 if(joystick.direction==RIGHT) {
200784123 2:5446154a4028 144 power=0;
200784123 2:5446154a4028 145 lcd.clearPixel(i,j);
200784123 2:5446154a4028 146 ///go to the inverse function
200784123 2:5446154a4028 147 screen=!screen;
200784123 2:5446154a4028 148 if(screen)
200784123 2:5446154a4028 149 lcd.inverseMode();
200784123 2:5446154a4028 150 if(!screen)
200784123 2:5446154a4028 151 lcd.normalMode();
200784123 2:5446154a4028 152 lcd.refresh();
200784123 2:5446154a4028 153 }
200784123 2:5446154a4028 154 }
200784123 2:5446154a4028 155 lcd.clearPixel(i,j);
200784123 2:5446154a4028 156 clear();
200784123 2:5446154a4028 157 }
200784123 2:5446154a4028 158 void menus(){
200784123 2:5446154a4028 159 power=0;
200784123 2:5446154a4028 160 lcd.clearPixel(i,j);
200784123 2:5446154a4028 161 clear();
200784123 2:5446154a4028 162 timeout.attach_us(&toggle,10);
200784123 2:5446154a4028 163 }
200784123 2:5446154a4028 164 void toggle(){
200784123 2:5446154a4028 165 power=0;
200784123 2:5446154a4028 166 l=!l;
200784123 2:5446154a4028 167 r=!r;
200784123 2:5446154a4028 168 u=!u;
200784123 2:5446154a4028 169 d=!d;
200784123 2:5446154a4028 170 c=0;
200784123 2:5446154a4028 171 b=!b;
200784123 2:5446154a4028 172 startFlag=1;
200784123 2:5446154a4028 173 }
200784123 2:5446154a4028 174 void Options(){
200784123 2:5446154a4028 175 power=0;
200784123 2:5446154a4028 176 lcd.clearPixel(i,j);
200784123 2:5446154a4028 177 clear();
200784123 2:5446154a4028 178 while(u) {
200784123 2:5446154a4028 179 lcd.clearPixel(i,j);
200784123 2:5446154a4028 180 while(joystick.direction!=CENTRE) {
200784123 2:5446154a4028 181 ///do nothing, just sit on current screen
200784123 2:5446154a4028 182 lcd.clearPixel(i,j);
200784123 2:5446154a4028 183 lcd.printString("~~~~~~~~~~~~~~",0,0);
200784123 2:5446154a4028 184 lcd.printString("^ Brightness ^",0,1);
200784123 2:5446154a4028 185 lcd.printString("< Reset",10,2);
200784123 2:5446154a4028 186 lcd.printString("End >",30,3);
200784123 2:5446154a4028 187 lcd.printString("v Back v",15,4);
200784123 2:5446154a4028 188 lcd.printString("~~~~~~~~~~~~~~",0,5);
200784123 2:5446154a4028 189 lcd.refresh();
200784123 2:5446154a4028 190 }
200784123 2:5446154a4028 191 lcd.clearPixel(i,j);
200784123 2:5446154a4028 192 lcd.printString("~~~~~~~~~~~~~~",0,0);
200784123 2:5446154a4028 193 lcd.printString("^ Brightness ^",0,1);
200784123 2:5446154a4028 194 lcd.printString("< Reset",10,2);
200784123 2:5446154a4028 195 lcd.printString("End >",30,3);
200784123 2:5446154a4028 196 lcd.printString("v Back v",15,4);
200784123 2:5446154a4028 197 lcd.printString("~~~~~~~~~~~~~~",0,5);
200784123 2:5446154a4028 198 lcd.refresh();
200784123 2:5446154a4028 199 if(joystick.direction==DOWN) {
200784123 2:5446154a4028 200 power=0;
200784123 2:5446154a4028 201 lcd.clearPixel(i,j);
200784123 2:5446154a4028 202 ///escape back to the first menu
200784123 2:5446154a4028 203 start();
200784123 2:5446154a4028 204 }
200784123 2:5446154a4028 205 if (joystick.direction==UP) {
200784123 2:5446154a4028 206 power=0;
200784123 2:5446154a4028 207 lcd.clearPixel(i,j);
200784123 2:5446154a4028 208 ///go to the brightness function
200784123 2:5446154a4028 209 clear();
200784123 2:5446154a4028 210 while(b) {
200784123 2:5446154a4028 211 lcd.clearPixel(i,j);
200784123 2:5446154a4028 212 red1=0.1*bright;
200784123 2:5446154a4028 213 red2=0.1*bright;
200784123 2:5446154a4028 214 red3=0.1*bright;
200784123 2:5446154a4028 215 yel=0.1*bright;
200784123 2:5446154a4028 216 push.rise(&Dark);
200784123 2:5446154a4028 217 butt.rise(&Light);
200784123 2:5446154a4028 218 lcd.printString("click joy - up",0,1);
200784123 2:5446154a4028 219 lcd.printString("or pink - down",0,3);
200784123 2:5446154a4028 220 lcd.printString("v Back v",15,5);
200784123 2:5446154a4028 221 lcd.refresh();
200784123 2:5446154a4028 222 if(joystick.direction==DOWN) {
200784123 2:5446154a4028 223 Options();
200784123 2:5446154a4028 224 }
200784123 2:5446154a4028 225 }
200784123 2:5446154a4028 226 }
200784123 2:5446154a4028 227 if (joystick.direction==LEFT) {
200784123 2:5446154a4028 228 power=0;
200784123 2:5446154a4028 229 lcd.clearPixel(i,j);
200784123 2:5446154a4028 230 toggle();
200784123 2:5446154a4028 231 score1=0;
200784123 2:5446154a4028 232 scoreWrite1();
200784123 2:5446154a4028 233 score2=0;
200784123 2:5446154a4028 234 scoreWrite2();
200784123 2:5446154a4028 235 score3=0;
200784123 2:5446154a4028 236 scoreWrite3();
200784123 2:5446154a4028 237 }
200784123 2:5446154a4028 238 if(joystick.direction==RIGHT) {
200784123 2:5446154a4028 239 power=0;
200784123 2:5446154a4028 240 lcd.clearPixel(i,j);
200784123 2:5446154a4028 241 winFlag=1;
200784123 2:5446154a4028 242 endGame();
200784123 2:5446154a4028 243 }
200784123 2:5446154a4028 244 }
200784123 2:5446154a4028 245 lcd.clearPixel(i,j);
200784123 2:5446154a4028 246 clear();
200784123 2:5446154a4028 247 }
200784123 2:5446154a4028 248 void Light(){
200784123 2:5446154a4028 249 timeout.attach_us(&light,100);
200784123 2:5446154a4028 250 }
200784123 2:5446154a4028 251 void Dark(){
200784123 2:5446154a4028 252 timeout.attach_us(&dark,100);
200784123 2:5446154a4028 253 }
200784123 2:5446154a4028 254 void light(){
200784123 2:5446154a4028 255 bright++;
200784123 2:5446154a4028 256 if(bright>10)
200784123 2:5446154a4028 257 bright=10;
200784123 2:5446154a4028 258 }
200784123 2:5446154a4028 259 void dark(){
200784123 2:5446154a4028 260 bright--;
200784123 2:5446154a4028 261 if(bright<1)
200784123 2:5446154a4028 262 bright=1;
200784123 2:5446154a4028 263 }
200784123 2:5446154a4028 264 void brightness(){
200784123 2:5446154a4028 265 lcd.setBrightness(0.1*bright);
200784123 2:5446154a4028 266 }
200784123 2:5446154a4028 267 void Levels(){
200784123 2:5446154a4028 268 power=0;
200784123 2:5446154a4028 269 push.rise(&menus);
200784123 2:5446154a4028 270 butt.rise(&menus);
200784123 2:5446154a4028 271 clear();
200784123 2:5446154a4028 272 while(d) {
200784123 2:5446154a4028 273 lcd.clearPixel(i,j);
200784123 2:5446154a4028 274 while(joystick.direction!=CENTRE) {
200784123 2:5446154a4028 275 ///do nothing, stay on current screen
200784123 2:5446154a4028 276 lcd.clearPixel(i,j);
200784123 2:5446154a4028 277 lcd.printString("~~~~~~~~~~~~~~",0,0);
200784123 2:5446154a4028 278 lcd.printString("Left = 1",10,2);
200784123 2:5446154a4028 279 lcd.printString("Down = 2",10,3);
200784123 2:5446154a4028 280 lcd.printString("Right = 3",10,4);
200784123 2:5446154a4028 281 lcd.printString("^ Back ^",15,1);
200784123 2:5446154a4028 282 lcd.printString("~~~~~~~~~~~~~~",0,5);
200784123 2:5446154a4028 283 lcd.refresh();
200784123 2:5446154a4028 284 }
200784123 2:5446154a4028 285 lcd.clearPixel(i,j);
200784123 2:5446154a4028 286 lcd.printString("~~~~~~~~~~~~~~",0,0);
200784123 2:5446154a4028 287 lcd.printString("Left = 1",10,2);
200784123 2:5446154a4028 288 lcd.printString("Down = 2",10,3);
200784123 2:5446154a4028 289 lcd.printString("Right = 3",10,4);
200784123 2:5446154a4028 290 lcd.printString("^ Back ^",15,1);
200784123 2:5446154a4028 291 lcd.printString("~~~~~~~~~~~~~~",0,5);
200784123 2:5446154a4028 292 lcd.refresh();
200784123 2:5446154a4028 293 if(joystick.direction==UP) {
200784123 2:5446154a4028 294 power=0;
200784123 2:5446154a4028 295 lcd.clearPixel(i,j);
200784123 2:5446154a4028 296 ///escape back to the first menu
200784123 2:5446154a4028 297 toggle();
200784123 2:5446154a4028 298 start();
200784123 2:5446154a4028 299 }
200784123 2:5446154a4028 300 if(joystick.direction==LEFT) {
200784123 2:5446154a4028 301 power=0;
200784123 2:5446154a4028 302 lcd.clearPixel(i,j);
200784123 2:5446154a4028 303 ///go to the preview screen for level 1
200784123 2:5446154a4028 304 clear();
200784123 2:5446154a4028 305 while(joystick.direction==LEFT) {
200784123 2:5446154a4028 306 lcd.clearPixel(i,j);
200784123 2:5446154a4028 307 lcd.printString("Level 1",15,2);
200784123 2:5446154a4028 308 lcd.refresh();
200784123 2:5446154a4028 309 mazeFlag=1;
200784123 2:5446154a4028 310 }
200784123 2:5446154a4028 311 clear();
200784123 2:5446154a4028 312 mainGame();
200784123 2:5446154a4028 313 }
200784123 2:5446154a4028 314 if(joystick.direction==DOWN) {
200784123 2:5446154a4028 315 power=0;
200784123 2:5446154a4028 316 lcd.clearPixel(i,j);
200784123 2:5446154a4028 317 ///go to the preview screen for level 2
200784123 2:5446154a4028 318 clear();
200784123 2:5446154a4028 319 while(joystick.direction==DOWN) {
200784123 2:5446154a4028 320 lcd.clearPixel(i,j);
200784123 2:5446154a4028 321 lcd.printString("Level 2",15,2);
200784123 2:5446154a4028 322 lcd.refresh();
200784123 2:5446154a4028 323 mazeFlag=2;
200784123 2:5446154a4028 324 }
200784123 2:5446154a4028 325 clear();
200784123 2:5446154a4028 326 mainGame();
200784123 2:5446154a4028 327 }
200784123 2:5446154a4028 328 if(joystick.direction==RIGHT) {
200784123 2:5446154a4028 329 power=0;
200784123 2:5446154a4028 330 lcd.clearPixel(i,j);
200784123 2:5446154a4028 331 ///go to the preview screen for level 3
200784123 2:5446154a4028 332 clear();
200784123 2:5446154a4028 333 while(joystick.direction==RIGHT) {
200784123 2:5446154a4028 334 power=0;
200784123 2:5446154a4028 335 lcd.clearPixel(i,j);
200784123 2:5446154a4028 336 lcd.printString("Level 3",15,2);
200784123 2:5446154a4028 337 lcd.refresh();
200784123 2:5446154a4028 338 mazeFlag=3;
200784123 2:5446154a4028 339 }
200784123 2:5446154a4028 340 clear();
200784123 2:5446154a4028 341 mainGame();
200784123 2:5446154a4028 342 }
200784123 2:5446154a4028 343
200784123 2:5446154a4028 344 }
200784123 2:5446154a4028 345 lcd.clearPixel(i,j);
200784123 2:5446154a4028 346 clear();
200784123 2:5446154a4028 347 }
200784123 2:5446154a4028 348 void Score(){
200784123 2:5446154a4028 349 power=0;
200784123 2:5446154a4028 350 lcd.clearPixel(i,j);
200784123 2:5446154a4028 351 push.rise(&menus);
200784123 2:5446154a4028 352 butt.rise(&menus);
200784123 2:5446154a4028 353 clear();
200784123 2:5446154a4028 354 while(l) {
200784123 2:5446154a4028 355 lcd.clearPixel(i,j);
200784123 2:5446154a4028 356 while(joystick.direction!=CENTRE) {
200784123 2:5446154a4028 357 ///do nothing, stay on current screen
200784123 2:5446154a4028 358 lcd.clearPixel(i,j);
200784123 2:5446154a4028 359 lcd.printString("~~~~~~~~~~~~~~",0,0);
200784123 2:5446154a4028 360 lcd.printString("High Score",10,1);
200784123 2:5446154a4028 361 lcd.printString("> Back >",15,4);
200784123 2:5446154a4028 362 lcd.printString("~~~~~~~~~~~~~~",0,5);
200784123 2:5446154a4028 363 lcd.refresh();
200784123 2:5446154a4028 364 }
200784123 2:5446154a4028 365 lcd.clearPixel(i,j);
200784123 2:5446154a4028 366 lcd.printString("~~~~~~~~~~~~~~",0,0);
200784123 2:5446154a4028 367 lcd.printString("High Score",10,1);
200784123 2:5446154a4028 368 lcd.printString("> Back >",15,4);
200784123 2:5446154a4028 369 lcd.printString("~~~~~~~~~~~~~~",0,5);
200784123 2:5446154a4028 370 lcd.refresh();
200784123 2:5446154a4028 371 if(joystick.direction==RIGHT) {
200784123 2:5446154a4028 372 power=0;
200784123 2:5446154a4028 373 ///escape back to the first menu
200784123 2:5446154a4028 374 start();
200784123 2:5446154a4028 375 }
200784123 2:5446154a4028 376 if(joystick.direction==UP) {
200784123 2:5446154a4028 377 power=0;
200784123 2:5446154a4028 378 lcd.clearPixel(i,j);
200784123 2:5446154a4028 379 ///show the high score/fastest time for level 1
200784123 2:5446154a4028 380 clear();
200784123 2:5446154a4028 381 while(joystick.direction!=DOWN){
200784123 2:5446154a4028 382 lcd.clearPixel(i,j);
200784123 2:5446154a4028 383 lcd.printString("Level 1 best",7,1);
200784123 2:5446154a4028 384 scoreRead1();
200784123 2:5446154a4028 385 lcd.refresh();
200784123 2:5446154a4028 386 }
200784123 2:5446154a4028 387 clear();
200784123 2:5446154a4028 388 }else if(joystick.direction==LEFT) {
200784123 2:5446154a4028 389 power=0;
200784123 2:5446154a4028 390 lcd.clearPixel(i,j);
200784123 2:5446154a4028 391 ///show the high score/fastest time for level 2
200784123 2:5446154a4028 392 clear();
200784123 2:5446154a4028 393 while(joystick.direction!=RIGHT){
200784123 2:5446154a4028 394 lcd.clearPixel(i,j);
200784123 2:5446154a4028 395 lcd.printString("Level 2 best",7,1);
200784123 2:5446154a4028 396 scoreRead2();
200784123 2:5446154a4028 397 lcd.refresh();
200784123 2:5446154a4028 398 }
200784123 2:5446154a4028 399 clear();
200784123 2:5446154a4028 400 }else if(joystick.direction==DOWN) {
200784123 2:5446154a4028 401 power=0;
200784123 2:5446154a4028 402 lcd.clearPixel(i,j);
200784123 2:5446154a4028 403 ///show the high score/fastest time for level 3
200784123 2:5446154a4028 404 clear();
200784123 2:5446154a4028 405 while(joystick.direction!=UP){
200784123 2:5446154a4028 406 lcd.clearPixel(i,j);
200784123 2:5446154a4028 407 lcd.printString("Level 3 best",7,1);
200784123 2:5446154a4028 408 scoreRead3();
200784123 2:5446154a4028 409 lcd.refresh();
200784123 2:5446154a4028 410 }
200784123 2:5446154a4028 411 clear();
200784123 2:5446154a4028 412 }
200784123 2:5446154a4028 413 }
200784123 2:5446154a4028 414 lcd.clearPixel(i,j);
200784123 2:5446154a4028 415 clear();
200784123 2:5446154a4028 416 }
200784123 2:5446154a4028 417
200784123 2:5446154a4028 418
200784123 2:5446154a4028 419 ///writes the score for the first level to file
200784123 2:5446154a4028 420 void scoreWrite1(){
200784123 2:5446154a4028 421 FILE *fp = fopen("/local/score1.txt","w");///opens the file score1.txt
200784123 2:5446154a4028 422 ///if the file doesn't exist it creates it, if it exists it updates
200784123 2:5446154a4028 423 fprintf(fp,"%i",score1);///prints the integer value for the score to file
200784123 2:5446154a4028 424 fclose(fp);
200784123 2:5446154a4028 425 }
200784123 2:5446154a4028 426 void scoreRead1(){
200784123 2:5446154a4028 427 int Score1;///tells it that the value to be read is an integer value
200784123 2:5446154a4028 428
200784123 2:5446154a4028 429 FILE *fp1 = fopen("/local/score1.txt","r");///opens score1.txt
200784123 2:5446154a4028 430 fscanf(fp1,"%i",&Score1);///searches the file for an integer followed by a string
200784123 2:5446154a4028 431 fclose(fp1);
200784123 2:5446154a4028 432
200784123 2:5446154a4028 433 char Points1[14];///bufer to store the points value
200784123 2:5446154a4028 434 sprintf(Points1,"%i",Score1);///sends the score to the buffer
200784123 2:5446154a4028 435 lcd.printString(Points1,30,3);///prints the score to the LCD
200784123 2:5446154a4028 436 }
200784123 2:5446154a4028 437
200784123 2:5446154a4028 438 ///writes the score for the second level to file
200784123 2:5446154a4028 439 void scoreWrite2(){
200784123 2:5446154a4028 440 FILE *fp2 = fopen("/local/score2.txt","w");///opens the file score2.txt
200784123 2:5446154a4028 441 ///if the file doesn't exist it creates it, if it exists it updates
200784123 2:5446154a4028 442 fprintf(fp2,"%i",score2);///prints the integer value for the score to file
200784123 2:5446154a4028 443 fclose(fp2);
200784123 2:5446154a4028 444 }
200784123 2:5446154a4028 445 void scoreRead2(){
200784123 2:5446154a4028 446 int Score2;///tells it that the value to be read is an integer value
200784123 2:5446154a4028 447
200784123 2:5446154a4028 448 FILE *fp3 = fopen("/local/score2.txt","r");///opens score2.txt
200784123 2:5446154a4028 449 fscanf(fp3,"%i",&Score2);///searches the file for an integer followed by a string
200784123 2:5446154a4028 450 fclose(fp3);
200784123 2:5446154a4028 451
200784123 2:5446154a4028 452 char Points2[14];///bufer to store the points value
200784123 2:5446154a4028 453 sprintf(Points2,"%i",Score2);///sends the score to the buffer
200784123 2:5446154a4028 454 lcd.printString(Points2,30,3);///prints the score to the LCD
200784123 2:5446154a4028 455 }
200784123 2:5446154a4028 456
200784123 2:5446154a4028 457 ///writes the score for the third level to file
200784123 2:5446154a4028 458 void scoreWrite3(){
200784123 2:5446154a4028 459 FILE *fp4 = fopen("/local/score3.txt","w");///opens the file score3.txt
200784123 2:5446154a4028 460 ///if the file doesn't exist it creates it, if it exists it updates
200784123 2:5446154a4028 461 fprintf(fp4,"%i",score3);///prints the integer value for the score to file
200784123 2:5446154a4028 462 fclose(fp4);
200784123 2:5446154a4028 463 }
200784123 2:5446154a4028 464 void scoreRead3(){
200784123 2:5446154a4028 465 int Score3;///tells it that the value to be read is an integer value
200784123 2:5446154a4028 466
200784123 2:5446154a4028 467 FILE *fp5 = fopen("/local/score3.txt","r");///opens score3.txt
200784123 2:5446154a4028 468 fscanf(fp5,"%i",&Score3);///searches the file for an integer followed by a string
200784123 2:5446154a4028 469 fclose(fp5);
eencae 0:b85460bc73b9 470
200784123 2:5446154a4028 471 char Points3[14];///bufer to store the points value
200784123 2:5446154a4028 472 sprintf(Points3,"%i",Score3);///sends the score to the buffer
200784123 2:5446154a4028 473 lcd.printString(Points3,30,3);///prints the score to the LCD
200784123 2:5446154a4028 474 }
200784123 2:5446154a4028 475
200784123 2:5446154a4028 476 void pause(){
200784123 2:5446154a4028 477 power=0;
200784123 2:5446154a4028 478 lcd.clearPixel(i,j);
200784123 2:5446154a4028 479 i=1;
200784123 2:5446154a4028 480 j=24;
200784123 2:5446154a4028 481 winFlag=0;
200784123 2:5446154a4028 482 life=0;
200784123 2:5446154a4028 483 }
200784123 2:5446154a4028 484 ///all of the functions that make up the main game
200784123 2:5446154a4028 485 void mainGame(){
200784123 2:5446154a4028 486 count.attach(&counter,1.0);///counts every second for the lives function etc.
200784123 2:5446154a4028 487 flash.attach(&Flash,0.25);
200784123 2:5446154a4028 488 push.rise(&pause);
200784123 2:5446154a4028 489 butt.rise(&pause);
200784123 2:5446154a4028 490 life=3;
200784123 2:5446154a4028 491 startFlag=0;
200784123 2:5446154a4028 492 winFlag=0;
200784123 2:5446154a4028 493 clear();
200784123 2:5446154a4028 494 maze1();
200784123 2:5446154a4028 495 i=1;
200784123 2:5446154a4028 496 j=24;
200784123 2:5446154a4028 497 while(life&&!winFlag) {
200784123 2:5446154a4028 498 power=0;
200784123 2:5446154a4028 499 border();
200784123 2:5446154a4028 500 if (printFlag) {
200784123 2:5446154a4028 501 printFlag=0;
200784123 2:5446154a4028 502 move();
200784123 2:5446154a4028 503 }
200784123 2:5446154a4028 504 collide1();
200784123 2:5446154a4028 505 winning();
200784123 2:5446154a4028 506 lives();
200784123 2:5446154a4028 507 if(flashFlag)
200784123 2:5446154a4028 508 lcd.clearPixel(i,j);
200784123 2:5446154a4028 509 if(!flashFlag||joystick.direction!=CENTRE)
200784123 2:5446154a4028 510 lcd.setPixel(i,j);
200784123 2:5446154a4028 511 lcd.refresh();
200784123 2:5446154a4028 512 }
200784123 2:5446154a4028 513 if(mazeFlag==1){
200784123 2:5446154a4028 514 score1=mazeFlag*life*(100-countFlag);///change this equation to change how the score is set
200784123 2:5446154a4028 515 if(screen)
200784123 2:5446154a4028 516 score1=score1+50;///change this equation to change how the score is set
200784123 2:5446154a4028 517
200784123 2:5446154a4028 518 int Score1;///tells it that the value to be read is an integer value
200784123 2:5446154a4028 519
200784123 2:5446154a4028 520 FILE *fp1 = fopen("/local/score1.txt","r");///opens score1.txt
200784123 2:5446154a4028 521 fscanf(fp1,"%i",&Score1);///searches the file for an integer followed by a string
200784123 2:5446154a4028 522 fclose(fp1);
200784123 2:5446154a4028 523
200784123 2:5446154a4028 524 if(score1>Score1)
200784123 2:5446154a4028 525 scoreWrite1();
200784123 2:5446154a4028 526 }
200784123 2:5446154a4028 527 if(mazeFlag==2){
200784123 2:5446154a4028 528 score2=mazeFlag*life*(100-countFlag);///change this equation to change how the score is set
200784123 2:5446154a4028 529 if(screen)
200784123 2:5446154a4028 530 score2=score2+50;///change this equation to change how the score is set
200784123 2:5446154a4028 531
200784123 2:5446154a4028 532 int Score2;///tells it that the value to be read is an integer value
200784123 2:5446154a4028 533
200784123 2:5446154a4028 534 FILE *fp3 = fopen("/local/score2.txt","r");///opens score2.txt
200784123 2:5446154a4028 535 fscanf(fp3,"%i",&Score2);///searches the file for an integer
200784123 2:5446154a4028 536 fclose(fp3);
200784123 2:5446154a4028 537
200784123 2:5446154a4028 538 if(score2>Score2)
200784123 2:5446154a4028 539 scoreWrite2();
200784123 2:5446154a4028 540 }
200784123 2:5446154a4028 541 if(mazeFlag==3){
200784123 2:5446154a4028 542 score3=mazeFlag*life*(100-countFlag);///change this equation to change how the score is set
200784123 2:5446154a4028 543 if(screen)
200784123 2:5446154a4028 544 score3=score3+50;///change this equation to change how the score is set
200784123 2:5446154a4028 545
200784123 2:5446154a4028 546 int Score3;///tells it that the value to be read is an integer value
200784123 2:5446154a4028 547
200784123 2:5446154a4028 548 FILE *fp5 = fopen("/local/score3.txt","r");///opens score3.txt
200784123 2:5446154a4028 549 fscanf(fp5,"%i",&Score3);///searches the file for an integer followed by a string
200784123 2:5446154a4028 550 fclose(fp5);
200784123 2:5446154a4028 551
200784123 2:5446154a4028 552 if(score3>Score3)
200784123 2:5446154a4028 553 scoreWrite3();
200784123 2:5446154a4028 554 }
200784123 2:5446154a4028 555 clear();
200784123 2:5446154a4028 556 }
200784123 2:5446154a4028 557
200784123 2:5446154a4028 558 ///mini game
200784123 2:5446154a4028 559 void endGame(){
200784123 2:5446154a4028 560 flash.attach(&Flash,0.25);
200784123 2:5446154a4028 561 red1=0;
200784123 2:5446154a4028 562 red2=0;
200784123 2:5446154a4028 563 red3=0;
200784123 2:5446154a4028 564 yel=0;
200784123 2:5446154a4028 565 buzzer=0;
200784123 2:5446154a4028 566 push.rise(&menus);
200784123 2:5446154a4028 567 butt.rise(&clear);
200784123 2:5446154a4028 568 lcd.clear();
200784123 2:5446154a4028 569 i=42;
200784123 2:5446154a4028 570 j=24;
200784123 2:5446154a4028 571 while(b) {
200784123 2:5446154a4028 572 power=0;
200784123 2:5446154a4028 573 endBorder();
200784123 2:5446154a4028 574 lcd.setPixel(i,j);
200784123 2:5446154a4028 575 if(flashFlag)
200784123 2:5446154a4028 576 lcd.clearPixel(i,j);
200784123 2:5446154a4028 577 if(!flashFlag||joystick.direction!=CENTRE)
200784123 2:5446154a4028 578 lcd.setPixel(i,j);
200784123 2:5446154a4028 579 if(printFlag) {
200784123 2:5446154a4028 580 printFlag=0;
200784123 2:5446154a4028 581 endMove();
200784123 2:5446154a4028 582 lcd.refresh();
200784123 2:5446154a4028 583 }
200784123 2:5446154a4028 584 }
200784123 2:5446154a4028 585 lcd.clearPixel(i,j);
200784123 2:5446154a4028 586 clear();
200784123 2:5446154a4028 587 lcd.refresh();
200784123 2:5446154a4028 588 i=1;
200784123 2:5446154a4028 589 j=24;
200784123 2:5446154a4028 590 }
200784123 2:5446154a4028 591
200784123 2:5446154a4028 592 ///function called to clear the screen in the mini game if the external button is pressed
200784123 2:5446154a4028 593 ///and anywhere else its called
200784123 2:5446154a4028 594 void clear(){
200784123 2:5446154a4028 595 lcd.clearPixel(i,j);
200784123 2:5446154a4028 596 lcd.clear();
200784123 2:5446154a4028 597 lcd.refresh();
200784123 2:5446154a4028 598 }
200784123 2:5446154a4028 599
200784123 2:5446154a4028 600 ///function linked to the flash ticker
200784123 2:5446154a4028 601 ///toggles the flag between 0 and 1 and sets the pixel to flash
200784123 2:5446154a4028 602 void Flash(){
200784123 2:5446154a4028 603 flashFlag=!flashFlag;
200784123 2:5446154a4028 604 lcd.setPixel(i,j);
200784123 2:5446154a4028 605 }
200784123 2:5446154a4028 606
200784123 2:5446154a4028 607 ///function called to check if the goal area has been reached in the time
200784123 2:5446154a4028 608 ///if not then the player is sent back to the begining of the level
200784123 2:5446154a4028 609 ///also indicates when time is running out on the buzzer and yellow LED
200784123 2:5446154a4028 610 ///added a safe zone at the start of the maze so the counter doesn't start straight away
200784123 2:5446154a4028 611 ///being in the safe zone stops the counter and flashes the yellow LED
200784123 2:5446154a4028 612 void counter(){
200784123 2:5446154a4028 613 buzzer=0;
200784123 2:5446154a4028 614 if((i==0||i==1||i==2||i==3)){
200784123 2:5446154a4028 615 yel=!yel;
200784123 2:5446154a4028 616 countFlag=0;
200784123 2:5446154a4028 617 }
200784123 2:5446154a4028 618 if(!(i==0||i==1||i==2||i==3))
200784123 2:5446154a4028 619 yel=0;
200784123 2:5446154a4028 620 if(startFlag){
200784123 2:5446154a4028 621 red1=0;
200784123 2:5446154a4028 622 red2=0;
200784123 2:5446154a4028 623 red3=0;
200784123 2:5446154a4028 624 yel=0;
200784123 2:5446154a4028 625 }
200784123 2:5446154a4028 626 /////the possition of the safe zone
200784123 2:5446154a4028 627 if(!startFlag&&!(i==0||i==1||i==2||i==3))
200784123 2:5446154a4028 628 countFlag++;
200784123 2:5446154a4028 629 /////if the current pixel is in the safe zone
200784123 2:5446154a4028 630 if(mazeFlag==3){
200784123 2:5446154a4028 631 if(!winFlag&&(countFlag==37||countFlag==38||countFlag==39)) {///change these numbers to change the warning time before the time limit
200784123 2:5446154a4028 632 yel=0.1*bright;
200784123 2:5446154a4028 633 buzzer=0.9;
200784123 2:5446154a4028 634 }
200784123 2:5446154a4028 635 if(countFlag==40) {///change this number to change the time limi for the level
200784123 2:5446154a4028 636 countFlag=0;
200784123 2:5446154a4028 637 yel=0;
200784123 2:5446154a4028 638 buzzer=0;
200784123 2:5446154a4028 639 life--;
200784123 2:5446154a4028 640 lcd.clearPixel(i,j);
200784123 2:5446154a4028 641 if(!winFlag) {
200784123 2:5446154a4028 642 i=1;
200784123 2:5446154a4028 643 j=24;
200784123 2:5446154a4028 644 }
200784123 2:5446154a4028 645 }
200784123 2:5446154a4028 646 }
200784123 2:5446154a4028 647 if(mazeFlag==2){
200784123 2:5446154a4028 648 if(!winFlag&&(countFlag==42||countFlag==43||countFlag==44)) {///change these numbers to change the warning time before the time limit
200784123 2:5446154a4028 649 yel=0.1*bright;
200784123 2:5446154a4028 650 buzzer=0.9;
200784123 2:5446154a4028 651 }
200784123 2:5446154a4028 652 if(countFlag==45) {///change this number to change the time limi for the level
200784123 2:5446154a4028 653 countFlag=0;
200784123 2:5446154a4028 654 yel=0;
200784123 2:5446154a4028 655 buzzer=0;
200784123 2:5446154a4028 656 life--;
200784123 2:5446154a4028 657 lcd.clearPixel(i,j);
200784123 2:5446154a4028 658 if(!winFlag) {
200784123 2:5446154a4028 659 i=1;
200784123 2:5446154a4028 660 j=24;
200784123 2:5446154a4028 661 }
200784123 2:5446154a4028 662 }
200784123 2:5446154a4028 663 }
200784123 2:5446154a4028 664 if(mazeFlag==1){
200784123 2:5446154a4028 665 if(!winFlag&&(countFlag==27||countFlag==28||countFlag==29)) {///change these numbers to change the warning time before the time limit
200784123 2:5446154a4028 666 yel=0.1*bright;
200784123 2:5446154a4028 667 buzzer=0.9;
200784123 2:5446154a4028 668 }
200784123 2:5446154a4028 669 if(countFlag==30) {///change this number to change the time limi for the level
200784123 2:5446154a4028 670 countFlag=0;
200784123 2:5446154a4028 671 yel=0;
200784123 2:5446154a4028 672 buzzer=0;
200784123 2:5446154a4028 673 life--;
200784123 2:5446154a4028 674 lcd.clearPixel(i,j);
200784123 2:5446154a4028 675 if(!winFlag) {
200784123 2:5446154a4028 676 i=1;
200784123 2:5446154a4028 677 j=24;
200784123 2:5446154a4028 678 }
200784123 2:5446154a4028 679 }
200784123 2:5446154a4028 680 }
200784123 2:5446154a4028 681 }
200784123 2:5446154a4028 682
200784123 2:5446154a4028 683 ///displays the number of remaining level attempts, or lives, on the red LEDS
200784123 2:5446154a4028 684 void lives(){
200784123 2:5446154a4028 685 if(life==3) {
200784123 2:5446154a4028 686 red1=0.1*bright;
200784123 2:5446154a4028 687 red2=0.1*bright;
200784123 2:5446154a4028 688 red3=0.1*bright;
200784123 2:5446154a4028 689 } else if(life==2) {
200784123 2:5446154a4028 690 red1=0.0;
200784123 2:5446154a4028 691 red2=0.1*bright;
200784123 2:5446154a4028 692 red3=0.1*bright;
200784123 2:5446154a4028 693 } else if(life==1) {
200784123 2:5446154a4028 694 red1=0.0;
200784123 2:5446154a4028 695 red2=0.0;
200784123 2:5446154a4028 696 red3=0.1*bright;
200784123 2:5446154a4028 697 } else {
200784123 2:5446154a4028 698 red1=0.0;
200784123 2:5446154a4028 699 red2=0.0;
200784123 2:5446154a4028 700 red3=0.0;
200784123 2:5446154a4028 701 start();
200784123 2:5446154a4028 702 }
200784123 2:5446154a4028 703 }
200784123 2:5446154a4028 704
200784123 2:5446154a4028 705 ///function that checks if the goal area has been reached
200784123 2:5446154a4028 706 void winning(){
200784123 2:5446154a4028 707 if(j==0){
200784123 2:5446154a4028 708 lcd.clearPixel(i,j);
200784123 2:5446154a4028 709 i=1;
200784123 2:5446154a4028 710 j=24;
200784123 2:5446154a4028 711 winFlag=1;
200784123 2:5446154a4028 712 }
200784123 2:5446154a4028 713 }
200784123 2:5446154a4028 714
200784123 2:5446154a4028 715 ///displays the maze by opening a picture and setting the pixels accordingly
200784123 2:5446154a4028 716 void maze1(){
200784123 2:5446154a4028 717 for (int x = 0; x <= ny-1; x++) {
200784123 2:5446154a4028 718 if(mazeFlag==1){
200784123 2:5446154a4028 719 img = fopen("/local/TEST.BMP", "r");///TEST.BMP, 3x3 walls, 5x5 corridoors, if the corners are solid then the player dies when they go into one beacause the number of neighbouring pixels is the same as a collision
200784123 2:5446154a4028 720 }
200784123 2:5446154a4028 721 if(mazeFlag==2){
200784123 2:5446154a4028 722 img = fopen("/local/MAZE1.BMP", "r");///MAZE1.BMP, 3x3 walls, 4x4 corridoors, if the corners are solid then the player dies when they go into one beacause the number of neighbouring pixels is the same as a collision
200784123 2:5446154a4028 723 }
200784123 2:5446154a4028 724 if(mazeFlag==3){
200784123 2:5446154a4028 725 img = fopen("/local/MAZE2.BMP", "r");////MAZE2.BMP, 3x3 walls, 3x3 corridorrs, if the corners are solid then the player dies when they go into one beacause the number of neighbouring pixels is the same as a collision
200784123 2:5446154a4028 726 }
200784123 2:5446154a4028 727 fseek (img , 54 + (nx*x*3) , SEEK_SET);
200784123 2:5446154a4028 728 fread(imgbuffer, (nx*3), 1, img);
200784123 2:5446154a4028 729 fclose(img);
200784123 2:5446154a4028 730 for (int y = 0; y <= nx-1; y++) {
200784123 2:5446154a4028 731 int red = imgbuffer[y*3];
200784123 2:5446154a4028 732 if (red == 0) {
200784123 2:5446154a4028 733 lcd.setPixel(y,(ny-1)-x);
200784123 2:5446154a4028 734 }
200784123 2:5446154a4028 735 }
200784123 2:5446154a4028 736 }
200784123 2:5446154a4028 737 lcd.refresh();
200784123 2:5446154a4028 738 }
200784123 2:5446154a4028 739
200784123 2:5446154a4028 740
200784123 2:5446154a4028 741 ///hardwall boundary so the player can't go off the screen
200784123 2:5446154a4028 742 void border(){
200784123 2:5446154a4028 743 if (i>=nx)
200784123 2:5446154a4028 744 i=nx-1;
200784123 2:5446154a4028 745 if (i<=0)
200784123 2:5446154a4028 746 i=0;
200784123 2:5446154a4028 747 if (j>=ny)
200784123 2:5446154a4028 748 j=ny-1;
200784123 2:5446154a4028 749 if (j<=0)
200784123 2:5446154a4028 750 j=0;
200784123 2:5446154a4028 751 }
200784123 2:5446154a4028 752
200784123 2:5446154a4028 753 ///portal boundary, so the player can go from one side to another or top to bottom, used in the mini game only
200784123 2:5446154a4028 754 void endBorder(){
200784123 2:5446154a4028 755 if (i>=nx)
200784123 2:5446154a4028 756 i=0;
200784123 2:5446154a4028 757 if (i<0)
200784123 2:5446154a4028 758 i=nx-1;
200784123 2:5446154a4028 759 if (j>=ny)
200784123 2:5446154a4028 760 j=0;
200784123 2:5446154a4028 761 if (j<0)
200784123 2:5446154a4028 762 j=ny-1;
200784123 2:5446154a4028 763 }
200784123 2:5446154a4028 764
200784123 2:5446154a4028 765 void collide1(){
200784123 2:5446154a4028 766 int block=0;
200784123 2:5446154a4028 767 if(joystick.direction==UP){
200784123 2:5446154a4028 768 if(lcd.getPixel(i,j)&&lcd.getPixel(i,j-1)){
200784123 2:5446154a4028 769 block++;
200784123 2:5446154a4028 770 }
200784123 2:5446154a4028 771 if(lcd.getPixel(i+1,j)||lcd.getPixel(i-1,j)){
200784123 2:5446154a4028 772 block++;
200784123 2:5446154a4028 773 }
200784123 2:5446154a4028 774 }
200784123 2:5446154a4028 775 if(joystick.direction==DOWN){
200784123 2:5446154a4028 776 if(lcd.getPixel(i,j)&&lcd.getPixel(i,j+1)){
200784123 2:5446154a4028 777 block++;
200784123 2:5446154a4028 778 }
200784123 2:5446154a4028 779 if(lcd.getPixel(i+1,j)||lcd.getPixel(i-1,j)){
200784123 2:5446154a4028 780 block++;
200784123 2:5446154a4028 781 }
200784123 2:5446154a4028 782 }
200784123 2:5446154a4028 783 if(joystick.direction==LEFT){
200784123 2:5446154a4028 784 if(lcd.getPixel(i,j)&&lcd.getPixel(i-1,j)){
200784123 2:5446154a4028 785 block++;
200784123 2:5446154a4028 786 }
200784123 2:5446154a4028 787 if(lcd.getPixel(i,j+1)||lcd.getPixel(i,j-1)){
200784123 2:5446154a4028 788 block++;
200784123 2:5446154a4028 789 }
200784123 2:5446154a4028 790 }
200784123 2:5446154a4028 791 if(joystick.direction==RIGHT){
200784123 2:5446154a4028 792 if(lcd.getPixel(i,j)&&lcd.getPixel(i+1,j)){
200784123 2:5446154a4028 793 block++;
200784123 2:5446154a4028 794 }
200784123 2:5446154a4028 795 if(lcd.getPixel(i,j+1)||lcd.getPixel(i,j-1)){
200784123 2:5446154a4028 796 block++;
200784123 2:5446154a4028 797 }
200784123 2:5446154a4028 798 }
200784123 2:5446154a4028 799 if(block==2){
200784123 2:5446154a4028 800 if(!(i==0||i==1||i==2||i==3)){///outside the safe zone
200784123 2:5446154a4028 801 lcd.clearPixel(i,j);
200784123 2:5446154a4028 802 clear();
200784123 2:5446154a4028 803 maze1();
200784123 2:5446154a4028 804 life--;
200784123 2:5446154a4028 805 i=1;
200784123 2:5446154a4028 806 j=24;
200784123 2:5446154a4028 807 countFlag=0;
200784123 2:5446154a4028 808 }else{
200784123 2:5446154a4028 809 lcd.clearPixel(i,j);
200784123 2:5446154a4028 810 clear();
200784123 2:5446154a4028 811 maze1();
200784123 2:5446154a4028 812 i=1;
200784123 2:5446154a4028 813 j=24;
200784123 2:5446154a4028 814 countFlag=0;
200784123 2:5446154a4028 815 }
200784123 2:5446154a4028 816 }
200784123 2:5446154a4028 817 }
200784123 2:5446154a4028 818
200784123 2:5446154a4028 819 ///checks the joystick direction and alters the possition of the pixel accordingly while clearing off where the pixel just was
200784123 2:5446154a4028 820 ///used in the main game only
200784123 2:5446154a4028 821 void move(){
200784123 2:5446154a4028 822 if (joystick.direction == DOWN) {
200784123 2:5446154a4028 823 j++;
200784123 2:5446154a4028 824 lcd.clearPixel(i,j-1);
200784123 2:5446154a4028 825 }
200784123 2:5446154a4028 826 if (joystick.direction == UP) {
200784123 2:5446154a4028 827 j--;
200784123 2:5446154a4028 828 lcd.clearPixel(i,j+1);
200784123 2:5446154a4028 829 }
200784123 2:5446154a4028 830 if (joystick.direction == LEFT) {
200784123 2:5446154a4028 831 i--;
200784123 2:5446154a4028 832 lcd.clearPixel(i+1,j);
200784123 2:5446154a4028 833 }
200784123 2:5446154a4028 834 if (joystick.direction == RIGHT) {
200784123 2:5446154a4028 835 i++;
200784123 2:5446154a4028 836 lcd.clearPixel(i-1,j);
200784123 2:5446154a4028 837 }
200784123 2:5446154a4028 838 }
200784123 2:5446154a4028 839
200784123 2:5446154a4028 840 ///checks the direction of the joystick and updates the position of the pixel accordingly
200784123 2:5446154a4028 841 ///keeps the path drawn unless cleared by the button, see clear()
200784123 2:5446154a4028 842 void endMove(){
200784123 2:5446154a4028 843 if (joystick.direction == DOWN) {
200784123 2:5446154a4028 844 j++;
200784123 2:5446154a4028 845 }
200784123 2:5446154a4028 846 if (joystick.direction == UP) {
200784123 2:5446154a4028 847 j--;
200784123 2:5446154a4028 848 }
200784123 2:5446154a4028 849 if (joystick.direction == LEFT) {
200784123 2:5446154a4028 850 i--;
200784123 2:5446154a4028 851 }
200784123 2:5446154a4028 852 if (joystick.direction == RIGHT) {
200784123 2:5446154a4028 853 i++;
200784123 2:5446154a4028 854 }
200784123 2:5446154a4028 855 }
200784123 2:5446154a4028 856
200784123 2:5446154a4028 857 /* Joystick
200784123 2:5446154a4028 858 Example code of how to read a joystick
200784123 2:5446154a4028 859
200784123 2:5446154a4028 860 https://www.sparkfun.com/products/9032
200784123 2:5446154a4028 861
200784123 2:5446154a4028 862 Craig A. Evans
200784123 2:5446154a4028 863 7 March 2015
200784123 2:5446154a4028 864 */
200784123 2:5446154a4028 865
200784123 2:5446154a4028 866 /// read default positions of the joystick to calibrate later readings
200784123 2:5446154a4028 867 void calibrateJoystick(){
200784123 2:5446154a4028 868 button.mode(PullDown);
200784123 2:5446154a4028 869 ///// must not move during calibration
200784123 2:5446154a4028 870 joystick.x0 = xPot; /// initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
200784123 2:5446154a4028 871 joystick.y0 = yPot;
200784123 2:5446154a4028 872 }
200784123 2:5446154a4028 873 void updateJoystick(){
200784123 2:5446154a4028 874 /// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
200784123 2:5446154a4028 875 joystick.x = xPot - joystick.x0;
200784123 2:5446154a4028 876 joystick.y = yPot - joystick.y0;
200784123 2:5446154a4028 877 /// read button state
200784123 2:5446154a4028 878 joystick.button = button;
200784123 2:5446154a4028 879
200784123 2:5446154a4028 880 /// calculate direction depending on x,y values
200784123 2:5446154a4028 881 /// tolerance allows a little lee-way in case joystick not exactly in the stated direction
200784123 2:5446154a4028 882
200784123 2:5446154a4028 883 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
200784123 2:5446154a4028 884 joystick.direction = CENTRE;
200784123 2:5446154a4028 885 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
200784123 2:5446154a4028 886 joystick.direction = DOWN;
200784123 2:5446154a4028 887 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
200784123 2:5446154a4028 888 joystick.direction = UP;
200784123 2:5446154a4028 889 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
200784123 2:5446154a4028 890 joystick.direction = RIGHT;
200784123 2:5446154a4028 891 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
200784123 2:5446154a4028 892 joystick.direction = LEFT;
200784123 2:5446154a4028 893 }
200784123 2:5446154a4028 894 /// set flag for printing
200784123 2:5446154a4028 895 printFlag = 1;
eencae 0:b85460bc73b9 896 }