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