Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
Fighter.cpp
- Committer:
- ozy
- Date:
- 2021-04-21
- Revision:
- 6:a1a7dc264fed
- Parent:
- 3:1d99b6ad4f9e
- Child:
- 7:737fb0c3dbef
File content as of revision 6:a1a7dc264fed:
#include "Fighter.h" #include "mbed.h" Fighter::Fighter() {} int Fighter::get_x() { return _x; } int Fighter::get_y() { return _y; } void Fighter::set_x(int x) { _x = x; } void Fighter::set_y(int y) { _y = y; } int Fighter::add_x(int x) { _x += x; return _x; } int Fighter::add_y(int y) { _y += y; return _y; } // function to move and draw my fighter void Fighter::draw(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) { // first draw out all the frames of the 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 }, }; 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 }, }; 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 }, }; 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 }, }; 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 }, }; const int kick_right[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 }, }; const int kick_left[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 }, }; const int punch_right[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 }, }; const int punch_left[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 }, }; 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 }, }; // 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); // get x and y coordinates of fighter int x_coord = get_x(); int y_coord = get_y(); printf("x = %i, y = %i \n", x_coord, y_coord); /* 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 lcd.drawSprite(x_coord,y_coord,12,10,(int *)standsprite);; // draw standing fighter lcd.refresh(); wait(0.3); // Preform kick move if user presses button A if (buttonA.read() == 1) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)kick_right); // 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) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)punch_right); // draw kick lcd.refresh(); wait(0.3); } // Guard if user presses button C while (buttonC.read() == 1) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)guard); // 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!) _x = add_x(5); // increment x position by 5 lcd.drawSprite(x_coord,y_coord,12,10,(int *)run_right); lcd.refresh(); wait(0.1); lcd.drawSprite(x_coord,y_coord,12,10,(int *)midrun_right); lcd.refresh(); wait(0.08); if (x_coord > 68) { // code to stop fighter moving out of lcd screen x_coord = add_x(-5); } // kick if user presses button A if (buttonA.read() == 1) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)kick_right); lcd.refresh(); wait(0.3); } // Preform punch move if user presses button B if (buttonB.read() == 1) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)punch_right); // draw punch 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 lcd.drawSprite(x_coord,y_coord,12,10,(int *)guard); // draw kick on same coordinates as the sprite lcd.refresh(); wait(0.3); } } else if(_x > 0.52) { // joystick moved to the left _x = add_x(-5); // decrement left by 5 lcd.drawSprite(x_coord,y_coord,12,10,(int *)run_left); lcd.refresh(); wait(0.1); lcd.drawSprite(x_coord,y_coord,12,10,(int *)midrun_left); lcd.refresh(); wait(0.08); if (x_coord < 3) { // code to stop fighter moving out of lcd screen x_coord = add_x(5); } // kick if user presses button A if (buttonA.read() == 1) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)kick_left); lcd.refresh(); wait(0.3); } if (buttonB.read() == 1) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)punch_left); // draw punch on same coordinates as the sprite lcd.refresh(); wait(0.3); } // Guard if user presses button C if (buttonC.read() == 1) { lcd.drawSprite(x_coord,y_coord,12,10,(int *)guard); // draw guard on same coordinates as the sprite lcd.refresh(); wait(0.3); } } }