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