A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.
Dependencies: ConfigFile N5110 PowerControl beep mbed
tower.h@14:c2c969e1c6e8, 2015-04-17 (annotated)
- Committer:
- el13drt
- Date:
- Fri Apr 17 16:12:40 2015 +0000
- Revision:
- 14:c2c969e1c6e8
- Parent:
- 13:a1b3a373c5a4
- Child:
- 15:ff3eb0091453
pre sleep
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 | 13:a1b3a373c5a4 | 8 | // create buzzer objecct |
el13drt | 13:a1b3a373c5a4 | 9 | Beep buzzer(p21); |
el13drt | 13:a1b3a373c5a4 | 10 | |
el13drt | 13:a1b3a373c5a4 | 11 | // create local file system |
el13drt | 13:a1b3a373c5a4 | 12 | //LocalFileSytem local("local"); |
el13drt | 13:a1b3a373c5a4 | 13 | |
el13drt | 13:a1b3a373c5a4 | 14 | // navigation/action buttons |
el13drt | 13:a1b3a373c5a4 | 15 | DigitalIn buttonA(p19); |
el13drt | 13:a1b3a373c5a4 | 16 | DigitalIn buttonB(p20); |
el13drt | 13:a1b3a373c5a4 | 17 | |
el13drt | 13:a1b3a373c5a4 | 18 | // LED indicators |
el13drt | 13:a1b3a373c5a4 | 19 | AnalogOut ledR(p18);// RED LED |
el13drt | 13:a1b3a373c5a4 | 20 | DigitalOut ledY(p24);// YELLOW LED |
el13drt | 13:a1b3a373c5a4 | 21 | |
el13drt | 13:a1b3a373c5a4 | 22 | // connections for joystick |
el13drt | 14:c2c969e1c6e8 | 23 | InterruptIn joyButton(p17);//Interrupt for ISR |
el13drt | 14:c2c969e1c6e8 | 24 | |
el13drt | 13:a1b3a373c5a4 | 25 | AnalogIn xPot(p15); |
el13drt | 13:a1b3a373c5a4 | 26 | AnalogIn yPot(p16); |
el13drt | 13:a1b3a373c5a4 | 27 | |
el13drt | 12:eedda6554615 | 28 | // Globabl Variables ///////////////////////// |
el13drt | 12:eedda6554615 | 29 | |
el13drt | 14:c2c969e1c6e8 | 30 | // flag for joystick reading |
el13drt | 14:c2c969e1c6e8 | 31 | int printFlag = 0; |
el13drt | 14:c2c969e1c6e8 | 32 | |
el13drt | 12:eedda6554615 | 33 | // boundary conditions |
el13drt | 12:eedda6554615 | 34 | int cells [84][48]; |
el13drt | 12:eedda6554615 | 35 | |
el13drt | 12:eedda6554615 | 36 | // player's score |
el13drt | 12:eedda6554615 | 37 | int score = 0; |
el13drt | 12:eedda6554615 | 38 | |
el13drt | 12:eedda6554615 | 39 | // difficulty - number of pixels hazards incrememnt by |
el13drt | 12:eedda6554615 | 40 | int fall = 2; |
el13drt | 12:eedda6554615 | 41 | |
el13drt | 12:eedda6554615 | 42 | // global variables for movement (pixelNinja) |
el13drt | 12:eedda6554615 | 43 | int a1 = 22; |
el13drt | 12:eedda6554615 | 44 | int a2 = 24; |
el13drt | 12:eedda6554615 | 45 | int a3 = 23; |
el13drt | 12:eedda6554615 | 46 | int a4 = 25; |
el13drt | 12:eedda6554615 | 47 | int a5 = 20; |
el13drt | 12:eedda6554615 | 48 | int a6 = 26; |
el13drt | 12:eedda6554615 | 49 | int a7 = 19; |
el13drt | 12:eedda6554615 | 50 | int a8 = 21; |
el13drt | 12:eedda6554615 | 51 | |
el13drt | 12:eedda6554615 | 52 | //global variable for random X co-ordinates |
el13drt | 12:eedda6554615 | 53 | int randX1; |
el13drt | 12:eedda6554615 | 54 | int randX2; |
el13drt | 12:eedda6554615 | 55 | int randX3; |
el13drt | 12:eedda6554615 | 56 | int randX4; |
el13drt | 12:eedda6554615 | 57 | int randX5; |
el13drt | 12:eedda6554615 | 58 | int randX6; |
el13drt | 12:eedda6554615 | 59 | |
el13drt | 12:eedda6554615 | 60 | // global variable for Y co-ordinates |
el13drt | 12:eedda6554615 | 61 | int randY1 = 0; |
el13drt | 12:eedda6554615 | 62 | int randY2 = 0; |
el13drt | 12:eedda6554615 | 63 | int randY3 = 0; |
el13drt | 12:eedda6554615 | 64 | int randY4 = 0; |
el13drt | 12:eedda6554615 | 65 | int randY5 = 0; |
el13drt | 12:eedda6554615 | 66 | int randY6 = 0; |
el13drt | 12:eedda6554615 | 67 | |
el13drt | 13:a1b3a373c5a4 | 68 | // function prototypes |
el13drt | 13:a1b3a373c5a4 | 69 | void calibrateJoystick(); |
el13drt | 13:a1b3a373c5a4 | 70 | void updateJoystick(); |
el13drt | 13:a1b3a373c5a4 | 71 | |
el13drt | 13:a1b3a373c5a4 | 72 | // timer to regularly read the joystick |
el13drt | 13:a1b3a373c5a4 | 73 | Ticker pollJoystick; |
el13drt | 13:a1b3a373c5a4 | 74 | |
el13drt | 13:a1b3a373c5a4 | 75 | // serial for debug |
el13drt | 13:a1b3a373c5a4 | 76 | Serial serial(USBTX,USBRX); |
el13drt | 13:a1b3a373c5a4 | 77 | |
el13drt | 13:a1b3a373c5a4 | 78 | // create enumerated type (0,1,2,3 etc. for direction) |
el13drt | 13:a1b3a373c5a4 | 79 | // could be extended for diagonals etc. |
el13drt | 13:a1b3a373c5a4 | 80 | enum DirectionName { |
el13drt | 13:a1b3a373c5a4 | 81 | UP, |
el13drt | 13:a1b3a373c5a4 | 82 | DOWN, |
el13drt | 13:a1b3a373c5a4 | 83 | LEFT, |
el13drt | 13:a1b3a373c5a4 | 84 | RIGHT, |
el13drt | 13:a1b3a373c5a4 | 85 | CENTRE, |
el13drt | 13:a1b3a373c5a4 | 86 | UNKNOWN |
el13drt | 13:a1b3a373c5a4 | 87 | }; |
el13drt | 13:a1b3a373c5a4 | 88 | |
el13drt | 13:a1b3a373c5a4 | 89 | // struct for Joystick |
el13drt | 13:a1b3a373c5a4 | 90 | typedef struct JoyStick Joystick; |
el13drt | 13:a1b3a373c5a4 | 91 | struct JoyStick { |
el13drt | 13:a1b3a373c5a4 | 92 | float x; // current x value |
el13drt | 13:a1b3a373c5a4 | 93 | float x0; // 'centred' x value |
el13drt | 13:a1b3a373c5a4 | 94 | float y; // current y value |
el13drt | 13:a1b3a373c5a4 | 95 | float y0; // 'centred' y value |
el13drt | 13:a1b3a373c5a4 | 96 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el13drt | 13:a1b3a373c5a4 | 97 | DirectionName direction; // current direction |
el13drt | 13:a1b3a373c5a4 | 98 | }; |
el13drt | 13:a1b3a373c5a4 | 99 | |
el13drt | 13:a1b3a373c5a4 | 100 | // create struct variable |
el13drt | 13:a1b3a373c5a4 | 101 | Joystick joystick; |
el13drt | 13:a1b3a373c5a4 | 102 | |
el13drt | 13:a1b3a373c5a4 | 103 | // set seed/randomise initial co-Ordinates |
el13drt | 13:a1b3a373c5a4 | 104 | void randomise() |
el13drt | 13:a1b3a373c5a4 | 105 | { |
el13drt | 13:a1b3a373c5a4 | 106 | srand (time(NULL));//initial seed for randomisation |
el13drt | 13:a1b3a373c5a4 | 107 | |
el13drt | 13:a1b3a373c5a4 | 108 | // initial random x co-ordinates |
el13drt | 13:a1b3a373c5a4 | 109 | // for falling hazards |
el13drt | 13:a1b3a373c5a4 | 110 | // (values between 3 and 76) |
el13drt | 13:a1b3a373c5a4 | 111 | randX1 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 112 | randX2 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 113 | randX3 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 114 | randX4 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 115 | randX5 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 116 | randX6 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 117 | } |
el13drt | 13:a1b3a373c5a4 | 118 | |
el13drt | 12:eedda6554615 | 119 | // static background |
el13drt | 12:eedda6554615 | 120 | void backGround() |
el13drt | 12:eedda6554615 | 121 | { |
el13drt | 12:eedda6554615 | 122 | // x, y, w, h, fill - draw ground |
el13drt | 12:eedda6554615 | 123 | lcd.drawRect(0,47,84,0,1); |
el13drt | 12:eedda6554615 | 124 | |
el13drt | 12:eedda6554615 | 125 | // x, y, w, h, fill - draw left wall |
el13drt | 12:eedda6554615 | 126 | lcd.drawRect(2,0,0,47,1); |
el13drt | 12:eedda6554615 | 127 | // left wall - brick line 1 |
el13drt | 12:eedda6554615 | 128 | for(int x=0; x<47; x+=4) |
el13drt | 12:eedda6554615 | 129 | lcd.drawLine(1,1,1,48,2); |
el13drt | 12:eedda6554615 | 130 | // left wall - brick line 2 |
el13drt | 12:eedda6554615 | 131 | for(int x=0; x<47; x+=4) |
el13drt | 12:eedda6554615 | 132 | lcd.drawLine(0,0,0,48,2); |
el13drt | 12:eedda6554615 | 133 | |
el13drt | 12:eedda6554615 | 134 | // x, y, w, h, fill - draw right wall |
el13drt | 12:eedda6554615 | 135 | lcd.drawRect(81,0,0,47,1); |
el13drt | 12:eedda6554615 | 136 | // right wall - brick line 1 |
el13drt | 12:eedda6554615 | 137 | for(int x=0; x<WIDTH; x+=4) |
el13drt | 12:eedda6554615 | 138 | lcd.drawLine(82,0,82,48,2); |
el13drt | 12:eedda6554615 | 139 | // right wall - brick line 2 |
el13drt | 12:eedda6554615 | 140 | for(int x=0; x<WIDTH; x+=4) |
el13drt | 12:eedda6554615 | 141 | lcd.drawLine(83,1,83,48,2); |
el13drt | 12:eedda6554615 | 142 | |
el13drt | 12:eedda6554615 | 143 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 144 | } |
el13drt | 12:eedda6554615 | 145 | |
el13drt | 12:eedda6554615 | 146 | //intro screen |
el13drt | 12:eedda6554615 | 147 | void welcome() |
el13drt | 12:eedda6554615 | 148 | { |
el13drt | 12:eedda6554615 | 149 | //bottom border |
el13drt | 12:eedda6554615 | 150 | lcd.drawRect(0,44,84,2,1); |
el13drt | 12:eedda6554615 | 151 | |
el13drt | 12:eedda6554615 | 152 | //top border |
el13drt | 12:eedda6554615 | 153 | lcd.drawRect(0,1,84,2,1); |
el13drt | 12:eedda6554615 | 154 | |
el13drt | 12:eedda6554615 | 155 | //print initials 'DRT' |
el13drt | 12:eedda6554615 | 156 | lcd.printChar('D',30,14); |
el13drt | 12:eedda6554615 | 157 | wait(0.6); |
el13drt | 12:eedda6554615 | 158 | lcd.printChar('R',37,14); |
el13drt | 12:eedda6554615 | 159 | wait(0.6); |
el13drt | 12:eedda6554615 | 160 | lcd.printChar('T',44,14); |
el13drt | 12:eedda6554615 | 161 | wait(0.6); |
el13drt | 12:eedda6554615 | 162 | |
el13drt | 12:eedda6554615 | 163 | //print 'presents...' |
el13drt | 12:eedda6554615 | 164 | lcd.printString("presents...",8,21); |
el13drt | 12:eedda6554615 | 165 | wait(1.0); |
el13drt | 12:eedda6554615 | 166 | |
el13drt | 12:eedda6554615 | 167 | //dramatic flashing |
el13drt | 13:a1b3a373c5a4 | 168 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 169 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 170 | wait(0.2); |
el13drt | 12:eedda6554615 | 171 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 172 | wait(0.2); |
el13drt | 13:a1b3a373c5a4 | 173 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 174 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 175 | wait(0.2); |
el13drt | 12:eedda6554615 | 176 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 177 | wait(1.0); |
el13drt | 12:eedda6554615 | 178 | |
el13drt | 12:eedda6554615 | 179 | //more dramatic flashing |
el13drt | 13:a1b3a373c5a4 | 180 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 181 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 182 | wait(0.2); |
el13drt | 12:eedda6554615 | 183 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 184 | wait(0.2); |
el13drt | 13:a1b3a373c5a4 | 185 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 186 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 187 | wait(0.2); |
el13drt | 12:eedda6554615 | 188 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 189 | wait(0.6); |
el13drt | 12:eedda6554615 | 190 | } |
el13drt | 12:eedda6554615 | 191 | |
el13drt | 12:eedda6554615 | 192 | //pixel ninja character |
el13drt | 12:eedda6554615 | 193 | void pixelNinja() |
el13drt | 12:eedda6554615 | 194 | { |
el13drt | 12:eedda6554615 | 195 | //x, y, w, h, fill - left leg |
el13drt | 12:eedda6554615 | 196 | lcd.drawRect(a1,39,0,7,1); |
el13drt | 12:eedda6554615 | 197 | //right leg |
el13drt | 12:eedda6554615 | 198 | lcd.drawRect(a2,39,0,7,1); |
el13drt | 12:eedda6554615 | 199 | //centre stick |
el13drt | 12:eedda6554615 | 200 | lcd.drawRect(a3,37,0,7,1); |
el13drt | 12:eedda6554615 | 201 | //back of the head |
el13drt | 12:eedda6554615 | 202 | lcd.drawRect(a1,33,0,4,1); |
el13drt | 12:eedda6554615 | 203 | //top of the head |
el13drt | 12:eedda6554615 | 204 | lcd.drawRect(a1,33,4,0,1); |
el13drt | 12:eedda6554615 | 205 | //jaw |
el13drt | 12:eedda6554615 | 206 | lcd.drawRect(a2,38,2,0,1); |
el13drt | 12:eedda6554615 | 207 | //right shoulder |
el13drt | 12:eedda6554615 | 208 | lcd.drawRect(a4,40,1,0,1); |
el13drt | 12:eedda6554615 | 209 | //left shoulder |
el13drt | 12:eedda6554615 | 210 | lcd.drawRect(a5,40,1,0,1); |
el13drt | 12:eedda6554615 | 211 | //left arm |
el13drt | 12:eedda6554615 | 212 | lcd.drawRect(a5,41,0,1,1); |
el13drt | 12:eedda6554615 | 213 | //right arm |
el13drt | 12:eedda6554615 | 214 | lcd.drawRect(a6,41,0,1,1); |
el13drt | 12:eedda6554615 | 215 | //right eye |
el13drt | 12:eedda6554615 | 216 | lcd.drawRect(a6,35,0,0,1); |
el13drt | 12:eedda6554615 | 217 | //mouth piece |
el13drt | 12:eedda6554615 | 218 | lcd.drawRect(a6,37,0,0,1); |
el13drt | 12:eedda6554615 | 219 | //left eye |
el13drt | 12:eedda6554615 | 220 | lcd.drawRect(a2,35,0,0,1); |
el13drt | 12:eedda6554615 | 221 | //sword handle |
el13drt | 12:eedda6554615 | 222 | lcd.drawRect(a7,36,0,0,1); |
el13drt | 12:eedda6554615 | 223 | lcd.drawRect(a5,37,0,0,1); |
el13drt | 12:eedda6554615 | 224 | lcd.drawRect(a8,38,0,0,1); |
el13drt | 12:eedda6554615 | 225 | } |
el13drt | 12:eedda6554615 | 226 | |
el13drt | 12:eedda6554615 | 227 | // stops ninja going through walls |
el13drt | 12:eedda6554615 | 228 | void ninjaBoundaries() |
el13drt | 12:eedda6554615 | 229 | { |
el13drt | 12:eedda6554615 | 230 | if(a6 > 79 ) |
el13drt | 12:eedda6554615 | 231 | a6 = 79; |
el13drt | 12:eedda6554615 | 232 | if(a4 > 78) |
el13drt | 12:eedda6554615 | 233 | a4 = 78; |
el13drt | 12:eedda6554615 | 234 | if(a2 > 77) |
el13drt | 12:eedda6554615 | 235 | a2 = 77; |
el13drt | 12:eedda6554615 | 236 | if(a3 > 76) |
el13drt | 12:eedda6554615 | 237 | a3 = 76; |
el13drt | 12:eedda6554615 | 238 | if(a1 > 75) |
el13drt | 12:eedda6554615 | 239 | a1 = 75; |
el13drt | 12:eedda6554615 | 240 | if(a8 > 74) |
el13drt | 12:eedda6554615 | 241 | a8 = 74; |
el13drt | 12:eedda6554615 | 242 | if(a5 > 73) |
el13drt | 12:eedda6554615 | 243 | a5 = 73; |
el13drt | 12:eedda6554615 | 244 | if(a7 > 72) |
el13drt | 12:eedda6554615 | 245 | a7 = 72; |
el13drt | 12:eedda6554615 | 246 | |
el13drt | 12:eedda6554615 | 247 | if(a6 < 11 ) |
el13drt | 12:eedda6554615 | 248 | a6 = 11; |
el13drt | 12:eedda6554615 | 249 | if(a4 < 10) |
el13drt | 12:eedda6554615 | 250 | a4 = 10; |
el13drt | 12:eedda6554615 | 251 | if(a2 < 9) |
el13drt | 12:eedda6554615 | 252 | a2 = 9; |
el13drt | 12:eedda6554615 | 253 | if(a3 < 8) |
el13drt | 12:eedda6554615 | 254 | a3 = 8; |
el13drt | 12:eedda6554615 | 255 | if(a1 < 7) |
el13drt | 12:eedda6554615 | 256 | a1 = 7; |
el13drt | 12:eedda6554615 | 257 | if(a8 < 6) |
el13drt | 12:eedda6554615 | 258 | a8 = 6; |
el13drt | 12:eedda6554615 | 259 | if(a5 < 5) |
el13drt | 12:eedda6554615 | 260 | a5 = 5; |
el13drt | 12:eedda6554615 | 261 | if(a7 < 4) |
el13drt | 12:eedda6554615 | 262 | a7 = 4; |
el13drt | 12:eedda6554615 | 263 | } |
el13drt | 12:eedda6554615 | 264 | |
el13drt | 12:eedda6554615 | 265 | // resets back to initial values |
el13drt | 12:eedda6554615 | 266 | void resetGame() |
el13drt | 12:eedda6554615 | 267 | { |
el13drt | 12:eedda6554615 | 268 | score = 0; |
el13drt | 12:eedda6554615 | 269 | |
el13drt | 12:eedda6554615 | 270 | a1 = 22; |
el13drt | 12:eedda6554615 | 271 | a2 = 24; |
el13drt | 12:eedda6554615 | 272 | a3 = 23; |
el13drt | 12:eedda6554615 | 273 | a4 = 25; |
el13drt | 12:eedda6554615 | 274 | a5 = 20; |
el13drt | 12:eedda6554615 | 275 | a6 = 26; |
el13drt | 12:eedda6554615 | 276 | a7 = 19; |
el13drt | 12:eedda6554615 | 277 | a8 = 21; |
el13drt | 12:eedda6554615 | 278 | |
el13drt | 12:eedda6554615 | 279 | // in this case the X values are given a |
el13drt | 12:eedda6554615 | 280 | // new random variable each time the player |
el13drt | 12:eedda6554615 | 281 | // dies or exits and starts a new game |
el13drt | 12:eedda6554615 | 282 | randX1 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 283 | randX2 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 284 | randX3 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 285 | randX4 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 286 | randX5 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 287 | randX6 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 288 | |
el13drt | 12:eedda6554615 | 289 | randY1 = 0; |
el13drt | 12:eedda6554615 | 290 | randY2 = 0; |
el13drt | 12:eedda6554615 | 291 | randY3 = 0; |
el13drt | 12:eedda6554615 | 292 | randY4 = 0; |
el13drt | 12:eedda6554615 | 293 | randY5 = 0; |
el13drt | 12:eedda6554615 | 294 | randY6 = 0; |
el13drt | 12:eedda6554615 | 295 | lcd.clear(); |
el13drt | 12:eedda6554615 | 296 | } |
el13drt | 12:eedda6554615 | 297 | |
el13drt | 12:eedda6554615 | 298 | // draws falling hazards |
el13drt | 12:eedda6554615 | 299 | void hazards() |
el13drt | 12:eedda6554615 | 300 | { |
el13drt | 12:eedda6554615 | 301 | // X, Y, radius, fill |
el13drt | 12:eedda6554615 | 302 | lcd.drawCircle(randX1,randY1,2,1); |
el13drt | 12:eedda6554615 | 303 | lcd.drawCircle(randX2,randY2,2,1); |
el13drt | 12:eedda6554615 | 304 | lcd.drawCircle(randX3,randY3,2,1); |
el13drt | 12:eedda6554615 | 305 | lcd.drawCircle(randX4,randY4,2,1); |
el13drt | 12:eedda6554615 | 306 | lcd.drawCircle(randX5,randY5,2,1); |
el13drt | 12:eedda6554615 | 307 | lcd.drawCircle(randX6,randY6,2,1); |
el13drt | 12:eedda6554615 | 308 | |
el13drt | 12:eedda6554615 | 309 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 310 | } |
el13drt | 12:eedda6554615 | 311 | |
el13drt | 12:eedda6554615 | 312 | // makes hazards fall - randomises X axis co-ordinates |
el13drt | 12:eedda6554615 | 313 | void hazardFall() |
el13drt | 12:eedda6554615 | 314 | { |
el13drt | 12:eedda6554615 | 315 | // increments randY1 variables |
el13drt | 12:eedda6554615 | 316 | // appearing to make them fall |
el13drt | 12:eedda6554615 | 317 | randY1 = randY1 += fall; |
el13drt | 12:eedda6554615 | 318 | randY2 = randY2 += fall; |
el13drt | 12:eedda6554615 | 319 | randY3 = randY3 += fall; |
el13drt | 12:eedda6554615 | 320 | randY4 = randY4 += fall; |
el13drt | 12:eedda6554615 | 321 | randY5 = randY5 += fall; |
el13drt | 12:eedda6554615 | 322 | randY6 = randY6 += fall; |
el13drt | 12:eedda6554615 | 323 | |
el13drt | 12:eedda6554615 | 324 | // loops the objects once they 'hit the floor' |
el13drt | 12:eedda6554615 | 325 | // this imitates a new set of objects falling |
el13drt | 12:eedda6554615 | 326 | |
el13drt | 12:eedda6554615 | 327 | if (randY1>=48) |
el13drt | 12:eedda6554615 | 328 | randY1=0; |
el13drt | 12:eedda6554615 | 329 | |
el13drt | 12:eedda6554615 | 330 | if (randY2>=48) |
el13drt | 12:eedda6554615 | 331 | randY2=0; |
el13drt | 12:eedda6554615 | 332 | |
el13drt | 12:eedda6554615 | 333 | if (randY3>=48) |
el13drt | 12:eedda6554615 | 334 | randY3=0; |
el13drt | 12:eedda6554615 | 335 | |
el13drt | 12:eedda6554615 | 336 | if (randY4>=48) |
el13drt | 12:eedda6554615 | 337 | randY4=0; |
el13drt | 12:eedda6554615 | 338 | |
el13drt | 12:eedda6554615 | 339 | if (randY5>=48) |
el13drt | 12:eedda6554615 | 340 | randY5=0; |
el13drt | 12:eedda6554615 | 341 | |
el13drt | 12:eedda6554615 | 342 | // each time the objects loop, a new pseudo random value |
el13drt | 12:eedda6554615 | 343 | // is assigned to the global variables (randX) to |
el13drt | 12:eedda6554615 | 344 | // randomise their positions |
el13drt | 12:eedda6554615 | 345 | |
el13drt | 12:eedda6554615 | 346 | if (randY6>=48) { |
el13drt | 12:eedda6554615 | 347 | randY6=0; |
el13drt | 12:eedda6554615 | 348 | |
el13drt | 12:eedda6554615 | 349 | score = score++;//increment score by 1 after each wave of hazards |
el13drt | 12:eedda6554615 | 350 | |
el13drt | 12:eedda6554615 | 351 | randX1 = rand() % 74 + 5;// psuedo random number |
el13drt | 12:eedda6554615 | 352 | randX2 = rand() % 74 + 5;// between 5 and 74 |
el13drt | 12:eedda6554615 | 353 | randX3 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 354 | randX4 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 355 | randX5 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 356 | randX6 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 357 | } |
el13drt | 12:eedda6554615 | 358 | } |
el13drt | 13:a1b3a373c5a4 | 359 | // |
el13drt | 13:a1b3a373c5a4 | 360 | //void writeDataToFile() |
el13drt | 12:eedda6554615 | 361 | //{ |
el13drt | 13:a1b3a373c5a4 | 362 | // if (!cfg.setValue("Key", "TestValue")) {//input data |
el13drt | 13:a1b3a373c5a4 | 363 | // error("Failure to set a value.\n"); |
el13drt | 13:a1b3a373c5a4 | 364 | // } |
el13drt | 13:a1b3a373c5a4 | 365 | // if (!cfg.write("/local/output.cfg")) {//writes |
el13drt | 13:a1b3a373c5a4 | 366 | // error("Failure to write a configuration file.\n"); |
el13drt | 13:a1b3a373c5a4 | 367 | // } |
el13drt | 12:eedda6554615 | 368 | //} |
el13drt | 12:eedda6554615 | 369 | |
el13drt | 12:eedda6554615 | 370 | // clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 371 | void startrek() |
el13drt | 12:eedda6554615 | 372 | { |
el13drt | 12:eedda6554615 | 373 | for (int i=3; i<81; i++)// loops through rows |
el13drt | 12:eedda6554615 | 374 | for (int j=0; j<47; j++) |
el13drt | 12:eedda6554615 | 375 | if (cells[i][j]) {// if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 376 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 377 | } else { |
el13drt | 12:eedda6554615 | 378 | lcd.clearPixel(i,j);// else remove the old ones |
el13drt | 12:eedda6554615 | 379 | } |
el13drt | 12:eedda6554615 | 380 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 381 | } |
el13drt | 12:eedda6554615 | 382 | |
el13drt | 12:eedda6554615 | 383 | // clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 384 | void refreshCursor1() |
el13drt | 12:eedda6554615 | 385 | { |
el13drt | 12:eedda6554615 | 386 | for (int i=70; i<80; i++)// loops through rows |
el13drt | 12:eedda6554615 | 387 | for (int j=17; j<25; j++) |
el13drt | 12:eedda6554615 | 388 | if (cells[i][j]) {// if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 389 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 390 | } else { |
el13drt | 12:eedda6554615 | 391 | lcd.clearPixel(i,j);// else remove the old ones |
el13drt | 12:eedda6554615 | 392 | } |
el13drt | 12:eedda6554615 | 393 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 394 | } |
el13drt | 12:eedda6554615 | 395 | |
el13drt | 12:eedda6554615 | 396 | // clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 397 | void refreshCursor2() |
el13drt | 12:eedda6554615 | 398 | { |
el13drt | 12:eedda6554615 | 399 | for (int i=70; i<80; i++)// loops through rows |
el13drt | 12:eedda6554615 | 400 | for (int j=25; j<32; j++) |
el13drt | 12:eedda6554615 | 401 | if (cells[i][j]) {// if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 402 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 403 | } else { |
el13drt | 12:eedda6554615 | 404 | lcd.clearPixel(i,j);// else remove the old ones |
el13drt | 12:eedda6554615 | 405 | } |
el13drt | 12:eedda6554615 | 406 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 407 | } |
el13drt | 12:eedda6554615 | 408 | |
el13drt | 12:eedda6554615 | 409 | // clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 410 | void refreshCursor3() |
el13drt | 12:eedda6554615 | 411 | { |
el13drt | 12:eedda6554615 | 412 | for (int i=70; i<80; i++)// loops through rows |
el13drt | 12:eedda6554615 | 413 | for (int j=32; j<40; j++) |
el13drt | 12:eedda6554615 | 414 | if (cells[i][j]) {// if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 415 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 416 | } else { |
el13drt | 12:eedda6554615 | 417 | lcd.clearPixel(i,j);// else remove the old ones |
el13drt | 12:eedda6554615 | 418 | } |
el13drt | 12:eedda6554615 | 419 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 420 | } |
el13drt | 12:eedda6554615 | 421 | |
el13drt | 13:a1b3a373c5a4 | 422 | // BEEP/LED when button is closed |
el13drt | 13:a1b3a373c5a4 | 423 | void actionButtons() |
el13drt | 12:eedda6554615 | 424 | { |
el13drt | 12:eedda6554615 | 425 | buttonA.mode(PullDown); |
el13drt | 13:a1b3a373c5a4 | 426 | buttonB.mode(PullDown); |
el13drt | 13:a1b3a373c5a4 | 427 | |
el13drt | 13:a1b3a373c5a4 | 428 | if (buttonA != 0) { |
el13drt | 12:eedda6554615 | 429 | ledY = 1; |
el13drt | 13:a1b3a373c5a4 | 430 | buzzer.beep(1000,0.2);//frequeny/duration |
el13drt | 13:a1b3a373c5a4 | 431 | serial.printf("buttonA\n");//print for debugging |
el13drt | 12:eedda6554615 | 432 | } else { |
el13drt | 12:eedda6554615 | 433 | ledY = 0; |
el13drt | 12:eedda6554615 | 434 | } |
el13drt | 13:a1b3a373c5a4 | 435 | if (buttonB != 0) { |
el13drt | 12:eedda6554615 | 436 | ledY = 1; |
el13drt | 13:a1b3a373c5a4 | 437 | buzzer.beep(400,0.2);//frequency/duration |
el13drt | 13:a1b3a373c5a4 | 438 | serial.printf("buttonB\n");//print for debugging |
el13drt | 12:eedda6554615 | 439 | } else { |
el13drt | 12:eedda6554615 | 440 | ledY = 0; |
el13drt | 12:eedda6554615 | 441 | } |
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 | 13:a1b3a373c5a4 | 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 | 13:a1b3a373c5a4 | 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 | 13:a1b3a373c5a4 | 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 | 13:a1b3a373c5a4 | 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 | |
el13drt | 13:a1b3a373c5a4 | 780 | // draw Sound FX settings |
el13drt | 13:a1b3a373c5a4 | 781 | void drawSoundFXMenu() |
el13drt | 13:a1b3a373c5a4 | 782 | { |
el13drt | 13:a1b3a373c5a4 | 783 | lcd.clear(); |
el13drt | 13:a1b3a373c5a4 | 784 | backGround(); |
el13drt | 13:a1b3a373c5a4 | 785 | lcd.drawRect(0,47,84,0,1);//bottom border |
el13drt | 13:a1b3a373c5a4 | 786 | lcd.drawRect(0,0,84,2,1);//top border |
el13drt | 13:a1b3a373c5a4 | 787 | lcd.printString("*Sound FX*",10,7);//title |
el13drt | 13:a1b3a373c5a4 | 788 | lcd.printString("ON",35,9);//title |
el13drt | 13:a1b3a373c5a4 | 789 | lcd.printString("OFF",33,10);//title |
el13drt | 13:a1b3a373c5a4 | 790 | } |
el13drt | 13:a1b3a373c5a4 | 791 | |
el13drt | 12:eedda6554615 | 792 | // actual game |
el13drt | 12:eedda6554615 | 793 | void game(int& exitFlag, int& exitOption) |
el13drt | 12:eedda6554615 | 794 | { |
el13drt | 13:a1b3a373c5a4 | 795 | actionButtons(); |
el13drt | 12:eedda6554615 | 796 | lcd.clear();//clears screen |
el13drt | 12:eedda6554615 | 797 | backGround();//draw background |
el13drt | 12:eedda6554615 | 798 | |
el13drt | 12:eedda6554615 | 799 | ///game/// |
el13drt | 12:eedda6554615 | 800 | while(1) { |
el13drt | 12:eedda6554615 | 801 | // print score - top left of display |
el13drt | 12:eedda6554615 | 802 | char buffer[14];//create buffer for string |
el13drt | 12:eedda6554615 | 803 | int length = sprintf(buffer,"Level:%d",score);//insert buffer |
el13drt | 12:eedda6554615 | 804 | if (length <= 14) //ensure length is smaller than screen |
el13drt | 12:eedda6554615 | 805 | lcd.printString(buffer,3,0);//display |
el13drt | 12:eedda6554615 | 806 | |
el13drt | 13:a1b3a373c5a4 | 807 | actionButtons(); |
el13drt | 12:eedda6554615 | 808 | pixelNinja();//set character |
el13drt | 12:eedda6554615 | 809 | hazards();//initiates hazards |
el13drt | 12:eedda6554615 | 810 | hazardFall();//increments hazards towards floor |
el13drt | 12:eedda6554615 | 811 | |
el13drt | 12:eedda6554615 | 812 | if (printFlag) { //if flag set, clear flag and print joystick values to serial port |
el13drt | 12:eedda6554615 | 813 | printFlag = 0; |
el13drt | 12:eedda6554615 | 814 | |
el13drt | 12:eedda6554615 | 815 | // check joystick direction |
el13drt | 12:eedda6554615 | 816 | if (joystick.direction == LEFT) { |
el13drt | 12:eedda6554615 | 817 | serial.printf(" LEFT\n"); |
el13drt | 12:eedda6554615 | 818 | a1 = a1-=2; |
el13drt | 12:eedda6554615 | 819 | a2 = a2-=2; |
el13drt | 12:eedda6554615 | 820 | a3 = a3-=2; |
el13drt | 12:eedda6554615 | 821 | a4 = a4-=2; |
el13drt | 12:eedda6554615 | 822 | a5 = a5-=2; |
el13drt | 12:eedda6554615 | 823 | a6 = a6-=2; |
el13drt | 12:eedda6554615 | 824 | a7 = a7-=2; |
el13drt | 12:eedda6554615 | 825 | a8 = a8-=2; |
el13drt | 12:eedda6554615 | 826 | ninjaBoundaries(); |
el13drt | 12:eedda6554615 | 827 | } |
el13drt | 12:eedda6554615 | 828 | if (joystick.direction == RIGHT) { |
el13drt | 12:eedda6554615 | 829 | serial.printf(" RIGHT\n"); |
el13drt | 12:eedda6554615 | 830 | a1 = a1+=2; |
el13drt | 12:eedda6554615 | 831 | a2 = a2+=2; |
el13drt | 12:eedda6554615 | 832 | a3 = a3+=2; |
el13drt | 12:eedda6554615 | 833 | a4 = a4+=2; |
el13drt | 12:eedda6554615 | 834 | a5 = a5+=2; |
el13drt | 12:eedda6554615 | 835 | a6 = a6+=2; |
el13drt | 12:eedda6554615 | 836 | a7 = a7+=2; |
el13drt | 12:eedda6554615 | 837 | a8 = a8+=2; |
el13drt | 12:eedda6554615 | 838 | ninjaBoundaries(); |
el13drt | 12:eedda6554615 | 839 | } |
el13drt | 12:eedda6554615 | 840 | if (joystick.direction == CENTRE) |
el13drt | 12:eedda6554615 | 841 | serial.printf(" CENTRE\n"); |
el13drt | 12:eedda6554615 | 842 | if (joystick.direction == UNKNOWN) |
el13drt | 12:eedda6554615 | 843 | serial.printf(" Unsupported direction\n"); |
el13drt | 12:eedda6554615 | 844 | |
el13drt | 12:eedda6554615 | 845 | // integer to represent character being |
el13drt | 12:eedda6554615 | 846 | // struck by falling object |
el13drt | 12:eedda6554615 | 847 | int contactPoint = 0; |
el13drt | 12:eedda6554615 | 848 | |
el13drt | 12:eedda6554615 | 849 | // contact points |
el13drt | 12:eedda6554615 | 850 | if(lcd.getPixel((a1+4),32)) |
el13drt | 12:eedda6554615 | 851 | contactPoint++; |
el13drt | 12:eedda6554615 | 852 | if(lcd.getPixel((a1),32)) |
el13drt | 12:eedda6554615 | 853 | contactPoint++; |
el13drt | 12:eedda6554615 | 854 | if(lcd.getPixel((a7),32)) |
el13drt | 12:eedda6554615 | 855 | contactPoint++; |
el13drt | 12:eedda6554615 | 856 | |
el13drt | 12:eedda6554615 | 857 | // if contact point is not zero |
el13drt | 12:eedda6554615 | 858 | // character has been hit |
el13drt | 12:eedda6554615 | 859 | // and the game ends |
el13drt | 12:eedda6554615 | 860 | if ( contactPoint !=0) { |
el13drt | 12:eedda6554615 | 861 | lcd.printString("Game Over",17,2); |
el13drt | 12:eedda6554615 | 862 | lcd.inverseMode(); |
el13drt | 13:a1b3a373c5a4 | 863 | buzzer.beep(2000,0.2);//frequeny/duration |
el13drt | 12:eedda6554615 | 864 | wait(0.5); |
el13drt | 12:eedda6554615 | 865 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 866 | wait(0.5); |
el13drt | 12:eedda6554615 | 867 | lcd.inverseMode(); |
el13drt | 13:a1b3a373c5a4 | 868 | buzzer.beep(2000,0.2); |
el13drt | 12:eedda6554615 | 869 | wait(0.5); |
el13drt | 12:eedda6554615 | 870 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 871 | wait(0.5); |
el13drt | 13:a1b3a373c5a4 | 872 | lcd.inverseMode(); |
el13drt | 13:a1b3a373c5a4 | 873 | buzzer.beep(2000,0.2); |
el13drt | 13:a1b3a373c5a4 | 874 | wait(0.5); |
el13drt | 13:a1b3a373c5a4 | 875 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 876 | resetGame(); |
el13drt | 12:eedda6554615 | 877 | break; |
el13drt | 12:eedda6554615 | 878 | } |
el13drt | 12:eedda6554615 | 879 | startrek();//clears unset pixels, keeps set pixels |
el13drt | 12:eedda6554615 | 880 | |
el13drt | 12:eedda6554615 | 881 | /// Exit Menu (Back button pressed)/// |
el13drt | 12:eedda6554615 | 882 | if(buttonB == 1) { |
el13drt | 12:eedda6554615 | 883 | drawExitMenu();//draws the exit menu |
el13drt | 12:eedda6554615 | 884 | |
el13drt | 12:eedda6554615 | 885 | while(1) { |
el13drt | 12:eedda6554615 | 886 | exitMenu(exitOption);//presents exit options |
el13drt | 12:eedda6554615 | 887 | |
el13drt | 12:eedda6554615 | 888 | // 'exit' option YES |
el13drt | 12:eedda6554615 | 889 | if((buttonA == 1)&&(exitOption == 0)) { //returns to menu |
el13drt | 13:a1b3a373c5a4 | 890 | actionButtons(); |
el13drt | 12:eedda6554615 | 891 | lcd.clear();//clears screen |
el13drt | 12:eedda6554615 | 892 | resetGame();//resets scores/objects |
el13drt | 12:eedda6554615 | 893 | exitFlag = 1;//sets exit flag |
el13drt | 12:eedda6554615 | 894 | break; |
el13drt | 12:eedda6554615 | 895 | } |
el13drt | 12:eedda6554615 | 896 | // 'exit' option NO - returns to game |
el13drt | 12:eedda6554615 | 897 | if((buttonA == 1)&&(exitOption == 1)) { |
el13drt | 12:eedda6554615 | 898 | break; |
el13drt | 12:eedda6554615 | 899 | } |
el13drt | 12:eedda6554615 | 900 | } |
el13drt | 12:eedda6554615 | 901 | // if 'exit' option YES, resets |
el13drt | 12:eedda6554615 | 902 | // game values returns to main menu |
el13drt | 12:eedda6554615 | 903 | if (exitFlag!=0) { //if exit flag set |
el13drt | 12:eedda6554615 | 904 | exitFlag = 0;//reset flag |
el13drt | 12:eedda6554615 | 905 | break;//break to main menu |
el13drt | 12:eedda6554615 | 906 | } |
el13drt | 12:eedda6554615 | 907 | } |
el13drt | 12:eedda6554615 | 908 | serial.printf("Score: %i \n",score); |
el13drt | 12:eedda6554615 | 909 | } |
el13drt | 12:eedda6554615 | 910 | } |
el13drt | 12:eedda6554615 | 911 | } |
el13drt | 12:eedda6554615 | 912 | |
el13drt | 12:eedda6554615 | 913 | // high scores screen |
el13drt | 12:eedda6554615 | 914 | void scores() |
el13drt | 12:eedda6554615 | 915 | { |
el13drt | 13:a1b3a373c5a4 | 916 | actionButtons(); |
el13drt | 12:eedda6554615 | 917 | lcd.clear();//clear screen |
el13drt | 12:eedda6554615 | 918 | backGround();//set background |
el13drt | 12:eedda6554615 | 919 | lcd.printString("High Scores",10,0);//title |
el13drt | 12:eedda6554615 | 920 | |
el13drt | 12:eedda6554615 | 921 | // players high scores - highest first |
el13drt | 12:eedda6554615 | 922 | char highScore1[14];//create buffer for strings |
el13drt | 12:eedda6554615 | 923 | char highScore2[14]; |
el13drt | 12:eedda6554615 | 924 | char highScore3[14]; |
el13drt | 12:eedda6554615 | 925 | |
el13drt | 12:eedda6554615 | 926 | int player1 = sprintf(highScore1,"1.DRT.....99",score);//insert buffer |
el13drt | 12:eedda6554615 | 927 | int player2 = sprintf(highScore2,"2.NRG.....98",score);//insert buffer |
el13drt | 12:eedda6554615 | 928 | int player3 = sprintf(highScore3,"3.GRT.....10",score);//insert buffer |
el13drt | 12:eedda6554615 | 929 | |
el13drt | 12:eedda6554615 | 930 | if (player1 <= 14) //ensure length is smaller than screen |
el13drt | 12:eedda6554615 | 931 | lcd.printString(highScore1,5,2);//display |
el13drt | 12:eedda6554615 | 932 | if (player2 <= 14) //ensure length is smaller than screen |
el13drt | 12:eedda6554615 | 933 | lcd.printString(highScore2,5,3);//display |
el13drt | 12:eedda6554615 | 934 | if (player3 <= 14) //ensure length is smaller than screen |
el13drt | 12:eedda6554615 | 935 | lcd.printString(highScore3,5,4);//display |
el13drt | 12:eedda6554615 | 936 | |
el13drt | 12:eedda6554615 | 937 | while(1) { |
el13drt | 13:a1b3a373c5a4 | 938 | actionButtons();//select |
el13drt | 12:eedda6554615 | 939 | |
el13drt | 12:eedda6554615 | 940 | // back to menu |
el13drt | 12:eedda6554615 | 941 | if(buttonB == 1) { |
el13drt | 12:eedda6554615 | 942 | lcd.clear(); |
el13drt | 12:eedda6554615 | 943 | break; |
el13drt | 12:eedda6554615 | 944 | } |
el13drt | 12:eedda6554615 | 945 | } |
el13drt | 13:a1b3a373c5a4 | 946 | } |
el13drt | 13:a1b3a373c5a4 | 947 | |
el13drt | 13:a1b3a373c5a4 | 948 | // options menu |
el13drt | 14:c2c969e1c6e8 | 949 | void optionsMenu() |
el13drt | 13:a1b3a373c5a4 | 950 | { |
el13drt | 13:a1b3a373c5a4 | 951 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 952 | drawOptionsMenu();//draws options menu |
el13drt | 14:c2c969e1c6e8 | 953 | // counters for navigation |
el13drt | 14:c2c969e1c6e8 | 954 | int option = 0; |
el13drt | 14:c2c969e1c6e8 | 955 | int subOption = 0; |
el13drt | 14:c2c969e1c6e8 | 956 | int fxOption = 0; |
el13drt | 14:c2c969e1c6e8 | 957 | |
el13drt | 13:a1b3a373c5a4 | 958 | while(1) { |
el13drt | 13:a1b3a373c5a4 | 959 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 960 | optionsMenu(option);//presents options |
el13drt | 13:a1b3a373c5a4 | 961 | |
el13drt | 13:a1b3a373c5a4 | 962 | ////////////////////// difficulty menu //////////////////////////////////// |
el13drt | 13:a1b3a373c5a4 | 963 | if ((option == 0)&&(buttonA == 1)) { |
el13drt | 13:a1b3a373c5a4 | 964 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 965 | drawDifficultyMenu();//draws difficulty menu |
el13drt | 13:a1b3a373c5a4 | 966 | |
el13drt | 13:a1b3a373c5a4 | 967 | while(1) { |
el13drt | 13:a1b3a373c5a4 | 968 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 969 | difficultyMenu(subOption);//presents difficulty options |
el13drt | 13:a1b3a373c5a4 | 970 | |
el13drt | 13:a1b3a373c5a4 | 971 | if(buttonB != 0) { |
el13drt | 13:a1b3a373c5a4 | 972 | lcd.clear(); |
el13drt | 13:a1b3a373c5a4 | 973 | break; |
el13drt | 13:a1b3a373c5a4 | 974 | } |
el13drt | 13:a1b3a373c5a4 | 975 | } |
el13drt | 13:a1b3a373c5a4 | 976 | } |
el13drt | 13:a1b3a373c5a4 | 977 | ///////////////////// sound FX menu ////////////////////////////////////// |
el13drt | 13:a1b3a373c5a4 | 978 | if((option ==1)&&(buttonA !=0)) { |
el13drt | 13:a1b3a373c5a4 | 979 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 980 | drawSoundFXMenu();//draw menu |
el13drt | 13:a1b3a373c5a4 | 981 | |
el13drt | 13:a1b3a373c5a4 | 982 | while(1) { |
el13drt | 13:a1b3a373c5a4 | 983 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 984 | soundFXMenu(fxOption); |
el13drt | 13:a1b3a373c5a4 | 985 | |
el13drt | 13:a1b3a373c5a4 | 986 | // back to options menu |
el13drt | 13:a1b3a373c5a4 | 987 | if(buttonB !=0) { |
el13drt | 13:a1b3a373c5a4 | 988 | lcd.clear(); |
el13drt | 13:a1b3a373c5a4 | 989 | break; |
el13drt | 13:a1b3a373c5a4 | 990 | } |
el13drt | 13:a1b3a373c5a4 | 991 | } |
el13drt | 13:a1b3a373c5a4 | 992 | } |
el13drt | 13:a1b3a373c5a4 | 993 | // back to mainmenu |
el13drt | 13:a1b3a373c5a4 | 994 | if(buttonB !=0) { |
el13drt | 13:a1b3a373c5a4 | 995 | lcd.clear(); |
el13drt | 13:a1b3a373c5a4 | 996 | break; |
el13drt | 13:a1b3a373c5a4 | 997 | } |
el13drt | 13:a1b3a373c5a4 | 998 | } |
el13drt | 13:a1b3a373c5a4 | 999 | } |
el13drt | 13:a1b3a373c5a4 | 1000 | |
el13drt | 13:a1b3a373c5a4 | 1001 | // read default positions of the joystick to calibrate later readings |
el13drt | 13:a1b3a373c5a4 | 1002 | void calibrateJoystick() |
el13drt | 13:a1b3a373c5a4 | 1003 | { |
el13drt | 14:c2c969e1c6e8 | 1004 | joyButton.mode(PullDown); |
el13drt | 13:a1b3a373c5a4 | 1005 | // must not move during calibration |
el13drt | 13:a1b3a373c5a4 | 1006 | joystick.x0 = xPot; //initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
el13drt | 13:a1b3a373c5a4 | 1007 | joystick.y0 = yPot; |
el13drt | 13:a1b3a373c5a4 | 1008 | } |
el13drt | 13:a1b3a373c5a4 | 1009 | |
el13drt | 13:a1b3a373c5a4 | 1010 | void updateJoystick() |
el13drt | 13:a1b3a373c5a4 | 1011 | { |
el13drt | 13:a1b3a373c5a4 | 1012 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
el13drt | 13:a1b3a373c5a4 | 1013 | joystick.x = xPot - joystick.x0; |
el13drt | 13:a1b3a373c5a4 | 1014 | joystick.y = yPot - joystick.y0; |
el13drt | 13:a1b3a373c5a4 | 1015 | // read button state |
el13drt | 14:c2c969e1c6e8 | 1016 | joystick.button = joyButton; |
el13drt | 13:a1b3a373c5a4 | 1017 | |
el13drt | 13:a1b3a373c5a4 | 1018 | // calculate direction depending on x,y values |
el13drt | 13:a1b3a373c5a4 | 1019 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
el13drt | 13:a1b3a373c5a4 | 1020 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1021 | joystick.direction = CENTRE; |
el13drt | 13:a1b3a373c5a4 | 1022 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1023 | joystick.direction = UP; |
el13drt | 13:a1b3a373c5a4 | 1024 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1025 | joystick.direction = DOWN; |
el13drt | 13:a1b3a373c5a4 | 1026 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1027 | joystick.direction = RIGHT; |
el13drt | 13:a1b3a373c5a4 | 1028 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1029 | joystick.direction = LEFT; |
el13drt | 13:a1b3a373c5a4 | 1030 | } else { |
el13drt | 13:a1b3a373c5a4 | 1031 | joystick.direction = UNKNOWN; |
el13drt | 13:a1b3a373c5a4 | 1032 | } |
el13drt | 13:a1b3a373c5a4 | 1033 | |
el13drt | 13:a1b3a373c5a4 | 1034 | // set flag for printing |
el13drt | 13:a1b3a373c5a4 | 1035 | printFlag = 1; |
el13drt | 12:eedda6554615 | 1036 | } |