Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

main.cpp

Committer:
ozy
Date:
2021-04-17
Revision:
1:3bdadf6f6dbd
Parent:
0:99b49fd71085
Child:
2:1703eb2a68f8

File content as of revision 1:3bdadf6f6dbd:

///////////// includes /////////////////////
#include "mbed.h"
#include "N5110.h"
#include "Fighter.h"
#include "Joystick.h"
#include "Menu.h"
#include "Enemy.h"
///////////// defines /////////////////////
#define SPEED         -69
#define GROUND         34
///////////// objects ///////////////////
Fighter fighter; 
Menu menu;
Enemy enemy;
DigitalIn buttonA(p29);
DigitalIn buttonB(p28);
DigitalIn buttonC(p27);
DigitalIn buttonD(p26);
AnalogIn  joy_v(p20);
AnalogIn  joy_h(p19);
N5110 lcd(p14,p8,p9,p10,p11,p13,p21);
BusOut leds(LED4,LED3,LED2,LED1);
Serial pc(USBTX, USBRX);
//////////// functions ////////////////////
void init();
void menu_screen();
void move_sprite();
void draw_background();
void move_enemy();
int get_user_input(int &input);



int main() {
    init();
    menu_screen();
    int input;
    while(1) {
        // lcd.clear();
        // draw_background();
        // move_enemy();
        // move_sprite();
        get_user_input(input);
    }
}

void init() { // initialize all devices
    lcd.init();
    lcd.setContrast(0.5);
}

void menu_screen() { 
    menu.main_menu(lcd); // printing the menu screen 
    wait(3.5f);
    menu.created_by(lcd); // 2nd menu screen
    wait(3.5f);
    menu.homescreen(lcd); // print homescreen
    menu.menu_selection(lcd, buttonA, buttonB, buttonC, buttonD); // function to ask user to select menu item
}


void draw_background() {
        // lcd.drawLine(x1, y1, x2, y2, type);
        lcd.drawLine(0,46,82,46,1);   // draws ground platform
        lcd.drawLine(0,46,0,25,1);   // draws first side wall
        lcd.drawLine(82,46,82,25,1);   // draws second side wall
        lcd.refresh();
    }


void move_sprite() {
    // 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);
/* 
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.05 to take account of fluctuation in joystick tolerance and noice on ADC
          fighter.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 )
          lcd.refresh();
          wait(0.2);
          
           // Preform kick move if user presses button A 
        while (buttonA.read() == 1) {
            fighter.kick_right(lcd, x+33, GROUND);  // draw kick on same coordinates as the standing sprite
            lcd.refresh();
            wait(0.2);
        }
        // Preform punch move if user presses button B
        while (buttonB.read() == 1) {
            fighter.punch_right(lcd, x+33, GROUND);  // draw kick on same coordinates as the standing sprite
            lcd.refresh();
            wait(0.2);
        }
        // Guard if user presses button C
        while (buttonC.read() == 1) {
            fighter.guard(lcd, x+33, GROUND);  // draw guard move on same coordinates as the standing sprite
            lcd.refresh();
            wait(0.2);
        }
        // Jump if user presses button D
        while (buttonD.read() == 1) {
            fighter.draw(lcd, x+33, GROUND-15);  // adding 15 the sprite y-coordinate to create jump move
            lcd.refresh();
            wait(0.2);
        }
    }
    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!)
        fighter.move_right(lcd, SPEED*x+70, GROUND); // draw standing fighter, multiply by speed -69 (negative to correct for direction!)
        lcd.refresh();
        wait(0.1);
        fighter.move_right2(lcd, SPEED*x+70, GROUND);
        lcd.refresh();
        wait(0.08);
        
        // kick if user presses button A 
        while (buttonA.read() == 1) {
                fighter.kick_right(lcd, SPEED*x+70, GROUND);  
                lcd.refresh();
                wait(0.2);
            }
        // Preform punch move if user presses button B
        while (buttonB.read() == 1) {
            fighter.punch_right(lcd, SPEED*x+70, GROUND);  // draw kick on same coordinates as the sprite
            lcd.refresh();
            wait(0.2);
        }
        // Guard if user presses button C
        while (buttonC.read() == 1) {
            fighter.guard(lcd, SPEED*x+70, GROUND);  // draw kick on same coordinates as the sprite
            lcd.refresh();
            wait(0.2);
        }
    }
    else if(x > 0.52) { // joystick moved to the left
        fighter.move_left(lcd, SPEED*x+71, GROUND);
        lcd.refresh();
        wait(0.1);
        fighter.move_left2(lcd, SPEED*x+71, 34);
        lcd.refresh();
        wait(0.08);
    }
    
    // kick if user presses button A 
        while (buttonA.read() == 1) {
            fighter.kick_left(lcd, SPEED*x+71, GROUND);  
            lcd.refresh();
            wait(0.2);
        }
        while (buttonB.read() == 1) {
            fighter.punch_left(lcd, SPEED*x+71, GROUND);  // draw kick on same coordinates as the sprite
            lcd.refresh();
            wait(0.2);
        }
        // Guard if user presses button C
        while (buttonC.read() == 1) {
            fighter.guard(lcd, SPEED*x+71, GROUND);  // draw kick on same coordinates as the sprite
            lcd.refresh();
            wait(0.2);
        }
}

void move_enemy() {
    // read each of the pins              
    float x = joy_h.read();
    float y = joy_v.read();
    printf("x = %.2f | y = %.2f \n",x,y);
    
    if(x > 0.48 && x < 0.52) { // joystick not moved - we use ± 0.05 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 )
          lcd.refresh();
          wait(0.2);
       // 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
            lcd.refresh();
            wait(0.2);
        }
        // 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
            lcd.refresh();
            wait(0.2);
        }
    }
    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!)
        enemy.move_right(lcd, SPEED*x+70, GROUND); // 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);
        lcd.refresh();
        wait(0.08);
        
        // kick if user presses button A 
        while (buttonA.read() == 1) {
                enemy.kick_right(lcd, SPEED*x+70, GROUND);  
                lcd.refresh();
                wait(0.2);
            }
        // 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
            lcd.refresh();
            wait(0.2);
        }
    }
    else if(x > 0.52) { // joystick moved to the left
        enemy.move_left(lcd, SPEED*x+71, GROUND);
        lcd.refresh();
        wait(0.1);
        enemy.move_left2(lcd, SPEED*x+71, 34);
        lcd.refresh();
        wait(0.08);
    }
    
    // kick if user presses button A 
        while (buttonA.read() == 1) {
            enemy.kick_left(lcd, SPEED*x+71, GROUND);  
            lcd.refresh();
            wait(0.2);
        }
        while (buttonB.read() == 1) {
            enemy.sword_left(lcd, SPEED*x+71, GROUND);  // draw kick on same coordinates as the sprite
            lcd.refresh();
            wait(0.2);
        }
}

int get_user_input(int &input) {
    while (1) {
        if (buttonA.read() == 1) { 
            input = 1;
            break;
        }
        if (buttonB.read() == 1) {
            input  = 2;
            break;
        }
        if (buttonC.read() == 1) {
            input  = 3;
            break;
        }
        if (buttonD.read() == 1) {
            input  = 4;
            break;
        }
    }
    printf("input is %i", input);
    return input;
}