Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Revision:
19:6c63e1dd7c85
Parent:
18:22bda659c70a
Child:
20:4ca04fd0965a
--- a/GameEngine.cpp	Thu Apr 29 22:40:08 2021 +0000
+++ b/GameEngine.cpp	Fri Apr 30 03:49:59 2021 +0000
@@ -2,11 +2,11 @@
 
 GameEngine::GameEngine() {}
 
-void GameEngine::init(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC){
+void GameEngine::init(){
     _enemy.init();
     _fighter.init();
-    set_fighter_health(30);
-    set_enemy_health(30);
+    set_fighter_health(40);
+    set_enemy_health(40);
 }
 
 int GameEngine::get_fighter_health() {
@@ -33,11 +33,34 @@
     _enemy_health -= enemy_health;
 }
 
+void GameEngine::draw_health_bars(N5110 &lcd) {
+    int F = get_fighter_health();
+    int E = get_enemy_health();
+    
+    if (F < 0) {       // if fighter loses
+        F = 0;          // F = 0 to avoid fighter health bar front-tracking into the enemy bar
+        game_over(lcd);    // game over!
+    }
+    else if(E < 0) {       // if enemy loses
+        E = 0;              // E = 0 to avoid enemy health bar back-tracking into fighter health bat
+        game_over(lcd);
+        // finish_him();   // finish him feature
+    }
+    lcd.printString("You", 0,1);
+    lcd.printString("Enemy", 55,1); // Kotal Khan is too long to be right-aligned so we have to write Enemy! :(
+    lcd.drawRect(0,0,F, 5, FILL_BLACK);
+    lcd.drawRect(0,0,40, 5, FILL_TRANSPARENT);
+    // ------------------------------------------------
+    lcd.drawRect(44,0, E, 5, FILL_BLACK);
+    lcd.drawRect(44,0,40, 5, FILL_TRANSPARENT);
+}
+
 void GameEngine::start(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn  &joy_v, AnalogIn  &joy_h) {
+    // function that draws fighter and allows user to control it 
+    _fighter.move_fighter(lcd, buttonA, buttonB, buttonC, buttonD, joy_v, joy_h);
     // initializing and drawing enemy AI agent
     enemy_AI(lcd);
-    // function that draws fighter and allows user to control it 
-    _fighter.move_fighter(lcd, buttonA, buttonB, buttonC, buttonD, joy_v, joy_h);
+    draw_health_bars(lcd);
     int fighter_health = get_fighter_health();
     int enemy_health = get_enemy_health();
     int collision = check_collision(lcd, buttonA, buttonB, buttonC);
@@ -57,7 +80,6 @@
     //printf("f pos = %i \n", fighter_pos);
     int enemy_pos = _enemy.get_x();
     //printf("e pos = %i \n", enemy_pos);
-    
     int input = 0;
     int diff = fighter_pos - enemy_pos;
     // printf("diff = %i \n", diff);
@@ -116,5 +138,10 @@
 }
 
 
+void GameEngine::game_over(N5110 &lcd) {
+    lcd.clear();
+    lcd.printString("Game Over", 20, 3);
+    lcd.printString("Loser!", 25, 4);
+    lcd.refresh();
+}
 
-