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 19:12:48 2015 +0000
Revision:
27:c50111a3b46b
Parent:
26:83e181f04c9a
Child:
28:d1bd8a91177e
20:12

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