A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.
Dependencies: ConfigFile N5110 PowerControl beep mbed
tower.h@51:df3bab1d6926, 2015-04-30 (annotated)
- Committer:
- el13drt
- Date:
- Thu Apr 30 15:55:43 2015 +0000
- Revision:
- 51:df3bab1d6926
- Parent:
- 50:b8a3b84ed221
- Child:
- 52:bb2acf0e248a
this works 30/4/15 16:55
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 | 51:df3bab1d6926 | 111 | void newScore(); |
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 | 51:df3bab1d6926 | 189 | |
el13drt | 13:a1b3a373c5a4 | 190 | }; |
el13drt | 13:a1b3a373c5a4 | 191 | |
el13drt | 35:9f55326c1b0a | 192 | //struct for Joystick |
el13drt | 13:a1b3a373c5a4 | 193 | typedef struct JoyStick Joystick; |
el13drt | 13:a1b3a373c5a4 | 194 | struct JoyStick { |
el13drt | 35:9f55326c1b0a | 195 | float x;//current x value |
el13drt | 35:9f55326c1b0a | 196 | float x0;//'centred' x value |
el13drt | 35:9f55326c1b0a | 197 | float y;//current y value |
el13drt | 35:9f55326c1b0a | 198 | float y0;//'centred' y value |
el13drt | 35:9f55326c1b0a | 199 | int button;//button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el13drt | 35:9f55326c1b0a | 200 | DirectionName direction;//current direction |
el13drt | 13:a1b3a373c5a4 | 201 | }; |
el13drt | 13:a1b3a373c5a4 | 202 | |
el13drt | 35:9f55326c1b0a | 203 | //create struct variable |
el13drt | 13:a1b3a373c5a4 | 204 | Joystick joystick; |
el13drt | 22:e5bfe0e7c508 | 205 | |
el13drt | 35:9f55326c1b0a | 206 | //if buttonA set flag A |
el13drt | 15:ff3eb0091453 | 207 | void timerExpiredA() |
el13drt | 15:ff3eb0091453 | 208 | { |
el13drt | 15:ff3eb0091453 | 209 | if(buttonA == 1) { |
el13drt | 15:ff3eb0091453 | 210 | buttonFlagA = 1; |
el13drt | 15:ff3eb0091453 | 211 | serial.printf("flagA set\n"); |
el13drt | 15:ff3eb0091453 | 212 | } |
el13drt | 15:ff3eb0091453 | 213 | } |
el13drt | 15:ff3eb0091453 | 214 | |
el13drt | 35:9f55326c1b0a | 215 | //if buttonB set flag B |
el13drt | 15:ff3eb0091453 | 216 | void timerExpiredB() |
el13drt | 15:ff3eb0091453 | 217 | { |
el13drt | 15:ff3eb0091453 | 218 | if(buttonB == 1) { |
el13drt | 15:ff3eb0091453 | 219 | buttonFlagB = 1; |
el13drt | 15:ff3eb0091453 | 220 | serial.printf("flagB set\n"); |
el13drt | 15:ff3eb0091453 | 221 | } |
el13drt | 15:ff3eb0091453 | 222 | } |
el13drt | 13:a1b3a373c5a4 | 223 | |
el13drt | 35:9f55326c1b0a | 224 | //set seed/randomise initial co-Ordinates |
el13drt | 13:a1b3a373c5a4 | 225 | void randomise() |
el13drt | 13:a1b3a373c5a4 | 226 | { |
el13drt | 13:a1b3a373c5a4 | 227 | srand (time(NULL));//initial seed for randomisation |
el13drt | 13:a1b3a373c5a4 | 228 | |
el13drt | 35:9f55326c1b0a | 229 | //initial random x co-ordinates |
el13drt | 35:9f55326c1b0a | 230 | //for falling hazards |
el13drt | 35:9f55326c1b0a | 231 | //(values between 3 and 76) |
el13drt | 13:a1b3a373c5a4 | 232 | randX1 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 233 | randX2 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 234 | randX3 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 235 | randX4 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 236 | randX5 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 237 | randX6 = rand() % 74 + 5; |
el13drt | 13:a1b3a373c5a4 | 238 | } |
el13drt | 13:a1b3a373c5a4 | 239 | |
el13drt | 35:9f55326c1b0a | 240 | //static background |
el13drt | 35:9f55326c1b0a | 241 | void drawBackground() |
el13drt | 12:eedda6554615 | 242 | { |
el13drt | 35:9f55326c1b0a | 243 | //x, y, w, h, fill - draw ground |
el13drt | 12:eedda6554615 | 244 | lcd.drawRect(0,47,84,0,1); |
el13drt | 12:eedda6554615 | 245 | |
el13drt | 35:9f55326c1b0a | 246 | //x, y, w, h, fill - draw left wall |
el13drt | 12:eedda6554615 | 247 | lcd.drawRect(2,0,0,47,1); |
el13drt | 35:9f55326c1b0a | 248 | //left wall - brick line 1 |
el13drt | 12:eedda6554615 | 249 | for(int x=0; x<47; x+=4) |
el13drt | 12:eedda6554615 | 250 | lcd.drawLine(1,1,1,48,2); |
el13drt | 35:9f55326c1b0a | 251 | //left wall - brick line 2 |
el13drt | 12:eedda6554615 | 252 | for(int x=0; x<47; x+=4) |
el13drt | 12:eedda6554615 | 253 | lcd.drawLine(0,0,0,48,2); |
el13drt | 12:eedda6554615 | 254 | |
el13drt | 35:9f55326c1b0a | 255 | //x, y, w, h, fill - draw right wall |
el13drt | 12:eedda6554615 | 256 | lcd.drawRect(81,0,0,47,1); |
el13drt | 35:9f55326c1b0a | 257 | //right wall - brick line 1 |
el13drt | 12:eedda6554615 | 258 | for(int x=0; x<WIDTH; x+=4) |
el13drt | 12:eedda6554615 | 259 | lcd.drawLine(82,0,82,48,2); |
el13drt | 35:9f55326c1b0a | 260 | //right wall - brick line 2 |
el13drt | 12:eedda6554615 | 261 | for(int x=0; x<WIDTH; x+=4) |
el13drt | 12:eedda6554615 | 262 | lcd.drawLine(83,1,83,48,2); |
el13drt | 12:eedda6554615 | 263 | |
el13drt | 48:e53a194661fa | 264 | //lcd.refresh(); |
el13drt | 12:eedda6554615 | 265 | } |
el13drt | 12:eedda6554615 | 266 | |
el13drt | 12:eedda6554615 | 267 | //intro screen |
el13drt | 35:9f55326c1b0a | 268 | void drawWelcome() |
el13drt | 12:eedda6554615 | 269 | { |
el13drt | 12:eedda6554615 | 270 | //bottom border |
el13drt | 12:eedda6554615 | 271 | lcd.drawRect(0,44,84,2,1); |
el13drt | 12:eedda6554615 | 272 | |
el13drt | 12:eedda6554615 | 273 | //top border |
el13drt | 12:eedda6554615 | 274 | lcd.drawRect(0,1,84,2,1); |
el13drt | 12:eedda6554615 | 275 | |
el13drt | 12:eedda6554615 | 276 | //print initials 'DRT' |
el13drt | 12:eedda6554615 | 277 | lcd.printChar('D',30,14); |
el13drt | 12:eedda6554615 | 278 | wait(0.6); |
el13drt | 12:eedda6554615 | 279 | lcd.printChar('R',37,14); |
el13drt | 12:eedda6554615 | 280 | wait(0.6); |
el13drt | 12:eedda6554615 | 281 | lcd.printChar('T',44,14); |
el13drt | 12:eedda6554615 | 282 | wait(0.6); |
el13drt | 12:eedda6554615 | 283 | |
el13drt | 12:eedda6554615 | 284 | //print 'presents...' |
el13drt | 12:eedda6554615 | 285 | lcd.printString("presents...",8,21); |
el13drt | 12:eedda6554615 | 286 | wait(1.0); |
el13drt | 12:eedda6554615 | 287 | |
el13drt | 12:eedda6554615 | 288 | //dramatic flashing |
el13drt | 13:a1b3a373c5a4 | 289 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 290 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 291 | wait(0.2); |
el13drt | 12:eedda6554615 | 292 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 293 | wait(0.2); |
el13drt | 13:a1b3a373c5a4 | 294 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 295 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 296 | wait(0.2); |
el13drt | 12:eedda6554615 | 297 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 298 | wait(1.0); |
el13drt | 12:eedda6554615 | 299 | |
el13drt | 12:eedda6554615 | 300 | //more dramatic flashing |
el13drt | 13:a1b3a373c5a4 | 301 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 302 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 303 | wait(0.2); |
el13drt | 12:eedda6554615 | 304 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 305 | wait(0.2); |
el13drt | 13:a1b3a373c5a4 | 306 | buzzer.beep(5000,0.3); |
el13drt | 12:eedda6554615 | 307 | lcd.inverseMode(); |
el13drt | 12:eedda6554615 | 308 | wait(0.2); |
el13drt | 12:eedda6554615 | 309 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 310 | wait(0.6); |
el13drt | 12:eedda6554615 | 311 | } |
el13drt | 12:eedda6554615 | 312 | |
el13drt | 12:eedda6554615 | 313 | //pixel ninja character |
el13drt | 35:9f55326c1b0a | 314 | void drawNinja() |
el13drt | 12:eedda6554615 | 315 | { |
el13drt | 12:eedda6554615 | 316 | //x, y, w, h, fill - left leg |
el13drt | 12:eedda6554615 | 317 | lcd.drawRect(a1,39,0,7,1); |
el13drt | 12:eedda6554615 | 318 | //right leg |
el13drt | 12:eedda6554615 | 319 | lcd.drawRect(a2,39,0,7,1); |
el13drt | 12:eedda6554615 | 320 | //centre stick |
el13drt | 12:eedda6554615 | 321 | lcd.drawRect(a3,37,0,7,1); |
el13drt | 12:eedda6554615 | 322 | //back of the head |
el13drt | 12:eedda6554615 | 323 | lcd.drawRect(a1,33,0,4,1); |
el13drt | 12:eedda6554615 | 324 | //top of the head |
el13drt | 12:eedda6554615 | 325 | lcd.drawRect(a1,33,4,0,1); |
el13drt | 12:eedda6554615 | 326 | //jaw |
el13drt | 12:eedda6554615 | 327 | lcd.drawRect(a2,38,2,0,1); |
el13drt | 12:eedda6554615 | 328 | //right shoulder |
el13drt | 12:eedda6554615 | 329 | lcd.drawRect(a4,40,1,0,1); |
el13drt | 12:eedda6554615 | 330 | //left shoulder |
el13drt | 12:eedda6554615 | 331 | lcd.drawRect(a5,40,1,0,1); |
el13drt | 12:eedda6554615 | 332 | //left arm |
el13drt | 12:eedda6554615 | 333 | lcd.drawRect(a5,41,0,1,1); |
el13drt | 12:eedda6554615 | 334 | //right arm |
el13drt | 12:eedda6554615 | 335 | lcd.drawRect(a6,41,0,1,1); |
el13drt | 12:eedda6554615 | 336 | //right eye |
el13drt | 12:eedda6554615 | 337 | lcd.drawRect(a6,35,0,0,1); |
el13drt | 12:eedda6554615 | 338 | //mouth piece |
el13drt | 12:eedda6554615 | 339 | lcd.drawRect(a6,37,0,0,1); |
el13drt | 12:eedda6554615 | 340 | //left eye |
el13drt | 12:eedda6554615 | 341 | lcd.drawRect(a2,35,0,0,1); |
el13drt | 12:eedda6554615 | 342 | //sword handle |
el13drt | 12:eedda6554615 | 343 | lcd.drawRect(a7,36,0,0,1); |
el13drt | 12:eedda6554615 | 344 | lcd.drawRect(a5,37,0,0,1); |
el13drt | 12:eedda6554615 | 345 | lcd.drawRect(a8,38,0,0,1); |
el13drt | 12:eedda6554615 | 346 | } |
el13drt | 12:eedda6554615 | 347 | |
el13drt | 35:9f55326c1b0a | 348 | //stops ninja going through walls |
el13drt | 12:eedda6554615 | 349 | void ninjaBoundaries() |
el13drt | 12:eedda6554615 | 350 | { |
el13drt | 12:eedda6554615 | 351 | if(a6 > 79 ) |
el13drt | 12:eedda6554615 | 352 | a6 = 79; |
el13drt | 12:eedda6554615 | 353 | if(a4 > 78) |
el13drt | 12:eedda6554615 | 354 | a4 = 78; |
el13drt | 12:eedda6554615 | 355 | if(a2 > 77) |
el13drt | 12:eedda6554615 | 356 | a2 = 77; |
el13drt | 12:eedda6554615 | 357 | if(a3 > 76) |
el13drt | 12:eedda6554615 | 358 | a3 = 76; |
el13drt | 12:eedda6554615 | 359 | if(a1 > 75) |
el13drt | 12:eedda6554615 | 360 | a1 = 75; |
el13drt | 12:eedda6554615 | 361 | if(a8 > 74) |
el13drt | 12:eedda6554615 | 362 | a8 = 74; |
el13drt | 12:eedda6554615 | 363 | if(a5 > 73) |
el13drt | 12:eedda6554615 | 364 | a5 = 73; |
el13drt | 12:eedda6554615 | 365 | if(a7 > 72) |
el13drt | 12:eedda6554615 | 366 | a7 = 72; |
el13drt | 12:eedda6554615 | 367 | |
el13drt | 12:eedda6554615 | 368 | if(a6 < 11 ) |
el13drt | 12:eedda6554615 | 369 | a6 = 11; |
el13drt | 12:eedda6554615 | 370 | if(a4 < 10) |
el13drt | 12:eedda6554615 | 371 | a4 = 10; |
el13drt | 12:eedda6554615 | 372 | if(a2 < 9) |
el13drt | 12:eedda6554615 | 373 | a2 = 9; |
el13drt | 12:eedda6554615 | 374 | if(a3 < 8) |
el13drt | 12:eedda6554615 | 375 | a3 = 8; |
el13drt | 12:eedda6554615 | 376 | if(a1 < 7) |
el13drt | 12:eedda6554615 | 377 | a1 = 7; |
el13drt | 12:eedda6554615 | 378 | if(a8 < 6) |
el13drt | 12:eedda6554615 | 379 | a8 = 6; |
el13drt | 12:eedda6554615 | 380 | if(a5 < 5) |
el13drt | 12:eedda6554615 | 381 | a5 = 5; |
el13drt | 12:eedda6554615 | 382 | if(a7 < 4) |
el13drt | 12:eedda6554615 | 383 | a7 = 4; |
el13drt | 12:eedda6554615 | 384 | } |
el13drt | 12:eedda6554615 | 385 | |
el13drt | 35:9f55326c1b0a | 386 | //resets variables initial values when game ends or is exited |
el13drt | 12:eedda6554615 | 387 | void resetGame() |
el13drt | 22:e5bfe0e7c508 | 388 | { |
el13drt | 12:eedda6554615 | 389 | score = 0; |
el13drt | 12:eedda6554615 | 390 | |
el13drt | 12:eedda6554615 | 391 | a1 = 22; |
el13drt | 12:eedda6554615 | 392 | a2 = 24; |
el13drt | 12:eedda6554615 | 393 | a3 = 23; |
el13drt | 12:eedda6554615 | 394 | a4 = 25; |
el13drt | 12:eedda6554615 | 395 | a5 = 20; |
el13drt | 12:eedda6554615 | 396 | a6 = 26; |
el13drt | 12:eedda6554615 | 397 | a7 = 19; |
el13drt | 12:eedda6554615 | 398 | a8 = 21; |
el13drt | 12:eedda6554615 | 399 | |
el13drt | 35:9f55326c1b0a | 400 | //in this case the X values are given a |
el13drt | 35:9f55326c1b0a | 401 | //new random variable each time the player |
el13drt | 35:9f55326c1b0a | 402 | //dies, exits or starts a new game |
el13drt | 12:eedda6554615 | 403 | randX1 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 404 | randX2 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 405 | randX3 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 406 | randX4 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 407 | randX5 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 408 | randX6 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 409 | |
el13drt | 12:eedda6554615 | 410 | randY1 = 0; |
el13drt | 12:eedda6554615 | 411 | randY2 = 0; |
el13drt | 12:eedda6554615 | 412 | randY3 = 0; |
el13drt | 12:eedda6554615 | 413 | randY4 = 0; |
el13drt | 12:eedda6554615 | 414 | randY5 = 0; |
el13drt | 12:eedda6554615 | 415 | randY6 = 0; |
el13drt | 12:eedda6554615 | 416 | lcd.clear(); |
el13drt | 12:eedda6554615 | 417 | } |
el13drt | 12:eedda6554615 | 418 | |
el13drt | 35:9f55326c1b0a | 419 | //draws falling hazards |
el13drt | 35:9f55326c1b0a | 420 | void drawHazards() |
el13drt | 12:eedda6554615 | 421 | { |
el13drt | 35:9f55326c1b0a | 422 | //X, Y, radius, fill |
el13drt | 12:eedda6554615 | 423 | lcd.drawCircle(randX1,randY1,2,1); |
el13drt | 12:eedda6554615 | 424 | lcd.drawCircle(randX2,randY2,2,1); |
el13drt | 12:eedda6554615 | 425 | lcd.drawCircle(randX3,randY3,2,1); |
el13drt | 12:eedda6554615 | 426 | lcd.drawCircle(randX4,randY4,2,1); |
el13drt | 12:eedda6554615 | 427 | lcd.drawCircle(randX5,randY5,2,1); |
el13drt | 12:eedda6554615 | 428 | lcd.drawCircle(randX6,randY6,2,1); |
el13drt | 12:eedda6554615 | 429 | |
el13drt | 40:56ab6d368e9b | 430 | //lcd.refresh(); |
el13drt | 12:eedda6554615 | 431 | } |
el13drt | 12:eedda6554615 | 432 | |
el13drt | 35:9f55326c1b0a | 433 | //makes hazards fall - randomises X axis co-ordinates |
el13drt | 12:eedda6554615 | 434 | void hazardFall() |
el13drt | 12:eedda6554615 | 435 | { |
el13drt | 35:9f55326c1b0a | 436 | //increments randY1 variables |
el13drt | 35:9f55326c1b0a | 437 | //appearing to make them fall |
el13drt | 12:eedda6554615 | 438 | randY1 = randY1 += fall; |
el13drt | 12:eedda6554615 | 439 | randY2 = randY2 += fall; |
el13drt | 12:eedda6554615 | 440 | randY3 = randY3 += fall; |
el13drt | 12:eedda6554615 | 441 | randY4 = randY4 += fall; |
el13drt | 12:eedda6554615 | 442 | randY5 = randY5 += fall; |
el13drt | 12:eedda6554615 | 443 | randY6 = randY6 += fall; |
el13drt | 12:eedda6554615 | 444 | |
el13drt | 35:9f55326c1b0a | 445 | //loops the objects once they 'hit the floor' |
el13drt | 35:9f55326c1b0a | 446 | //this imitates a new set of objects falling |
el13drt | 12:eedda6554615 | 447 | |
el13drt | 12:eedda6554615 | 448 | if (randY1>=48) |
el13drt | 12:eedda6554615 | 449 | randY1=0; |
el13drt | 12:eedda6554615 | 450 | |
el13drt | 12:eedda6554615 | 451 | if (randY2>=48) |
el13drt | 12:eedda6554615 | 452 | randY2=0; |
el13drt | 12:eedda6554615 | 453 | |
el13drt | 12:eedda6554615 | 454 | if (randY3>=48) |
el13drt | 12:eedda6554615 | 455 | randY3=0; |
el13drt | 12:eedda6554615 | 456 | |
el13drt | 12:eedda6554615 | 457 | if (randY4>=48) |
el13drt | 12:eedda6554615 | 458 | randY4=0; |
el13drt | 12:eedda6554615 | 459 | |
el13drt | 12:eedda6554615 | 460 | if (randY5>=48) |
el13drt | 12:eedda6554615 | 461 | randY5=0; |
el13drt | 12:eedda6554615 | 462 | |
el13drt | 35:9f55326c1b0a | 463 | //each time the objects loop, a new pseudo random value |
el13drt | 35:9f55326c1b0a | 464 | //is assigned to the global variables (randX) to |
el13drt | 35:9f55326c1b0a | 465 | //randomise their positions |
el13drt | 12:eedda6554615 | 466 | |
el13drt | 12:eedda6554615 | 467 | if (randY6>=48) { |
el13drt | 12:eedda6554615 | 468 | randY6=0; |
el13drt | 12:eedda6554615 | 469 | |
el13drt | 12:eedda6554615 | 470 | score = score++;//increment score by 1 after each wave of hazards |
el13drt | 12:eedda6554615 | 471 | |
el13drt | 35:9f55326c1b0a | 472 | randX1 = rand() % 74 + 5;//psuedo random number |
el13drt | 35:9f55326c1b0a | 473 | randX2 = rand() % 74 + 5;//between 5 and 74 |
el13drt | 12:eedda6554615 | 474 | randX3 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 475 | randX4 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 476 | randX5 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 477 | randX6 = rand() % 74 + 5; |
el13drt | 12:eedda6554615 | 478 | } |
el13drt | 12:eedda6554615 | 479 | } |
el13drt | 12:eedda6554615 | 480 | |
el13drt | 35:9f55326c1b0a | 481 | //clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 482 | void startrek() |
el13drt | 12:eedda6554615 | 483 | { |
el13drt | 35:9f55326c1b0a | 484 | for (int i=3; i<81; i++)//loops through rows |
el13drt | 12:eedda6554615 | 485 | for (int j=0; j<47; j++) |
el13drt | 35:9f55326c1b0a | 486 | if (cells[i][j]) {//if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 487 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 488 | } else { |
el13drt | 35:9f55326c1b0a | 489 | lcd.clearPixel(i,j);//else remove the old ones |
el13drt | 12:eedda6554615 | 490 | } |
el13drt | 40:56ab6d368e9b | 491 | //lcd.refresh(); |
el13drt | 12:eedda6554615 | 492 | } |
el13drt | 12:eedda6554615 | 493 | |
el13drt | 35:9f55326c1b0a | 494 | //clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 495 | void refreshCursor1() |
el13drt | 12:eedda6554615 | 496 | { |
el13drt | 35:9f55326c1b0a | 497 | for (int i=70; i<80; i++)//loops through rows |
el13drt | 12:eedda6554615 | 498 | for (int j=17; j<25; j++) |
el13drt | 35:9f55326c1b0a | 499 | if (cells[i][j]) {//if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 500 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 501 | } else { |
el13drt | 35:9f55326c1b0a | 502 | lcd.clearPixel(i,j);//else remove the old ones |
el13drt | 12:eedda6554615 | 503 | } |
el13drt | 12:eedda6554615 | 504 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 505 | } |
el13drt | 12:eedda6554615 | 506 | |
el13drt | 35:9f55326c1b0a | 507 | //clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 508 | void refreshCursor2() |
el13drt | 12:eedda6554615 | 509 | { |
el13drt | 35:9f55326c1b0a | 510 | for (int i=70; i<80; i++)//loops through rows |
el13drt | 12:eedda6554615 | 511 | for (int j=25; j<32; j++) |
el13drt | 35:9f55326c1b0a | 512 | if (cells[i][j]) {//if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 513 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 514 | } else { |
el13drt | 35:9f55326c1b0a | 515 | lcd.clearPixel(i,j);//else remove the old ones |
el13drt | 12:eedda6554615 | 516 | } |
el13drt | 12:eedda6554615 | 517 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 518 | } |
el13drt | 12:eedda6554615 | 519 | |
el13drt | 35:9f55326c1b0a | 520 | //clears old pixels and keeps set pixels |
el13drt | 12:eedda6554615 | 521 | void refreshCursor3() |
el13drt | 12:eedda6554615 | 522 | { |
el13drt | 35:9f55326c1b0a | 523 | for (int i=70; i<80; i++)//loops through rows |
el13drt | 12:eedda6554615 | 524 | for (int j=32; j<40; j++) |
el13drt | 35:9f55326c1b0a | 525 | if (cells[i][j]) {//if there's a pixel then keep it |
el13drt | 12:eedda6554615 | 526 | lcd.setPixel(i,j); |
el13drt | 12:eedda6554615 | 527 | } else { |
el13drt | 35:9f55326c1b0a | 528 | lcd.clearPixel(i,j);//else remove the old ones |
el13drt | 12:eedda6554615 | 529 | } |
el13drt | 12:eedda6554615 | 530 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 531 | } |
el13drt | 12:eedda6554615 | 532 | |
el13drt | 35:9f55326c1b0a | 533 | //beep/light when buttons are closed |
el13drt | 17:242ccf6a8442 | 534 | void actionButtons() |
el13drt | 17:242ccf6a8442 | 535 | { |
el13drt | 17:242ccf6a8442 | 536 | if((FX == 0)&&(buttonA||buttonB)) { |
el13drt | 46:613eabb6ea86 | 537 | ledA = 1; |
el13drt | 17:242ccf6a8442 | 538 | buzzer.beep(1500,0.3); |
el13drt | 17:242ccf6a8442 | 539 | } |
el13drt | 17:242ccf6a8442 | 540 | if (buttonA || buttonB) { |
el13drt | 46:613eabb6ea86 | 541 | ledA = 1; |
el13drt | 17:242ccf6a8442 | 542 | } else { |
el13drt | 46:613eabb6ea86 | 543 | ledA = 0; |
el13drt | 17:242ccf6a8442 | 544 | } |
el13drt | 17:242ccf6a8442 | 545 | } |
el13drt | 17:242ccf6a8442 | 546 | |
el13drt | 35:9f55326c1b0a | 547 | //presents main menu options |
el13drt | 12:eedda6554615 | 548 | void mainMenu(int& mainOption) |
el13drt | 12:eedda6554615 | 549 | { |
el13drt | 17:242ccf6a8442 | 550 | actionButtons();//set audible/light for button |
el13drt | 12:eedda6554615 | 551 | |
el13drt | 35:9f55326c1b0a | 552 | //joystick selection |
el13drt | 14:c2c969e1c6e8 | 553 | if (printFlag) {//if flag set, clear flag and print joystick values to serial port |
el13drt | 12:eedda6554615 | 554 | printFlag = 0; |
el13drt | 12:eedda6554615 | 555 | |
el13drt | 35:9f55326c1b0a | 556 | //option up |
el13drt | 12:eedda6554615 | 557 | if (joystick.direction == UP) { |
el13drt | 12:eedda6554615 | 558 | serial.printf(" UP\n"); |
el13drt | 12:eedda6554615 | 559 | mainOption = mainOption--; |
el13drt | 12:eedda6554615 | 560 | if (mainOption < 0)mainOption = 0; |
el13drt | 12:eedda6554615 | 561 | } |
el13drt | 35:9f55326c1b0a | 562 | //option down |
el13drt | 12:eedda6554615 | 563 | if (joystick.direction == DOWN) { |
el13drt | 12:eedda6554615 | 564 | serial.printf(" DOWN\n"); |
el13drt | 12:eedda6554615 | 565 | mainOption = mainOption++; |
el13drt | 12:eedda6554615 | 566 | if (mainOption > 2)mainOption = 2; |
el13drt | 12:eedda6554615 | 567 | } |
el13drt | 35:9f55326c1b0a | 568 | //Centre / Unknown orientation |
el13drt | 12:eedda6554615 | 569 | if (joystick.direction == CENTRE) |
el13drt | 12:eedda6554615 | 570 | serial.printf(" CENTRE\n"); |
el13drt | 12:eedda6554615 | 571 | if (joystick.direction == UNKNOWN) |
el13drt | 12:eedda6554615 | 572 | serial.printf(" Unsupported direction\n"); |
el13drt | 12:eedda6554615 | 573 | |
el13drt | 35:9f55326c1b0a | 574 | //'Play Game' option 1 |
el13drt | 12:eedda6554615 | 575 | if (mainOption == 0) { |
el13drt | 12:eedda6554615 | 576 | lcd.printString("Play Game",3,4); |
el13drt | 12:eedda6554615 | 577 | } |
el13drt | 35:9f55326c1b0a | 578 | //'High Scores' option 2 |
el13drt | 12:eedda6554615 | 579 | if (mainOption == 1) { |
el13drt | 12:eedda6554615 | 580 | lcd.printString(" Scores ",3,4); |
el13drt | 12:eedda6554615 | 581 | } |
el13drt | 35:9f55326c1b0a | 582 | //'Options' option 3 |
el13drt | 12:eedda6554615 | 583 | if (mainOption == 2) { |
el13drt | 12:eedda6554615 | 584 | lcd.printString(" Options ",3,4); |
el13drt | 12:eedda6554615 | 585 | } |
el13drt | 12:eedda6554615 | 586 | } |
el13drt | 12:eedda6554615 | 587 | } |
el13drt | 12:eedda6554615 | 588 | |
el13drt | 35:9f55326c1b0a | 589 | //draws main menu |
el13drt | 12:eedda6554615 | 590 | void drawMainMenu() |
el13drt | 12:eedda6554615 | 591 | { |
el13drt | 12:eedda6554615 | 592 | //bottom border |
el13drt | 12:eedda6554615 | 593 | lcd.drawRect(0,47,84,0,1); |
el13drt | 12:eedda6554615 | 594 | //top border |
el13drt | 12:eedda6554615 | 595 | lcd.drawRect(0,0,84,2,1); |
el13drt | 12:eedda6554615 | 596 | |
el13drt | 12:eedda6554615 | 597 | //print 'Xtreme Tower' |
el13drt | 12:eedda6554615 | 598 | lcd.printString("Xtreme",4,25); |
el13drt | 12:eedda6554615 | 599 | lcd.printString("Tower",44,25); |
el13drt | 12:eedda6554615 | 600 | |
el13drt | 12:eedda6554615 | 601 | //title outline |
el13drt | 12:eedda6554615 | 602 | lcd.drawRect(3,6,77,10,0); |
el13drt | 12:eedda6554615 | 603 | |
el13drt | 12:eedda6554615 | 604 | ////castle //x, y, w, h, fill////////////////////// |
el13drt | 12:eedda6554615 | 605 | |
el13drt | 12:eedda6554615 | 606 | //castle main bulk |
el13drt | 12:eedda6554615 | 607 | lcd.drawRect(59,32,21,8,1); |
el13drt | 12:eedda6554615 | 608 | |
el13drt | 12:eedda6554615 | 609 | //left window bulk |
el13drt | 12:eedda6554615 | 610 | lcd.drawRect(59,22,2,10,1); |
el13drt | 12:eedda6554615 | 611 | //centre left window bulk |
el13drt | 12:eedda6554615 | 612 | lcd.drawRect(65,22,2,10,1); |
el13drt | 12:eedda6554615 | 613 | //centre right window bulk |
el13drt | 12:eedda6554615 | 614 | lcd.drawRect(72,22,2,10,1); |
el13drt | 12:eedda6554615 | 615 | //right window bulk |
el13drt | 12:eedda6554615 | 616 | lcd.drawRect(78,22,2,10,1); |
el13drt | 12:eedda6554615 | 617 | //central window bulk |
el13drt | 12:eedda6554615 | 618 | lcd.drawRect(68,25,3,7,1); |
el13drt | 12:eedda6554615 | 619 | |
el13drt | 12:eedda6554615 | 620 | //central window bulk |
el13drt | 12:eedda6554615 | 621 | lcd.drawRect(75,28,0,0,1); |
el13drt | 12:eedda6554615 | 622 | lcd.drawRect(77,28,0,0,1); |
el13drt | 12:eedda6554615 | 623 | lcd.drawRect(64,28,0,0,1); |
el13drt | 12:eedda6554615 | 624 | lcd.drawRect(62,28,0,0,1); |
el13drt | 12:eedda6554615 | 625 | |
el13drt | 12:eedda6554615 | 626 | //above left window bulk |
el13drt | 12:eedda6554615 | 627 | lcd.drawRect(62,25,3,2,1); |
el13drt | 12:eedda6554615 | 628 | //above right window bulk |
el13drt | 12:eedda6554615 | 629 | lcd.drawRect(75,25,2,2,1); |
el13drt | 12:eedda6554615 | 630 | |
el13drt | 12:eedda6554615 | 631 | //lower right line |
el13drt | 12:eedda6554615 | 632 | lcd.drawRect(71,42,9,0,1); |
el13drt | 12:eedda6554615 | 633 | //upper right line |
el13drt | 12:eedda6554615 | 634 | lcd.drawRect(70,41,10,0,1); |
el13drt | 12:eedda6554615 | 635 | |
el13drt | 12:eedda6554615 | 636 | //upper left line |
el13drt | 12:eedda6554615 | 637 | lcd.drawRect(59,41,10,0,1); |
el13drt | 12:eedda6554615 | 638 | //lower left line |
el13drt | 12:eedda6554615 | 639 | lcd.drawRect(59,42,9,0,1); |
el13drt | 12:eedda6554615 | 640 | |
el13drt | 12:eedda6554615 | 641 | //bottom left bulk |
el13drt | 12:eedda6554615 | 642 | lcd.drawRect(59,43,8,3,1); |
el13drt | 12:eedda6554615 | 643 | //bottom right bulk |
el13drt | 12:eedda6554615 | 644 | lcd.drawRect(72,43,8,3,1); |
el13drt | 12:eedda6554615 | 645 | |
el13drt | 12:eedda6554615 | 646 | //option arrows - lower |
el13drt | 12:eedda6554615 | 647 | lcd.drawRect(27,42,4,0,1); |
el13drt | 12:eedda6554615 | 648 | lcd.drawRect(28,43,2,0,1); |
el13drt | 12:eedda6554615 | 649 | lcd.drawRect(29,44,0,0,1); |
el13drt | 12:eedda6554615 | 650 | |
el13drt | 12:eedda6554615 | 651 | //option arrows - higher |
el13drt | 12:eedda6554615 | 652 | lcd.drawRect(27,29,4,0,1); |
el13drt | 12:eedda6554615 | 653 | lcd.drawRect(28,28,2,0,1); |
el13drt | 12:eedda6554615 | 654 | lcd.drawRect(29,27,0,0,1); |
el13drt | 12:eedda6554615 | 655 | } |
el13drt | 12:eedda6554615 | 656 | |
el13drt | 35:9f55326c1b0a | 657 | //presents exit menu options |
el13drt | 12:eedda6554615 | 658 | void exitMenu(int& exitOption) |
el13drt | 12:eedda6554615 | 659 | { |
el13drt | 17:242ccf6a8442 | 660 | actionButtons(); |
el13drt | 14:c2c969e1c6e8 | 661 | if (printFlag) {//if flag set, clear flag and print joystick values to serial port |
el13drt | 12:eedda6554615 | 662 | printFlag = 0; |
el13drt | 12:eedda6554615 | 663 | |
el13drt | 35:9f55326c1b0a | 664 | //check joystick direction |
el13drt | 12:eedda6554615 | 665 | if (joystick.direction == LEFT) { |
el13drt | 12:eedda6554615 | 666 | serial.printf(" LEFT\n"); |
el13drt | 12:eedda6554615 | 667 | exitOption--; |
el13drt | 12:eedda6554615 | 668 | if(exitOption < 0)exitOption = 0; |
el13drt | 12:eedda6554615 | 669 | } |
el13drt | 12:eedda6554615 | 670 | if (joystick.direction == RIGHT) { |
el13drt | 12:eedda6554615 | 671 | serial.printf(" RIGHT\n"); |
el13drt | 12:eedda6554615 | 672 | exitOption++; |
el13drt | 12:eedda6554615 | 673 | if(exitOption > 1)exitOption = 1; |
el13drt | 12:eedda6554615 | 674 | } |
el13drt | 12:eedda6554615 | 675 | if (joystick.direction == CENTRE) |
el13drt | 12:eedda6554615 | 676 | serial.printf(" CENTRE\n"); |
el13drt | 12:eedda6554615 | 677 | if (joystick.direction == UNKNOWN) |
el13drt | 12:eedda6554615 | 678 | serial.printf(" Unsupported direction\n"); |
el13drt | 12:eedda6554615 | 679 | } |
el13drt | 35:9f55326c1b0a | 680 | //draws option cursor |
el13drt | 12:eedda6554615 | 681 | if(exitOption == 0) { |
el13drt | 12:eedda6554615 | 682 | lcd.printString("YES",29,27); |
el13drt | 12:eedda6554615 | 683 | } |
el13drt | 12:eedda6554615 | 684 | if(exitOption == 1) { |
el13drt | 12:eedda6554615 | 685 | lcd.printString(" NO",29,27); |
el13drt | 12:eedda6554615 | 686 | } |
el13drt | 12:eedda6554615 | 687 | } |
el13drt | 12:eedda6554615 | 688 | |
el13drt | 35:9f55326c1b0a | 689 | //draws exit menu |
el13drt | 12:eedda6554615 | 690 | void drawExitMenu() |
el13drt | 12:eedda6554615 | 691 | { |
el13drt | 35:9f55326c1b0a | 692 | //set exit menu |
el13drt | 12:eedda6554615 | 693 | lcd.clear(); |
el13drt | 12:eedda6554615 | 694 | lcd.drawRect(8,6,70,30,0);//title outline |
el13drt | 12:eedda6554615 | 695 | lcd.printString("Exit Game?",10,25); |
el13drt | 35:9f55326c1b0a | 696 | drawBackground(); |
el13drt | 12:eedda6554615 | 697 | |
el13drt | 35:9f55326c1b0a | 698 | //option arrow - right |
el13drt | 12:eedda6554615 | 699 | lcd.drawRect(55,25,0,4,1); |
el13drt | 12:eedda6554615 | 700 | lcd.drawRect(56,26,0,2,1); |
el13drt | 12:eedda6554615 | 701 | lcd.drawRect(57,27,0,0,1); |
el13drt | 12:eedda6554615 | 702 | |
el13drt | 35:9f55326c1b0a | 703 | //option arrow - left |
el13drt | 12:eedda6554615 | 704 | lcd.drawRect(27,25,0,4,1); |
el13drt | 12:eedda6554615 | 705 | lcd.drawRect(26,26,0,2,1); |
el13drt | 12:eedda6554615 | 706 | lcd.drawRect(25,27,0,0,1); |
el13drt | 12:eedda6554615 | 707 | } |
el13drt | 12:eedda6554615 | 708 | |
el13drt | 35:9f55326c1b0a | 709 | //presents the options |
el13drt | 12:eedda6554615 | 710 | void optionsMenu(int& option) |
el13drt | 12:eedda6554615 | 711 | { |
el13drt | 35:9f55326c1b0a | 712 | //joystick selection |
el13drt | 12:eedda6554615 | 713 | if (printFlag) { //if flag set, clear flag and print joystick values to serial port |
el13drt | 12:eedda6554615 | 714 | printFlag = 0; |
el13drt | 12:eedda6554615 | 715 | |
el13drt | 35:9f55326c1b0a | 716 | //option up |
el13drt | 12:eedda6554615 | 717 | if (joystick.direction == UP) { |
el13drt | 12:eedda6554615 | 718 | serial.printf(" UP\n"); |
el13drt | 12:eedda6554615 | 719 | option = option--; |
el13drt | 12:eedda6554615 | 720 | if (option < 0)option = 0; |
el13drt | 12:eedda6554615 | 721 | } |
el13drt | 35:9f55326c1b0a | 722 | //option down |
el13drt | 12:eedda6554615 | 723 | if (joystick.direction == DOWN) { |
el13drt | 12:eedda6554615 | 724 | serial.printf(" DOWN\n"); |
el13drt | 12:eedda6554615 | 725 | option = option++; |
el13drt | 12:eedda6554615 | 726 | if (option > 1)option = 1; |
el13drt | 12:eedda6554615 | 727 | } |
el13drt | 35:9f55326c1b0a | 728 | //Centre / Unknown orientation |
el13drt | 12:eedda6554615 | 729 | if (joystick.direction == CENTRE) |
el13drt | 12:eedda6554615 | 730 | serial.printf(" CENTRE\n"); |
el13drt | 12:eedda6554615 | 731 | if (joystick.direction == UNKNOWN) |
el13drt | 12:eedda6554615 | 732 | serial.printf(" Unsupported direction\n"); |
el13drt | 12:eedda6554615 | 733 | |
el13drt | 35:9f55326c1b0a | 734 | //'Difficulty' option 1 |
el13drt | 12:eedda6554615 | 735 | if (option == 0) { |
el13drt | 12:eedda6554615 | 736 | lcd.drawCircle(72,27,2,1); |
el13drt | 12:eedda6554615 | 737 | refreshCursor3(); |
el13drt | 12:eedda6554615 | 738 | } |
el13drt | 35:9f55326c1b0a | 739 | //'Sound FX' option 2 |
el13drt | 12:eedda6554615 | 740 | if (option == 1) { |
el13drt | 12:eedda6554615 | 741 | lcd.drawCircle(72,35,2,1); |
el13drt | 12:eedda6554615 | 742 | refreshCursor2(); |
el13drt | 12:eedda6554615 | 743 | } |
el13drt | 12:eedda6554615 | 744 | } |
el13drt | 12:eedda6554615 | 745 | } |
el13drt | 12:eedda6554615 | 746 | |
el13drt | 35:9f55326c1b0a | 747 | //draws options menu |
el13drt | 12:eedda6554615 | 748 | void drawOptionsMenu() |
el13drt | 12:eedda6554615 | 749 | { |
el13drt | 12:eedda6554615 | 750 | lcd.clear();//clear screen |
el13drt | 35:9f55326c1b0a | 751 | drawBackground(); |
el13drt | 12:eedda6554615 | 752 | lcd.drawRect(3,6,77,10,0);//title outline |
el13drt | 12:eedda6554615 | 753 | lcd.drawRect(0,47,84,0,1);//bottom border |
el13drt | 12:eedda6554615 | 754 | lcd.drawRect(0,0,84,2,1);//top border |
el13drt | 12:eedda6554615 | 755 | lcd.printString("Options",20,7);//title |
el13drt | 12:eedda6554615 | 756 | lcd.printString("Difficulty",3,9); |
el13drt | 12:eedda6554615 | 757 | lcd.printString("Sound FX",3,10); |
el13drt | 12:eedda6554615 | 758 | } |
el13drt | 12:eedda6554615 | 759 | |
el13drt | 35:9f55326c1b0a | 760 | //present difficulty options |
el13drt | 12:eedda6554615 | 761 | void difficultyMenu(int& subOption) |
el13drt | 12:eedda6554615 | 762 | { |
el13drt | 17:242ccf6a8442 | 763 | actionButtons(); |
el13drt | 44:8ccea2400347 | 764 | //subOption = fall;//keeps cursor on selected |
el13drt | 45:644acbbb63ed | 765 | // //option even after exiting menu |
el13drt | 12:eedda6554615 | 766 | |
el13drt | 35:9f55326c1b0a | 767 | //joystick selection |
el13drt | 12:eedda6554615 | 768 | if (printFlag) {//if flag set, clear flag,print joystick values |
el13drt | 12:eedda6554615 | 769 | printFlag = 0; |
el13drt | 12:eedda6554615 | 770 | |
el13drt | 35:9f55326c1b0a | 771 | //option up |
el13drt | 12:eedda6554615 | 772 | if (joystick.direction == UP) { |
el13drt | 12:eedda6554615 | 773 | serial.printf(" UP\n"); |
el13drt | 12:eedda6554615 | 774 | subOption = subOption--; |
el13drt | 45:644acbbb63ed | 775 | if (subOption < 1)subOption = 1; |
el13drt | 12:eedda6554615 | 776 | } |
el13drt | 35:9f55326c1b0a | 777 | //option down |
el13drt | 12:eedda6554615 | 778 | if (joystick.direction == DOWN) { |
el13drt | 12:eedda6554615 | 779 | serial.printf(" DOWN\n"); |
el13drt | 12:eedda6554615 | 780 | subOption = subOption++; |
el13drt | 45:644acbbb63ed | 781 | if (subOption > 3)subOption = 3; |
el13drt | 12:eedda6554615 | 782 | } |
el13drt | 35:9f55326c1b0a | 783 | //Centre / Unknown orientation |
el13drt | 12:eedda6554615 | 784 | if (joystick.direction == CENTRE) |
el13drt | 12:eedda6554615 | 785 | serial.printf(" CENTRE\n"); |
el13drt | 12:eedda6554615 | 786 | if (joystick.direction == UNKNOWN) |
el13drt | 12:eedda6554615 | 787 | serial.printf(" Unsupported direction\n"); |
el13drt | 12:eedda6554615 | 788 | |
el13drt | 35:9f55326c1b0a | 789 | //'Easy' option 1 |
el13drt | 45:644acbbb63ed | 790 | if (subOption == 1) { |
el13drt | 12:eedda6554615 | 791 | lcd.drawCircle(72,19,2,1); |
el13drt | 12:eedda6554615 | 792 | refreshCursor2(); |
el13drt | 12:eedda6554615 | 793 | refreshCursor3(); |
el13drt | 18:c38e1966d870 | 794 | if(buttonFlagA) { //select easy |
el13drt | 18:c38e1966d870 | 795 | buttonFlagA = 0; |
el13drt | 12:eedda6554615 | 796 | fall = 1; |
el13drt | 12:eedda6554615 | 797 | } |
el13drt | 12:eedda6554615 | 798 | } |
el13drt | 35:9f55326c1b0a | 799 | //'Normal' option 2 |
el13drt | 45:644acbbb63ed | 800 | if (subOption == 2) { |
el13drt | 12:eedda6554615 | 801 | lcd.drawCircle(72,27,2,1); |
el13drt | 12:eedda6554615 | 802 | refreshCursor1(); |
el13drt | 12:eedda6554615 | 803 | refreshCursor3(); |
el13drt | 12:eedda6554615 | 804 | |
el13drt | 18:c38e1966d870 | 805 | if(buttonFlagA) { //select normal |
el13drt | 18:c38e1966d870 | 806 | buttonFlagA = 0; |
el13drt | 12:eedda6554615 | 807 | fall = 2; |
el13drt | 12:eedda6554615 | 808 | } |
el13drt | 12:eedda6554615 | 809 | } |
el13drt | 35:9f55326c1b0a | 810 | //'Forget It' option 3 |
el13drt | 45:644acbbb63ed | 811 | if (subOption == 3) { |
el13drt | 12:eedda6554615 | 812 | lcd.drawCircle(72,35,2,1); |
el13drt | 12:eedda6554615 | 813 | refreshCursor1(); |
el13drt | 12:eedda6554615 | 814 | refreshCursor2(); |
el13drt | 12:eedda6554615 | 815 | |
el13drt | 18:c38e1966d870 | 816 | if(buttonFlagA) { //select difficult |
el13drt | 18:c38e1966d870 | 817 | buttonFlagA = 0; |
el13drt | 12:eedda6554615 | 818 | fall = 3; |
el13drt | 12:eedda6554615 | 819 | } |
el13drt | 12:eedda6554615 | 820 | } |
el13drt | 12:eedda6554615 | 821 | } |
el13drt | 12:eedda6554615 | 822 | } |
el13drt | 12:eedda6554615 | 823 | |
el13drt | 35:9f55326c1b0a | 824 | //draw difficulty settings |
el13drt | 12:eedda6554615 | 825 | void drawDifficultyMenu() |
el13drt | 12:eedda6554615 | 826 | { |
el13drt | 12:eedda6554615 | 827 | lcd.clear(); |
el13drt | 35:9f55326c1b0a | 828 | drawBackground(); |
el13drt | 12:eedda6554615 | 829 | lcd.drawRect(0,47,84,0,1);//bottom border |
el13drt | 12:eedda6554615 | 830 | lcd.drawRect(0,0,84,2,1);//top border |
el13drt | 12:eedda6554615 | 831 | lcd.printString("*Difficulty*",5,7);//title |
el13drt | 12:eedda6554615 | 832 | lcd.printString("Easy",5,8);//title |
el13drt | 12:eedda6554615 | 833 | lcd.printString("Normal",5,9);//title |
el13drt | 12:eedda6554615 | 834 | lcd.printString("Forget It",5,10);//title |
el13drt | 12:eedda6554615 | 835 | } |
el13drt | 12:eedda6554615 | 836 | |
el13drt | 35:9f55326c1b0a | 837 | //present sound FX options |
el13drt | 13:a1b3a373c5a4 | 838 | void soundFXMenu(int& fxOption) |
el13drt | 13:a1b3a373c5a4 | 839 | { |
el13drt | 17:242ccf6a8442 | 840 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 841 | |
el13drt | 35:9f55326c1b0a | 842 | //joystick selection |
el13drt | 13:a1b3a373c5a4 | 843 | if (printFlag) {//if flag set, clear flag,print joystick values |
el13drt | 13:a1b3a373c5a4 | 844 | printFlag = 0; |
el13drt | 13:a1b3a373c5a4 | 845 | |
el13drt | 35:9f55326c1b0a | 846 | //option up |
el13drt | 13:a1b3a373c5a4 | 847 | if (joystick.direction == UP) { |
el13drt | 13:a1b3a373c5a4 | 848 | serial.printf(" UP\n"); |
el13drt | 13:a1b3a373c5a4 | 849 | fxOption = fxOption--; |
el13drt | 13:a1b3a373c5a4 | 850 | if (fxOption < 0)fxOption = 0; |
el13drt | 13:a1b3a373c5a4 | 851 | } |
el13drt | 35:9f55326c1b0a | 852 | //option down |
el13drt | 13:a1b3a373c5a4 | 853 | if (joystick.direction == DOWN) { |
el13drt | 13:a1b3a373c5a4 | 854 | serial.printf(" DOWN\n"); |
el13drt | 13:a1b3a373c5a4 | 855 | fxOption = fxOption++; |
el13drt | 20:993c12c6a7bc | 856 | if (fxOption > 1)fxOption = 1; |
el13drt | 13:a1b3a373c5a4 | 857 | } |
el13drt | 35:9f55326c1b0a | 858 | //Centre / Unknown orientation |
el13drt | 13:a1b3a373c5a4 | 859 | if (joystick.direction == CENTRE) |
el13drt | 13:a1b3a373c5a4 | 860 | serial.printf(" CENTRE\n"); |
el13drt | 13:a1b3a373c5a4 | 861 | if (joystick.direction == UNKNOWN) |
el13drt | 13:a1b3a373c5a4 | 862 | serial.printf(" Unsupported direction\n"); |
el13drt | 17:242ccf6a8442 | 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 | 36:b02b7935475a | 972 | //if initial 3 display selected character |
el13drt | 36:b02b7935475a | 973 | if (initial == 0) { |
el13drt | 36:b02b7935475a | 974 | x=fsm[state1].output1; |
el13drt | 36:b02b7935475a | 975 | lcd.printChar(x,25,26); |
el13drt | 36:b02b7935475a | 976 | } |
el13drt | 36:b02b7935475a | 977 | //if initial 1 display selected character |
el13drt | 36:b02b7935475a | 978 | if(initial == 1) { |
el13drt | 36:b02b7935475a | 979 | y=fsm[state2].output2; |
el13drt | 36:b02b7935475a | 980 | lcd.printChar(y,37,26); |
el13drt | 36:b02b7935475a | 981 | } |
el13drt | 36:b02b7935475a | 982 | //if initial 2 display selected character |
el13drt | 36:b02b7935475a | 983 | if(initial == 2) { |
el13drt | 36:b02b7935475a | 984 | z=fsm[state3].output3; |
el13drt | 36:b02b7935475a | 985 | lcd.printChar(z,49,26); |
el13drt | 36:b02b7935475a | 986 | } |
el13drt | 25:70048c7e02c7 | 987 | if(buttonFlagA) { |
el13drt | 40:56ab6d368e9b | 988 | actionButtons(); |
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 | highScore3 = score; |
el13drt | 35:9f55326c1b0a | 997 | sprintf (player3initials, "3.%c%c%c.....%i",x,y,z,highScore3); |
el13drt | 34:9dc844bdc776 | 998 | } |
el13drt | 35:9f55326c1b0a | 999 | //if player beats High Score 3 and 2, replace HighScore2 with new score |
el13drt | 34:9dc844bdc776 | 1000 | if(score >= highScore2 && score< highScore1) { |
el13drt | 34:9dc844bdc776 | 1001 | highScore3 = highScore2; |
el13drt | 34:9dc844bdc776 | 1002 | n = score; |
el13drt | 34:9dc844bdc776 | 1003 | highScore2 = n; |
el13drt | 48:e53a194661fa | 1004 | sprintf (player3initials, "3.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3); |
el13drt | 35:9f55326c1b0a | 1005 | sprintf (player2initials, "2.%c%c%c.....%i",x,y,z,highScore2); |
el13drt | 34:9dc844bdc776 | 1006 | } |
el13drt | 35:9f55326c1b0a | 1007 | //if player beats High Score 1, 2 and 3, replace highScore1 with new score |
el13drt | 34:9dc844bdc776 | 1008 | if(score >= highScore1 ) { |
el13drt | 34:9dc844bdc776 | 1009 | highScore3 = highScore2; |
el13drt | 34:9dc844bdc776 | 1010 | highScore2 = highScore1; |
el13drt | 34:9dc844bdc776 | 1011 | n = score; |
el13drt | 34:9dc844bdc776 | 1012 | highScore1 = n; |
el13drt | 48:e53a194661fa | 1013 | sprintf (player3initials, "3.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3); |
el13drt | 48:e53a194661fa | 1014 | sprintf (player2initials, "2.%c%c%c.....%i",player1initials[2],player1initials[3],player1initials[4],highScore2); |
el13drt | 35:9f55326c1b0a | 1015 | sprintf (player1initials, "1.%c%c%c.....%i",x,y,z,highScore1); |
el13drt | 34:9dc844bdc776 | 1016 | } |
el13drt | 40:56ab6d368e9b | 1017 | state1 = 0; |
el13drt | 40:56ab6d368e9b | 1018 | state2 = 0; |
el13drt | 40:56ab6d368e9b | 1019 | state3 = 0; |
el13drt | 21:1fbbd8ebb3d9 | 1020 | } |
el13drt | 21:1fbbd8ebb3d9 | 1021 | } |
el13drt | 21:1fbbd8ebb3d9 | 1022 | |
el13drt | 35:9f55326c1b0a | 1023 | //actual game |
el13drt | 12:eedda6554615 | 1024 | void game(int& exitFlag, int& exitOption) |
el13drt | 12:eedda6554615 | 1025 | { |
el13drt | 17:242ccf6a8442 | 1026 | actionButtons(); |
el13drt | 12:eedda6554615 | 1027 | lcd.clear();//clears screen |
el13drt | 35:9f55326c1b0a | 1028 | drawBackground();//draw background |
el13drt | 12:eedda6554615 | 1029 | |
el13drt | 12:eedda6554615 | 1030 | ///game/// |
el13drt | 12:eedda6554615 | 1031 | while(1) { |
el13drt | 35:9f55326c1b0a | 1032 | //print score - top left of display |
el13drt | 12:eedda6554615 | 1033 | char buffer[14];//create buffer for string |
el13drt | 12:eedda6554615 | 1034 | int length = sprintf(buffer,"Level:%d",score);//insert buffer |
el13drt | 30:a05628b65926 | 1035 | lcd.printString(buffer,3,0);//display |
el13drt | 12:eedda6554615 | 1036 | |
el13drt | 17:242ccf6a8442 | 1037 | actionButtons(); |
el13drt | 35:9f55326c1b0a | 1038 | drawNinja();//set character |
el13drt | 35:9f55326c1b0a | 1039 | drawHazards();//initiates hazards |
el13drt | 12:eedda6554615 | 1040 | hazardFall();//increments hazards towards floor |
el13drt | 12:eedda6554615 | 1041 | |
el13drt | 12:eedda6554615 | 1042 | if (printFlag) { //if flag set, clear flag and print joystick values to serial port |
el13drt | 12:eedda6554615 | 1043 | printFlag = 0; |
el13drt | 12:eedda6554615 | 1044 | |
el13drt | 35:9f55326c1b0a | 1045 | //check joystick direction |
el13drt | 12:eedda6554615 | 1046 | if (joystick.direction == LEFT) { |
el13drt | 12:eedda6554615 | 1047 | serial.printf(" LEFT\n"); |
el13drt | 12:eedda6554615 | 1048 | a1 = a1-=2; |
el13drt | 12:eedda6554615 | 1049 | a2 = a2-=2; |
el13drt | 12:eedda6554615 | 1050 | a3 = a3-=2; |
el13drt | 12:eedda6554615 | 1051 | a4 = a4-=2; |
el13drt | 12:eedda6554615 | 1052 | a5 = a5-=2; |
el13drt | 12:eedda6554615 | 1053 | a6 = a6-=2; |
el13drt | 12:eedda6554615 | 1054 | a7 = a7-=2; |
el13drt | 12:eedda6554615 | 1055 | a8 = a8-=2; |
el13drt | 12:eedda6554615 | 1056 | ninjaBoundaries(); |
el13drt | 12:eedda6554615 | 1057 | } |
el13drt | 12:eedda6554615 | 1058 | if (joystick.direction == RIGHT) { |
el13drt | 12:eedda6554615 | 1059 | serial.printf(" RIGHT\n"); |
el13drt | 12:eedda6554615 | 1060 | a1 = a1+=2; |
el13drt | 12:eedda6554615 | 1061 | a2 = a2+=2; |
el13drt | 12:eedda6554615 | 1062 | a3 = a3+=2; |
el13drt | 12:eedda6554615 | 1063 | a4 = a4+=2; |
el13drt | 12:eedda6554615 | 1064 | a5 = a5+=2; |
el13drt | 12:eedda6554615 | 1065 | a6 = a6+=2; |
el13drt | 12:eedda6554615 | 1066 | a7 = a7+=2; |
el13drt | 12:eedda6554615 | 1067 | a8 = a8+=2; |
el13drt | 12:eedda6554615 | 1068 | ninjaBoundaries(); |
el13drt | 12:eedda6554615 | 1069 | } |
el13drt | 12:eedda6554615 | 1070 | if (joystick.direction == CENTRE) |
el13drt | 12:eedda6554615 | 1071 | serial.printf(" CENTRE\n"); |
el13drt | 12:eedda6554615 | 1072 | if (joystick.direction == UNKNOWN) |
el13drt | 12:eedda6554615 | 1073 | serial.printf(" Unsupported direction\n"); |
el13drt | 22:e5bfe0e7c508 | 1074 | |
el13drt | 35:9f55326c1b0a | 1075 | //integer to represent character being |
el13drt | 35:9f55326c1b0a | 1076 | //struck by falling object |
el13drt | 12:eedda6554615 | 1077 | int contactPoint = 0; |
el13drt | 12:eedda6554615 | 1078 | |
el13drt | 35:9f55326c1b0a | 1079 | //contact points |
el13drt | 12:eedda6554615 | 1080 | if(lcd.getPixel((a1+4),32)) |
el13drt | 12:eedda6554615 | 1081 | contactPoint++; |
el13drt | 12:eedda6554615 | 1082 | if(lcd.getPixel((a1),32)) |
el13drt | 12:eedda6554615 | 1083 | contactPoint++; |
el13drt | 12:eedda6554615 | 1084 | if(lcd.getPixel((a7),32)) |
el13drt | 12:eedda6554615 | 1085 | contactPoint++; |
el13drt | 12:eedda6554615 | 1086 | |
el13drt | 35:9f55326c1b0a | 1087 | //if contact point is not zero |
el13drt | 35:9f55326c1b0a | 1088 | //character has been hit |
el13drt | 35:9f55326c1b0a | 1089 | //and the game ends |
el13drt | 12:eedda6554615 | 1090 | if ( contactPoint !=0) { |
el13drt | 12:eedda6554615 | 1091 | lcd.printString("Game Over",17,2); |
el13drt | 12:eedda6554615 | 1092 | lcd.inverseMode(); |
el13drt | 13:a1b3a373c5a4 | 1093 | buzzer.beep(2000,0.2);//frequeny/duration |
el13drt | 12:eedda6554615 | 1094 | wait(0.5); |
el13drt | 12:eedda6554615 | 1095 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 1096 | wait(0.5); |
el13drt | 12:eedda6554615 | 1097 | lcd.inverseMode(); |
el13drt | 13:a1b3a373c5a4 | 1098 | buzzer.beep(2000,0.2); |
el13drt | 12:eedda6554615 | 1099 | wait(0.5); |
el13drt | 12:eedda6554615 | 1100 | lcd.normalMode(); |
el13drt | 12:eedda6554615 | 1101 | wait(0.5); |
el13drt | 13:a1b3a373c5a4 | 1102 | lcd.inverseMode(); |
el13drt | 13:a1b3a373c5a4 | 1103 | buzzer.beep(2000,0.2); |
el13drt | 13:a1b3a373c5a4 | 1104 | wait(0.5); |
el13drt | 13:a1b3a373c5a4 | 1105 | lcd.normalMode(); |
el13drt | 30:a05628b65926 | 1106 | newScore();//enter initial screen if previous scores are beaten |
el13drt | 12:eedda6554615 | 1107 | resetGame(); |
el13drt | 12:eedda6554615 | 1108 | break; |
el13drt | 12:eedda6554615 | 1109 | } |
el13drt | 40:56ab6d368e9b | 1110 | lcd.refresh(); |
el13drt | 12:eedda6554615 | 1111 | startrek();//clears unset pixels, keeps set pixels |
el13drt | 17:242ccf6a8442 | 1112 | |
el13drt | 35:9f55326c1b0a | 1113 | ///Exit Menu (Back button pressed)/// |
el13drt | 18:c38e1966d870 | 1114 | if(buttonFlagB) { |
el13drt | 20:993c12c6a7bc | 1115 | buttonFlagB = 0;//reset flags |
el13drt | 20:993c12c6a7bc | 1116 | buttonFlagA = 0; |
el13drt | 17:242ccf6a8442 | 1117 | actionButtons(); |
el13drt | 12:eedda6554615 | 1118 | drawExitMenu();//draws the exit menu |
el13drt | 12:eedda6554615 | 1119 | |
el13drt | 12:eedda6554615 | 1120 | while(1) { |
el13drt | 12:eedda6554615 | 1121 | exitMenu(exitOption);//presents exit options |
el13drt | 40:56ab6d368e9b | 1122 | |
el13drt | 35:9f55326c1b0a | 1123 | //'exit' option YES |
el13drt | 18:c38e1966d870 | 1124 | if((buttonFlagA)&&(exitOption == 0)) { //returns to menu |
el13drt | 43:cf3f34502b2a | 1125 | exitOption = 1;//reset intial option value |
el13drt | 20:993c12c6a7bc | 1126 | buttonFlagA = 0;//reset flags |
el13drt | 20:993c12c6a7bc | 1127 | buttonFlagB = 0; |
el13drt | 17:242ccf6a8442 | 1128 | actionButtons(); |
el13drt | 12:eedda6554615 | 1129 | lcd.clear();//clears screen |
el13drt | 12:eedda6554615 | 1130 | resetGame();//resets scores/objects |
el13drt | 12:eedda6554615 | 1131 | exitFlag = 1;//sets exit flag |
el13drt | 12:eedda6554615 | 1132 | break; |
el13drt | 12:eedda6554615 | 1133 | } |
el13drt | 35:9f55326c1b0a | 1134 | //'exit' option NO - returns to game |
el13drt | 18:c38e1966d870 | 1135 | if((buttonFlagA)&&(exitOption == 1)) { |
el13drt | 20:993c12c6a7bc | 1136 | buttonFlagA = 0;//resets flags |
el13drt | 20:993c12c6a7bc | 1137 | buttonFlagB = 0; |
el13drt | 40:56ab6d368e9b | 1138 | |
el13drt | 12:eedda6554615 | 1139 | break; |
el13drt | 12:eedda6554615 | 1140 | } |
el13drt | 35:9f55326c1b0a | 1141 | sleep();//put while to sleep |
el13drt | 12:eedda6554615 | 1142 | } |
el13drt | 35:9f55326c1b0a | 1143 | //if 'exit' option YES, resets |
el13drt | 35:9f55326c1b0a | 1144 | //game values returns to main menu |
el13drt | 12:eedda6554615 | 1145 | if (exitFlag!=0) { //if exit flag set |
el13drt | 12:eedda6554615 | 1146 | exitFlag = 0;//reset flag |
el13drt | 12:eedda6554615 | 1147 | break;//break to main menu |
el13drt | 12:eedda6554615 | 1148 | } |
el13drt | 12:eedda6554615 | 1149 | } |
el13drt | 12:eedda6554615 | 1150 | serial.printf("Score: %i \n",score); |
el13drt | 12:eedda6554615 | 1151 | } |
el13drt | 12:eedda6554615 | 1152 | } |
el13drt | 12:eedda6554615 | 1153 | } |
el13drt | 12:eedda6554615 | 1154 | |
el13drt | 35:9f55326c1b0a | 1155 | //high scores screen |
el13drt | 12:eedda6554615 | 1156 | void scores() |
el13drt | 12:eedda6554615 | 1157 | { |
el13drt | 17:242ccf6a8442 | 1158 | actionButtons(); |
el13drt | 12:eedda6554615 | 1159 | lcd.clear();//clear screen |
el13drt | 35:9f55326c1b0a | 1160 | drawBackground();//set background |
el13drt | 12:eedda6554615 | 1161 | lcd.printString("High Scores",10,0);//title |
el13drt | 25:70048c7e02c7 | 1162 | |
el13drt | 35:9f55326c1b0a | 1163 | //prints scores with names |
el13drt | 30:a05628b65926 | 1164 | lcd.printString(player1initials,5,2);//display |
el13drt | 12:eedda6554615 | 1165 | |
el13drt | 30:a05628b65926 | 1166 | lcd.printString(player2initials,5,3);//display |
el13drt | 12:eedda6554615 | 1167 | |
el13drt | 30:a05628b65926 | 1168 | lcd.printString(player3initials,5,4);//display |
el13drt | 12:eedda6554615 | 1169 | |
el13drt | 12:eedda6554615 | 1170 | while(1) { |
el13drt | 17:242ccf6a8442 | 1171 | actionButtons();//select |
el13drt | 12:eedda6554615 | 1172 | |
el13drt | 35:9f55326c1b0a | 1173 | //back to menu |
el13drt | 18:c38e1966d870 | 1174 | if(buttonFlagB) { |
el13drt | 25:70048c7e02c7 | 1175 | buttonFlagA = 0;//reset flags |
el13drt | 25:70048c7e02c7 | 1176 | buttonFlagB = 0; |
el13drt | 12:eedda6554615 | 1177 | lcd.clear(); |
el13drt | 12:eedda6554615 | 1178 | break; |
el13drt | 12:eedda6554615 | 1179 | } |
el13drt | 35:9f55326c1b0a | 1180 | sleep();//put while to sleep |
el13drt | 12:eedda6554615 | 1181 | } |
el13drt | 13:a1b3a373c5a4 | 1182 | } |
el13drt | 13:a1b3a373c5a4 | 1183 | |
el13drt | 35:9f55326c1b0a | 1184 | //options menu |
el13drt | 14:c2c969e1c6e8 | 1185 | void optionsMenu() |
el13drt | 13:a1b3a373c5a4 | 1186 | { |
el13drt | 14:c2c969e1c6e8 | 1187 | int option = 0; |
el13drt | 45:644acbbb63ed | 1188 | int subOption = fall;//keeps cursor on selected option, even after exiting screen |
el13drt | 14:c2c969e1c6e8 | 1189 | int fxOption = 0; |
el13drt | 17:242ccf6a8442 | 1190 | actionButtons(); |
el13drt | 15:ff3eb0091453 | 1191 | drawOptionsMenu();//draws options menu |
el13drt | 35:9f55326c1b0a | 1192 | //counters for navigation |
el13drt | 17:242ccf6a8442 | 1193 | |
el13drt | 13:a1b3a373c5a4 | 1194 | while(1) { |
el13drt | 17:242ccf6a8442 | 1195 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 1196 | optionsMenu(option);//presents options |
el13drt | 13:a1b3a373c5a4 | 1197 | |
el13drt | 35:9f55326c1b0a | 1198 | //////////////////////difficulty menu //////////////////////////////////// |
el13drt | 15:ff3eb0091453 | 1199 | if ((option == 0)&&(buttonFlagA)) { |
el13drt | 15:ff3eb0091453 | 1200 | buttonFlagA = 0;//reset flag |
el13drt | 17:242ccf6a8442 | 1201 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 1202 | drawDifficultyMenu();//draws difficulty menu |
el13drt | 13:a1b3a373c5a4 | 1203 | |
el13drt | 13:a1b3a373c5a4 | 1204 | while(1) { |
el13drt | 17:242ccf6a8442 | 1205 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 1206 | difficultyMenu(subOption);//presents difficulty options |
el13drt | 13:a1b3a373c5a4 | 1207 | |
el13drt | 15:ff3eb0091453 | 1208 | if(buttonFlagB) { |
el13drt | 20:993c12c6a7bc | 1209 | buttonFlagB = 0;//reset flags |
el13drt | 20:993c12c6a7bc | 1210 | buttonFlagA = 0; |
el13drt | 15:ff3eb0091453 | 1211 | lcd.clear();//clear screen |
el13drt | 15:ff3eb0091453 | 1212 | break;//return back |
el13drt | 13:a1b3a373c5a4 | 1213 | } |
el13drt | 35:9f55326c1b0a | 1214 | sleep();//put while to sleep |
el13drt | 13:a1b3a373c5a4 | 1215 | } |
el13drt | 17:242ccf6a8442 | 1216 | drawOptionsMenu(); |
el13drt | 13:a1b3a373c5a4 | 1217 | } |
el13drt | 35:9f55326c1b0a | 1218 | /////////////////////sound FX menu ////////////////////////////////////// |
el13drt | 15:ff3eb0091453 | 1219 | if((option ==1)&&(buttonFlagA)) { |
el13drt | 20:993c12c6a7bc | 1220 | buttonFlagA = 0;//reset flags |
el13drt | 20:993c12c6a7bc | 1221 | buttonFlagB = 0; |
el13drt | 17:242ccf6a8442 | 1222 | actionButtons(); |
el13drt | 13:a1b3a373c5a4 | 1223 | drawSoundFXMenu();//draw menu |
el13drt | 13:a1b3a373c5a4 | 1224 | |
el13drt | 13:a1b3a373c5a4 | 1225 | while(1) { |
el13drt | 17:242ccf6a8442 | 1226 | actionButtons(); |
el13drt | 17:242ccf6a8442 | 1227 | soundFXMenu(fxOption);//presents options |
el13drt | 13:a1b3a373c5a4 | 1228 | |
el13drt | 35:9f55326c1b0a | 1229 | //back to options menu |
el13drt | 15:ff3eb0091453 | 1230 | if(buttonFlagB) { |
el13drt | 20:993c12c6a7bc | 1231 | buttonFlagB = 0;//reset flags |
el13drt | 20:993c12c6a7bc | 1232 | buttonFlagA = 0; |
el13drt | 15:ff3eb0091453 | 1233 | lcd.clear();//clear screen |
el13drt | 15:ff3eb0091453 | 1234 | break;//return back |
el13drt | 13:a1b3a373c5a4 | 1235 | } |
el13drt | 35:9f55326c1b0a | 1236 | sleep();//put while to sleep |
el13drt | 13:a1b3a373c5a4 | 1237 | } |
el13drt | 15:ff3eb0091453 | 1238 | drawOptionsMenu(); |
el13drt | 13:a1b3a373c5a4 | 1239 | } |
el13drt | 35:9f55326c1b0a | 1240 | //back to mainmenu |
el13drt | 15:ff3eb0091453 | 1241 | if(buttonFlagB) { |
el13drt | 20:993c12c6a7bc | 1242 | buttonFlagB = 0;//reset flags |
el13drt | 20:993c12c6a7bc | 1243 | buttonFlagA = 0; |
el13drt | 15:ff3eb0091453 | 1244 | lcd.clear();//clear |
el13drt | 15:ff3eb0091453 | 1245 | break;//return back |
el13drt | 13:a1b3a373c5a4 | 1246 | } |
el13drt | 35:9f55326c1b0a | 1247 | sleep();//put while to sleep |
el13drt | 13:a1b3a373c5a4 | 1248 | } |
el13drt | 13:a1b3a373c5a4 | 1249 | } |
el13drt | 13:a1b3a373c5a4 | 1250 | |
el13drt | 35:9f55326c1b0a | 1251 | //read default positions of the joystick to calibrate later readings |
el13drt | 13:a1b3a373c5a4 | 1252 | void calibrateJoystick() |
el13drt | 13:a1b3a373c5a4 | 1253 | { |
el13drt | 14:c2c969e1c6e8 | 1254 | joyButton.mode(PullDown); |
el13drt | 35:9f55326c1b0a | 1255 | //must not move during calibration |
el13drt | 13:a1b3a373c5a4 | 1256 | joystick.x0 = xPot; //initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
el13drt | 13:a1b3a373c5a4 | 1257 | joystick.y0 = yPot; |
el13drt | 13:a1b3a373c5a4 | 1258 | } |
el13drt | 13:a1b3a373c5a4 | 1259 | |
el13drt | 30:a05628b65926 | 1260 | //reads and updates position of joystick according to voltage readings |
el13drt | 13:a1b3a373c5a4 | 1261 | void updateJoystick() |
el13drt | 13:a1b3a373c5a4 | 1262 | { |
el13drt | 35:9f55326c1b0a | 1263 | //read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
el13drt | 13:a1b3a373c5a4 | 1264 | joystick.x = xPot - joystick.x0; |
el13drt | 13:a1b3a373c5a4 | 1265 | joystick.y = yPot - joystick.y0; |
el13drt | 35:9f55326c1b0a | 1266 | //read button state |
el13drt | 14:c2c969e1c6e8 | 1267 | joystick.button = joyButton; |
el13drt | 13:a1b3a373c5a4 | 1268 | |
el13drt | 35:9f55326c1b0a | 1269 | //calculate direction depending on x,y values |
el13drt | 35:9f55326c1b0a | 1270 | //tolerance allows a little lee-way in case joystick not exactly in the stated direction |
el13drt | 13:a1b3a373c5a4 | 1271 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1272 | joystick.direction = CENTRE; |
el13drt | 13:a1b3a373c5a4 | 1273 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13drt | 41:07d2afbfd3f4 | 1274 | joystick.direction = DOWN; |
el13drt | 13:a1b3a373c5a4 | 1275 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13drt | 41:07d2afbfd3f4 | 1276 | joystick.direction = UP; |
el13drt | 13:a1b3a373c5a4 | 1277 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1278 | joystick.direction = RIGHT; |
el13drt | 13:a1b3a373c5a4 | 1279 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13drt | 13:a1b3a373c5a4 | 1280 | joystick.direction = LEFT; |
el13drt | 13:a1b3a373c5a4 | 1281 | } else { |
el13drt | 13:a1b3a373c5a4 | 1282 | joystick.direction = UNKNOWN; |
el13drt | 13:a1b3a373c5a4 | 1283 | } |
el13drt | 13:a1b3a373c5a4 | 1284 | |
el13drt | 35:9f55326c1b0a | 1285 | //set flag for printing |
el13drt | 13:a1b3a373c5a4 | 1286 | printFlag = 1; |
el13drt | 12:eedda6554615 | 1287 | } |