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