Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Revision:
10:e83899f11e8a
Parent:
9:8c840b08a633
Child:
11:b344a64b75bf
--- a/Fighter.cpp	Fri Apr 23 22:20:24 2021 +0000
+++ b/Fighter.cpp	Fri Apr 23 23:49:10 2021 +0000
@@ -15,7 +15,7 @@
 void Fighter::init() {
     // initialize fighter position
     set_x(15);
-    set_y(34);  
+    set_y(34);
 }
 
 void Fighter::set_x(int x) {
@@ -72,12 +72,7 @@
     { 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 },
@@ -92,9 +87,21 @@
     { 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);
+    
+    // code to toggle between both move left animations (show some leg movement!)
+    int x = rand() % 2;
+    if (x == 0) {
+        x = 1;
+        lcd.drawSprite(_x,_y,12,10,(int *)run_left);
+    }
+    else if(x == 1) {
+        x = 0;
+        lcd.drawSprite(_x,_y,12,10,(int *)midrun_left);
+    }
+    
 }
 
+
 // ************************************************************************
 
 void Fighter::move_right(N5110 &lcd) {
@@ -113,12 +120,7 @@
     { 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 },
@@ -133,8 +135,18 @@
     { 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);
+    
+    // code to toggle between both move right animations
+    int x = rand() % 2;
+    if (x == 0) {
+        x = 1;
+        lcd.drawSprite(_x,_y,12,10,(int *)run_right);
+    }
+    else if(x == 1) {
+        x = 0;
+        lcd.drawSprite(_x,_y,12,10,(int *)midrun_right);
+    }
+    
 }
 
 // ************************************************************************
@@ -256,38 +268,24 @@
 */
     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
         if (buttonC.read() == 1) {
             guard(lcd);  // draw guard move frame
-            wait(0.5);
-            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
+        add_x(4);           // 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);
@@ -297,22 +295,16 @@
         // 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
         if (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
-            wait(0.5);
-            lcd.refresh();
-            wait(0.3);
         }
+        /*
         if (buttonD.read() == 1) {       // code for fighter to jump while running
             add_y(-13);        // temporarily increase y coordinates
             add_x(10);         // increment by 10
@@ -324,39 +316,28 @@
             wait(0.15);
             lcd.refresh();
         }
+        */
     }
-
     else if(x > 0.52) { // joystick moved to the left
-        add_x(-5);      // decrement left by 5
+        add_x(-4);      // 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
+        if (x_pos <= 8) {   // 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
         if (buttonC.read() == 1) {
             guard(lcd);  
-            wait(0.5);
-            lcd.refresh();
-            wait(0.3);
         }
+        /*
         if (buttonD.read() == 1) {       // code for fighter to jump while running
             add_y(-13);        // temporarily increase y coordinates
             add_x(-10);         // decrement by 10
@@ -368,5 +349,8 @@
             wait(0.15);
             lcd.refresh();
         }
+        */
     }
+    // lcd.refresh();
+    // wait(0.2);
 }
\ No newline at end of file