Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Committer:
ozy
Date:
Tue Apr 27 23:21:21 2021 +0000
Revision:
17:928edcdd1058
Parent:
15:7fd2d34f3be5
Child:
18:22bda659c70a
Fighter and Enemy Collision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ozy 0:99b49fd71085 1 #include "Fighter.h"
ozy 0:99b49fd71085 2 #include "mbed.h"
ozy 9:8c840b08a633 3 #include <stdlib.h>
ozy 0:99b49fd71085 4
ozy 0:99b49fd71085 5 Fighter::Fighter() {}
ozy 0:99b49fd71085 6
ozy 3:1d99b6ad4f9e 7 int Fighter::get_x() {
ozy 3:1d99b6ad4f9e 8 return _x;
ozy 3:1d99b6ad4f9e 9 }
ozy 3:1d99b6ad4f9e 10
ozy 3:1d99b6ad4f9e 11 int Fighter::get_y() {
ozy 3:1d99b6ad4f9e 12 return _y;
ozy 3:1d99b6ad4f9e 13 }
ozy 3:1d99b6ad4f9e 14
ozy 7:737fb0c3dbef 15 void Fighter::init() {
ozy 7:737fb0c3dbef 16 // initialize fighter position
ozy 7:737fb0c3dbef 17 set_x(15);
ozy 10:e83899f11e8a 18 set_y(34);
ozy 7:737fb0c3dbef 19 }
ozy 7:737fb0c3dbef 20
ozy 3:1d99b6ad4f9e 21 void Fighter::set_x(int x) {
ozy 3:1d99b6ad4f9e 22 _x = x;
ozy 3:1d99b6ad4f9e 23 }
ozy 3:1d99b6ad4f9e 24
ozy 3:1d99b6ad4f9e 25 void Fighter::set_y(int y) {
ozy 3:1d99b6ad4f9e 26 _y = y;
ozy 3:1d99b6ad4f9e 27 }
ozy 3:1d99b6ad4f9e 28
ozy 7:737fb0c3dbef 29 void Fighter::add_x(int x) {
ozy 3:1d99b6ad4f9e 30 _x += x;
ozy 3:1d99b6ad4f9e 31 }
ozy 3:1d99b6ad4f9e 32
ozy 7:737fb0c3dbef 33 void Fighter::add_y(int y) {
ozy 3:1d99b6ad4f9e 34 _y += y;
ozy 3:1d99b6ad4f9e 35 }
ozy 3:1d99b6ad4f9e 36
ozy 9:8c840b08a633 37
ozy 7:737fb0c3dbef 38 void Fighter::draw(N5110 &lcd) { // drawing standing Fighter
ozy 0:99b49fd71085 39
ozy 0:99b49fd71085 40 const int standsprite[12][10] = {
ozy 0:99b49fd71085 41 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 42 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 43 { 0,0,1,1,1,1,1,1,0,0 },
ozy 0:99b49fd71085 44 { 0,0,1,0,1,1,0,1,0,0 },
ozy 0:99b49fd71085 45 { 0,1,1,0,1,1,0,1,1,0 },
ozy 0:99b49fd71085 46 { 0,1,0,0,1,1,0,0,1,0 },
ozy 0:99b49fd71085 47 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 48 { 0,0,1,1,0,0,1,1,0,0 },
ozy 0:99b49fd71085 49 { 0,0,1,0,0,0,0,1,0,0 },
ozy 0:99b49fd71085 50 { 0,1,1,0,0,0,0,1,1,0 },
ozy 0:99b49fd71085 51 { 0,1,0,0,0,0,0,0,1,0 },
ozy 0:99b49fd71085 52 { 0,1,0,0,0,0,0,0,1,0 },
ozy 0:99b49fd71085 53 };
ozy 7:737fb0c3dbef 54
ozy 7:737fb0c3dbef 55 lcd.drawSprite(_x,_y,12,10,(int *)standsprite);
ozy 7:737fb0c3dbef 56 }
ozy 7:737fb0c3dbef 57
ozy 7:737fb0c3dbef 58 // ************************************************************************
ozy 7:737fb0c3dbef 59
ozy 7:737fb0c3dbef 60 void Fighter::move_left(N5110 &lcd){
ozy 0:99b49fd71085 61 const int run_left[12][10] = {
ozy 0:99b49fd71085 62 { 0,0,1,1,1,0,0,0,0,0 },
ozy 0:99b49fd71085 63 { 0,0,1,1,1,0,0,0,0,0 },
ozy 0:99b49fd71085 64 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 65 { 0,0,1,0,1,1,1,1,0,0 },
ozy 0:99b49fd71085 66 { 1,1,1,0,1,1,0,1,1,0 },
ozy 0:99b49fd71085 67 { 0,0,0,0,1,1,0,0,0,1 },
ozy 0:99b49fd71085 68 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 69 { 0,0,1,1,0,0,1,1,0,0 },
ozy 0:99b49fd71085 70 { 0,1,1,0,0,0,1,1,1,1 },
ozy 0:99b49fd71085 71 { 0,0,1,1,0,0,0,0,0,0 },
ozy 0:99b49fd71085 72 { 0,0,0,1,1,0,0,0,0,0 },
ozy 0:99b49fd71085 73 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 74 };
ozy 7:737fb0c3dbef 75
ozy 7:737fb0c3dbef 76 const int midrun_left[12][10] = {
ozy 0:99b49fd71085 77 { 0,0,1,1,1,0,0,0,0,0 },
ozy 0:99b49fd71085 78 { 0,0,1,1,1,0,0,0,0,0 },
ozy 0:99b49fd71085 79 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 80 { 0,0,1,0,1,1,1,1,0,0 },
ozy 0:99b49fd71085 81 { 1,1,1,0,1,1,0,1,1,0 },
ozy 0:99b49fd71085 82 { 0,0,0,0,1,1,0,0,0,1 },
ozy 0:99b49fd71085 83 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 84 { 0,0,1,1,0,0,1,1,0,0 },
ozy 1:3bdadf6f6dbd 85 { 0,0,0,1,1,1,1,0,0,0 },
ozy 1:3bdadf6f6dbd 86 { 0,0,0,0,1,1,0,0,0,0 },
ozy 1:3bdadf6f6dbd 87 { 0,0,0,0,1,0,1,0,0,0 },
ozy 1:3bdadf6f6dbd 88 { 0,0,0,1,0,0,0,1,0,0 },
ozy 0:99b49fd71085 89 };
ozy 10:e83899f11e8a 90
ozy 10:e83899f11e8a 91 // code to toggle between both move left animations (show some leg movement!)
ozy 10:e83899f11e8a 92 int x = rand() % 2;
ozy 10:e83899f11e8a 93 if (x == 0) {
ozy 10:e83899f11e8a 94 x = 1;
ozy 10:e83899f11e8a 95 lcd.drawSprite(_x,_y,12,10,(int *)run_left);
ozy 10:e83899f11e8a 96 }
ozy 10:e83899f11e8a 97 else if(x == 1) {
ozy 10:e83899f11e8a 98 x = 0;
ozy 10:e83899f11e8a 99 lcd.drawSprite(_x,_y,12,10,(int *)midrun_left);
ozy 10:e83899f11e8a 100 }
ozy 10:e83899f11e8a 101
ozy 7:737fb0c3dbef 102 }
ozy 7:737fb0c3dbef 103
ozy 7:737fb0c3dbef 104 // ************************************************************************
ozy 7:737fb0c3dbef 105
ozy 7:737fb0c3dbef 106 void Fighter::move_right(N5110 &lcd) {
ozy 7:737fb0c3dbef 107
ozy 0:99b49fd71085 108 const int run_right[12][10] = {
ozy 0:99b49fd71085 109 { 0,0,0,0,0,1,1,1,0,0 },
ozy 0:99b49fd71085 110 { 0,0,0,0,0,1,1,1,0,0 },
ozy 0:99b49fd71085 111 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 112 { 0,0,1,1,1,1,0,1,0,0 },
ozy 0:99b49fd71085 113 { 0,1,1,0,1,1,0,1,1,1 },
ozy 0:99b49fd71085 114 { 1,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 115 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 116 { 0,0,1,1,0,0,1,1,0,0 },
ozy 0:99b49fd71085 117 { 1,1,1,1,0,0,0,1,1,0 },
ozy 0:99b49fd71085 118 { 0,0,0,0,0,0,1,1,0,0 },
ozy 0:99b49fd71085 119 { 0,0,0,0,0,1,1,0,0,0 },
ozy 0:99b49fd71085 120 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 121 };
ozy 0:99b49fd71085 122
ozy 7:737fb0c3dbef 123 const int midrun_right[12][10] = {
ozy 0:99b49fd71085 124 { 0,0,0,0,0,1,1,1,0,0 },
ozy 0:99b49fd71085 125 { 0,0,0,0,0,1,1,1,0,0 },
ozy 0:99b49fd71085 126 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 127 { 0,0,1,1,1,1,0,1,0,0 },
ozy 0:99b49fd71085 128 { 0,1,1,0,1,1,0,1,1,1 },
ozy 0:99b49fd71085 129 { 1,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 130 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 131 { 0,0,1,1,0,0,1,1,0,0 },
ozy 0:99b49fd71085 132 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 133 { 0,0,0,0,1,1,0,0,0,0 },
ozy 1:3bdadf6f6dbd 134 { 0,0,0,1,0,1,0,0,0,0 },
ozy 0:99b49fd71085 135 { 0,0,1,0,0,0,1,0,0,0 },
ozy 0:99b49fd71085 136 };
ozy 10:e83899f11e8a 137
ozy 10:e83899f11e8a 138 // code to toggle between both move right animations
ozy 10:e83899f11e8a 139 int x = rand() % 2;
ozy 10:e83899f11e8a 140 if (x == 0) {
ozy 10:e83899f11e8a 141 x = 1;
ozy 10:e83899f11e8a 142 lcd.drawSprite(_x,_y,12,10,(int *)run_right);
ozy 10:e83899f11e8a 143 }
ozy 10:e83899f11e8a 144 else if(x == 1) {
ozy 10:e83899f11e8a 145 x = 0;
ozy 10:e83899f11e8a 146 lcd.drawSprite(_x,_y,12,10,(int *)midrun_right);
ozy 10:e83899f11e8a 147 }
ozy 10:e83899f11e8a 148
ozy 7:737fb0c3dbef 149 }
ozy 7:737fb0c3dbef 150
ozy 7:737fb0c3dbef 151 // ************************************************************************
ozy 7:737fb0c3dbef 152
ozy 17:928edcdd1058 153 int Fighter::kick_right(N5110 &lcd) {
ozy 7:737fb0c3dbef 154 const int kick[12][10] = {
ozy 0:99b49fd71085 155 { 0,1,1,0,0,0,0,0,0,0 },
ozy 0:99b49fd71085 156 { 0,1,1,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 157 { 0,0,0,1,1,1,1,0,0,1 },
ozy 0:99b49fd71085 158 { 0,0,1,1,1,0,0,0,1,1 },
ozy 0:99b49fd71085 159 { 0,1,1,1,1,1,1,1,0,0 },
ozy 0:99b49fd71085 160 { 0,1,0,0,1,1,1,0,0,0 },
ozy 0:99b49fd71085 161 { 1,1,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 162 { 0,0,0,0,1,0,0,0,0,0 },
ozy 0:99b49fd71085 163 { 0,0,0,1,1,0,0,0,0,0 },
ozy 0:99b49fd71085 164 { 0,0,0,1,0,0,0,0,0,0 },
ozy 0:99b49fd71085 165 { 0,0,1,1,0,0,0,0,0,0 },
ozy 0:99b49fd71085 166 { 0,0,1,1,0,0,0,0,0,0 },
ozy 0:99b49fd71085 167 };
ozy 7:737fb0c3dbef 168 lcd.drawSprite(_x,_y,12,10,(int *)kick);
ozy 17:928edcdd1058 169 return 1; //confirm collision
ozy 7:737fb0c3dbef 170 }
ozy 7:737fb0c3dbef 171
ozy 7:737fb0c3dbef 172 // ************************************************************************
ozy 7:737fb0c3dbef 173
ozy 17:928edcdd1058 174 int Fighter::kick_left(N5110 &lcd) {
ozy 7:737fb0c3dbef 175 const int kick[12][10] = {
ozy 0:99b49fd71085 176 { 0,0,0,0,0,0,0,1,1,0 },
ozy 0:99b49fd71085 177 { 0,0,0,0,1,1,0,1,1,0 },
ozy 0:99b49fd71085 178 { 1,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 179 { 1,1,0,0,0,1,1,1,0,0 },
ozy 0:99b49fd71085 180 { 0,0,1,1,1,1,1,1,1,0 },
ozy 0:99b49fd71085 181 { 0,0,0,1,1,1,0,0,1,0 },
ozy 0:99b49fd71085 182 { 0,0,0,0,1,1,0,0,1,1 },
ozy 0:99b49fd71085 183 { 0,0,0,0,0,1,0,0,0,0 },
ozy 0:99b49fd71085 184 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 185 { 0,0,0,0,1,0,0,0,0,0 },
ozy 0:99b49fd71085 186 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 187 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 188 };
ozy 7:737fb0c3dbef 189 lcd.drawSprite(_x,_y,12,10,(int *)kick);
ozy 17:928edcdd1058 190 return 1; //confirm collision
ozy 7:737fb0c3dbef 191 }
ozy 7:737fb0c3dbef 192
ozy 7:737fb0c3dbef 193 // ************************************************************************
ozy 7:737fb0c3dbef 194
ozy 17:928edcdd1058 195 int Fighter::punch_right(N5110 &lcd) {
ozy 7:737fb0c3dbef 196 const int punch[12][10] = {
ozy 0:99b49fd71085 197 { 0,0,0,0,1,1,0,0,0,1 },
ozy 0:99b49fd71085 198 { 0,0,0,0,1,1,0,0,1,1 },
ozy 0:99b49fd71085 199 { 0,0,1,1,1,1,0,1,1,0 },
ozy 0:99b49fd71085 200 { 0,1,0,0,1,1,1,1,0,0 },
ozy 0:99b49fd71085 201 { 0,1,1,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 202 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 203 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 204 { 0,0,0,1,0,0,1,1,0,0 },
ozy 0:99b49fd71085 205 { 0,0,1,1,0,0,1,1,0,0 },
ozy 0:99b49fd71085 206 { 0,0,1,0,0,0,1,0,0,0 },
ozy 0:99b49fd71085 207 { 0,1,1,0,0,0,1,0,0,0 },
ozy 0:99b49fd71085 208 { 1,1,0,0,0,1,1,0,0,0 },
ozy 0:99b49fd71085 209 };
ozy 7:737fb0c3dbef 210 lcd.drawSprite(_x,_y,12,10,(int *)punch);
ozy 17:928edcdd1058 211 return 1; //confirm collision
ozy 7:737fb0c3dbef 212 }
ozy 7:737fb0c3dbef 213
ozy 7:737fb0c3dbef 214 // ************************************************************************
ozy 7:737fb0c3dbef 215
ozy 17:928edcdd1058 216 int Fighter::punch_left(N5110 &lcd) {
ozy 7:737fb0c3dbef 217 const int punch[12][10] = {
ozy 0:99b49fd71085 218 { 1,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 219 { 1,1,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 220 { 0,1,1,0,1,1,1,1,0,0 },
ozy 0:99b49fd71085 221 { 0,0,1,1,1,1,0,0,1,0 },
ozy 0:99b49fd71085 222 { 0,0,0,0,1,1,0,1,1,0 },
ozy 0:99b49fd71085 223 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 224 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 225 { 0,0,1,1,0,0,1,0,0,0 },
ozy 0:99b49fd71085 226 { 0,0,1,1,0,0,1,1,0,0 },
ozy 0:99b49fd71085 227 { 0,0,0,1,0,0,0,1,0,0 },
ozy 0:99b49fd71085 228 { 0,0,0,1,0,0,0,1,1,0 },
ozy 0:99b49fd71085 229 { 0,0,0,1,1,0,0,0,1,1 },
ozy 0:99b49fd71085 230 };
ozy 7:737fb0c3dbef 231 lcd.drawSprite(_x,_y,12,10,(int *)punch);
ozy 17:928edcdd1058 232 return 1; //confirm collision
ozy 7:737fb0c3dbef 233 }
ozy 7:737fb0c3dbef 234
ozy 7:737fb0c3dbef 235 // ************************************************************************
ozy 7:737fb0c3dbef 236
ozy 7:737fb0c3dbef 237 void Fighter::guard(N5110 &lcd) {
ozy 0:99b49fd71085 238 const int guard[12][10] = {
ozy 0:99b49fd71085 239 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 240 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 241 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 242 { 0,0,1,0,1,1,0,1,0,0 },
ozy 0:99b49fd71085 243 { 0,0,1,0,1,1,0,1,0,0 },
ozy 0:99b49fd71085 244 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 245 { 0,0,0,0,1,1,0,0,0,0 },
ozy 0:99b49fd71085 246 { 0,0,0,1,1,1,1,0,0,0 },
ozy 0:99b49fd71085 247 { 0,0,1,1,1,1,1,1,0,0 },
ozy 0:99b49fd71085 248 { 0,1,0,0,0,0,0,0,1,0 },
ozy 0:99b49fd71085 249 { 0,1,0,0,0,0,0,0,1,0 },
ozy 0:99b49fd71085 250 { 0,1,0,0,0,0,0,0,1,0 },
ozy 0:99b49fd71085 251 };
ozy 7:737fb0c3dbef 252 lcd.drawSprite(_x,_y,12,10,(int *)guard);
ozy 7:737fb0c3dbef 253 }
ozy 6:a1a7dc264fed 254
ozy 7:737fb0c3dbef 255 void Fighter::move_fighter(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) {
ozy 6:a1a7dc264fed 256 // getting joystick coordinates using read() function
ozy 6:a1a7dc264fed 257 // 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 258 // read each of the pins
ozy 7:737fb0c3dbef 259 float x = joy_h.read();
ozy 7:737fb0c3dbef 260 float y = joy_v.read();
ozy 8:e2e2eb4ea0ca 261 // printf("x = %.2f | y = %.2f \n",x,y);
ozy 7:737fb0c3dbef 262
ozy 7:737fb0c3dbef 263 int x_pos = get_x();
ozy 8:e2e2eb4ea0ca 264 // printf("fighter x pos = %i \n", x_pos);
ozy 7:737fb0c3dbef 265
ozy 6:a1a7dc264fed 266 /*
ozy 6:a1a7dc264fed 267 Code idea:
ozy 6:a1a7dc264fed 268 if joystick is not moved , display standing sprite
ozy 6:a1a7dc264fed 269 if joystick is moved right, toggle between runright and midrunright
ozy 6:a1a7dc264fed 270 if joystick is moved left, toggle between runleft and midrunleft
ozy 6:a1a7dc264fed 271 */
ozy 7:737fb0c3dbef 272 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 7:737fb0c3dbef 273 draw(lcd); // draw standing fighter
ozy 6:a1a7dc264fed 274
ozy 6:a1a7dc264fed 275 // Preform kick move if user presses button A
ozy 6:a1a7dc264fed 276 if (buttonA.read() == 1) {
ozy 7:737fb0c3dbef 277 kick_right(lcd); // draw kick on same coordinates as the standing sprite
ozy 6:a1a7dc264fed 278 }
ozy 6:a1a7dc264fed 279 // Preform punch move if user presses button B
ozy 6:a1a7dc264fed 280 if (buttonB.read() == 1) {
ozy 7:737fb0c3dbef 281 punch_right(lcd); // draw kick
ozy 6:a1a7dc264fed 282 }
ozy 6:a1a7dc264fed 283 // Guard if user presses button C
ozy 8:e2e2eb4ea0ca 284 if (buttonC.read() == 1) {
ozy 7:737fb0c3dbef 285 guard(lcd); // draw guard move frame
ozy 6:a1a7dc264fed 286 }
ozy 15:7fd2d34f3be5 287 if (buttonD.read() == 0) { // code to return fighter to ground when user uses the jump button whilst moving right/left
ozy 15:7fd2d34f3be5 288 int y_pos = get_y();
ozy 15:7fd2d34f3be5 289 if (y_pos != 34) {
ozy 15:7fd2d34f3be5 290 set_y(34);
ozy 15:7fd2d34f3be5 291 }
ozy 15:7fd2d34f3be5 292 }
ozy 7:737fb0c3dbef 293 }
ozy 7:737fb0c3dbef 294 else if(x < 0.48) { // joystick moved to the right
ozy 6:a1a7dc264fed 295 // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!)
ozy 10:e83899f11e8a 296 add_x(4); // increment by 5
ozy 7:737fb0c3dbef 297 move_right(lcd);
ozy 7:737fb0c3dbef 298
ozy 7:737fb0c3dbef 299 if (x_pos >= 65) { // code to stop fighter moving out of lcd screen
ozy 7:737fb0c3dbef 300 add_x(-5);
ozy 6:a1a7dc264fed 301 }
ozy 7:737fb0c3dbef 302
ozy 7:737fb0c3dbef 303
ozy 6:a1a7dc264fed 304 // kick if user presses button A
ozy 6:a1a7dc264fed 305 if (buttonA.read() == 1) {
ozy 7:737fb0c3dbef 306 kick_right(lcd);
ozy 7:737fb0c3dbef 307 }
ozy 6:a1a7dc264fed 308 // Preform punch move if user presses button B
ozy 6:a1a7dc264fed 309 if (buttonB.read() == 1) {
ozy 7:737fb0c3dbef 310 punch_right(lcd); // draw kick on same coordinates as the sprite
ozy 10:e83899f11e8a 311 }
ozy 6:a1a7dc264fed 312 // Guard if user presses button C
ozy 8:e2e2eb4ea0ca 313 if (buttonC.read() == 1) { // we use while not if here because user would hold to guard
ozy 7:737fb0c3dbef 314 guard(lcd); // draw kick on same coordinates as the sprite
ozy 6:a1a7dc264fed 315 }
ozy 15:7fd2d34f3be5 316 if (buttonD.read() == 1) { // computing jump move
ozy 15:7fd2d34f3be5 317 move_right(lcd);
ozy 15:7fd2d34f3be5 318 add_y(-12); // add 12 to y position
ozy 15:7fd2d34f3be5 319 add_x(5);
ozy 15:7fd2d34f3be5 320 int y_pos = get_y();
ozy 15:7fd2d34f3be5 321 if (y_pos < 22) { // to avoid sprite jumping more than 12 everytime they click D
ozy 15:7fd2d34f3be5 322 set_y(22);
ozy 15:7fd2d34f3be5 323 }
ozy 15:7fd2d34f3be5 324 if (x_pos >= 65) { // code to stop fighter moving out of lcd screen while jumping!
ozy 15:7fd2d34f3be5 325 add_x(-5);
ozy 15:7fd2d34f3be5 326 }
ozy 15:7fd2d34f3be5 327 }
ozy 15:7fd2d34f3be5 328 if (buttonD.read() == 0) { // code to return fighter to ground
ozy 15:7fd2d34f3be5 329 int y_pos = get_y();
ozy 15:7fd2d34f3be5 330 if (y_pos != 34) {
ozy 15:7fd2d34f3be5 331 set_y(34);
ozy 15:7fd2d34f3be5 332 }
ozy 15:7fd2d34f3be5 333 }
ozy 6:a1a7dc264fed 334 }
ozy 7:737fb0c3dbef 335 else if(x > 0.52) { // joystick moved to the left
ozy 10:e83899f11e8a 336 add_x(-4); // decrement left by 5
ozy 7:737fb0c3dbef 337 move_left(lcd);
ozy 0:99b49fd71085 338
ozy 10:e83899f11e8a 339 if (x_pos <= 8) { // code to stop fighter moving out of lcd screen
ozy 7:737fb0c3dbef 340 add_x(5);
ozy 7:737fb0c3dbef 341 }
ozy 0:99b49fd71085 342
ozy 7:737fb0c3dbef 343 // kick if user presses button A
ozy 7:737fb0c3dbef 344 if (buttonA.read() == 1) {
ozy 7:737fb0c3dbef 345 kick_left(lcd);
ozy 7:737fb0c3dbef 346 }
ozy 7:737fb0c3dbef 347 if (buttonB.read() == 1) {
ozy 7:737fb0c3dbef 348 punch_left(lcd); // draw punch on same coordinates as the sprite
ozy 7:737fb0c3dbef 349 }
ozy 7:737fb0c3dbef 350 // Guard if user presses button C
ozy 8:e2e2eb4ea0ca 351 if (buttonC.read() == 1) {
ozy 8:e2e2eb4ea0ca 352 guard(lcd);
ozy 7:737fb0c3dbef 353 }
ozy 15:7fd2d34f3be5 354 // code to make fighter jump
ozy 15:7fd2d34f3be5 355 if (buttonD.read() == 1) { // computing jump move for left movement
ozy 15:7fd2d34f3be5 356 move_left(lcd);
ozy 15:7fd2d34f3be5 357 add_y(-12); // add 12 to y position
ozy 15:7fd2d34f3be5 358 add_x(-5);
ozy 15:7fd2d34f3be5 359 int y_pos = get_y();
ozy 15:7fd2d34f3be5 360 if (y_pos < 22) { // to avoid sprite jumping more than 12 everytime they click D
ozy 15:7fd2d34f3be5 361 set_y(22);
ozy 15:7fd2d34f3be5 362 }
ozy 15:7fd2d34f3be5 363 if (x_pos <= 8) { // code to stop fighter moving out of lcd screen while jumping!
ozy 15:7fd2d34f3be5 364 add_x(5);
ozy 15:7fd2d34f3be5 365 }
ozy 15:7fd2d34f3be5 366 }
ozy 15:7fd2d34f3be5 367 if (buttonD.read() == 0) { // code to return fighter to ground
ozy 15:7fd2d34f3be5 368 int y_pos = get_y();
ozy 15:7fd2d34f3be5 369 if (y_pos != 34) {
ozy 15:7fd2d34f3be5 370 set_y(34);
ozy 15:7fd2d34f3be5 371 }
ozy 15:7fd2d34f3be5 372 }
ozy 7:737fb0c3dbef 373 }
ozy 7:737fb0c3dbef 374 }