Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
Diff: Fighter.cpp
- Revision:
- 7:737fb0c3dbef
- Parent:
- 6:a1a7dc264fed
- Child:
- 8:e2e2eb4ea0ca
--- a/Fighter.cpp Wed Apr 21 22:56:55 2021 +0000 +++ b/Fighter.cpp Thu Apr 22 01:24:12 2021 +0000 @@ -12,6 +12,12 @@ return _y; } +void Fighter::init() { + // initialize fighter position + set_x(15); + set_y(34); +} + void Fighter::set_x(int x) { _x = x; } @@ -20,20 +26,16 @@ _y = y; } -int Fighter::add_x(int x) { +void Fighter::add_x(int x) { _x += x; - return _x; } -int Fighter::add_y(int y) { +void 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) { +void Fighter::draw(N5110 &lcd) { // drawing standing Fighter -// 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 }, @@ -48,7 +50,13 @@ { 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 }, @@ -63,8 +71,13 @@ { 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] = { +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 }, @@ -78,7 +91,13 @@ { 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 }, @@ -93,8 +112,13 @@ { 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); +} - const int midrun_right[12][10] = { +// ************************************************************************ + +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 }, @@ -108,8 +132,14 @@ { 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] = { + +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 }, @@ -123,8 +153,13 @@ { 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] = { +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 }, @@ -138,8 +173,13 @@ { 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] = { +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 }, @@ -153,8 +193,13 @@ { 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] = { +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 }, @@ -168,7 +213,12 @@ { 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 }, @@ -183,119 +233,114 @@ { 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); - - // 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); - + 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 - lcd.drawSprite(x_coord,y_coord,12,10,(int *)standsprite);; // draw standing fighter + 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) { - lcd.drawSprite(x_coord,y_coord,12,10,(int *)kick_right); // draw kick on same coordinates as the standing sprite + 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) { - lcd.drawSprite(x_coord,y_coord,12,10,(int *)punch_right); // draw kick + punch_right(lcd); // 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 + guard(lcd); // draw guard move frame lcd.refresh(); wait(0.3); } -} -else if(_x < 0.48) { // joystick moved to the right + } + 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); + add_x(5); // increment by 5 + move_right(lcd); lcd.refresh(); wait(0.1); - lcd.drawSprite(x_coord,y_coord,12,10,(int *)midrun_right); + move_right2(lcd); lcd.refresh(); wait(0.08); - - if (x_coord > 68) { // code to stop fighter moving out of lcd screen - x_coord = add_x(-5); + + 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) { - lcd.drawSprite(x_coord,y_coord,12,10,(int *)kick_right); + kick_right(lcd); 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 + 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 - 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 + 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); + } + } +} \ No newline at end of file