Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Committer:
ozy
Date:
Mon Apr 26 22:19:28 2021 +0000
Revision:
14:7f23841685ad
Parent:
13:eaf070d5f599
Child:
15:7fd2d34f3be5
Code after enemy move around success

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ozy 6:a1a7dc264fed 1 #include "GameEngine.h"
ozy 6:a1a7dc264fed 2
ozy 6:a1a7dc264fed 3 GameEngine::GameEngine() {}
ozy 6:a1a7dc264fed 4
ozy 7:737fb0c3dbef 5 void GameEngine::init(){
ozy 7:737fb0c3dbef 6 _fighter.init();
ozy 14:7f23841685ad 7 _enemy.init();
ozy 6:a1a7dc264fed 8 }
ozy 6:a1a7dc264fed 9
ozy 7:737fb0c3dbef 10 void GameEngine::start(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) {
ozy 12:b4477a312158 11 // initializing and drawing enemy
ozy 12:b4477a312158 12 enemy_AI(lcd);
ozy 7:737fb0c3dbef 13 // function that draws fighter and allows user to control it
ozy 7:737fb0c3dbef 14 _fighter.move_fighter(lcd, buttonA, buttonB, buttonC, buttonD, joy_v, joy_h);
ozy 7:737fb0c3dbef 15 lcd.refresh();
ozy 10:e83899f11e8a 16
ozy 7:737fb0c3dbef 17 }
ozy 7:737fb0c3dbef 18
ozy 8:e2e2eb4ea0ca 19 void GameEngine::enemy_AI(N5110 &lcd) {
ozy 8:e2e2eb4ea0ca 20 int fighter_pos = _fighter.get_x();
ozy 8:e2e2eb4ea0ca 21 printf("f pos = %i \n", fighter_pos);
ozy 8:e2e2eb4ea0ca 22 int enemy_pos = _enemy.get_x();
ozy 8:e2e2eb4ea0ca 23 printf("e pos = %i \n", enemy_pos);
ozy 10:e83899f11e8a 24
ozy 8:e2e2eb4ea0ca 25 int input = 0;
ozy 8:e2e2eb4ea0ca 26 int diff = fighter_pos - enemy_pos;
ozy 8:e2e2eb4ea0ca 27 printf("diff = %i \n", diff);
ozy 12:b4477a312158 28 // if difference is positive, fighter is to the right so enemy must look right (if input is 1, enemy looks right) and vice versa
ozy 8:e2e2eb4ea0ca 29 if (diff > 0) {
ozy 8:e2e2eb4ea0ca 30 input = 1;
ozy 8:e2e2eb4ea0ca 31 }
ozy 8:e2e2eb4ea0ca 32 else {input = 0;}
ozy 12:b4477a312158 33 if (diff >= -15 && diff <= 15){ // randomize enemy fight moves only in fighting range
ozy 14:7f23841685ad 34 _enemy.draw(lcd, input);
ozy 12:b4477a312158 35 _enemy.randomize_moves(lcd, input);
ozy 13:eaf070d5f599 36 // check for collision (without guard ON)
ozy 13:eaf070d5f599 37 // function to reduce enemy health or fighter health
ozy 13:eaf070d5f599 38 // check_enemy_dead and deploy next god
ozy 12:b4477a312158 39 }
ozy 14:7f23841685ad 40 // code for enemy to move to fighter if he is to the right
ozy 14:7f23841685ad 41 else if ((enemy_pos < fighter_pos) && (diff > 15) && (rand()%2 > 0)) {
ozy 14:7f23841685ad 42 _enemy.add_x(5);
ozy 14:7f23841685ad 43 _enemy.move_right(lcd);
ozy 14:7f23841685ad 44 }
ozy 14:7f23841685ad 45 // code for enemy to move to fighter if he is to the left
ozy 14:7f23841685ad 46 else if ((enemy_pos > fighter_pos) && (diff < 15) && (rand()%2 > 0)) {
ozy 14:7f23841685ad 47 _enemy.add_x(-5);
ozy 13:eaf070d5f599 48 _enemy.move_left(lcd);
ozy 13:eaf070d5f599 49 }
ozy 14:7f23841685ad 50 else {
ozy 14:7f23841685ad 51 _enemy.draw(lcd, input);
ozy 13:eaf070d5f599 52 }
ozy 8:e2e2eb4ea0ca 53 }
ozy 13:eaf070d5f599 54 /*
ozy 13:eaf070d5f599 55 void GameEngine::enemy_move_around(N5110 &lcd) {
ozy 13:eaf070d5f599 56 int fighter_pos = _fighter.get_x();
ozy 13:eaf070d5f599 57 printf("f pos = %i \n", fighter_pos);
ozy 13:eaf070d5f599 58 int enemy_pos = _enemy.get_x();
ozy 13:eaf070d5f599 59 printf("e pos = %i \n", enemy_pos);
ozy 13:eaf070d5f599 60 int diff = fighter_pos - enemy_pos;
ozy 13:eaf070d5f599 61 if (diff < 0) { // negative difference so fighter is to the left so enemy must move left
ozy 13:eaf070d5f599 62 _enemy.move_left(lcd);
ozy 13:eaf070d5f599 63 }
ozy 13:eaf070d5f599 64 }
ozy 13:eaf070d5f599 65 */
ozy 8:e2e2eb4ea0ca 66
ozy 8:e2e2eb4ea0ca 67