Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Revision:
5:889ad974b64d
Parent:
4:6b1de03716d6
Child:
6:a1a7dc264fed
--- a/main.cpp	Mon Apr 19 20:04:44 2021 +0000
+++ b/main.cpp	Tue Apr 20 09:01:35 2021 +0000
@@ -6,9 +6,7 @@
 #include "Menu.h"
 #include "Enemy.h"
 ///////////// defines /////////////////////
-#define SPEED         -69
 #define GROUND         34
-#define DISTANCE       10
 ///////////// objects ///////////////////
 Fighter fighter;
 Menu menu;
@@ -28,7 +26,6 @@
 void move_sprite();
 void draw_background();
 void move_enemy();
-int get_user_input(int &input);
 
 
 
@@ -40,7 +37,6 @@
         draw_background();
         move_sprite();
         // move_enemy();
-        // get_user_input(input);
     }
 }
 
@@ -49,6 +45,8 @@
     lcd.setContrast(0.5);
     fighter.set_x(33);  // setting initial position of fighter
     fighter.set_y(GROUND);  // placing fighter on ground
+    enemy.set_x(33);  // setting initial position of fighter
+    enemy.set_y(GROUND);  // placing fighter on ground
 }
 
 void menu_screen() {
@@ -78,7 +76,7 @@
     float x = joy_h.read();
     float y = joy_v.read();
     printf("x = %.2f | y = %.2f \n",x,y);
-    
+    // get x position of fighter
     int x_pos = fighter.get_x();
     printf("fighter x pos = %i \n", x_pos);
     
@@ -185,67 +183,80 @@
     float x = joy_h.read();
     float y = joy_v.read();
     printf("x = %.2f | y = %.2f \n",x,y);
+    // get x position of enemy
+    int x_pos = enemy.get_x();
+    printf("enemy x pos = %i \n", x_pos);
 
     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
-          enemy.draw(lcd, x+33, GROUND); // draw standing fighter and add 30 print it on middle of lcd screen
-          // ... and the y-coordinate to place sprite exactly on ground  is 34 ( 46 - 12(height of sprite) = 34 )
+          enemy.draw(lcd); // draw enemy
           lcd.refresh();
-          wait(0.2);
+          wait(0.3);
        // Preform kick move if user presses button A
-        while (buttonA.read() == 1) {
-            enemy.kick_right(lcd, x+33, GROUND);  // draw kick on same coordinates as the standing sprite
+        if (buttonA.read() == 1) {
+            enemy.kick_right(lcd);  // draw kick on same coordinates as the standing sprite
             lcd.refresh();
-            wait(0.2);
+            wait(0.3);
         }
         // Preform punch move if user presses button B
-        while (buttonB.read() == 1) {
-            enemy.sword_right(lcd, x+33, GROUND);  // draw kick on same coordinates as the standing sprite
+        if (buttonB.read() == 1) {
+            enemy.sword_right(lcd);  // draw sword move on same coordinates as the standing sprite
             lcd.refresh();
-            wait(0.2);
+            wait(0.3);
         }
     }
     else if(x < 0.48) { //  joystick moved to the right
+        enemy.add_x(5);           // increment by 5
         // print  the  move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!)
-        enemy.move_right(lcd, SPEED*x+70, GROUND); // draw standing fighter, multiply by speed -69 (negative to correct for direction!)
+        enemy.move_right(lcd); // draw standing fighter, multiply by speed -69 (negative to correct for direction!)
         lcd.refresh();
         wait(0.1);
-        enemy.move_right2(lcd, SPEED*x+70, GROUND);
+        enemy.move_right2(lcd);
         lcd.refresh();
         wait(0.08);
-
+        
+        if (x_pos >= 65) {   // code to stop enemy moving out of lcd screen
+            enemy.add_x(-5);
+        }
+        
         // kick if user presses button A
-        while (buttonA.read() == 1) {
-                enemy.kick_right(lcd, SPEED*x+70, GROUND);
-                lcd.refresh();
-                wait(0.2);
-            }
+        if (buttonA.read() == 1) {
+            enemy.kick_right(lcd);
+            lcd.refresh();
+            wait(0.3);
+        }
         // Preform punch move if user presses button B
-        while (buttonB.read() == 1) {
-            enemy.sword_right(lcd, SPEED*x+70, GROUND);  // draw kick on same coordinates as the sprite
+        if (buttonB.read() == 1) {
+            enemy.sword_right(lcd);  // draw kick on same coordinates as the sprite
             lcd.refresh();
-            wait(0.2);
+            wait(0.3);
         }
     }
     else if(x > 0.52) { // joystick moved to the left
-        enemy.move_left(lcd, SPEED*x+71, GROUND);
+        enemy.add_x(-5);           // decrement by 5
+        enemy.move_left(lcd);
         lcd.refresh();
         wait(0.1);
-        enemy.move_left2(lcd, SPEED*x+71, 34);
+        enemy.move_left2(lcd);
         lcd.refresh();
         wait(0.08);
-    }
+    
+    
+    if (x_pos <= 6) {   // code to stop enemy moving out of lcd screen
+            enemy.add_x(5);
+        }
 
     // kick if user presses button A
-        while (buttonA.read() == 1) {
-            enemy.kick_left(lcd, SPEED*x+71, GROUND);
+        if (buttonA.read() == 1) {
+            enemy.kick_left(lcd);
             lcd.refresh();
-            wait(0.2);
+            wait(0.3);
         }
-        while (buttonB.read() == 1) {
-            enemy.sword_left(lcd, SPEED*x+71, GROUND);  // draw kick on same coordinates as the sprite
+        if (buttonB.read() == 1) {
+            enemy.sword_left(lcd);  // draw kick on same coordinates as the sprite
             lcd.refresh();
-            wait(0.2);
+            wait(0.3);
         }
+    }
 }