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