Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
Abandoned.cpp@20:4ca04fd0965a, 2021-05-07 (annotated)
- Committer:
- ozy
- Date:
- Fri May 07 16:58:36 2021 +0000
- Revision:
- 20:4ca04fd0965a
- Parent:
- 18:22bda659c70a
- Child:
- 22:225d4031c090
Before changing to seamless version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ozy | 6:a1a7dc264fed | 1 | // This file includes abandoned code that was replaced |
ozy | 6:a1a7dc264fed | 2 | /* |
ozy | 20:4ca04fd0965a | 3 | void play_MK_theme() { |
ozy | 20:4ca04fd0965a | 4 | // Mortal Kombat Theme Tune |
ozy | 20:4ca04fd0965a | 5 | const int note_array[] = { |
ozy | 20:4ca04fd0965a | 6 | NOTE_A3, NOTE_A3, NOTE_C4, NOTE_A3, |
ozy | 20:4ca04fd0965a | 7 | NOTE_D4, NOTE_A3, NOTE_E4, NOTE_D4, |
ozy | 20:4ca04fd0965a | 8 | NOTE_C4, NOTE_C4, NOTE_E4, NOTE_C4, |
ozy | 20:4ca04fd0965a | 9 | NOTE_G4, NOTE_C4, NOTE_E4, NOTE_C4, |
ozy | 20:4ca04fd0965a | 10 | |
ozy | 20:4ca04fd0965a | 11 | NOTE_G3, NOTE_G3, NOTE_B3, NOTE_G3, |
ozy | 20:4ca04fd0965a | 12 | NOTE_C4, NOTE_G3, NOTE_D4, NOTE_C4, |
ozy | 20:4ca04fd0965a | 13 | NOTE_F3, NOTE_F3, NOTE_A3, NOTE_F3, |
ozy | 20:4ca04fd0965a | 14 | NOTE_C4, NOTE_F3, NOTE_C4, NOTE_B3, |
ozy | 20:4ca04fd0965a | 15 | // ***************************************** |
ozy | 20:4ca04fd0965a | 16 | NOTE_A3, NOTE_A3, NOTE_A3, NOTE_A3, |
ozy | 20:4ca04fd0965a | 17 | NOTE_G3, NOTE_C4, NOTE_A3, NOTE_A3, |
ozy | 20:4ca04fd0965a | 18 | NOTE_A3, NOTE_A3, NOTE_G3, NOTE_E3, |
ozy | 20:4ca04fd0965a | 19 | NOTE_A3, NOTE_A3, NOTE_A3, NOTE_A3, |
ozy | 20:4ca04fd0965a | 20 | |
ozy | 20:4ca04fd0965a | 21 | NOTE_G3, NOTE_C4, NOTE_A3, NOTE_A3, |
ozy | 20:4ca04fd0965a | 22 | NOTE_A3, NOTE_A3, NOTE_A3, NOTE_A3, |
ozy | 20:4ca04fd0965a | 23 | // ***************************************** |
ozy | 20:4ca04fd0965a | 24 | NOTE_A3, NOTE_E4, NOTE_A3, NOTE_C4, |
ozy | 20:4ca04fd0965a | 25 | NOTE_A3, NOTE_B3, NOTE_A3, NOTE_C4, |
ozy | 20:4ca04fd0965a | 26 | NOTE_A3, NOTE_B3, NOTE_G3, NOTE_A3, |
ozy | 20:4ca04fd0965a | 27 | NOTE_E4, NOTE_A3, NOTE_C4, NOTE_A3, |
ozy | 20:4ca04fd0965a | 28 | |
ozy | 20:4ca04fd0965a | 29 | NOTE_B3, NOTE_A3, NOTE_C4, NOTE_A3, |
ozy | 20:4ca04fd0965a | 30 | NOTE_B3, NOTE_G3, NOTE_A3, NOTE_E4, |
ozy | 20:4ca04fd0965a | 31 | NOTE_A3, NOTE_C4, NOTE_A3, NOTE_B3, |
ozy | 20:4ca04fd0965a | 32 | NOTE_A3, NOTE_C4, NOTE_A3, NOTE_B3, |
ozy | 20:4ca04fd0965a | 33 | |
ozy | 20:4ca04fd0965a | 34 | NOTE_G3, NOTE_A3, NOTE_E4, NOTE_A3, |
ozy | 20:4ca04fd0965a | 35 | NOTE_C4, NOTE_G3, NOTE_G3, NOTE_G3, |
ozy | 20:4ca04fd0965a | 36 | NOTE_A3, NOTE_A3, |
ozy | 20:4ca04fd0965a | 37 | }; |
ozy | 20:4ca04fd0965a | 38 | |
ozy | 20:4ca04fd0965a | 39 | // 8 corresponds to 1/8 |
ozy | 20:4ca04fd0965a | 40 | const int duration_array[] = { |
ozy | 20:4ca04fd0965a | 41 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 42 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 43 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 44 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 45 | |
ozy | 20:4ca04fd0965a | 46 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 47 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 48 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 49 | 7,7,7,7, |
ozy | 20:4ca04fd0965a | 50 | // ***************************************** |
ozy | 20:4ca04fd0965a | 51 | 5,5,5,5, |
ozy | 20:4ca04fd0965a | 52 | 7,7,5,5, |
ozy | 20:4ca04fd0965a | 53 | 5,5,7,7, |
ozy | 20:4ca04fd0965a | 54 | 5,5,5,5, |
ozy | 20:4ca04fd0965a | 55 | |
ozy | 20:4ca04fd0965a | 56 | 6,6,5,5, |
ozy | 20:4ca04fd0965a | 57 | 7,10,7,6, |
ozy | 20:4ca04fd0965a | 58 | // ***************************************** |
ozy | 20:4ca04fd0965a | 59 | 8,6,8,6, |
ozy | 20:4ca04fd0965a | 60 | 8,6,8,6, |
ozy | 20:4ca04fd0965a | 61 | 8,8,6,8, |
ozy | 20:4ca04fd0965a | 62 | 6,8,6,8, |
ozy | 20:4ca04fd0965a | 63 | |
ozy | 20:4ca04fd0965a | 64 | 6,8,6,8, |
ozy | 20:4ca04fd0965a | 65 | 8,6,8,6, |
ozy | 20:4ca04fd0965a | 66 | 8,6,8,6, |
ozy | 20:4ca04fd0965a | 67 | 8,6,8,8, |
ozy | 20:4ca04fd0965a | 68 | |
ozy | 20:4ca04fd0965a | 69 | 6,8,6,8, |
ozy | 20:4ca04fd0965a | 70 | 6,8,6,8, |
ozy | 20:4ca04fd0965a | 71 | 8,5, |
ozy | 20:4ca04fd0965a | 72 | }; |
ozy | 20:4ca04fd0965a | 73 | int n = sizeof(note_array)/sizeof(int); |
ozy | 20:4ca04fd0965a | 74 | // tell it the number of notes, arrays, BPM and whether to repeat |
ozy | 20:4ca04fd0965a | 75 | dac.play_melody(n,note_array,duration_array,110.0,true); |
ozy | 20:4ca04fd0965a | 76 | } |
ozy | 20:4ca04fd0965a | 77 | */ |
ozy | 20:4ca04fd0965a | 78 | /* |
ozy | 18:22bda659c70a | 79 | Function works but better solutions available |
ozy | 20:4ca04fd0965a | 80 | |
ozy | 18:22bda659c70a | 81 | void GameEngine::check_health(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC) { |
ozy | 18:22bda659c70a | 82 | int F = 40; |
ozy | 18:22bda659c70a | 83 | int E = 40; |
ozy | 18:22bda659c70a | 84 | int collision = check_collision(lcd, buttonA, buttonB, buttonC); |
ozy | 18:22bda659c70a | 85 | if (collision == 1) { |
ozy | 18:22bda659c70a | 86 | F -= 4; |
ozy | 18:22bda659c70a | 87 | printf("fighter take health bar \n"); |
ozy | 18:22bda659c70a | 88 | } |
ozy | 18:22bda659c70a | 89 | // enemy health bar |
ozy | 18:22bda659c70a | 90 | // x y w h, type |
ozy | 18:22bda659c70a | 91 | lcd.drawRect(0,0,E,8,FILL_BLACK); |
ozy | 18:22bda659c70a | 92 | lcd.drawRect(0,0,40,8,FILL_TRANSPARENT); |
ozy | 18:22bda659c70a | 93 | if (collision == 0) { |
ozy | 18:22bda659c70a | 94 | E -= 4; |
ozy | 18:22bda659c70a | 95 | printf("enemy take health bar \n"); |
ozy | 18:22bda659c70a | 96 | } |
ozy | 18:22bda659c70a | 97 | } |
ozy | 18:22bda659c70a | 98 | */ |
ozy | 18:22bda659c70a | 99 | |
ozy | 18:22bda659c70a | 100 | /* |
ozy | 18:22bda659c70a | 101 | int get_fighter_health() { |
ozy | 18:22bda659c70a | 102 | // check collision before reducing fighter's health |
ozy | 18:22bda659c70a | 103 | int collision = game.check_collision(lcd, buttonA, buttonB, buttonC); |
ozy | 18:22bda659c70a | 104 | int fighter_health = HEALTH; |
ozy | 18:22bda659c70a | 105 | int enemy_health = HEALTH; |
ozy | 18:22bda659c70a | 106 | int lost = 0; |
ozy | 18:22bda659c70a | 107 | if (collision == 0) { |
ozy | 18:22bda659c70a | 108 | fighter_health--; |
ozy | 18:22bda659c70a | 109 | printf("fighter health is = %i", enemy_health); |
ozy | 18:22bda659c70a | 110 | if (fighter_health == 0) { // if fighter has zero health, fighter is the loser |
ozy | 18:22bda659c70a | 111 | gameover(0); |
ozy | 18:22bda659c70a | 112 | lost = 0; |
ozy | 18:22bda659c70a | 113 | } |
ozy | 18:22bda659c70a | 114 | } |
ozy | 18:22bda659c70a | 115 | else if (collision == 1) { |
ozy | 18:22bda659c70a | 116 | enemy_health--; |
ozy | 18:22bda659c70a | 117 | printf("enemy health is = %i", enemy_health); |
ozy | 18:22bda659c70a | 118 | if (enemy_health == 0) { // if fighter has zero health, fighter is the loser |
ozy | 18:22bda659c70a | 119 | gameover(1); |
ozy | 18:22bda659c70a | 120 | lost = 1; |
ozy | 18:22bda659c70a | 121 | } |
ozy | 18:22bda659c70a | 122 | } |
ozy | 18:22bda659c70a | 123 | return lost; |
ozy | 18:22bda659c70a | 124 | } |
ozy | 18:22bda659c70a | 125 | |
ozy | 18:22bda659c70a | 126 | int GameEngine::get_fighter_health(int collision, int health) { |
ozy | 18:22bda659c70a | 127 | if (collision == 0) { |
ozy | 18:22bda659c70a | 128 | health--; |
ozy | 18:22bda659c70a | 129 | } |
ozy | 18:22bda659c70a | 130 | //printf("f health = %i\n", health); |
ozy | 18:22bda659c70a | 131 | return health; |
ozy | 18:22bda659c70a | 132 | } |
ozy | 18:22bda659c70a | 133 | |
ozy | 18:22bda659c70a | 134 | int GameEngine::get_enemy_health(int collision, int health) { |
ozy | 18:22bda659c70a | 135 | if (collision == 1) { |
ozy | 18:22bda659c70a | 136 | health--; |
ozy | 18:22bda659c70a | 137 | } |
ozy | 18:22bda659c70a | 138 | //printf("enemy health = %i\n", health); |
ozy | 18:22bda659c70a | 139 | return health; |
ozy | 18:22bda659c70a | 140 | } |
ozy | 18:22bda659c70a | 141 | |
ozy | 6:a1a7dc264fed | 142 | |
ozy | 6:a1a7dc264fed | 143 | void move_sprite() { |
ozy | 6:a1a7dc264fed | 144 | // getting joystick coordinates using read() function |
ozy | 6:a1a7dc264fed | 145 | // joystick centered at (0.50,0.50) with utmost left at (1.00,0.50) and utmost right being (0.00,0.50) |
ozy | 6:a1a7dc264fed | 146 | // read each of the pins |
ozy | 6:a1a7dc264fed | 147 | float x = joy_h.read(); |
ozy | 6:a1a7dc264fed | 148 | float y = joy_v.read(); |
ozy | 6:a1a7dc264fed | 149 | printf("x = %.2f | y = %.2f \n",x,y); |
ozy | 6:a1a7dc264fed | 150 | // get x position of fighter |
ozy | 6:a1a7dc264fed | 151 | int x_pos = fighter.get_x(); |
ozy | 6:a1a7dc264fed | 152 | printf("fighter x pos = %i \n", x_pos); |
ozy | 6:a1a7dc264fed | 153 | |
ozy | 6:a1a7dc264fed | 154 | / * |
ozy | 6:a1a7dc264fed | 155 | Code idea: |
ozy | 6:a1a7dc264fed | 156 | if joystick is not moved , display standing sprite |
ozy | 6:a1a7dc264fed | 157 | if joystick is moved right, toggle between runright and midrunright |
ozy | 6:a1a7dc264fed | 158 | if joystick is moved left, toggle between runleft and midrunleft |
ozy | 6:a1a7dc264fed | 159 | * / |
ozy | 6:a1a7dc264fed | 160 | if(x > 0.48 && x < 0.52) { // joystick not moved - we use ± 0.02 to take account of fluctuation in joystick tolerance and noice on ADC |
ozy | 6:a1a7dc264fed | 161 | fighter.draw(lcd); // draw standing fighter |
ozy | 6:a1a7dc264fed | 162 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 163 | wait(0.3); |
ozy | 6:a1a7dc264fed | 164 | |
ozy | 6:a1a7dc264fed | 165 | // Preform kick move if user presses button A |
ozy | 6:a1a7dc264fed | 166 | if (buttonA.read() == 1) { |
ozy | 6:a1a7dc264fed | 167 | fighter.kick_right(lcd); // draw kick on same coordinates as the standing sprite |
ozy | 6:a1a7dc264fed | 168 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 169 | wait(0.3); |
ozy | 6:a1a7dc264fed | 170 | } |
ozy | 6:a1a7dc264fed | 171 | // Preform punch move if user presses button B |
ozy | 6:a1a7dc264fed | 172 | if (buttonB.read() == 1) { |
ozy | 6:a1a7dc264fed | 173 | fighter.punch_right(lcd); // draw kick |
ozy | 6:a1a7dc264fed | 174 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 175 | wait(0.3); |
ozy | 6:a1a7dc264fed | 176 | } |
ozy | 6:a1a7dc264fed | 177 | // Guard if user presses button C |
ozy | 6:a1a7dc264fed | 178 | while (buttonC.read() == 1) { |
ozy | 6:a1a7dc264fed | 179 | fighter.guard(lcd); // draw guard move frame |
ozy | 6:a1a7dc264fed | 180 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 181 | wait(0.3); |
ozy | 6:a1a7dc264fed | 182 | } |
ozy | 6:a1a7dc264fed | 183 | } |
ozy | 6:a1a7dc264fed | 184 | else if(x < 0.48) { // joystick moved to the right |
ozy | 6:a1a7dc264fed | 185 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 6:a1a7dc264fed | 186 | fighter.add_x(5); // increment by 5 |
ozy | 6:a1a7dc264fed | 187 | fighter.move_right(lcd); |
ozy | 6:a1a7dc264fed | 188 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 189 | wait(0.1); |
ozy | 6:a1a7dc264fed | 190 | fighter.move_right2(lcd); |
ozy | 6:a1a7dc264fed | 191 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 192 | wait(0.08); |
ozy | 6:a1a7dc264fed | 193 | |
ozy | 6:a1a7dc264fed | 194 | if (x_pos >= 65) { // code to stop fighter moving out of lcd screen |
ozy | 6:a1a7dc264fed | 195 | fighter.add_x(-5); |
ozy | 6:a1a7dc264fed | 196 | } |
ozy | 6:a1a7dc264fed | 197 | |
ozy | 6:a1a7dc264fed | 198 | |
ozy | 6:a1a7dc264fed | 199 | // kick if user presses button A |
ozy | 6:a1a7dc264fed | 200 | if (buttonA.read() == 1) { |
ozy | 6:a1a7dc264fed | 201 | fighter.kick_right(lcd); |
ozy | 6:a1a7dc264fed | 202 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 203 | wait(0.3); |
ozy | 6:a1a7dc264fed | 204 | } |
ozy | 6:a1a7dc264fed | 205 | // Preform punch move if user presses button B |
ozy | 6:a1a7dc264fed | 206 | if (buttonB.read() == 1) { |
ozy | 6:a1a7dc264fed | 207 | fighter.punch_right(lcd); // draw kick on same coordinates as the sprite |
ozy | 6:a1a7dc264fed | 208 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 209 | wait(0.3); |
ozy | 6:a1a7dc264fed | 210 | } |
ozy | 6:a1a7dc264fed | 211 | // Guard if user presses button C |
ozy | 6:a1a7dc264fed | 212 | while (buttonC.read() == 1) { // we use while not if here because user would hold to guard |
ozy | 6:a1a7dc264fed | 213 | fighter.guard(lcd); // draw kick on same coordinates as the sprite |
ozy | 6:a1a7dc264fed | 214 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 215 | wait(0.3); |
ozy | 6:a1a7dc264fed | 216 | } |
ozy | 6:a1a7dc264fed | 217 | } |
ozy | 6:a1a7dc264fed | 218 | |
ozy | 6:a1a7dc264fed | 219 | else if(x > 0.52) { // joystick moved to the left |
ozy | 6:a1a7dc264fed | 220 | fighter.add_x(-5); // decrement left by 5 |
ozy | 6:a1a7dc264fed | 221 | fighter.move_left(lcd); |
ozy | 6:a1a7dc264fed | 222 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 223 | wait(0.1); |
ozy | 6:a1a7dc264fed | 224 | fighter.move_left2(lcd); |
ozy | 6:a1a7dc264fed | 225 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 226 | wait(0.08); |
ozy | 6:a1a7dc264fed | 227 | |
ozy | 6:a1a7dc264fed | 228 | if (x_pos <= 6) { // code to stop fighter moving out of lcd screen |
ozy | 6:a1a7dc264fed | 229 | fighter.add_x(5); |
ozy | 6:a1a7dc264fed | 230 | } |
ozy | 6:a1a7dc264fed | 231 | |
ozy | 6:a1a7dc264fed | 232 | // kick if user presses button A |
ozy | 6:a1a7dc264fed | 233 | if (buttonA.read() == 1) { |
ozy | 6:a1a7dc264fed | 234 | fighter.kick_left(lcd); |
ozy | 6:a1a7dc264fed | 235 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 236 | wait(0.3); |
ozy | 6:a1a7dc264fed | 237 | } |
ozy | 6:a1a7dc264fed | 238 | if (buttonB.read() == 1) { |
ozy | 6:a1a7dc264fed | 239 | fighter.punch_left(lcd); // draw punch on same coordinates as the sprite |
ozy | 6:a1a7dc264fed | 240 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 241 | wait(0.3); |
ozy | 6:a1a7dc264fed | 242 | } |
ozy | 6:a1a7dc264fed | 243 | // Guard if user presses button C |
ozy | 6:a1a7dc264fed | 244 | if (buttonC.read() == 1) { |
ozy | 6:a1a7dc264fed | 245 | fighter.guard(lcd); // draw guard on same coordinates as the sprite |
ozy | 6:a1a7dc264fed | 246 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 247 | wait(0.3); |
ozy | 6:a1a7dc264fed | 248 | } |
ozy | 6:a1a7dc264fed | 249 | } |
ozy | 6:a1a7dc264fed | 250 | } |
ozy | 6:a1a7dc264fed | 251 | |
ozy | 6:a1a7dc264fed | 252 | void move_enemy() { |
ozy | 6:a1a7dc264fed | 253 | // read each of the pins |
ozy | 6:a1a7dc264fed | 254 | float x = joy_h.read(); |
ozy | 6:a1a7dc264fed | 255 | float y = joy_v.read(); |
ozy | 6:a1a7dc264fed | 256 | printf("x = %.2f | y = %.2f \n",x,y); |
ozy | 6:a1a7dc264fed | 257 | // get x position of enemy |
ozy | 6:a1a7dc264fed | 258 | int x_pos = enemy.get_x(); |
ozy | 6:a1a7dc264fed | 259 | printf("enemy x pos = %i \n", x_pos); |
ozy | 6:a1a7dc264fed | 260 | |
ozy | 6:a1a7dc264fed | 261 | if(x > 0.48 && x < 0.52) { // joystick not moved - we use ± 0.02 to take account of fluctuation in joystick tolerance and noice on ADC |
ozy | 6:a1a7dc264fed | 262 | enemy.draw(lcd); // draw enemy |
ozy | 6:a1a7dc264fed | 263 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 264 | wait(0.3); |
ozy | 6:a1a7dc264fed | 265 | // Preform kick move if user presses button A |
ozy | 6:a1a7dc264fed | 266 | if (buttonA.read() == 1) { |
ozy | 6:a1a7dc264fed | 267 | enemy.kick_right(lcd); // draw kick on same coordinates as the standing sprite |
ozy | 6:a1a7dc264fed | 268 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 269 | wait(0.3); |
ozy | 6:a1a7dc264fed | 270 | } |
ozy | 6:a1a7dc264fed | 271 | // Preform punch move if user presses button B |
ozy | 6:a1a7dc264fed | 272 | if (buttonB.read() == 1) { |
ozy | 6:a1a7dc264fed | 273 | enemy.sword_right(lcd); // draw sword move on same coordinates as the standing sprite |
ozy | 6:a1a7dc264fed | 274 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 275 | wait(0.3); |
ozy | 6:a1a7dc264fed | 276 | } |
ozy | 6:a1a7dc264fed | 277 | } |
ozy | 6:a1a7dc264fed | 278 | else if(x < 0.48) { // joystick moved to the right |
ozy | 6:a1a7dc264fed | 279 | enemy.add_x(5); // increment by 5 |
ozy | 6:a1a7dc264fed | 280 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 6:a1a7dc264fed | 281 | enemy.move_right(lcd); // draw standing fighter, multiply by speed -69 (negative to correct for direction!) |
ozy | 6:a1a7dc264fed | 282 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 283 | wait(0.1); |
ozy | 6:a1a7dc264fed | 284 | enemy.move_right2(lcd); |
ozy | 6:a1a7dc264fed | 285 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 286 | wait(0.08); |
ozy | 6:a1a7dc264fed | 287 | |
ozy | 6:a1a7dc264fed | 288 | if (x_pos >= 65) { // code to stop enemy moving out of lcd screen |
ozy | 6:a1a7dc264fed | 289 | enemy.add_x(-5); |
ozy | 6:a1a7dc264fed | 290 | } |
ozy | 6:a1a7dc264fed | 291 | |
ozy | 6:a1a7dc264fed | 292 | // kick if user presses button A |
ozy | 6:a1a7dc264fed | 293 | if (buttonA.read() == 1) { |
ozy | 6:a1a7dc264fed | 294 | enemy.kick_right(lcd); |
ozy | 6:a1a7dc264fed | 295 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 296 | wait(0.3); |
ozy | 6:a1a7dc264fed | 297 | } |
ozy | 6:a1a7dc264fed | 298 | // Preform punch move if user presses button B |
ozy | 6:a1a7dc264fed | 299 | if (buttonB.read() == 1) { |
ozy | 6:a1a7dc264fed | 300 | enemy.sword_right(lcd); // draw kick on same coordinates as the sprite |
ozy | 6:a1a7dc264fed | 301 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 302 | wait(0.3); |
ozy | 6:a1a7dc264fed | 303 | } |
ozy | 6:a1a7dc264fed | 304 | } |
ozy | 6:a1a7dc264fed | 305 | else if(x > 0.52) { // joystick moved to the left |
ozy | 6:a1a7dc264fed | 306 | enemy.add_x(-5); // decrement by 5 |
ozy | 6:a1a7dc264fed | 307 | enemy.move_left(lcd); |
ozy | 6:a1a7dc264fed | 308 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 309 | wait(0.1); |
ozy | 6:a1a7dc264fed | 310 | enemy.move_left2(lcd); |
ozy | 6:a1a7dc264fed | 311 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 312 | wait(0.08); |
ozy | 6:a1a7dc264fed | 313 | |
ozy | 6:a1a7dc264fed | 314 | |
ozy | 6:a1a7dc264fed | 315 | if (x_pos <= 6) { // code to stop enemy moving out of lcd screen |
ozy | 6:a1a7dc264fed | 316 | enemy.add_x(5); |
ozy | 6:a1a7dc264fed | 317 | } |
ozy | 6:a1a7dc264fed | 318 | |
ozy | 6:a1a7dc264fed | 319 | // kick if user presses button A |
ozy | 6:a1a7dc264fed | 320 | if (buttonA.read() == 1) { |
ozy | 6:a1a7dc264fed | 321 | enemy.kick_left(lcd); |
ozy | 6:a1a7dc264fed | 322 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 323 | wait(0.3); |
ozy | 6:a1a7dc264fed | 324 | } |
ozy | 6:a1a7dc264fed | 325 | if (buttonB.read() == 1) { |
ozy | 6:a1a7dc264fed | 326 | enemy.sword_left(lcd); // draw kick on same coordinates as the sprite |
ozy | 6:a1a7dc264fed | 327 | lcd.refresh(); |
ozy | 6:a1a7dc264fed | 328 | wait(0.3); |
ozy | 6:a1a7dc264fed | 329 | } |
ozy | 6:a1a7dc264fed | 330 | } |
ozy | 6:a1a7dc264fed | 331 | } |
ozy | 0:99b49fd71085 | 332 | |
ozy | 0:99b49fd71085 | 333 | /* |
ozy | 6:a1a7dc264fed | 334 | |
ozy | 3:1d99b6ad4f9e | 335 | int get_user_input(int &input) { |
ozy | 3:1d99b6ad4f9e | 336 | while (1) { |
ozy | 3:1d99b6ad4f9e | 337 | if (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 338 | input = 1; |
ozy | 3:1d99b6ad4f9e | 339 | break; |
ozy | 3:1d99b6ad4f9e | 340 | } |
ozy | 3:1d99b6ad4f9e | 341 | if (buttonB.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 342 | input = 2; |
ozy | 3:1d99b6ad4f9e | 343 | break; |
ozy | 3:1d99b6ad4f9e | 344 | } |
ozy | 3:1d99b6ad4f9e | 345 | if (buttonC.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 346 | input = 3; |
ozy | 3:1d99b6ad4f9e | 347 | break; |
ozy | 3:1d99b6ad4f9e | 348 | } |
ozy | 3:1d99b6ad4f9e | 349 | if (buttonD.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 350 | input = 4; |
ozy | 3:1d99b6ad4f9e | 351 | break; |
ozy | 3:1d99b6ad4f9e | 352 | } |
ozy | 3:1d99b6ad4f9e | 353 | } |
ozy | 3:1d99b6ad4f9e | 354 | return input; |
ozy | 3:1d99b6ad4f9e | 355 | } |
ozy | 3:1d99b6ad4f9e | 356 | |
ozy | 3:1d99b6ad4f9e | 357 | |
ozy | 3:1d99b6ad4f9e | 358 | |
ozy | 0:99b49fd71085 | 359 | void abandoned_function() { |
ozy | 0:99b49fd71085 | 360 | // getting joystick coordinates using Vector2D |
ozy | 0:99b49fd71085 | 361 | // joystick centered at 0 with (1,0) being utmost right and (-1,0) being utmost left |
ozy | 0:99b49fd71085 | 362 | Vector2D coord = joystick.get_coord(); |
ozy | 0:99b49fd71085 | 363 | printf("Coord = %f | %f\n", coord.x, coord.y); |
ozy | 0:99b49fd71085 | 364 | float x = coord.x; |
ozy | 0:99b49fd71085 | 365 | float y = coord.y; |
ozy | 0:99b49fd71085 | 366 | |
ozy | 0:99b49fd71085 | 367 | loop idea: |
ozy | 0:99b49fd71085 | 368 | while nothing pressed, display standsprite |
ozy | 0:99b49fd71085 | 369 | while moving right, toggle between runright and midrunright |
ozy | 0:99b49fd71085 | 370 | while moving left, toggle between runleft and midrunleft |
ozy | 0:99b49fd71085 | 371 | |
ozy | 0:99b49fd71085 | 372 | while (x > -0.02 && x < 0.02) { // joystick not moved - we use ± 0.02 to take account of fluctuation in joystick input |
ozy | 0:99b49fd71085 | 373 | fighter.draw(lcd, x, 34); // we write 34 as the y-coordinate to place sprite exactly on ground ( 46 - 12(height of sprite) = 34 ) |
ozy | 0:99b49fd71085 | 374 | printf("*Coord = %f | %f\n",x, y); |
ozy | 0:99b49fd71085 | 375 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 376 | } |
ozy | 0:99b49fd71085 | 377 | |
ozy | 0:99b49fd71085 | 378 | while (x > 0.02) { // Joystick moved to the right |
ozy | 0:99b49fd71085 | 379 | // toggle between the 2 move_right animations with a small delay in between to make it smooth |
ozy | 0:99b49fd71085 | 380 | fighter.update_forward(x); |
ozy | 0:99b49fd71085 | 381 | printf("**Coord = %f | %f\n", x, y); |
ozy | 0:99b49fd71085 | 382 | fighter.move_right(lcd, x, 34); |
ozy | 0:99b49fd71085 | 383 | wait(0.2); |
ozy | 0:99b49fd71085 | 384 | fighter.update_forward(x); |
ozy | 0:99b49fd71085 | 385 | fighter.move_right2(lcd, x, 34); |
ozy | 0:99b49fd71085 | 386 | wait(0.2); |
ozy | 0:99b49fd71085 | 387 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 388 | } |
ozy | 0:99b49fd71085 | 389 | while (x < -0.02) { // Joystick moved to the left |
ozy | 0:99b49fd71085 | 390 | // toggle between the 2 move_left animations with a small delay in between to make it smooth |
ozy | 0:99b49fd71085 | 391 | fighter.update_backward(x); |
ozy | 0:99b49fd71085 | 392 | printf("***Coord = %f | %f\n", x, y); |
ozy | 0:99b49fd71085 | 393 | fighter.move_left(lcd, x, 34); |
ozy | 0:99b49fd71085 | 394 | wait(0.2); |
ozy | 0:99b49fd71085 | 395 | fighter.update_backward(x); |
ozy | 0:99b49fd71085 | 396 | fighter.move_left2(lcd, x, 34); |
ozy | 0:99b49fd71085 | 397 | wait(0.2); |
ozy | 0:99b49fd71085 | 398 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 399 | } |
ozy | 0:99b49fd71085 | 400 | } |
ozy | 0:99b49fd71085 | 401 | |
ozy | 0:99b49fd71085 | 402 | |
ozy | 0:99b49fd71085 | 403 | const int logo[17][19] = { |
ozy | 0:99b49fd71085 | 404 | |
ozy | 0:99b49fd71085 | 405 | { 1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1 }, |
ozy | 0:99b49fd71085 | 406 | { 0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 407 | { 0,0,0,0,0,1,0,1,1,1,0,0,0,0,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 408 | { 0,0,0,0,1,0,1,1,1,1,1,0,0,0,0,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 409 | { 0,0,0,1,0,1,1,0,0,1,1,1,1,0,0,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 410 | { 0,0,1,0,1,1,1,0,1,1,1,1,1,1,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 411 | { 0,0,1,0,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 412 | { 0,0,1,0,1,1,1,0,0,0,1,1,1,1,1,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 413 | { 0,0,1,0,1,1,1,1,0,0,0,0,0,1,1,1,0,1,0 }, |
ozy | 0:99b49fd71085 | 414 | { 0,0,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 415 | { 0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 416 | { 0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,0,1,0 }, |
ozy | 0:99b49fd71085 | 417 | { 0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 418 | { 0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 419 | { 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 420 | { 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 421 | { 1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1 }, |
ozy | 0:99b49fd71085 | 422 | |
ozy | 0:99b49fd71085 | 423 | }; |
ozy | 0:99b49fd71085 | 424 | */ |
ozy | 1:3bdadf6f6dbd | 425 | |
ozy | 1:3bdadf6f6dbd | 426 | /* |
ozy | 1:3bdadf6f6dbd | 427 | void menu_select() { |
ozy | 1:3bdadf6f6dbd | 428 | char button = get_user_input(); |
ozy | 1:3bdadf6f6dbd | 429 | switch (button) { |
ozy | 1:3bdadf6f6dbd | 430 | case 'A': |
ozy | 1:3bdadf6f6dbd | 431 | menu.play(lcd); |
ozy | 1:3bdadf6f6dbd | 432 | break; |
ozy | 1:3bdadf6f6dbd | 433 | case 'B': |
ozy | 1:3bdadf6f6dbd | 434 | menu.tutorial(lcd); |
ozy | 1:3bdadf6f6dbd | 435 | break; |
ozy | 1:3bdadf6f6dbd | 436 | case 'C': |
ozy | 1:3bdadf6f6dbd | 437 | menu.options_menu(lcd); |
ozy | 1:3bdadf6f6dbd | 438 | break; |
ozy | 1:3bdadf6f6dbd | 439 | default: |
ozy | 1:3bdadf6f6dbd | 440 | menu.homescreen(lcd); |
ozy | 1:3bdadf6f6dbd | 441 | break; |
ozy | 1:3bdadf6f6dbd | 442 | } |
ozy | 1:3bdadf6f6dbd | 443 | } |
ozy | 1:3bdadf6f6dbd | 444 | |
ozy | 1:3bdadf6f6dbd | 445 | char get_user_input() { |
ozy | 1:3bdadf6f6dbd | 446 | char button; |
ozy | 1:3bdadf6f6dbd | 447 | while (buttonA.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 448 | button = 'A'; |
ozy | 1:3bdadf6f6dbd | 449 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 450 | } |
ozy | 1:3bdadf6f6dbd | 451 | while (buttonB.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 452 | button = 'B'; |
ozy | 1:3bdadf6f6dbd | 453 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 454 | } |
ozy | 1:3bdadf6f6dbd | 455 | while (buttonC.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 456 | button = 'C'; |
ozy | 1:3bdadf6f6dbd | 457 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 458 | } |
ozy | 1:3bdadf6f6dbd | 459 | while (buttonD.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 460 | button = 'D'; |
ozy | 1:3bdadf6f6dbd | 461 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 462 | } |
ozy | 1:3bdadf6f6dbd | 463 | pc.printf("Value is %c", button); |
ozy | 1:3bdadf6f6dbd | 464 | return button; |
ozy | 1:3bdadf6f6dbd | 465 | } |
ozy | 1:3bdadf6f6dbd | 466 | */ |
ozy | 20:4ca04fd0965a | 467 | |
ozy | 20:4ca04fd0965a | 468 | /* |
ozy | 20:4ca04fd0965a | 469 | // function - gets user input from buttons A B C D |
ozy | 20:4ca04fd0965a | 470 | int Menu::get_user_input(int &input, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD) { |
ozy | 20:4ca04fd0965a | 471 | // we use address-of operator for int input to pass the actual reference for the input value not just a copy of it |
ozy | 20:4ca04fd0965a | 472 | while (1) { |
ozy | 20:4ca04fd0965a | 473 | if (buttonA.read() == 1) { |
ozy | 20:4ca04fd0965a | 474 | input = 1; |
ozy | 20:4ca04fd0965a | 475 | break; |
ozy | 20:4ca04fd0965a | 476 | } |
ozy | 20:4ca04fd0965a | 477 | if (buttonB.read() == 1) { |
ozy | 20:4ca04fd0965a | 478 | input = 2; |
ozy | 20:4ca04fd0965a | 479 | break; |
ozy | 20:4ca04fd0965a | 480 | } |
ozy | 20:4ca04fd0965a | 481 | if (buttonC.read() == 1) { |
ozy | 20:4ca04fd0965a | 482 | input = 3; |
ozy | 20:4ca04fd0965a | 483 | break; |
ozy | 20:4ca04fd0965a | 484 | } |
ozy | 20:4ca04fd0965a | 485 | if (buttonD.read() == 1) { |
ozy | 20:4ca04fd0965a | 486 | input = 4; |
ozy | 20:4ca04fd0965a | 487 | break; |
ozy | 20:4ca04fd0965a | 488 | } |
ozy | 20:4ca04fd0965a | 489 | } |
ozy | 20:4ca04fd0965a | 490 | printf("input is %i", input); |
ozy | 20:4ca04fd0965a | 491 | return input; |
ozy | 20:4ca04fd0965a | 492 | } |
ozy | 20:4ca04fd0965a | 493 | */ |
ozy | 20:4ca04fd0965a | 494 | |
ozy | 20:4ca04fd0965a | 495 | /* |
ozy | 20:4ca04fd0965a | 496 | void Menu::menu_selection(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD) { |
ozy | 20:4ca04fd0965a | 497 | // this function takes the user input and selects the relevant menu item |
ozy | 20:4ca04fd0965a | 498 | |
ozy | 20:4ca04fd0965a | 499 | while (1) { |
ozy | 20:4ca04fd0965a | 500 | // working code |
ozy | 20:4ca04fd0965a | 501 | if (buttonA.read() == 1) { // we use if statement instead of while loop so that button input do not interfere with the next menu, only this one! |
ozy | 20:4ca04fd0965a | 502 | play(lcd); |
ozy | 20:4ca04fd0965a | 503 | break; |
ozy | 20:4ca04fd0965a | 504 | } |
ozy | 20:4ca04fd0965a | 505 | if (buttonB.read() == 1) { |
ozy | 20:4ca04fd0965a | 506 | tutorial(lcd); |
ozy | 20:4ca04fd0965a | 507 | break; |
ozy | 20:4ca04fd0965a | 508 | } |
ozy | 20:4ca04fd0965a | 509 | if (buttonC.read() == 1) { |
ozy | 20:4ca04fd0965a | 510 | options_menu(lcd, buttonA, buttonB, buttonC); |
ozy | 20:4ca04fd0965a | 511 | break; |
ozy | 20:4ca04fd0965a | 512 | } |
ozy | 20:4ca04fd0965a | 513 | if (buttonD.read() == 1) { // user can click button D to return to main menu |
ozy | 20:4ca04fd0965a | 514 | homescreen(lcd); |
ozy | 20:4ca04fd0965a | 515 | continue; |
ozy | 20:4ca04fd0965a | 516 | } |
ozy | 20:4ca04fd0965a | 517 | } |
ozy | 20:4ca04fd0965a | 518 | } |
ozy | 20:4ca04fd0965a | 519 | */ |
ozy | 20:4ca04fd0965a | 520 | |
ozy | 20:4ca04fd0965a | 521 | /* |
ozy | 20:4ca04fd0965a | 522 | if (input == 1){ // button A pressed |
ozy | 20:4ca04fd0965a | 523 | play(lcd); |
ozy | 20:4ca04fd0965a | 524 | } |
ozy | 20:4ca04fd0965a | 525 | else if (input == 2) { // button B pressed |
ozy | 20:4ca04fd0965a | 526 | tutorial(lcd); |
ozy | 20:4ca04fd0965a | 527 | } |
ozy | 20:4ca04fd0965a | 528 | else if (input == 3) { // button C pressed |
ozy | 20:4ca04fd0965a | 529 | options_menu(lcd, buttonA, buttonB, buttonC, buttonD); |
ozy | 20:4ca04fd0965a | 530 | } |
ozy | 20:4ca04fd0965a | 531 | else if (input == 4) { // button D pressed - return back to main menu |
ozy | 20:4ca04fd0965a | 532 | homescreen(lcd); |
ozy | 20:4ca04fd0965a | 533 | } |
ozy | 20:4ca04fd0965a | 534 | */ |
ozy | 20:4ca04fd0965a | 535 | // ****************************************************************************************************************************** |