Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Revision:
6:a1a7dc264fed
Parent:
3:1d99b6ad4f9e
Child:
7:737fb0c3dbef
--- a/Fighter.cpp	Tue Apr 20 09:01:35 2021 +0000
+++ b/Fighter.cpp	Wed Apr 21 22:56:55 2021 +0000
@@ -20,16 +20,20 @@
     _y = y;
 }
 
-void Fighter::add_x(int x) {
+int Fighter::add_x(int x) {
     _x += x;
+    return _x;
 }
 
-void Fighter::add_y(int y) {
+int Fighter::add_y(int y) {
     _y += y;
+    return _y;
 }
 
-void Fighter::draw(N5110 &lcd) {    // drawing standing Fighter
+// 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 },
@@ -44,13 +48,7 @@
     { 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 },
@@ -65,13 +63,8 @@
     { 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] =   {
+    
+    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 },
@@ -85,13 +78,7 @@
     { 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 },
@@ -106,13 +93,8 @@
     { 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] =   {
+    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 },
@@ -127,13 +109,7 @@
     { 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] =   {
+    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 },
@@ -147,13 +123,8 @@
     { 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] =   {
+    
+    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 },
@@ -167,13 +138,8 @@
     { 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] =   {
+    
+    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 },
@@ -187,13 +153,8 @@
     { 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] =   {
+    
+    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 },
@@ -207,12 +168,7 @@
     { 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 },
@@ -227,7 +183,115 @@
     { 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);
+
+    
+    // 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);
+        }
+    }
+
 }