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