Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
Diff: main.cpp
- Revision:
- 3:1d99b6ad4f9e
- Parent:
- 2:1703eb2a68f8
- Child:
- 4:6b1de03716d6
--- a/main.cpp Sat Apr 17 12:35:18 2021 +0000 +++ b/main.cpp Mon Apr 19 18:38:59 2021 +0000 @@ -8,14 +8,15 @@ ///////////// defines ///////////////////// #define SPEED -69 #define GROUND 34 +#define DISTANCE 10 ///////////// objects /////////////////// -Fighter fighter; +Fighter fighter; Menu menu; Enemy enemy; -InterruptIn buttonA(p29); -InterruptIn buttonB(p28); -InterruptIn buttonC(p27); -InterruptIn buttonD(p26); +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); @@ -28,41 +29,30 @@ void draw_background(); void move_enemy(); int get_user_input(int &input); -void buttonA_isr(); -void buttonB_isr(); -void buttonC_isr(); -void buttonD_isr(); -void button_init(); -void check_interrupt(); -//////////// initializations //////////////////// -volatile int g_buttonA_flag = 0; -volatile int g_buttonB_flag = 0; -volatile int g_buttonC_flag = 0; -volatile int g_buttonD_flag = 0; + int main() { init(); - menu_screen(); + // menu_screen(); while(1) { lcd.clear(); - // draw_background(); + draw_background(); + move_sprite(); // move_enemy(); - // move_sprite(); - check_interrupt(); - // put the MCU to sleep until an interrupt wakes it up - sleep(); + // get_user_input(input); } } void init() { // initialize all devices lcd.init(); lcd.setContrast(0.5); - button_init(); + fighter.set_x(33); // setting initial position of fighter + fighter.set_y(GROUND); // placing fighter on ground } -void menu_screen() { - menu.main_menu(lcd); // printing the menu screen +void menu_screen() { + menu.main_menu(lcd); // printing the menu screen wait(3.5f); menu.created_by(lcd); // 2nd menu screen wait(3.5f); @@ -83,116 +73,113 @@ 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 + + // 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: +/* +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 ) + 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 + fighter.draw(lcd); // draw standing fighter 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 + + // Preform kick move if user presses button A + if (buttonA.read() == 1) { + fighter.kick_right(lcd); // 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 + if (buttonB.read() == 1) { + fighter.punch_right(lcd); // draw kick 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 + fighter.guard(lcd); // draw guard move frame 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!) + fighter.add_x(4); // increment by 4 + fighter.move_right(lcd); lcd.refresh(); wait(0.1); - fighter.move_right2(lcd, SPEED*x+70, GROUND); + fighter.move_right2(lcd); lcd.refresh(); wait(0.08); - - // kick if user presses button A - while (buttonA.read() == 1) { - fighter.kick_right(lcd, SPEED*x+70, GROUND); + + // kick if user presses button A + if (buttonA.read() == 1) { + fighter.kick_right(lcd); 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 + if (buttonB.read() == 1) { + fighter.punch_right(lcd); // 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 + while (buttonC.read() == 1) { // we use while not if here because user would hold to guard + fighter.guard(lcd); // 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); + fighter.add_x(-4); // decrement left + fighter.move_left(lcd); lcd.refresh(); wait(0.1); - fighter.move_left2(lcd, SPEED*x+71, 34); + fighter.move_left2(lcd); lcd.refresh(); wait(0.08); - } - // kick if user presses button A - while (buttonA.read() == 1) { - fighter.kick_left(lcd, SPEED*x+71, GROUND); + + // kick if user presses button A + if (buttonA.read() == 1) { + fighter.kick_left(lcd); 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 + if (buttonB.read() == 1) { + fighter.punch_left(lcd); // draw punch 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 + if (buttonC.read() == 1) { + fighter.guard(lcd); // draw guard on same coordinates as the sprite lcd.refresh(); wait(0.2); } + } } void move_enemy() { - // read each of the pins + // 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 + // 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(); @@ -213,10 +200,10 @@ enemy.move_right2(lcd, SPEED*x+70, GROUND); lcd.refresh(); wait(0.08); - - // kick if user presses button A + + // kick if user presses button A while (buttonA.read() == 1) { - enemy.kick_right(lcd, SPEED*x+70, GROUND); + enemy.kick_right(lcd, SPEED*x+70, GROUND); lcd.refresh(); wait(0.2); } @@ -235,10 +222,10 @@ lcd.refresh(); wait(0.08); } - - // kick if user presses button A + + // kick if user presses button A while (buttonA.read() == 1) { - enemy.kick_left(lcd, SPEED*x+71, GROUND); + enemy.kick_left(lcd, SPEED*x+71, GROUND); lcd.refresh(); wait(0.2); } @@ -249,77 +236,5 @@ } } -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; - } - } - return input; -} - -// Button A B C D event-triggered interrupt -void buttonA_isr() { - g_buttonA_flag = 1; // set flag in ISR -} -void buttonB_isr() { - g_buttonB_flag = 1; // set flag in ISR -} - -void buttonC_isr() { - g_buttonC_flag = 1; // set flag in ISR -} -void buttonD_isr() { - g_buttonD_flag = 1; // set flag in ISR -} -void button_init() { - buttonA.rise(&buttonA_isr); - buttonB.rise(&buttonB_isr); - buttonC.rise(&buttonC_isr); - buttonD.rise(&buttonD_isr); - buttonA.mode(PullNone); - buttonB.mode(PullNone); - buttonC.mode(PullNone); - buttonD.mode(PullNone); -} - -void check_interrupt() { - int input; - get_user_input(input); - // check if flag i.e. interrupt has occured - if (g_buttonA_flag == 1) { - g_buttonA_flag = 0; - printf("user input is %i ", input); - wait(0.1); - } - else if (g_buttonB_flag == 1) { - g_buttonB_flag = 0; - printf("user input is %i ", input); - wait(0.1); - } - else if (g_buttonC_flag == 1) { - g_buttonC_flag = 0; - printf("user input is %i ", input); - wait(0.1); - } - else if (g_buttonD_flag == 1) { - g_buttonD_flag = 0; - printf("user input is %i ", input); - wait(0.1); - } -}