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