Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Committer:
ozy
Date:
Sun May 09 12:31:38 2021 +0000
Revision:
22:225d4031c090
Parent:
18:22bda659c70a
DONE

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 18:22bda659c70a 293 if (buttonD.read() == 1) { // computing jump move
ozy 18:22bda659c70a 294 draw(lcd);
ozy 18:22bda659c70a 295 add_y(-12); // add 12 to y position
ozy 18:22bda659c70a 296 int y_pos = get_y();
ozy 18:22bda659c70a 297 if (y_pos < 22) { // to avoid sprite jumping more than 12 everytime user clicks D
ozy 18:22bda659c70a 298 set_y(22);
ozy 18:22bda659c70a 299 }
ozy 18:22bda659c70a 300 }
ozy 7:737fb0c3dbef 301 }
ozy 7:737fb0c3dbef 302 else if(x < 0.48) { // joystick moved to the right
ozy 6:a1a7dc264fed 303 // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!)
ozy 10:e83899f11e8a 304 add_x(4); // increment by 5
ozy 7:737fb0c3dbef 305 move_right(lcd);
ozy 7:737fb0c3dbef 306
ozy 7:737fb0c3dbef 307 if (x_pos >= 65) { // code to stop fighter moving out of lcd screen
ozy 7:737fb0c3dbef 308 add_x(-5);
ozy 6:a1a7dc264fed 309 }
ozy 7:737fb0c3dbef 310
ozy 7:737fb0c3dbef 311
ozy 6:a1a7dc264fed 312 // kick if user presses button A
ozy 6:a1a7dc264fed 313 if (buttonA.read() == 1) {
ozy 7:737fb0c3dbef 314 kick_right(lcd);
ozy 7:737fb0c3dbef 315 }
ozy 6:a1a7dc264fed 316 // Preform punch move if user presses button B
ozy 6:a1a7dc264fed 317 if (buttonB.read() == 1) {
ozy 7:737fb0c3dbef 318 punch_right(lcd); // draw kick on same coordinates as the sprite
ozy 10:e83899f11e8a 319 }
ozy 6:a1a7dc264fed 320 // Guard if user presses button C
ozy 8:e2e2eb4ea0ca 321 if (buttonC.read() == 1) { // we use while not if here because user would hold to guard
ozy 7:737fb0c3dbef 322 guard(lcd); // draw kick on same coordinates as the sprite
ozy 6:a1a7dc264fed 323 }
ozy 15:7fd2d34f3be5 324 if (buttonD.read() == 1) { // computing jump move
ozy 15:7fd2d34f3be5 325 move_right(lcd);
ozy 15:7fd2d34f3be5 326 add_y(-12); // add 12 to y position
ozy 18:22bda659c70a 327 add_x(5); // add to x position
ozy 15:7fd2d34f3be5 328 int y_pos = get_y();
ozy 18:22bda659c70a 329 if (y_pos < 22) { // to avoid sprite jumping more than 12 everytime user clicks D
ozy 15:7fd2d34f3be5 330 set_y(22);
ozy 15:7fd2d34f3be5 331 }
ozy 15:7fd2d34f3be5 332 if (x_pos >= 65) { // code to stop fighter moving out of lcd screen while jumping!
ozy 15:7fd2d34f3be5 333 add_x(-5);
ozy 15:7fd2d34f3be5 334 }
ozy 15:7fd2d34f3be5 335 }
ozy 15:7fd2d34f3be5 336 if (buttonD.read() == 0) { // code to return fighter to ground
ozy 15:7fd2d34f3be5 337 int y_pos = get_y();
ozy 15:7fd2d34f3be5 338 if (y_pos != 34) {
ozy 15:7fd2d34f3be5 339 set_y(34);
ozy 15:7fd2d34f3be5 340 }
ozy 15:7fd2d34f3be5 341 }
ozy 6:a1a7dc264fed 342 }
ozy 7:737fb0c3dbef 343 else if(x > 0.52) { // joystick moved to the left
ozy 10:e83899f11e8a 344 add_x(-4); // decrement left by 5
ozy 7:737fb0c3dbef 345 move_left(lcd);
ozy 0:99b49fd71085 346
ozy 10:e83899f11e8a 347 if (x_pos <= 8) { // code to stop fighter moving out of lcd screen
ozy 7:737fb0c3dbef 348 add_x(5);
ozy 7:737fb0c3dbef 349 }
ozy 0:99b49fd71085 350
ozy 7:737fb0c3dbef 351 // kick if user presses button A
ozy 7:737fb0c3dbef 352 if (buttonA.read() == 1) {
ozy 7:737fb0c3dbef 353 kick_left(lcd);
ozy 7:737fb0c3dbef 354 }
ozy 7:737fb0c3dbef 355 if (buttonB.read() == 1) {
ozy 7:737fb0c3dbef 356 punch_left(lcd); // draw punch on same coordinates as the sprite
ozy 7:737fb0c3dbef 357 }
ozy 7:737fb0c3dbef 358 // Guard if user presses button C
ozy 8:e2e2eb4ea0ca 359 if (buttonC.read() == 1) {
ozy 18:22bda659c70a 360 guard(lcd);
ozy 7:737fb0c3dbef 361 }
ozy 15:7fd2d34f3be5 362 // code to make fighter jump
ozy 15:7fd2d34f3be5 363 if (buttonD.read() == 1) { // computing jump move for left movement
ozy 15:7fd2d34f3be5 364 move_left(lcd);
ozy 15:7fd2d34f3be5 365 add_y(-12); // add 12 to y position
ozy 15:7fd2d34f3be5 366 add_x(-5);
ozy 15:7fd2d34f3be5 367 int y_pos = get_y();
ozy 15:7fd2d34f3be5 368 if (y_pos < 22) { // to avoid sprite jumping more than 12 everytime they click D
ozy 15:7fd2d34f3be5 369 set_y(22);
ozy 15:7fd2d34f3be5 370 }
ozy 15:7fd2d34f3be5 371 if (x_pos <= 8) { // code to stop fighter moving out of lcd screen while jumping!
ozy 15:7fd2d34f3be5 372 add_x(5);
ozy 15:7fd2d34f3be5 373 }
ozy 15:7fd2d34f3be5 374 }
ozy 18:22bda659c70a 375 if (buttonD.read() == 0) { // code to return fighter to ground
ozy 15:7fd2d34f3be5 376 int y_pos = get_y();
ozy 15:7fd2d34f3be5 377 if (y_pos != 34) {
ozy 15:7fd2d34f3be5 378 set_y(34);
ozy 15:7fd2d34f3be5 379 }
ozy 15:7fd2d34f3be5 380 }
ozy 7:737fb0c3dbef 381 }
ozy 7:737fb0c3dbef 382 }