A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Committer:
el13drt
Date:
Mon Apr 20 15:28:01 2015 +0000
Revision:
24:eb80956e2e95
Parent:
23:548299d45a60
Child:
25:70048c7e02c7
pre save initials

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el13drt 13:a1b3a373c5a4 1
el13drt 13:a1b3a373c5a4 2 // change this to alter tolerance of joystick direction
el13drt 13:a1b3a373c5a4 3 #define DIRECTION_TOLERANCE 0.05
el13drt 12:eedda6554615 4
el13drt 24:eb80956e2e95 5 #include <string>
el13drt 24:eb80956e2e95 6
el13drt 24:eb80956e2e95 7 //using namespace :: std;
el13drt 24:eb80956e2e95 8
el13drt 12:eedda6554615 9 // VCC,SCE,RST,D/C,MOSI,SCLK,LED - set pins for LCD
el13drt 12:eedda6554615 10 N5110 lcd(p7,p8,p9,p10,p11,p13,p22);
el13drt 12:eedda6554615 11
el13drt 15:ff3eb0091453 12 // timers to check state of buttons
el13drt 17:242ccf6a8442 13 Ticker timerA;//buttonA
el13drt 17:242ccf6a8442 14 Ticker timerB;//buttonB
el13drt 15:ff3eb0091453 15
el13drt 13:a1b3a373c5a4 16 // create buzzer objecct
el13drt 13:a1b3a373c5a4 17 Beep buzzer(p21);
el13drt 13:a1b3a373c5a4 18
el13drt 13:a1b3a373c5a4 19 // create local file system
el13drt 13:a1b3a373c5a4 20 //LocalFileSytem local("local");
el13drt 13:a1b3a373c5a4 21
el13drt 13:a1b3a373c5a4 22 // navigation/action buttons
el13drt 13:a1b3a373c5a4 23 DigitalIn buttonA(p19);
el13drt 13:a1b3a373c5a4 24 DigitalIn buttonB(p20);
el13drt 13:a1b3a373c5a4 25
el13drt 13:a1b3a373c5a4 26 // LED indicators
el13drt 13:a1b3a373c5a4 27 AnalogOut ledR(p18);// RED LED
el13drt 13:a1b3a373c5a4 28 DigitalOut ledY(p24);// YELLOW LED
el13drt 13:a1b3a373c5a4 29
el13drt 13:a1b3a373c5a4 30 // connections for joystick
el13drt 14:c2c969e1c6e8 31 InterruptIn joyButton(p17);//Interrupt for ISR
el13drt 13:a1b3a373c5a4 32 AnalogIn xPot(p15);
el13drt 13:a1b3a373c5a4 33 AnalogIn yPot(p16);
el13drt 13:a1b3a373c5a4 34
el13drt 12:eedda6554615 35 // Globabl Variables /////////////////////////
el13drt 12:eedda6554615 36
el13drt 17:242ccf6a8442 37 // sound FX toggle
el13drt 17:242ccf6a8442 38 int FX = 0;
el13drt 17:242ccf6a8442 39
el13drt 15:ff3eb0091453 40 // timer flags to check state of buttons
el13drt 15:ff3eb0091453 41 int buttonFlagA = 0;
el13drt 15:ff3eb0091453 42 int buttonFlagB = 0;
el13drt 15:ff3eb0091453 43
el13drt 14:c2c969e1c6e8 44 // flag for joystick reading
el13drt 14:c2c969e1c6e8 45 int printFlag = 0;
el13drt 14:c2c969e1c6e8 46
el13drt 12:eedda6554615 47 // boundary conditions
el13drt 12:eedda6554615 48 int cells [84][48];
el13drt 12:eedda6554615 49
el13drt 12:eedda6554615 50 // player's score
el13drt 12:eedda6554615 51 int score = 0;
el13drt 12:eedda6554615 52
el13drt 21:1fbbd8ebb3d9 53 int highScore1 = 5;
el13drt 21:1fbbd8ebb3d9 54 int highScore2 = 3;
el13drt 23:548299d45a60 55 int highScore3 = 0;
el13drt 21:1fbbd8ebb3d9 56
el13drt 12:eedda6554615 57 // difficulty - number of pixels hazards incrememnt by
el13drt 12:eedda6554615 58 int fall = 2;
el13drt 12:eedda6554615 59
el13drt 12:eedda6554615 60 // global variables for movement (pixelNinja)
el13drt 12:eedda6554615 61 int a1 = 22;
el13drt 12:eedda6554615 62 int a2 = 24;
el13drt 12:eedda6554615 63 int a3 = 23;
el13drt 12:eedda6554615 64 int a4 = 25;
el13drt 12:eedda6554615 65 int a5 = 20;
el13drt 12:eedda6554615 66 int a6 = 26;
el13drt 12:eedda6554615 67 int a7 = 19;
el13drt 12:eedda6554615 68 int a8 = 21;
el13drt 12:eedda6554615 69
el13drt 12:eedda6554615 70 //global variable for random X co-ordinates
el13drt 12:eedda6554615 71 int randX1;
el13drt 12:eedda6554615 72 int randX2;
el13drt 12:eedda6554615 73 int randX3;
el13drt 12:eedda6554615 74 int randX4;
el13drt 12:eedda6554615 75 int randX5;
el13drt 12:eedda6554615 76 int randX6;
el13drt 12:eedda6554615 77
el13drt 12:eedda6554615 78 // global variable for Y co-ordinates
el13drt 12:eedda6554615 79 int randY1 = 0;
el13drt 12:eedda6554615 80 int randY2 = 0;
el13drt 12:eedda6554615 81 int randY3 = 0;
el13drt 12:eedda6554615 82 int randY4 = 0;
el13drt 12:eedda6554615 83 int randY5 = 0;
el13drt 12:eedda6554615 84 int randY6 = 0;
el13drt 12:eedda6554615 85
el13drt 24:eb80956e2e95 86 // struct for choosing initials
el13drt 24:eb80956e2e95 87 struct State {
el13drt 24:eb80956e2e95 88 char output;
el13drt 24:eb80956e2e95 89 int nextState[2];
el13drt 24:eb80956e2e95 90 };
el13drt 24:eb80956e2e95 91 typedef const struct State STYP;
el13drt 23:548299d45a60 92
el13drt 24:eb80956e2e95 93 int state = 0;//integer for changing states
el13drt 23:548299d45a60 94
el13drt 23:548299d45a60 95 // array for Alphabet
el13drt 23:548299d45a60 96 STYP fsm[26] = {
el13drt 24:eb80956e2e95 97
el13drt 24:eb80956e2e95 98 //output//nextstate//previousstate//
el13drt 24:eb80956e2e95 99
el13drt 23:548299d45a60 100 {'A',{1,25}},
el13drt 23:548299d45a60 101 {'B',{2,0}},
el13drt 23:548299d45a60 102 {'C',{3,1}},
el13drt 23:548299d45a60 103 {'D',{4,2}},
el13drt 23:548299d45a60 104 {'E',{5,3}},
el13drt 23:548299d45a60 105 {'F',{6,4}},
el13drt 23:548299d45a60 106 {'G',{7,5}},
el13drt 23:548299d45a60 107 {'H',{8,6}},
el13drt 23:548299d45a60 108 {'I',{9,7}},
el13drt 23:548299d45a60 109 {'J',{10,8}},
el13drt 23:548299d45a60 110 {'K',{11,9}},
el13drt 23:548299d45a60 111 {'L',{12,10}},
el13drt 23:548299d45a60 112 {'M',{13,11}},
el13drt 23:548299d45a60 113 {'N',{14,12}},
el13drt 23:548299d45a60 114 {'O',{15,13}},
el13drt 23:548299d45a60 115 {'P',{16,14}},
el13drt 23:548299d45a60 116 {'Q',{17,15}},
el13drt 23:548299d45a60 117 {'R',{18,16}},
el13drt 23:548299d45a60 118 {'S',{19,17}},
el13drt 23:548299d45a60 119 {'T',{20,18}},
el13drt 23:548299d45a60 120 {'U',{21,19}},
el13drt 23:548299d45a60 121 {'V',{22,20}},
el13drt 23:548299d45a60 122 {'W',{23,21}},
el13drt 23:548299d45a60 123 {'X',{24,22}},
el13drt 23:548299d45a60 124 {'Y',{25,23}},
el13drt 23:548299d45a60 125 {'Z',{0,24}},
el13drt 23:548299d45a60 126 };
el13drt 23:548299d45a60 127
el13drt 13:a1b3a373c5a4 128 // function prototypes
el13drt 13:a1b3a373c5a4 129 void calibrateJoystick();
el13drt 13:a1b3a373c5a4 130 void updateJoystick();
el13drt 13:a1b3a373c5a4 131
el13drt 13:a1b3a373c5a4 132 // timer to regularly read the joystick
el13drt 13:a1b3a373c5a4 133 Ticker pollJoystick;
el13drt 13:a1b3a373c5a4 134
el13drt 13:a1b3a373c5a4 135 // serial for debug
el13drt 13:a1b3a373c5a4 136 Serial serial(USBTX,USBRX);
el13drt 13:a1b3a373c5a4 137
el13drt 13:a1b3a373c5a4 138 // create enumerated type (0,1,2,3 etc. for direction)
el13drt 13:a1b3a373c5a4 139 // could be extended for diagonals etc.
el13drt 13:a1b3a373c5a4 140 enum DirectionName {
el13drt 13:a1b3a373c5a4 141 UP,
el13drt 13:a1b3a373c5a4 142 DOWN,
el13drt 13:a1b3a373c5a4 143 LEFT,
el13drt 13:a1b3a373c5a4 144 RIGHT,
el13drt 13:a1b3a373c5a4 145 CENTRE,
el13drt 13:a1b3a373c5a4 146 UNKNOWN
el13drt 13:a1b3a373c5a4 147 };
el13drt 13:a1b3a373c5a4 148
el13drt 13:a1b3a373c5a4 149 // struct for Joystick
el13drt 13:a1b3a373c5a4 150 typedef struct JoyStick Joystick;
el13drt 13:a1b3a373c5a4 151 struct JoyStick {
el13drt 13:a1b3a373c5a4 152 float x; // current x value
el13drt 13:a1b3a373c5a4 153 float x0; // 'centred' x value
el13drt 13:a1b3a373c5a4 154 float y; // current y value
el13drt 13:a1b3a373c5a4 155 float y0; // 'centred' y value
el13drt 13:a1b3a373c5a4 156 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
el13drt 13:a1b3a373c5a4 157 DirectionName direction; // current direction
el13drt 13:a1b3a373c5a4 158 };
el13drt 13:a1b3a373c5a4 159
el13drt 13:a1b3a373c5a4 160 // create struct variable
el13drt 13:a1b3a373c5a4 161 Joystick joystick;
el13drt 22:e5bfe0e7c508 162
el13drt 22:e5bfe0e7c508 163 // if buttonA set flag A
el13drt 15:ff3eb0091453 164 void timerExpiredA()
el13drt 15:ff3eb0091453 165 {
el13drt 15:ff3eb0091453 166 if(buttonA == 1) {
el13drt 15:ff3eb0091453 167 buttonFlagA = 1;
el13drt 15:ff3eb0091453 168 serial.printf("flagA set\n");
el13drt 15:ff3eb0091453 169 }
el13drt 15:ff3eb0091453 170 }
el13drt 15:ff3eb0091453 171
el13drt 22:e5bfe0e7c508 172 // if buttonB set flag B
el13drt 15:ff3eb0091453 173 void timerExpiredB()
el13drt 15:ff3eb0091453 174 {
el13drt 15:ff3eb0091453 175 if(buttonB == 1) {
el13drt 15:ff3eb0091453 176 buttonFlagB = 1;
el13drt 15:ff3eb0091453 177 serial.printf("flagB set\n");
el13drt 15:ff3eb0091453 178 }
el13drt 15:ff3eb0091453 179 }
el13drt 13:a1b3a373c5a4 180
el13drt 13:a1b3a373c5a4 181 // set seed/randomise initial co-Ordinates
el13drt 13:a1b3a373c5a4 182 void randomise()
el13drt 13:a1b3a373c5a4 183 {
el13drt 13:a1b3a373c5a4 184 srand (time(NULL));//initial seed for randomisation
el13drt 13:a1b3a373c5a4 185
el13drt 13:a1b3a373c5a4 186 // initial random x co-ordinates
el13drt 13:a1b3a373c5a4 187 // for falling hazards
el13drt 13:a1b3a373c5a4 188 // (values between 3 and 76)
el13drt 13:a1b3a373c5a4 189 randX1 = rand() % 74 + 5;
el13drt 13:a1b3a373c5a4 190 randX2 = rand() % 74 + 5;
el13drt 13:a1b3a373c5a4 191 randX3 = rand() % 74 + 5;
el13drt 13:a1b3a373c5a4 192 randX4 = rand() % 74 + 5;
el13drt 13:a1b3a373c5a4 193 randX5 = rand() % 74 + 5;
el13drt 13:a1b3a373c5a4 194 randX6 = rand() % 74 + 5;
el13drt 13:a1b3a373c5a4 195 }
el13drt 13:a1b3a373c5a4 196
el13drt 12:eedda6554615 197 // static background
el13drt 12:eedda6554615 198 void backGround()
el13drt 12:eedda6554615 199 {
el13drt 12:eedda6554615 200 // x, y, w, h, fill - draw ground
el13drt 12:eedda6554615 201 lcd.drawRect(0,47,84,0,1);
el13drt 12:eedda6554615 202
el13drt 12:eedda6554615 203 // x, y, w, h, fill - draw left wall
el13drt 12:eedda6554615 204 lcd.drawRect(2,0,0,47,1);
el13drt 12:eedda6554615 205 // left wall - brick line 1
el13drt 12:eedda6554615 206 for(int x=0; x<47; x+=4)
el13drt 12:eedda6554615 207 lcd.drawLine(1,1,1,48,2);
el13drt 12:eedda6554615 208 // left wall - brick line 2
el13drt 12:eedda6554615 209 for(int x=0; x<47; x+=4)
el13drt 12:eedda6554615 210 lcd.drawLine(0,0,0,48,2);
el13drt 12:eedda6554615 211
el13drt 12:eedda6554615 212 // x, y, w, h, fill - draw right wall
el13drt 12:eedda6554615 213 lcd.drawRect(81,0,0,47,1);
el13drt 12:eedda6554615 214 // right wall - brick line 1
el13drt 12:eedda6554615 215 for(int x=0; x<WIDTH; x+=4)
el13drt 12:eedda6554615 216 lcd.drawLine(82,0,82,48,2);
el13drt 12:eedda6554615 217 // right wall - brick line 2
el13drt 12:eedda6554615 218 for(int x=0; x<WIDTH; x+=4)
el13drt 12:eedda6554615 219 lcd.drawLine(83,1,83,48,2);
el13drt 12:eedda6554615 220
el13drt 12:eedda6554615 221 lcd.refresh();
el13drt 12:eedda6554615 222 }
el13drt 12:eedda6554615 223
el13drt 12:eedda6554615 224 //intro screen
el13drt 12:eedda6554615 225 void welcome()
el13drt 12:eedda6554615 226 {
el13drt 12:eedda6554615 227 //bottom border
el13drt 12:eedda6554615 228 lcd.drawRect(0,44,84,2,1);
el13drt 12:eedda6554615 229
el13drt 12:eedda6554615 230 //top border
el13drt 12:eedda6554615 231 lcd.drawRect(0,1,84,2,1);
el13drt 12:eedda6554615 232
el13drt 12:eedda6554615 233 //print initials 'DRT'
el13drt 12:eedda6554615 234 lcd.printChar('D',30,14);
el13drt 12:eedda6554615 235 wait(0.6);
el13drt 12:eedda6554615 236 lcd.printChar('R',37,14);
el13drt 12:eedda6554615 237 wait(0.6);
el13drt 12:eedda6554615 238 lcd.printChar('T',44,14);
el13drt 12:eedda6554615 239 wait(0.6);
el13drt 12:eedda6554615 240
el13drt 12:eedda6554615 241 //print 'presents...'
el13drt 12:eedda6554615 242 lcd.printString("presents...",8,21);
el13drt 12:eedda6554615 243 wait(1.0);
el13drt 12:eedda6554615 244
el13drt 12:eedda6554615 245 //dramatic flashing
el13drt 13:a1b3a373c5a4 246 buzzer.beep(5000,0.3);
el13drt 12:eedda6554615 247 lcd.inverseMode();
el13drt 12:eedda6554615 248 wait(0.2);
el13drt 12:eedda6554615 249 lcd.normalMode();
el13drt 12:eedda6554615 250 wait(0.2);
el13drt 13:a1b3a373c5a4 251 buzzer.beep(5000,0.3);
el13drt 12:eedda6554615 252 lcd.inverseMode();
el13drt 12:eedda6554615 253 wait(0.2);
el13drt 12:eedda6554615 254 lcd.normalMode();
el13drt 12:eedda6554615 255 wait(1.0);
el13drt 12:eedda6554615 256
el13drt 12:eedda6554615 257 //more dramatic flashing
el13drt 13:a1b3a373c5a4 258 buzzer.beep(5000,0.3);
el13drt 12:eedda6554615 259 lcd.inverseMode();
el13drt 12:eedda6554615 260 wait(0.2);
el13drt 12:eedda6554615 261 lcd.normalMode();
el13drt 12:eedda6554615 262 wait(0.2);
el13drt 13:a1b3a373c5a4 263 buzzer.beep(5000,0.3);
el13drt 12:eedda6554615 264 lcd.inverseMode();
el13drt 12:eedda6554615 265 wait(0.2);
el13drt 12:eedda6554615 266 lcd.normalMode();
el13drt 12:eedda6554615 267 wait(0.6);
el13drt 12:eedda6554615 268 }
el13drt 12:eedda6554615 269
el13drt 12:eedda6554615 270 //pixel ninja character
el13drt 12:eedda6554615 271 void pixelNinja()
el13drt 12:eedda6554615 272 {
el13drt 12:eedda6554615 273 //x, y, w, h, fill - left leg
el13drt 12:eedda6554615 274 lcd.drawRect(a1,39,0,7,1);
el13drt 12:eedda6554615 275 //right leg
el13drt 12:eedda6554615 276 lcd.drawRect(a2,39,0,7,1);
el13drt 12:eedda6554615 277 //centre stick
el13drt 12:eedda6554615 278 lcd.drawRect(a3,37,0,7,1);
el13drt 12:eedda6554615 279 //back of the head
el13drt 12:eedda6554615 280 lcd.drawRect(a1,33,0,4,1);
el13drt 12:eedda6554615 281 //top of the head
el13drt 12:eedda6554615 282 lcd.drawRect(a1,33,4,0,1);
el13drt 12:eedda6554615 283 //jaw
el13drt 12:eedda6554615 284 lcd.drawRect(a2,38,2,0,1);
el13drt 12:eedda6554615 285 //right shoulder
el13drt 12:eedda6554615 286 lcd.drawRect(a4,40,1,0,1);
el13drt 12:eedda6554615 287 //left shoulder
el13drt 12:eedda6554615 288 lcd.drawRect(a5,40,1,0,1);
el13drt 12:eedda6554615 289 //left arm
el13drt 12:eedda6554615 290 lcd.drawRect(a5,41,0,1,1);
el13drt 12:eedda6554615 291 //right arm
el13drt 12:eedda6554615 292 lcd.drawRect(a6,41,0,1,1);
el13drt 12:eedda6554615 293 //right eye
el13drt 12:eedda6554615 294 lcd.drawRect(a6,35,0,0,1);
el13drt 12:eedda6554615 295 //mouth piece
el13drt 12:eedda6554615 296 lcd.drawRect(a6,37,0,0,1);
el13drt 12:eedda6554615 297 //left eye
el13drt 12:eedda6554615 298 lcd.drawRect(a2,35,0,0,1);
el13drt 12:eedda6554615 299 //sword handle
el13drt 12:eedda6554615 300 lcd.drawRect(a7,36,0,0,1);
el13drt 12:eedda6554615 301 lcd.drawRect(a5,37,0,0,1);
el13drt 12:eedda6554615 302 lcd.drawRect(a8,38,0,0,1);
el13drt 12:eedda6554615 303 }
el13drt 12:eedda6554615 304
el13drt 12:eedda6554615 305 // stops ninja going through walls
el13drt 12:eedda6554615 306 void ninjaBoundaries()
el13drt 12:eedda6554615 307 {
el13drt 12:eedda6554615 308 if(a6 > 79 )
el13drt 12:eedda6554615 309 a6 = 79;
el13drt 12:eedda6554615 310 if(a4 > 78)
el13drt 12:eedda6554615 311 a4 = 78;
el13drt 12:eedda6554615 312 if(a2 > 77)
el13drt 12:eedda6554615 313 a2 = 77;
el13drt 12:eedda6554615 314 if(a3 > 76)
el13drt 12:eedda6554615 315 a3 = 76;
el13drt 12:eedda6554615 316 if(a1 > 75)
el13drt 12:eedda6554615 317 a1 = 75;
el13drt 12:eedda6554615 318 if(a8 > 74)
el13drt 12:eedda6554615 319 a8 = 74;
el13drt 12:eedda6554615 320 if(a5 > 73)
el13drt 12:eedda6554615 321 a5 = 73;
el13drt 12:eedda6554615 322 if(a7 > 72)
el13drt 12:eedda6554615 323 a7 = 72;
el13drt 12:eedda6554615 324
el13drt 12:eedda6554615 325 if(a6 < 11 )
el13drt 12:eedda6554615 326 a6 = 11;
el13drt 12:eedda6554615 327 if(a4 < 10)
el13drt 12:eedda6554615 328 a4 = 10;
el13drt 12:eedda6554615 329 if(a2 < 9)
el13drt 12:eedda6554615 330 a2 = 9;
el13drt 12:eedda6554615 331 if(a3 < 8)
el13drt 12:eedda6554615 332 a3 = 8;
el13drt 12:eedda6554615 333 if(a1 < 7)
el13drt 12:eedda6554615 334 a1 = 7;
el13drt 12:eedda6554615 335 if(a8 < 6)
el13drt 12:eedda6554615 336 a8 = 6;
el13drt 12:eedda6554615 337 if(a5 < 5)
el13drt 12:eedda6554615 338 a5 = 5;
el13drt 12:eedda6554615 339 if(a7 < 4)
el13drt 12:eedda6554615 340 a7 = 4;
el13drt 12:eedda6554615 341 }
el13drt 12:eedda6554615 342
el13drt 12:eedda6554615 343 // resets back to initial values
el13drt 12:eedda6554615 344 void resetGame()
el13drt 22:e5bfe0e7c508 345 {
el13drt 12:eedda6554615 346 score = 0;
el13drt 12:eedda6554615 347
el13drt 12:eedda6554615 348 a1 = 22;
el13drt 12:eedda6554615 349 a2 = 24;
el13drt 12:eedda6554615 350 a3 = 23;
el13drt 12:eedda6554615 351 a4 = 25;
el13drt 12:eedda6554615 352 a5 = 20;
el13drt 12:eedda6554615 353 a6 = 26;
el13drt 12:eedda6554615 354 a7 = 19;
el13drt 12:eedda6554615 355 a8 = 21;
el13drt 12:eedda6554615 356
el13drt 12:eedda6554615 357 // in this case the X values are given a
el13drt 12:eedda6554615 358 // new random variable each time the player
el13drt 12:eedda6554615 359 // dies or exits and starts a new game
el13drt 12:eedda6554615 360 randX1 = rand() % 74 + 5;
el13drt 12:eedda6554615 361 randX2 = rand() % 74 + 5;
el13drt 12:eedda6554615 362 randX3 = rand() % 74 + 5;
el13drt 12:eedda6554615 363 randX4 = rand() % 74 + 5;
el13drt 12:eedda6554615 364 randX5 = rand() % 74 + 5;
el13drt 12:eedda6554615 365 randX6 = rand() % 74 + 5;
el13drt 12:eedda6554615 366
el13drt 12:eedda6554615 367 randY1 = 0;
el13drt 12:eedda6554615 368 randY2 = 0;
el13drt 12:eedda6554615 369 randY3 = 0;
el13drt 12:eedda6554615 370 randY4 = 0;
el13drt 12:eedda6554615 371 randY5 = 0;
el13drt 12:eedda6554615 372 randY6 = 0;
el13drt 12:eedda6554615 373 lcd.clear();
el13drt 12:eedda6554615 374 }
el13drt 12:eedda6554615 375
el13drt 12:eedda6554615 376 // draws falling hazards
el13drt 12:eedda6554615 377 void hazards()
el13drt 12:eedda6554615 378 {
el13drt 12:eedda6554615 379 // X, Y, radius, fill
el13drt 12:eedda6554615 380 lcd.drawCircle(randX1,randY1,2,1);
el13drt 12:eedda6554615 381 lcd.drawCircle(randX2,randY2,2,1);
el13drt 12:eedda6554615 382 lcd.drawCircle(randX3,randY3,2,1);
el13drt 12:eedda6554615 383 lcd.drawCircle(randX4,randY4,2,1);
el13drt 12:eedda6554615 384 lcd.drawCircle(randX5,randY5,2,1);
el13drt 12:eedda6554615 385 lcd.drawCircle(randX6,randY6,2,1);
el13drt 12:eedda6554615 386
el13drt 12:eedda6554615 387 lcd.refresh();
el13drt 12:eedda6554615 388 }
el13drt 12:eedda6554615 389
el13drt 12:eedda6554615 390 // makes hazards fall - randomises X axis co-ordinates
el13drt 12:eedda6554615 391 void hazardFall()
el13drt 12:eedda6554615 392 {
el13drt 12:eedda6554615 393 // increments randY1 variables
el13drt 12:eedda6554615 394 // appearing to make them fall
el13drt 12:eedda6554615 395 randY1 = randY1 += fall;
el13drt 12:eedda6554615 396 randY2 = randY2 += fall;
el13drt 12:eedda6554615 397 randY3 = randY3 += fall;
el13drt 12:eedda6554615 398 randY4 = randY4 += fall;
el13drt 12:eedda6554615 399 randY5 = randY5 += fall;
el13drt 12:eedda6554615 400 randY6 = randY6 += fall;
el13drt 12:eedda6554615 401
el13drt 12:eedda6554615 402 // loops the objects once they 'hit the floor'
el13drt 12:eedda6554615 403 // this imitates a new set of objects falling
el13drt 12:eedda6554615 404
el13drt 12:eedda6554615 405 if (randY1>=48)
el13drt 12:eedda6554615 406 randY1=0;
el13drt 12:eedda6554615 407
el13drt 12:eedda6554615 408 if (randY2>=48)
el13drt 12:eedda6554615 409 randY2=0;
el13drt 12:eedda6554615 410
el13drt 12:eedda6554615 411 if (randY3>=48)
el13drt 12:eedda6554615 412 randY3=0;
el13drt 12:eedda6554615 413
el13drt 12:eedda6554615 414 if (randY4>=48)
el13drt 12:eedda6554615 415 randY4=0;
el13drt 12:eedda6554615 416
el13drt 12:eedda6554615 417 if (randY5>=48)
el13drt 12:eedda6554615 418 randY5=0;
el13drt 12:eedda6554615 419
el13drt 12:eedda6554615 420 // each time the objects loop, a new pseudo random value
el13drt 12:eedda6554615 421 // is assigned to the global variables (randX) to
el13drt 12:eedda6554615 422 // randomise their positions
el13drt 12:eedda6554615 423
el13drt 12:eedda6554615 424 if (randY6>=48) {
el13drt 12:eedda6554615 425 randY6=0;
el13drt 12:eedda6554615 426
el13drt 12:eedda6554615 427 score = score++;//increment score by 1 after each wave of hazards
el13drt 12:eedda6554615 428
el13drt 12:eedda6554615 429 randX1 = rand() % 74 + 5;// psuedo random number
el13drt 12:eedda6554615 430 randX2 = rand() % 74 + 5;// between 5 and 74
el13drt 12:eedda6554615 431 randX3 = rand() % 74 + 5;
el13drt 12:eedda6554615 432 randX4 = rand() % 74 + 5;
el13drt 12:eedda6554615 433 randX5 = rand() % 74 + 5;
el13drt 12:eedda6554615 434 randX6 = rand() % 74 + 5;
el13drt 12:eedda6554615 435 }
el13drt 12:eedda6554615 436 }
el13drt 12:eedda6554615 437
el13drt 12:eedda6554615 438 // clears old pixels and keeps set pixels
el13drt 12:eedda6554615 439 void startrek()
el13drt 12:eedda6554615 440 {
el13drt 12:eedda6554615 441 for (int i=3; i<81; i++)// loops through rows
el13drt 12:eedda6554615 442 for (int j=0; j<47; j++)
el13drt 12:eedda6554615 443 if (cells[i][j]) {// if there's a pixel then keep it
el13drt 12:eedda6554615 444 lcd.setPixel(i,j);
el13drt 12:eedda6554615 445 } else {
el13drt 12:eedda6554615 446 lcd.clearPixel(i,j);// else remove the old ones
el13drt 12:eedda6554615 447 }
el13drt 12:eedda6554615 448 lcd.refresh();
el13drt 12:eedda6554615 449 }
el13drt 12:eedda6554615 450
el13drt 12:eedda6554615 451 // clears old pixels and keeps set pixels
el13drt 12:eedda6554615 452 void refreshCursor1()
el13drt 12:eedda6554615 453 {
el13drt 12:eedda6554615 454 for (int i=70; i<80; i++)// loops through rows
el13drt 12:eedda6554615 455 for (int j=17; j<25; j++)
el13drt 12:eedda6554615 456 if (cells[i][j]) {// if there's a pixel then keep it
el13drt 12:eedda6554615 457 lcd.setPixel(i,j);
el13drt 12:eedda6554615 458 } else {
el13drt 12:eedda6554615 459 lcd.clearPixel(i,j);// else remove the old ones
el13drt 12:eedda6554615 460 }
el13drt 12:eedda6554615 461 lcd.refresh();
el13drt 12:eedda6554615 462 }
el13drt 12:eedda6554615 463
el13drt 12:eedda6554615 464 // clears old pixels and keeps set pixels
el13drt 12:eedda6554615 465 void refreshCursor2()
el13drt 12:eedda6554615 466 {
el13drt 12:eedda6554615 467 for (int i=70; i<80; i++)// loops through rows
el13drt 12:eedda6554615 468 for (int j=25; j<32; j++)
el13drt 12:eedda6554615 469 if (cells[i][j]) {// if there's a pixel then keep it
el13drt 12:eedda6554615 470 lcd.setPixel(i,j);
el13drt 12:eedda6554615 471 } else {
el13drt 12:eedda6554615 472 lcd.clearPixel(i,j);// else remove the old ones
el13drt 12:eedda6554615 473 }
el13drt 12:eedda6554615 474 lcd.refresh();
el13drt 12:eedda6554615 475 }
el13drt 12:eedda6554615 476
el13drt 12:eedda6554615 477 // clears old pixels and keeps set pixels
el13drt 12:eedda6554615 478 void refreshCursor3()
el13drt 12:eedda6554615 479 {
el13drt 12:eedda6554615 480 for (int i=70; i<80; i++)// loops through rows
el13drt 12:eedda6554615 481 for (int j=32; j<40; j++)
el13drt 12:eedda6554615 482 if (cells[i][j]) {// if there's a pixel then keep it
el13drt 12:eedda6554615 483 lcd.setPixel(i,j);
el13drt 12:eedda6554615 484 } else {
el13drt 12:eedda6554615 485 lcd.clearPixel(i,j);// else remove the old ones
el13drt 12:eedda6554615 486 }
el13drt 12:eedda6554615 487 lcd.refresh();
el13drt 12:eedda6554615 488 }
el13drt 12:eedda6554615 489
el13drt 17:242ccf6a8442 490 // beep/light when buttons are closed
el13drt 17:242ccf6a8442 491 void actionButtons()
el13drt 17:242ccf6a8442 492 {
el13drt 17:242ccf6a8442 493 if((FX == 0)&&(buttonA||buttonB)) {
el13drt 17:242ccf6a8442 494 ledY = 1;
el13drt 17:242ccf6a8442 495 buzzer.beep(1500,0.3);
el13drt 17:242ccf6a8442 496 }
el13drt 17:242ccf6a8442 497 if (buttonA || buttonB) {
el13drt 17:242ccf6a8442 498 ledY = 1;
el13drt 17:242ccf6a8442 499 } else {
el13drt 17:242ccf6a8442 500 ledY = 0;
el13drt 17:242ccf6a8442 501 }
el13drt 17:242ccf6a8442 502 }
el13drt 17:242ccf6a8442 503
el13drt 12:eedda6554615 504 // presents main menu options
el13drt 12:eedda6554615 505 void mainMenu(int& mainOption)
el13drt 12:eedda6554615 506 {
el13drt 17:242ccf6a8442 507 actionButtons();//set audible/light for button
el13drt 12:eedda6554615 508
el13drt 12:eedda6554615 509 // joystick selection
el13drt 14:c2c969e1c6e8 510 if (printFlag) {//if flag set, clear flag and print joystick values to serial port
el13drt 12:eedda6554615 511 printFlag = 0;
el13drt 12:eedda6554615 512
el13drt 12:eedda6554615 513 // option up
el13drt 12:eedda6554615 514 if (joystick.direction == UP) {
el13drt 12:eedda6554615 515 serial.printf(" UP\n");
el13drt 12:eedda6554615 516 mainOption = mainOption--;
el13drt 12:eedda6554615 517 if (mainOption < 0)mainOption = 0;
el13drt 12:eedda6554615 518 }
el13drt 12:eedda6554615 519 // option down
el13drt 12:eedda6554615 520 if (joystick.direction == DOWN) {
el13drt 12:eedda6554615 521 serial.printf(" DOWN\n");
el13drt 12:eedda6554615 522 mainOption = mainOption++;
el13drt 12:eedda6554615 523 if (mainOption > 2)mainOption = 2;
el13drt 12:eedda6554615 524 }
el13drt 12:eedda6554615 525 // Centre / Unknown orientation
el13drt 12:eedda6554615 526 if (joystick.direction == CENTRE)
el13drt 12:eedda6554615 527 serial.printf(" CENTRE\n");
el13drt 12:eedda6554615 528 if (joystick.direction == UNKNOWN)
el13drt 12:eedda6554615 529 serial.printf(" Unsupported direction\n");
el13drt 12:eedda6554615 530
el13drt 12:eedda6554615 531 // 'Play Game' option 1
el13drt 12:eedda6554615 532 if (mainOption == 0) {
el13drt 12:eedda6554615 533 lcd.printString("Play Game",3,4);
el13drt 12:eedda6554615 534 }
el13drt 12:eedda6554615 535 // 'High Scores' option 2
el13drt 12:eedda6554615 536 if (mainOption == 1) {
el13drt 12:eedda6554615 537 lcd.printString(" Scores ",3,4);
el13drt 12:eedda6554615 538 }
el13drt 12:eedda6554615 539 // 'Options' option 3
el13drt 12:eedda6554615 540 if (mainOption == 2) {
el13drt 12:eedda6554615 541 lcd.printString(" Options ",3,4);
el13drt 12:eedda6554615 542 }
el13drt 12:eedda6554615 543 }
el13drt 12:eedda6554615 544 }
el13drt 12:eedda6554615 545
el13drt 12:eedda6554615 546 // draws main menu
el13drt 12:eedda6554615 547 void drawMainMenu()
el13drt 12:eedda6554615 548 {
el13drt 12:eedda6554615 549 //bottom border
el13drt 12:eedda6554615 550 lcd.drawRect(0,47,84,0,1);
el13drt 12:eedda6554615 551 //top border
el13drt 12:eedda6554615 552 lcd.drawRect(0,0,84,2,1);
el13drt 12:eedda6554615 553
el13drt 12:eedda6554615 554 //print 'Xtreme Tower'
el13drt 12:eedda6554615 555 lcd.printString("Xtreme",4,25);
el13drt 12:eedda6554615 556 lcd.printString("Tower",44,25);
el13drt 12:eedda6554615 557
el13drt 12:eedda6554615 558 //title outline
el13drt 12:eedda6554615 559 lcd.drawRect(3,6,77,10,0);
el13drt 12:eedda6554615 560
el13drt 12:eedda6554615 561 ////castle //x, y, w, h, fill//////////////////////
el13drt 12:eedda6554615 562
el13drt 12:eedda6554615 563 //castle main bulk
el13drt 12:eedda6554615 564 lcd.drawRect(59,32,21,8,1);
el13drt 12:eedda6554615 565
el13drt 12:eedda6554615 566 //left window bulk
el13drt 12:eedda6554615 567 lcd.drawRect(59,22,2,10,1);
el13drt 12:eedda6554615 568 //centre left window bulk
el13drt 12:eedda6554615 569 lcd.drawRect(65,22,2,10,1);
el13drt 12:eedda6554615 570 //centre right window bulk
el13drt 12:eedda6554615 571 lcd.drawRect(72,22,2,10,1);
el13drt 12:eedda6554615 572 //right window bulk
el13drt 12:eedda6554615 573 lcd.drawRect(78,22,2,10,1);
el13drt 12:eedda6554615 574 //central window bulk
el13drt 12:eedda6554615 575 lcd.drawRect(68,25,3,7,1);
el13drt 12:eedda6554615 576
el13drt 12:eedda6554615 577 //central window bulk
el13drt 12:eedda6554615 578 lcd.drawRect(75,28,0,0,1);
el13drt 12:eedda6554615 579 lcd.drawRect(77,28,0,0,1);
el13drt 12:eedda6554615 580 lcd.drawRect(64,28,0,0,1);
el13drt 12:eedda6554615 581 lcd.drawRect(62,28,0,0,1);
el13drt 12:eedda6554615 582
el13drt 12:eedda6554615 583 //above left window bulk
el13drt 12:eedda6554615 584 lcd.drawRect(62,25,3,2,1);
el13drt 12:eedda6554615 585 //above right window bulk
el13drt 12:eedda6554615 586 lcd.drawRect(75,25,2,2,1);
el13drt 12:eedda6554615 587
el13drt 12:eedda6554615 588 //lower right line
el13drt 12:eedda6554615 589 lcd.drawRect(71,42,9,0,1);
el13drt 12:eedda6554615 590 //upper right line
el13drt 12:eedda6554615 591 lcd.drawRect(70,41,10,0,1);
el13drt 12:eedda6554615 592
el13drt 12:eedda6554615 593 //upper left line
el13drt 12:eedda6554615 594 lcd.drawRect(59,41,10,0,1);
el13drt 12:eedda6554615 595 //lower left line
el13drt 12:eedda6554615 596 lcd.drawRect(59,42,9,0,1);
el13drt 12:eedda6554615 597
el13drt 12:eedda6554615 598 //bottom left bulk
el13drt 12:eedda6554615 599 lcd.drawRect(59,43,8,3,1);
el13drt 12:eedda6554615 600 //bottom right bulk
el13drt 12:eedda6554615 601 lcd.drawRect(72,43,8,3,1);
el13drt 12:eedda6554615 602
el13drt 12:eedda6554615 603 //option arrows - lower
el13drt 12:eedda6554615 604 lcd.drawRect(27,42,4,0,1);
el13drt 12:eedda6554615 605 lcd.drawRect(28,43,2,0,1);
el13drt 12:eedda6554615 606 lcd.drawRect(29,44,0,0,1);
el13drt 12:eedda6554615 607
el13drt 12:eedda6554615 608 //option arrows - higher
el13drt 12:eedda6554615 609 lcd.drawRect(27,29,4,0,1);
el13drt 12:eedda6554615 610 lcd.drawRect(28,28,2,0,1);
el13drt 12:eedda6554615 611 lcd.drawRect(29,27,0,0,1);
el13drt 12:eedda6554615 612 }
el13drt 12:eedda6554615 613
el13drt 12:eedda6554615 614 // presents exit menu options
el13drt 12:eedda6554615 615 void exitMenu(int& exitOption)
el13drt 12:eedda6554615 616 {
el13drt 17:242ccf6a8442 617 actionButtons();
el13drt 14:c2c969e1c6e8 618 if (printFlag) {//if flag set, clear flag and print joystick values to serial port
el13drt 12:eedda6554615 619 printFlag = 0;
el13drt 12:eedda6554615 620
el13drt 12:eedda6554615 621 // check joystick direction
el13drt 12:eedda6554615 622 if (joystick.direction == LEFT) {
el13drt 12:eedda6554615 623 serial.printf(" LEFT\n");
el13drt 12:eedda6554615 624 exitOption--;
el13drt 12:eedda6554615 625 if(exitOption < 0)exitOption = 0;
el13drt 12:eedda6554615 626 }
el13drt 12:eedda6554615 627 if (joystick.direction == RIGHT) {
el13drt 12:eedda6554615 628 serial.printf(" RIGHT\n");
el13drt 12:eedda6554615 629 exitOption++;
el13drt 12:eedda6554615 630 if(exitOption > 1)exitOption = 1;
el13drt 12:eedda6554615 631 }
el13drt 12:eedda6554615 632 if (joystick.direction == CENTRE)
el13drt 12:eedda6554615 633 serial.printf(" CENTRE\n");
el13drt 12:eedda6554615 634 if (joystick.direction == UNKNOWN)
el13drt 12:eedda6554615 635 serial.printf(" Unsupported direction\n");
el13drt 12:eedda6554615 636 }
el13drt 12:eedda6554615 637 // draws option cursor
el13drt 12:eedda6554615 638 if(exitOption == 0) {
el13drt 12:eedda6554615 639 lcd.printString("YES",29,27);
el13drt 12:eedda6554615 640 }
el13drt 12:eedda6554615 641 if(exitOption == 1) {
el13drt 12:eedda6554615 642 lcd.printString(" NO",29,27);
el13drt 12:eedda6554615 643 }
el13drt 12:eedda6554615 644 }
el13drt 12:eedda6554615 645
el13drt 12:eedda6554615 646 // draws exit menu
el13drt 12:eedda6554615 647 void drawExitMenu()
el13drt 12:eedda6554615 648 {
el13drt 12:eedda6554615 649 // set exit menu
el13drt 12:eedda6554615 650 lcd.clear();
el13drt 12:eedda6554615 651 lcd.drawRect(8,6,70,30,0);//title outline
el13drt 12:eedda6554615 652 lcd.printString("Exit Game?",10,25);
el13drt 12:eedda6554615 653 backGround();
el13drt 12:eedda6554615 654
el13drt 12:eedda6554615 655 // option arrow - right
el13drt 12:eedda6554615 656 lcd.drawRect(55,25,0,4,1);
el13drt 12:eedda6554615 657 lcd.drawRect(56,26,0,2,1);
el13drt 12:eedda6554615 658 lcd.drawRect(57,27,0,0,1);
el13drt 12:eedda6554615 659
el13drt 12:eedda6554615 660 // option arrow - left
el13drt 12:eedda6554615 661 lcd.drawRect(27,25,0,4,1);
el13drt 12:eedda6554615 662 lcd.drawRect(26,26,0,2,1);
el13drt 12:eedda6554615 663 lcd.drawRect(25,27,0,0,1);
el13drt 12:eedda6554615 664 }
el13drt 12:eedda6554615 665
el13drt 12:eedda6554615 666 // presents the options
el13drt 12:eedda6554615 667 void optionsMenu(int& option)
el13drt 12:eedda6554615 668 {
el13drt 12:eedda6554615 669 // joystick selection
el13drt 12:eedda6554615 670 if (printFlag) { //if flag set, clear flag and print joystick values to serial port
el13drt 12:eedda6554615 671 printFlag = 0;
el13drt 12:eedda6554615 672
el13drt 12:eedda6554615 673 // option up
el13drt 12:eedda6554615 674 if (joystick.direction == UP) {
el13drt 12:eedda6554615 675 serial.printf(" UP\n");
el13drt 12:eedda6554615 676 option = option--;
el13drt 12:eedda6554615 677 if (option < 0)option = 0;
el13drt 12:eedda6554615 678 }
el13drt 12:eedda6554615 679 // option down
el13drt 12:eedda6554615 680 if (joystick.direction == DOWN) {
el13drt 12:eedda6554615 681 serial.printf(" DOWN\n");
el13drt 12:eedda6554615 682 option = option++;
el13drt 12:eedda6554615 683 if (option > 1)option = 1;
el13drt 12:eedda6554615 684 }
el13drt 12:eedda6554615 685 // Centre / Unknown orientation
el13drt 12:eedda6554615 686 if (joystick.direction == CENTRE)
el13drt 12:eedda6554615 687 serial.printf(" CENTRE\n");
el13drt 12:eedda6554615 688 if (joystick.direction == UNKNOWN)
el13drt 12:eedda6554615 689 serial.printf(" Unsupported direction\n");
el13drt 12:eedda6554615 690
el13drt 12:eedda6554615 691 // 'Difficulty' option 1
el13drt 12:eedda6554615 692 if (option == 0) {
el13drt 12:eedda6554615 693 lcd.drawCircle(72,27,2,1);
el13drt 12:eedda6554615 694 refreshCursor3();
el13drt 12:eedda6554615 695 }
el13drt 12:eedda6554615 696 // 'Sound FX' option 2
el13drt 12:eedda6554615 697 if (option == 1) {
el13drt 12:eedda6554615 698 lcd.drawCircle(72,35,2,1);
el13drt 12:eedda6554615 699 refreshCursor2();
el13drt 12:eedda6554615 700 }
el13drt 12:eedda6554615 701 }
el13drt 12:eedda6554615 702 }
el13drt 12:eedda6554615 703
el13drt 12:eedda6554615 704 // draws options menu
el13drt 12:eedda6554615 705 void drawOptionsMenu()
el13drt 12:eedda6554615 706 {
el13drt 12:eedda6554615 707 lcd.clear();//clear screen
el13drt 12:eedda6554615 708 backGround();
el13drt 12:eedda6554615 709 lcd.drawRect(3,6,77,10,0);//title outline
el13drt 12:eedda6554615 710 lcd.drawRect(0,47,84,0,1);//bottom border
el13drt 12:eedda6554615 711 lcd.drawRect(0,0,84,2,1);//top border
el13drt 12:eedda6554615 712 lcd.printString("Options",20,7);//title
el13drt 12:eedda6554615 713 lcd.printString("Difficulty",3,9);
el13drt 12:eedda6554615 714 lcd.printString("Sound FX",3,10);
el13drt 12:eedda6554615 715 }
el13drt 12:eedda6554615 716
el13drt 12:eedda6554615 717 // present difficulty options
el13drt 12:eedda6554615 718 void difficultyMenu(int& subOption)
el13drt 12:eedda6554615 719 {
el13drt 17:242ccf6a8442 720 actionButtons();
el13drt 12:eedda6554615 721
el13drt 12:eedda6554615 722 // joystick selection
el13drt 12:eedda6554615 723 if (printFlag) {//if flag set, clear flag,print joystick values
el13drt 12:eedda6554615 724 printFlag = 0;
el13drt 12:eedda6554615 725
el13drt 12:eedda6554615 726 // option up
el13drt 12:eedda6554615 727 if (joystick.direction == UP) {
el13drt 12:eedda6554615 728 serial.printf(" UP\n");
el13drt 12:eedda6554615 729 subOption = subOption--;
el13drt 12:eedda6554615 730 if (subOption < 0)subOption = 0;
el13drt 12:eedda6554615 731 }
el13drt 12:eedda6554615 732 // option down
el13drt 12:eedda6554615 733 if (joystick.direction == DOWN) {
el13drt 12:eedda6554615 734 serial.printf(" DOWN\n");
el13drt 12:eedda6554615 735 subOption = subOption++;
el13drt 12:eedda6554615 736 if (subOption > 2)subOption = 2;
el13drt 12:eedda6554615 737 }
el13drt 12:eedda6554615 738 // Centre / Unknown orientation
el13drt 12:eedda6554615 739 if (joystick.direction == CENTRE)
el13drt 12:eedda6554615 740 serial.printf(" CENTRE\n");
el13drt 12:eedda6554615 741 if (joystick.direction == UNKNOWN)
el13drt 12:eedda6554615 742 serial.printf(" Unsupported direction\n");
el13drt 12:eedda6554615 743
el13drt 12:eedda6554615 744 // 'Easy' option 1
el13drt 12:eedda6554615 745 if (subOption == 0) {
el13drt 12:eedda6554615 746 lcd.drawCircle(72,19,2,1);
el13drt 12:eedda6554615 747 refreshCursor2();
el13drt 12:eedda6554615 748 refreshCursor3();
el13drt 18:c38e1966d870 749 if(buttonFlagA) { //select easy
el13drt 18:c38e1966d870 750 buttonFlagA = 0;
el13drt 12:eedda6554615 751 fall = 1;
el13drt 12:eedda6554615 752 }
el13drt 12:eedda6554615 753 }
el13drt 12:eedda6554615 754 // 'Normal' option 2
el13drt 12:eedda6554615 755 if (subOption == 1) {
el13drt 12:eedda6554615 756 lcd.drawCircle(72,27,2,1);
el13drt 12:eedda6554615 757 refreshCursor1();
el13drt 12:eedda6554615 758 refreshCursor3();
el13drt 12:eedda6554615 759
el13drt 18:c38e1966d870 760 if(buttonFlagA) { //select normal
el13drt 18:c38e1966d870 761 buttonFlagA = 0;
el13drt 12:eedda6554615 762 fall = 2;
el13drt 12:eedda6554615 763 }
el13drt 12:eedda6554615 764 }
el13drt 12:eedda6554615 765 // 'Forget It' option 3
el13drt 12:eedda6554615 766 if (subOption == 2) {
el13drt 12:eedda6554615 767 lcd.drawCircle(72,35,2,1);
el13drt 12:eedda6554615 768 refreshCursor1();
el13drt 12:eedda6554615 769 refreshCursor2();
el13drt 12:eedda6554615 770
el13drt 18:c38e1966d870 771 if(buttonFlagA) { //select difficult
el13drt 18:c38e1966d870 772 buttonFlagA = 0;
el13drt 12:eedda6554615 773 fall = 3;
el13drt 12:eedda6554615 774 }
el13drt 12:eedda6554615 775 }
el13drt 12:eedda6554615 776 }
el13drt 12:eedda6554615 777 }
el13drt 12:eedda6554615 778
el13drt 12:eedda6554615 779 // draw difficulty settings
el13drt 12:eedda6554615 780 void drawDifficultyMenu()
el13drt 12:eedda6554615 781 {
el13drt 12:eedda6554615 782 lcd.clear();
el13drt 12:eedda6554615 783 backGround();
el13drt 12:eedda6554615 784 lcd.drawRect(0,47,84,0,1);//bottom border
el13drt 12:eedda6554615 785 lcd.drawRect(0,0,84,2,1);//top border
el13drt 12:eedda6554615 786 lcd.printString("*Difficulty*",5,7);//title
el13drt 12:eedda6554615 787 lcd.printString("Easy",5,8);//title
el13drt 12:eedda6554615 788 lcd.printString("Normal",5,9);//title
el13drt 12:eedda6554615 789 lcd.printString("Forget It",5,10);//title
el13drt 12:eedda6554615 790 }
el13drt 12:eedda6554615 791
el13drt 13:a1b3a373c5a4 792 // present sound FX options
el13drt 13:a1b3a373c5a4 793 void soundFXMenu(int& fxOption)
el13drt 13:a1b3a373c5a4 794 {
el13drt 17:242ccf6a8442 795 actionButtons();
el13drt 13:a1b3a373c5a4 796
el13drt 13:a1b3a373c5a4 797 // joystick selection
el13drt 13:a1b3a373c5a4 798 if (printFlag) {//if flag set, clear flag,print joystick values
el13drt 13:a1b3a373c5a4 799 printFlag = 0;
el13drt 13:a1b3a373c5a4 800
el13drt 13:a1b3a373c5a4 801 // option up
el13drt 13:a1b3a373c5a4 802 if (joystick.direction == UP) {
el13drt 13:a1b3a373c5a4 803 serial.printf(" UP\n");
el13drt 13:a1b3a373c5a4 804 fxOption = fxOption--;
el13drt 13:a1b3a373c5a4 805 if (fxOption < 0)fxOption = 0;
el13drt 13:a1b3a373c5a4 806 }
el13drt 13:a1b3a373c5a4 807 // option down
el13drt 13:a1b3a373c5a4 808 if (joystick.direction == DOWN) {
el13drt 13:a1b3a373c5a4 809 serial.printf(" DOWN\n");
el13drt 13:a1b3a373c5a4 810 fxOption = fxOption++;
el13drt 20:993c12c6a7bc 811 if (fxOption > 1)fxOption = 1;
el13drt 13:a1b3a373c5a4 812 }
el13drt 13:a1b3a373c5a4 813 // Centre / Unknown orientation
el13drt 13:a1b3a373c5a4 814 if (joystick.direction == CENTRE)
el13drt 13:a1b3a373c5a4 815 serial.printf(" CENTRE\n");
el13drt 13:a1b3a373c5a4 816 if (joystick.direction == UNKNOWN)
el13drt 13:a1b3a373c5a4 817 serial.printf(" Unsupported direction\n");
el13drt 17:242ccf6a8442 818 }
el13drt 13:a1b3a373c5a4 819
el13drt 17:242ccf6a8442 820 // 'ON' option 1
el13drt 17:242ccf6a8442 821 if (fxOption == 0) {
el13drt 17:242ccf6a8442 822 lcd.drawCircle(72,27,2,1);//draw cursor 'ON'
el13drt 17:242ccf6a8442 823 refreshCursor1();
el13drt 17:242ccf6a8442 824 refreshCursor3();
el13drt 13:a1b3a373c5a4 825
el13drt 17:242ccf6a8442 826 if(buttonFlagA) {
el13drt 17:242ccf6a8442 827 buttonFlagA =0;
el13drt 17:242ccf6a8442 828 FX = 0;
el13drt 19:e0fd493816ae 829 serial.printf("FX = %d\n",FX);
el13drt 13:a1b3a373c5a4 830 }
el13drt 17:242ccf6a8442 831 }
el13drt 17:242ccf6a8442 832 // 'OFF' option 2
el13drt 17:242ccf6a8442 833 if (fxOption == 1) {
el13drt 17:242ccf6a8442 834 lcd.drawCircle(72,35,2,1);//draw cursor 'OFF'
el13drt 17:242ccf6a8442 835 refreshCursor1();
el13drt 17:242ccf6a8442 836 refreshCursor2();
el13drt 13:a1b3a373c5a4 837
el13drt 17:242ccf6a8442 838 if(buttonFlagA) {
el13drt 17:242ccf6a8442 839 buttonFlagA =0;
el13drt 17:242ccf6a8442 840 FX = 1;
el13drt 19:e0fd493816ae 841 serial.printf("FX = %d\n",FX);
el13drt 13:a1b3a373c5a4 842 }
el13drt 13:a1b3a373c5a4 843 }
el13drt 13:a1b3a373c5a4 844 }
el13drt 13:a1b3a373c5a4 845
el13drt 13:a1b3a373c5a4 846 // draw Sound FX settings
el13drt 13:a1b3a373c5a4 847 void drawSoundFXMenu()
el13drt 13:a1b3a373c5a4 848 {
el13drt 13:a1b3a373c5a4 849 lcd.clear();
el13drt 13:a1b3a373c5a4 850 backGround();
el13drt 13:a1b3a373c5a4 851 lcd.drawRect(0,47,84,0,1);//bottom border
el13drt 13:a1b3a373c5a4 852 lcd.drawRect(0,0,84,2,1);//top border
el13drt 13:a1b3a373c5a4 853 lcd.printString("*Sound FX*",10,7);//title
el13drt 13:a1b3a373c5a4 854 lcd.printString("ON",35,9);//title
el13drt 13:a1b3a373c5a4 855 lcd.printString("OFF",33,10);//title
el13drt 13:a1b3a373c5a4 856 }
el13drt 13:a1b3a373c5a4 857
el13drt 21:1fbbd8ebb3d9 858 // if any of the high scores are beaten, they are replaced.
el13drt 24:eb80956e2e95 859 // player enters initials using struct
el13drt 21:1fbbd8ebb3d9 860 void newScore()
el13drt 22:e5bfe0e7c508 861 {
el13drt 21:1fbbd8ebb3d9 862 if(score >= highScore3) {
el13drt 22:e5bfe0e7c508 863
el13drt 22:e5bfe0e7c508 864 lcd.clear();
el13drt 22:e5bfe0e7c508 865 backGround();
el13drt 23:548299d45a60 866 lcd.printString("High Score!!",7,0);//title
el13drt 23:548299d45a60 867 lcd.printString("Enter ID",19,4);//title
el13drt 22:e5bfe0e7c508 868 int n;//local variable used for storing temporary global variable
el13drt 24:eb80956e2e95 869 int initial = 0;
el13drt 22:e5bfe0e7c508 870
el13drt 22:e5bfe0e7c508 871 // if player beats High Score 3, replace it with new score
el13drt 22:e5bfe0e7c508 872 if(score >= highScore3) {
el13drt 22:e5bfe0e7c508 873 n = score;
el13drt 22:e5bfe0e7c508 874 highScore3 = n;
el13drt 22:e5bfe0e7c508 875 }
el13drt 22:e5bfe0e7c508 876 // if player beats High Score 3 and 2, replace HighScore2 with new score
el13drt 22:e5bfe0e7c508 877 if(score >= highScore2) {
el13drt 22:e5bfe0e7c508 878 highScore3 = highScore2;
el13drt 22:e5bfe0e7c508 879 n = score;
el13drt 22:e5bfe0e7c508 880 highScore2 = n;
el13drt 22:e5bfe0e7c508 881 }
el13drt 22:e5bfe0e7c508 882 // if player beats High Score 1, 2 and 3, replace highScore1 with new score
el13drt 22:e5bfe0e7c508 883 if(score >= highScore1) {
el13drt 21:1fbbd8ebb3d9 884
el13drt 22:e5bfe0e7c508 885 highScore3 = highScore2;
el13drt 22:e5bfe0e7c508 886 highScore2 = highScore1;
el13drt 22:e5bfe0e7c508 887 n = score;
el13drt 22:e5bfe0e7c508 888 highScore1 = n;
el13drt 22:e5bfe0e7c508 889 }
el13drt 22:e5bfe0e7c508 890 while(1) {
el13drt 24:eb80956e2e95 891
el13drt 22:e5bfe0e7c508 892 actionButtons();
el13drt 24:eb80956e2e95 893
el13drt 22:e5bfe0e7c508 894 // joystick selection
el13drt 22:e5bfe0e7c508 895 if (printFlag) {//if flag set, clear flag,print joystick values
el13drt 22:e5bfe0e7c508 896 printFlag = 0;
el13drt 22:e5bfe0e7c508 897
el13drt 22:e5bfe0e7c508 898 // option up
el13drt 22:e5bfe0e7c508 899 if (joystick.direction == UP) {
el13drt 22:e5bfe0e7c508 900 serial.printf(" UP\n");
el13drt 23:548299d45a60 901 state = state--;
el13drt 23:548299d45a60 902 if (state < 0)state = 0;
el13drt 22:e5bfe0e7c508 903 }
el13drt 22:e5bfe0e7c508 904 // option down
el13drt 22:e5bfe0e7c508 905 if (joystick.direction == DOWN) {
el13drt 22:e5bfe0e7c508 906 serial.printf(" DOWN\n");
el13drt 23:548299d45a60 907 state = state++;
el13drt 23:548299d45a60 908 if (state > 26)state = 26;
el13drt 22:e5bfe0e7c508 909 }
el13drt 24:eb80956e2e95 910
el13drt 24:eb80956e2e95 911 if (joystick.direction == LEFT) {
el13drt 24:eb80956e2e95 912 serial.printf(" LEFT\n");
el13drt 24:eb80956e2e95 913 initial = initial--;
el13drt 24:eb80956e2e95 914 if (initial < 0)initial = 0;
el13drt 24:eb80956e2e95 915
el13drt 24:eb80956e2e95 916 }
el13drt 24:eb80956e2e95 917 if (joystick.direction == RIGHT) {
el13drt 24:eb80956e2e95 918 serial.printf(" RIGHT\n");
el13drt 24:eb80956e2e95 919 initial = initial++;
el13drt 24:eb80956e2e95 920 if (initial > 2)initial = 2;
el13drt 24:eb80956e2e95 921 }
el13drt 22:e5bfe0e7c508 922 // Centre / Unknown orientation
el13drt 22:e5bfe0e7c508 923 if (joystick.direction == CENTRE)
el13drt 22:e5bfe0e7c508 924 serial.printf(" CENTRE\n");
el13drt 22:e5bfe0e7c508 925 if (joystick.direction == UNKNOWN)
el13drt 22:e5bfe0e7c508 926 serial.printf(" Unsupported direction\n");
el13drt 22:e5bfe0e7c508 927 }
el13drt 23:548299d45a60 928
el13drt 24:eb80956e2e95 929 char buffer1[14];//create buffer for strings
el13drt 24:eb80956e2e95 930 char buffer2[14];
el13drt 24:eb80956e2e95 931 char buffer3[14];
el13drt 23:548299d45a60 932
el13drt 24:eb80956e2e95 933 if (initial == 0) {
el13drt 24:eb80956e2e95 934 int initial1 = sprintf(buffer1,"%c",fsm[state].output);//insert scores}
el13drt 24:eb80956e2e95 935 if (initial1 <= 14) //ensure length is smaller than screen
el13drt 24:eb80956e2e95 936 lcd.printString(buffer1,25,26);//display
el13drt 24:eb80956e2e95 937 }
el13drt 24:eb80956e2e95 938 if(initial == 1) {
el13drt 24:eb80956e2e95 939 int initial2 = sprintf(buffer2,"%c",fsm[state].output);//into buffers
el13drt 24:eb80956e2e95 940 if (initial2 <= 14) //ensure length is smaller than screen
el13drt 24:eb80956e2e95 941 lcd.printString(buffer2,37,26);//display
el13drt 24:eb80956e2e95 942 }
el13drt 24:eb80956e2e95 943 if(initial == 2) {
el13drt 24:eb80956e2e95 944 int initial3 = sprintf(buffer3,"%c",fsm[state].output);
el13drt 24:eb80956e2e95 945 if (initial3 <= 14) //ensure length is smaller than screen
el13drt 24:eb80956e2e95 946 lcd.printString(buffer3,49,26);//display
el13drt 24:eb80956e2e95 947 }
el13drt 24:eb80956e2e95 948 //inwhile
el13drt 23:548299d45a60 949
el13drt 22:e5bfe0e7c508 950 }
el13drt 21:1fbbd8ebb3d9 951 }
el13drt 21:1fbbd8ebb3d9 952 }
el13drt 21:1fbbd8ebb3d9 953
el13drt 12:eedda6554615 954 // actual game
el13drt 12:eedda6554615 955 void game(int& exitFlag, int& exitOption)
el13drt 12:eedda6554615 956 {
el13drt 17:242ccf6a8442 957 actionButtons();
el13drt 12:eedda6554615 958 lcd.clear();//clears screen
el13drt 12:eedda6554615 959 backGround();//draw background
el13drt 12:eedda6554615 960
el13drt 12:eedda6554615 961 ///game///
el13drt 12:eedda6554615 962 while(1) {
el13drt 12:eedda6554615 963 // print score - top left of display
el13drt 12:eedda6554615 964 char buffer[14];//create buffer for string
el13drt 12:eedda6554615 965 int length = sprintf(buffer,"Level:%d",score);//insert buffer
el13drt 12:eedda6554615 966 if (length <= 14) //ensure length is smaller than screen
el13drt 12:eedda6554615 967 lcd.printString(buffer,3,0);//display
el13drt 12:eedda6554615 968
el13drt 17:242ccf6a8442 969 actionButtons();
el13drt 12:eedda6554615 970 pixelNinja();//set character
el13drt 12:eedda6554615 971 hazards();//initiates hazards
el13drt 12:eedda6554615 972 hazardFall();//increments hazards towards floor
el13drt 12:eedda6554615 973
el13drt 12:eedda6554615 974 if (printFlag) { //if flag set, clear flag and print joystick values to serial port
el13drt 12:eedda6554615 975 printFlag = 0;
el13drt 12:eedda6554615 976
el13drt 12:eedda6554615 977 // check joystick direction
el13drt 12:eedda6554615 978 if (joystick.direction == LEFT) {
el13drt 12:eedda6554615 979 serial.printf(" LEFT\n");
el13drt 12:eedda6554615 980 a1 = a1-=2;
el13drt 12:eedda6554615 981 a2 = a2-=2;
el13drt 12:eedda6554615 982 a3 = a3-=2;
el13drt 12:eedda6554615 983 a4 = a4-=2;
el13drt 12:eedda6554615 984 a5 = a5-=2;
el13drt 12:eedda6554615 985 a6 = a6-=2;
el13drt 12:eedda6554615 986 a7 = a7-=2;
el13drt 12:eedda6554615 987 a8 = a8-=2;
el13drt 12:eedda6554615 988 ninjaBoundaries();
el13drt 12:eedda6554615 989 }
el13drt 12:eedda6554615 990 if (joystick.direction == RIGHT) {
el13drt 12:eedda6554615 991 serial.printf(" RIGHT\n");
el13drt 12:eedda6554615 992 a1 = a1+=2;
el13drt 12:eedda6554615 993 a2 = a2+=2;
el13drt 12:eedda6554615 994 a3 = a3+=2;
el13drt 12:eedda6554615 995 a4 = a4+=2;
el13drt 12:eedda6554615 996 a5 = a5+=2;
el13drt 12:eedda6554615 997 a6 = a6+=2;
el13drt 12:eedda6554615 998 a7 = a7+=2;
el13drt 12:eedda6554615 999 a8 = a8+=2;
el13drt 12:eedda6554615 1000 ninjaBoundaries();
el13drt 12:eedda6554615 1001 }
el13drt 22:e5bfe0e7c508 1002
el13drt 21:1fbbd8ebb3d9 1003 /////////////////////readWrite();
el13drt 22:e5bfe0e7c508 1004
el13drt 12:eedda6554615 1005 if (joystick.direction == CENTRE)
el13drt 12:eedda6554615 1006 serial.printf(" CENTRE\n");
el13drt 12:eedda6554615 1007 if (joystick.direction == UNKNOWN)
el13drt 12:eedda6554615 1008 serial.printf(" Unsupported direction\n");
el13drt 22:e5bfe0e7c508 1009
el13drt 12:eedda6554615 1010 // integer to represent character being
el13drt 12:eedda6554615 1011 // struck by falling object
el13drt 12:eedda6554615 1012 int contactPoint = 0;
el13drt 12:eedda6554615 1013
el13drt 12:eedda6554615 1014 // contact points
el13drt 12:eedda6554615 1015 if(lcd.getPixel((a1+4),32))
el13drt 12:eedda6554615 1016 contactPoint++;
el13drt 12:eedda6554615 1017 if(lcd.getPixel((a1),32))
el13drt 12:eedda6554615 1018 contactPoint++;
el13drt 12:eedda6554615 1019 if(lcd.getPixel((a7),32))
el13drt 12:eedda6554615 1020 contactPoint++;
el13drt 12:eedda6554615 1021
el13drt 12:eedda6554615 1022 // if contact point is not zero
el13drt 12:eedda6554615 1023 // character has been hit
el13drt 12:eedda6554615 1024 // and the game ends
el13drt 12:eedda6554615 1025 if ( contactPoint !=0) {
el13drt 22:e5bfe0e7c508 1026 //newScore();
el13drt 12:eedda6554615 1027 lcd.printString("Game Over",17,2);
el13drt 12:eedda6554615 1028 lcd.inverseMode();
el13drt 13:a1b3a373c5a4 1029 buzzer.beep(2000,0.2);//frequeny/duration
el13drt 12:eedda6554615 1030 wait(0.5);
el13drt 12:eedda6554615 1031 lcd.normalMode();
el13drt 12:eedda6554615 1032 wait(0.5);
el13drt 12:eedda6554615 1033 lcd.inverseMode();
el13drt 13:a1b3a373c5a4 1034 buzzer.beep(2000,0.2);
el13drt 12:eedda6554615 1035 wait(0.5);
el13drt 12:eedda6554615 1036 lcd.normalMode();
el13drt 12:eedda6554615 1037 wait(0.5);
el13drt 13:a1b3a373c5a4 1038 lcd.inverseMode();
el13drt 13:a1b3a373c5a4 1039 buzzer.beep(2000,0.2);
el13drt 13:a1b3a373c5a4 1040 wait(0.5);
el13drt 13:a1b3a373c5a4 1041 lcd.normalMode();
el13drt 22:e5bfe0e7c508 1042 newScore();
el13drt 12:eedda6554615 1043 resetGame();
el13drt 12:eedda6554615 1044 break;
el13drt 12:eedda6554615 1045 }
el13drt 12:eedda6554615 1046 startrek();//clears unset pixels, keeps set pixels
el13drt 17:242ccf6a8442 1047
el13drt 12:eedda6554615 1048
el13drt 12:eedda6554615 1049 /// Exit Menu (Back button pressed)///
el13drt 18:c38e1966d870 1050 if(buttonFlagB) {
el13drt 20:993c12c6a7bc 1051 buttonFlagB = 0;//reset flags
el13drt 20:993c12c6a7bc 1052 buttonFlagA = 0;
el13drt 17:242ccf6a8442 1053 actionButtons();
el13drt 12:eedda6554615 1054 drawExitMenu();//draws the exit menu
el13drt 12:eedda6554615 1055
el13drt 12:eedda6554615 1056 while(1) {
el13drt 12:eedda6554615 1057 exitMenu(exitOption);//presents exit options
el13drt 12:eedda6554615 1058
el13drt 12:eedda6554615 1059 // 'exit' option YES
el13drt 18:c38e1966d870 1060 if((buttonFlagA)&&(exitOption == 0)) { //returns to menu
el13drt 20:993c12c6a7bc 1061 buttonFlagA = 0;//reset flags
el13drt 20:993c12c6a7bc 1062 buttonFlagB = 0;
el13drt 17:242ccf6a8442 1063 actionButtons();
el13drt 12:eedda6554615 1064 lcd.clear();//clears screen
el13drt 12:eedda6554615 1065 resetGame();//resets scores/objects
el13drt 12:eedda6554615 1066 exitFlag = 1;//sets exit flag
el13drt 12:eedda6554615 1067 break;
el13drt 12:eedda6554615 1068 }
el13drt 12:eedda6554615 1069 // 'exit' option NO - returns to game
el13drt 18:c38e1966d870 1070 if((buttonFlagA)&&(exitOption == 1)) {
el13drt 20:993c12c6a7bc 1071 buttonFlagA = 0;//resets flags
el13drt 20:993c12c6a7bc 1072 buttonFlagB = 0;
el13drt 12:eedda6554615 1073 break;
el13drt 12:eedda6554615 1074 }
el13drt 18:c38e1966d870 1075 sleep();//put while to sleep
el13drt 12:eedda6554615 1076 }
el13drt 12:eedda6554615 1077 // if 'exit' option YES, resets
el13drt 12:eedda6554615 1078 // game values returns to main menu
el13drt 12:eedda6554615 1079 if (exitFlag!=0) { //if exit flag set
el13drt 12:eedda6554615 1080 exitFlag = 0;//reset flag
el13drt 12:eedda6554615 1081 break;//break to main menu
el13drt 12:eedda6554615 1082 }
el13drt 12:eedda6554615 1083 }
el13drt 12:eedda6554615 1084 serial.printf("Score: %i \n",score);
el13drt 12:eedda6554615 1085 }
el13drt 12:eedda6554615 1086 }
el13drt 12:eedda6554615 1087 }
el13drt 12:eedda6554615 1088
el13drt 12:eedda6554615 1089 // high scores screen
el13drt 12:eedda6554615 1090 void scores()
el13drt 12:eedda6554615 1091 {
el13drt 17:242ccf6a8442 1092 actionButtons();
el13drt 12:eedda6554615 1093 lcd.clear();//clear screen
el13drt 12:eedda6554615 1094 backGround();//set background
el13drt 12:eedda6554615 1095 lcd.printString("High Scores",10,0);//title
el13drt 24:eb80956e2e95 1096
el13drt 12:eedda6554615 1097 // players high scores - highest first
el13drt 21:1fbbd8ebb3d9 1098 char buffer1[14];//create buffer for strings
el13drt 21:1fbbd8ebb3d9 1099 char buffer2[14];
el13drt 21:1fbbd8ebb3d9 1100 char buffer3[14];
el13drt 24:eb80956e2e95 1101
el13drt 24:eb80956e2e95 1102 char name1 = 'D';
el13drt 12:eedda6554615 1103
el13drt 24:eb80956e2e95 1104 int player1 = sprintf(buffer1,"1.%c.....%i",name1,highScore1);//insert scores
el13drt 21:1fbbd8ebb3d9 1105 int player2 = sprintf(buffer2,"2.NRG.....%i",highScore2);//into buffers
el13drt 21:1fbbd8ebb3d9 1106 int player3 = sprintf(buffer3,"3.GRT.....%i",highScore3);
el13drt 12:eedda6554615 1107
el13drt 12:eedda6554615 1108 if (player1 <= 14) //ensure length is smaller than screen
el13drt 21:1fbbd8ebb3d9 1109 lcd.printString(buffer1,5,2);//display
el13drt 12:eedda6554615 1110 if (player2 <= 14) //ensure length is smaller than screen
el13drt 21:1fbbd8ebb3d9 1111 lcd.printString(buffer2,5,3);//display
el13drt 12:eedda6554615 1112 if (player3 <= 14) //ensure length is smaller than screen
el13drt 21:1fbbd8ebb3d9 1113 lcd.printString(buffer3,5,4);//display
el13drt 12:eedda6554615 1114
el13drt 12:eedda6554615 1115 while(1) {
el13drt 17:242ccf6a8442 1116 actionButtons();//select
el13drt 12:eedda6554615 1117
el13drt 12:eedda6554615 1118 // back to menu
el13drt 18:c38e1966d870 1119 if(buttonFlagB) {
el13drt 18:c38e1966d870 1120 buttonFlagB = 0;//reset flag
el13drt 12:eedda6554615 1121 lcd.clear();
el13drt 12:eedda6554615 1122 break;
el13drt 12:eedda6554615 1123 }
el13drt 18:c38e1966d870 1124 sleep();//put while to sleep
el13drt 12:eedda6554615 1125 }
el13drt 13:a1b3a373c5a4 1126 }
el13drt 13:a1b3a373c5a4 1127
el13drt 13:a1b3a373c5a4 1128 // options menu
el13drt 14:c2c969e1c6e8 1129 void optionsMenu()
el13drt 13:a1b3a373c5a4 1130 {
el13drt 14:c2c969e1c6e8 1131 int option = 0;
el13drt 14:c2c969e1c6e8 1132 int subOption = 0;
el13drt 14:c2c969e1c6e8 1133 int fxOption = 0;
el13drt 17:242ccf6a8442 1134 actionButtons();
el13drt 15:ff3eb0091453 1135 drawOptionsMenu();//draws options menu
el13drt 15:ff3eb0091453 1136 // counters for navigation
el13drt 17:242ccf6a8442 1137
el13drt 13:a1b3a373c5a4 1138 while(1) {
el13drt 17:242ccf6a8442 1139 actionButtons();
el13drt 13:a1b3a373c5a4 1140 optionsMenu(option);//presents options
el13drt 13:a1b3a373c5a4 1141
el13drt 13:a1b3a373c5a4 1142 ////////////////////// difficulty menu ////////////////////////////////////
el13drt 15:ff3eb0091453 1143 if ((option == 0)&&(buttonFlagA)) {
el13drt 15:ff3eb0091453 1144 buttonFlagA = 0;//reset flag
el13drt 17:242ccf6a8442 1145 actionButtons();
el13drt 13:a1b3a373c5a4 1146 drawDifficultyMenu();//draws difficulty menu
el13drt 13:a1b3a373c5a4 1147
el13drt 13:a1b3a373c5a4 1148 while(1) {
el13drt 17:242ccf6a8442 1149 actionButtons();
el13drt 13:a1b3a373c5a4 1150 difficultyMenu(subOption);//presents difficulty options
el13drt 13:a1b3a373c5a4 1151
el13drt 15:ff3eb0091453 1152 if(buttonFlagB) {
el13drt 20:993c12c6a7bc 1153 buttonFlagB = 0;//reset flags
el13drt 20:993c12c6a7bc 1154 buttonFlagA = 0;
el13drt 15:ff3eb0091453 1155 lcd.clear();//clear screen
el13drt 15:ff3eb0091453 1156 break;//return back
el13drt 13:a1b3a373c5a4 1157 }
el13drt 17:242ccf6a8442 1158 sleep();//put while to sleep
el13drt 13:a1b3a373c5a4 1159 }
el13drt 17:242ccf6a8442 1160 drawOptionsMenu();
el13drt 13:a1b3a373c5a4 1161 }
el13drt 13:a1b3a373c5a4 1162 ///////////////////// sound FX menu //////////////////////////////////////
el13drt 15:ff3eb0091453 1163 if((option ==1)&&(buttonFlagA)) {
el13drt 20:993c12c6a7bc 1164 buttonFlagA = 0;//reset flags
el13drt 20:993c12c6a7bc 1165 buttonFlagB = 0;
el13drt 17:242ccf6a8442 1166 actionButtons();
el13drt 13:a1b3a373c5a4 1167 drawSoundFXMenu();//draw menu
el13drt 13:a1b3a373c5a4 1168
el13drt 13:a1b3a373c5a4 1169 while(1) {
el13drt 17:242ccf6a8442 1170 actionButtons();
el13drt 17:242ccf6a8442 1171 soundFXMenu(fxOption);//presents options
el13drt 13:a1b3a373c5a4 1172
el13drt 13:a1b3a373c5a4 1173 // back to options menu
el13drt 15:ff3eb0091453 1174 if(buttonFlagB) {
el13drt 20:993c12c6a7bc 1175 buttonFlagB = 0;//reset flags
el13drt 20:993c12c6a7bc 1176 buttonFlagA = 0;
el13drt 15:ff3eb0091453 1177 lcd.clear();//clear screen
el13drt 15:ff3eb0091453 1178 break;//return back
el13drt 13:a1b3a373c5a4 1179 }
el13drt 17:242ccf6a8442 1180 sleep();//put while to sleep
el13drt 13:a1b3a373c5a4 1181 }
el13drt 15:ff3eb0091453 1182 drawOptionsMenu();
el13drt 13:a1b3a373c5a4 1183 }
el13drt 13:a1b3a373c5a4 1184 // back to mainmenu
el13drt 15:ff3eb0091453 1185 if(buttonFlagB) {
el13drt 20:993c12c6a7bc 1186 buttonFlagB = 0;//reset flags
el13drt 20:993c12c6a7bc 1187 buttonFlagA = 0;
el13drt 15:ff3eb0091453 1188 lcd.clear();//clear
el13drt 15:ff3eb0091453 1189 break;//return back
el13drt 13:a1b3a373c5a4 1190 }
el13drt 22:e5bfe0e7c508 1191 sleep();//put while to sleep
el13drt 13:a1b3a373c5a4 1192 }
el13drt 13:a1b3a373c5a4 1193 }
el13drt 13:a1b3a373c5a4 1194
el13drt 13:a1b3a373c5a4 1195 // read default positions of the joystick to calibrate later readings
el13drt 13:a1b3a373c5a4 1196 void calibrateJoystick()
el13drt 13:a1b3a373c5a4 1197 {
el13drt 14:c2c969e1c6e8 1198 joyButton.mode(PullDown);
el13drt 13:a1b3a373c5a4 1199 // must not move during calibration
el13drt 13:a1b3a373c5a4 1200 joystick.x0 = xPot; //initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el13drt 13:a1b3a373c5a4 1201 joystick.y0 = yPot;
el13drt 13:a1b3a373c5a4 1202 }
el13drt 13:a1b3a373c5a4 1203
el13drt 13:a1b3a373c5a4 1204 void updateJoystick()
el13drt 13:a1b3a373c5a4 1205 {
el13drt 13:a1b3a373c5a4 1206 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el13drt 13:a1b3a373c5a4 1207 joystick.x = xPot - joystick.x0;
el13drt 13:a1b3a373c5a4 1208 joystick.y = yPot - joystick.y0;
el13drt 13:a1b3a373c5a4 1209 // read button state
el13drt 14:c2c969e1c6e8 1210 joystick.button = joyButton;
el13drt 13:a1b3a373c5a4 1211
el13drt 13:a1b3a373c5a4 1212 // calculate direction depending on x,y values
el13drt 13:a1b3a373c5a4 1213 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el13drt 13:a1b3a373c5a4 1214 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el13drt 13:a1b3a373c5a4 1215 joystick.direction = CENTRE;
el13drt 13:a1b3a373c5a4 1216 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el13drt 13:a1b3a373c5a4 1217 joystick.direction = UP;
el13drt 13:a1b3a373c5a4 1218 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el13drt 13:a1b3a373c5a4 1219 joystick.direction = DOWN;
el13drt 13:a1b3a373c5a4 1220 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el13drt 13:a1b3a373c5a4 1221 joystick.direction = RIGHT;
el13drt 13:a1b3a373c5a4 1222 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el13drt 13:a1b3a373c5a4 1223 joystick.direction = LEFT;
el13drt 13:a1b3a373c5a4 1224 } else {
el13drt 13:a1b3a373c5a4 1225 joystick.direction = UNKNOWN;
el13drt 13:a1b3a373c5a4 1226 }
el13drt 13:a1b3a373c5a4 1227
el13drt 13:a1b3a373c5a4 1228 // set flag for printing
el13drt 13:a1b3a373c5a4 1229 printFlag = 1;
el13drt 12:eedda6554615 1230 }