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