Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
Fighter.cpp
- Committer:
- ozy
- Date:
- 2021-04-22
- Revision:
- 7:737fb0c3dbef
- Parent:
- 6:a1a7dc264fed
- Child:
- 8:e2e2eb4ea0ca
File content as of revision 7:737fb0c3dbef:
#include "Fighter.h" #include "mbed.h" Fighter::Fighter() {} int Fighter::get_x() { return _x; } int Fighter::get_y() { return _y; } void Fighter::init() { // initialize fighter position set_x(15); set_y(34); } void Fighter::set_x(int x) { _x = x; } void Fighter::set_y(int y) { _y = y; } void Fighter::add_x(int x) { _x += x; } void Fighter::add_y(int y) { _y += y; } void Fighter::draw(N5110 &lcd) { // drawing standing Fighter const int standsprite[12][10] = { { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,1,1,1,1,1,1,0,0 }, { 0,0,1,0,1,1,0,1,0,0 }, { 0,1,1,0,1,1,0,1,1,0 }, { 0,1,0,0,1,1,0,0,1,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,0,0,1,1,0,0 }, { 0,0,1,0,0,0,0,1,0,0 }, { 0,1,1,0,0,0,0,1,1,0 }, { 0,1,0,0,0,0,0,0,1,0 }, { 0,1,0,0,0,0,0,0,1,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)standsprite); } // ************************************************************************ void Fighter::move_left(N5110 &lcd){ const int run_left[12][10] = { { 0,0,1,1,1,0,0,0,0,0 }, { 0,0,1,1,1,0,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,0,1,1,1,1,0,0 }, { 1,1,1,0,1,1,0,1,1,0 }, { 0,0,0,0,1,1,0,0,0,1 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,0,0,1,1,0,0 }, { 0,1,1,0,0,0,1,1,1,1 }, { 0,0,1,1,0,0,0,0,0,0 }, { 0,0,0,1,1,0,0,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)run_left); } // ************************************************************************ void Fighter::move_left2(N5110 &lcd){ const int midrun_left[12][10] = { { 0,0,1,1,1,0,0,0,0,0 }, { 0,0,1,1,1,0,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,0,1,1,1,1,0,0 }, { 1,1,1,0,1,1,0,1,1,0 }, { 0,0,0,0,1,1,0,0,0,1 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,0,0,1,1,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,0,1,0,1,0,0,0 }, { 0,0,0,1,0,0,0,1,0,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)midrun_left); } // ************************************************************************ void Fighter::move_right(N5110 &lcd) { const int run_right[12][10] = { { 0,0,0,0,0,1,1,1,0,0 }, { 0,0,0,0,0,1,1,1,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,1,1,0,1,0,0 }, { 0,1,1,0,1,1,0,1,1,1 }, { 1,0,0,0,1,1,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,0,0,1,1,0,0 }, { 1,1,1,1,0,0,0,1,1,0 }, { 0,0,0,0,0,0,1,1,0,0 }, { 0,0,0,0,0,1,1,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)run_right); } // ************************************************************************ void Fighter::move_right2(N5110 &lcd) { const int midrun_right[12][10] = { { 0,0,0,0,0,1,1,1,0,0 }, { 0,0,0,0,0,1,1,1,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,1,1,0,1,0,0 }, { 0,1,1,0,1,1,0,1,1,1 }, { 1,0,0,0,1,1,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,0,0,1,1,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,1,0,1,0,0,0,0 }, { 0,0,1,0,0,0,1,0,0,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)midrun_right); } // ************************************************************************ void Fighter::kick_right(N5110 &lcd) { const int kick[12][10] = { { 0,1,1,0,0,0,0,0,0,0 }, { 0,1,1,0,1,1,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,1 }, { 0,0,1,1,1,0,0,0,1,1 }, { 0,1,1,1,1,1,1,1,0,0 }, { 0,1,0,0,1,1,1,0,0,0 }, { 1,1,0,0,1,1,0,0,0,0 }, { 0,0,0,0,1,0,0,0,0,0 }, { 0,0,0,1,1,0,0,0,0,0 }, { 0,0,0,1,0,0,0,0,0,0 }, { 0,0,1,1,0,0,0,0,0,0 }, { 0,0,1,1,0,0,0,0,0,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)kick); } // ************************************************************************ void Fighter::kick_left(N5110 &lcd) { const int kick[12][10] = { { 0,0,0,0,0,0,0,1,1,0 }, { 0,0,0,0,1,1,0,1,1,0 }, { 1,0,0,1,1,1,1,0,0,0 }, { 1,1,0,0,0,1,1,1,0,0 }, { 0,0,1,1,1,1,1,1,1,0 }, { 0,0,0,1,1,1,0,0,1,0 }, { 0,0,0,0,1,1,0,0,1,1 }, { 0,0,0,0,0,1,0,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,0,1,0,0,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)kick); } // ************************************************************************ void Fighter::punch_right(N5110 &lcd) { const int punch[12][10] = { { 0,0,0,0,1,1,0,0,0,1 }, { 0,0,0,0,1,1,0,0,1,1 }, { 0,0,1,1,1,1,0,1,1,0 }, { 0,1,0,0,1,1,1,1,0,0 }, { 0,1,1,0,1,1,0,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,0,1,0,0,1,1,0,0 }, { 0,0,1,1,0,0,1,1,0,0 }, { 0,0,1,0,0,0,1,0,0,0 }, { 0,1,1,0,0,0,1,0,0,0 }, { 1,1,0,0,0,1,1,0,0,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)punch); } // ************************************************************************ void Fighter::punch_left(N5110 &lcd) { const int punch[12][10] = { { 1,0,0,0,1,1,0,0,0,0 }, { 1,1,0,0,1,1,0,0,0,0 }, { 0,1,1,0,1,1,1,1,0,0 }, { 0,0,1,1,1,1,0,0,1,0 }, { 0,0,0,0,1,1,0,1,1,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,0,0,1,0,0,0 }, { 0,0,1,1,0,0,1,1,0,0 }, { 0,0,0,1,0,0,0,1,0,0 }, { 0,0,0,1,0,0,0,1,1,0 }, { 0,0,0,1,1,0,0,0,1,1 }, }; lcd.drawSprite(_x,_y,12,10,(int *)punch); } // ************************************************************************ void Fighter::guard(N5110 &lcd) { const int guard[12][10] = { { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,0,1,1,0,1,0,0 }, { 0,0,1,0,1,1,0,1,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,1,1,1,1,0,0 }, { 0,1,0,0,0,0,0,0,1,0 }, { 0,1,0,0,0,0,0,0,1,0 }, { 0,1,0,0,0,0,0,0,1,0 }, }; lcd.drawSprite(_x,_y,12,10,(int *)guard); } void Fighter::move_fighter(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) { // getting joystick coordinates using read() function // joystick centered at (0.50,0.50) with utmost left at (1.00,0.50) and utmost right being (0.00,0.50) // read each of the pins float x = joy_h.read(); float y = joy_v.read(); printf("x = %.2f | y = %.2f \n",x,y); int x_pos = get_x(); printf("fighter x pos = %i \n", x_pos); /* Code idea: if joystick is not moved , display standing sprite if joystick is moved right, toggle between runright and midrunright if joystick is moved left, toggle between runleft and midrunleft */ 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 draw(lcd); // draw standing fighter lcd.refresh(); wait(0.3); // Preform kick move if user presses button A if (buttonA.read() == 1) { kick_right(lcd); // draw kick on same coordinates as the standing sprite lcd.refresh(); wait(0.3); } // Preform punch move if user presses button B if (buttonB.read() == 1) { punch_right(lcd); // draw kick lcd.refresh(); wait(0.3); } // Guard if user presses button C while (buttonC.read() == 1) { guard(lcd); // draw guard move frame lcd.refresh(); wait(0.3); } } else if(x < 0.48) { // joystick moved to the right // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) add_x(5); // increment by 5 move_right(lcd); lcd.refresh(); wait(0.1); move_right2(lcd); lcd.refresh(); wait(0.08); if (x_pos >= 65) { // code to stop fighter moving out of lcd screen add_x(-5); } // kick if user presses button A if (buttonA.read() == 1) { kick_right(lcd); lcd.refresh(); wait(0.3); } // Preform punch move if user presses button B if (buttonB.read() == 1) { punch_right(lcd); // draw kick on same coordinates as the sprite lcd.refresh(); wait(0.3); } // Guard if user presses button C while (buttonC.read() == 1) { // we use while not if here because user would hold to guard guard(lcd); // draw kick on same coordinates as the sprite lcd.refresh(); wait(0.3); } } else if(x > 0.52) { // joystick moved to the left add_x(-5); // decrement left by 5 move_left(lcd); lcd.refresh(); wait(0.1); move_left2(lcd); lcd.refresh(); wait(0.08); if (x_pos <= 6) { // code to stop fighter moving out of lcd screen add_x(5); } // kick if user presses button A if (buttonA.read() == 1) { kick_left(lcd); lcd.refresh(); wait(0.3); } if (buttonB.read() == 1) { punch_left(lcd); // draw punch on same coordinates as the sprite lcd.refresh(); wait(0.3); } // Guard if user presses button C while (buttonC.read() == 1) { guard(lcd); // draw guard on same coordinates as the sprite lcd.refresh(); wait(0.3); } } }