Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Committer:
ozy
Date:
Mon Apr 19 18:38:59 2021 +0000
Revision:
3:1d99b6ad4f9e
Parent:
2:1703eb2a68f8
Child:
4:6b1de03716d6
Code after new fighter class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ozy 0:99b49fd71085 1 ///////////// includes /////////////////////
ozy 0:99b49fd71085 2 #include "mbed.h"
ozy 0:99b49fd71085 3 #include "N5110.h"
ozy 0:99b49fd71085 4 #include "Fighter.h"
ozy 0:99b49fd71085 5 #include "Joystick.h"
ozy 0:99b49fd71085 6 #include "Menu.h"
ozy 1:3bdadf6f6dbd 7 #include "Enemy.h"
ozy 0:99b49fd71085 8 ///////////// defines /////////////////////
ozy 0:99b49fd71085 9 #define SPEED -69
ozy 0:99b49fd71085 10 #define GROUND 34
ozy 3:1d99b6ad4f9e 11 #define DISTANCE 10
ozy 0:99b49fd71085 12 ///////////// objects ///////////////////
ozy 3:1d99b6ad4f9e 13 Fighter fighter;
ozy 0:99b49fd71085 14 Menu menu;
ozy 1:3bdadf6f6dbd 15 Enemy enemy;
ozy 3:1d99b6ad4f9e 16 DigitalIn buttonA(p29);
ozy 3:1d99b6ad4f9e 17 DigitalIn buttonB(p28);
ozy 3:1d99b6ad4f9e 18 DigitalIn buttonC(p27);
ozy 3:1d99b6ad4f9e 19 DigitalIn buttonD(p26);
ozy 0:99b49fd71085 20 AnalogIn joy_v(p20);
ozy 0:99b49fd71085 21 AnalogIn joy_h(p19);
ozy 0:99b49fd71085 22 N5110 lcd(p14,p8,p9,p10,p11,p13,p21);
ozy 0:99b49fd71085 23 BusOut leds(LED4,LED3,LED2,LED1);
ozy 0:99b49fd71085 24 Serial pc(USBTX, USBRX);
ozy 0:99b49fd71085 25 //////////// functions ////////////////////
ozy 0:99b49fd71085 26 void init();
ozy 0:99b49fd71085 27 void menu_screen();
ozy 0:99b49fd71085 28 void move_sprite();
ozy 0:99b49fd71085 29 void draw_background();
ozy 1:3bdadf6f6dbd 30 void move_enemy();
ozy 1:3bdadf6f6dbd 31 int get_user_input(int &input);
ozy 3:1d99b6ad4f9e 32
ozy 0:99b49fd71085 33
ozy 0:99b49fd71085 34
ozy 0:99b49fd71085 35 int main() {
ozy 0:99b49fd71085 36 init();
ozy 3:1d99b6ad4f9e 37 // menu_screen();
ozy 0:99b49fd71085 38 while(1) {
ozy 2:1703eb2a68f8 39 lcd.clear();
ozy 3:1d99b6ad4f9e 40 draw_background();
ozy 3:1d99b6ad4f9e 41 move_sprite();
ozy 1:3bdadf6f6dbd 42 // move_enemy();
ozy 3:1d99b6ad4f9e 43 // get_user_input(input);
ozy 0:99b49fd71085 44 }
ozy 0:99b49fd71085 45 }
ozy 0:99b49fd71085 46
ozy 0:99b49fd71085 47 void init() { // initialize all devices
ozy 0:99b49fd71085 48 lcd.init();
ozy 0:99b49fd71085 49 lcd.setContrast(0.5);
ozy 3:1d99b6ad4f9e 50 fighter.set_x(33); // setting initial position of fighter
ozy 3:1d99b6ad4f9e 51 fighter.set_y(GROUND); // placing fighter on ground
ozy 0:99b49fd71085 52 }
ozy 0:99b49fd71085 53
ozy 3:1d99b6ad4f9e 54 void menu_screen() {
ozy 3:1d99b6ad4f9e 55 menu.main_menu(lcd); // printing the menu screen
ozy 0:99b49fd71085 56 wait(3.5f);
ozy 0:99b49fd71085 57 menu.created_by(lcd); // 2nd menu screen
ozy 0:99b49fd71085 58 wait(3.5f);
ozy 1:3bdadf6f6dbd 59 menu.homescreen(lcd); // print homescreen
ozy 1:3bdadf6f6dbd 60 menu.menu_selection(lcd, buttonA, buttonB, buttonC, buttonD); // function to ask user to select menu item
ozy 0:99b49fd71085 61 }
ozy 0:99b49fd71085 62
ozy 0:99b49fd71085 63
ozy 0:99b49fd71085 64 void draw_background() {
ozy 0:99b49fd71085 65 // lcd.drawLine(x1, y1, x2, y2, type);
ozy 0:99b49fd71085 66 lcd.drawLine(0,46,82,46,1); // draws ground platform
ozy 0:99b49fd71085 67 lcd.drawLine(0,46,0,25,1); // draws first side wall
ozy 0:99b49fd71085 68 lcd.drawLine(82,46,82,25,1); // draws second side wall
ozy 0:99b49fd71085 69 lcd.refresh();
ozy 0:99b49fd71085 70 }
ozy 0:99b49fd71085 71
ozy 0:99b49fd71085 72
ozy 0:99b49fd71085 73 void move_sprite() {
ozy 0:99b49fd71085 74 // getting joystick coordinates using read() function
ozy 0:99b49fd71085 75 // joystick centered at (0.50,0.50) with utmost left at (1.00,0.50) and utmost right being (0.00,0.50)
ozy 3:1d99b6ad4f9e 76
ozy 3:1d99b6ad4f9e 77 // read each of the pins
ozy 0:99b49fd71085 78 float x = joy_h.read();
ozy 0:99b49fd71085 79 float y = joy_v.read();
ozy 0:99b49fd71085 80 printf("x = %.2f | y = %.2f \n",x,y);
ozy 3:1d99b6ad4f9e 81 /*
ozy 3:1d99b6ad4f9e 82 Code idea:
ozy 0:99b49fd71085 83 if joystick is not moved , display standing sprite
ozy 0:99b49fd71085 84 if joystick is moved right, toggle between runright and midrunright
ozy 0:99b49fd71085 85 if joystick is moved left, toggle between runleft and midrunleft
ozy 0:99b49fd71085 86 */
ozy 3:1d99b6ad4f9e 87 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
ozy 3:1d99b6ad4f9e 88 fighter.draw(lcd); // draw standing fighter
ozy 0:99b49fd71085 89 lcd.refresh();
ozy 0:99b49fd71085 90 wait(0.2);
ozy 3:1d99b6ad4f9e 91
ozy 3:1d99b6ad4f9e 92 // Preform kick move if user presses button A
ozy 3:1d99b6ad4f9e 93 if (buttonA.read() == 1) {
ozy 3:1d99b6ad4f9e 94 fighter.kick_right(lcd); // draw kick on same coordinates as the standing sprite
ozy 0:99b49fd71085 95 lcd.refresh();
ozy 0:99b49fd71085 96 wait(0.2);
ozy 0:99b49fd71085 97 }
ozy 0:99b49fd71085 98 // Preform punch move if user presses button B
ozy 3:1d99b6ad4f9e 99 if (buttonB.read() == 1) {
ozy 3:1d99b6ad4f9e 100 fighter.punch_right(lcd); // draw kick
ozy 0:99b49fd71085 101 lcd.refresh();
ozy 0:99b49fd71085 102 wait(0.2);
ozy 0:99b49fd71085 103 }
ozy 0:99b49fd71085 104 // Guard if user presses button C
ozy 0:99b49fd71085 105 while (buttonC.read() == 1) {
ozy 3:1d99b6ad4f9e 106 fighter.guard(lcd); // draw guard move frame
ozy 0:99b49fd71085 107 lcd.refresh();
ozy 0:99b49fd71085 108 wait(0.2);
ozy 0:99b49fd71085 109 }
ozy 0:99b49fd71085 110 }
ozy 1:3bdadf6f6dbd 111 else if(x < 0.48) { // joystick moved to the right
ozy 0:99b49fd71085 112 // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!)
ozy 3:1d99b6ad4f9e 113 fighter.add_x(4); // increment by 4
ozy 3:1d99b6ad4f9e 114 fighter.move_right(lcd);
ozy 0:99b49fd71085 115 lcd.refresh();
ozy 0:99b49fd71085 116 wait(0.1);
ozy 3:1d99b6ad4f9e 117 fighter.move_right2(lcd);
ozy 0:99b49fd71085 118 lcd.refresh();
ozy 0:99b49fd71085 119 wait(0.08);
ozy 3:1d99b6ad4f9e 120
ozy 3:1d99b6ad4f9e 121 // kick if user presses button A
ozy 3:1d99b6ad4f9e 122 if (buttonA.read() == 1) {
ozy 3:1d99b6ad4f9e 123 fighter.kick_right(lcd);
ozy 0:99b49fd71085 124 lcd.refresh();
ozy 0:99b49fd71085 125 wait(0.2);
ozy 0:99b49fd71085 126 }
ozy 0:99b49fd71085 127 // Preform punch move if user presses button B
ozy 3:1d99b6ad4f9e 128 if (buttonB.read() == 1) {
ozy 3:1d99b6ad4f9e 129 fighter.punch_right(lcd); // draw kick on same coordinates as the sprite
ozy 0:99b49fd71085 130 lcd.refresh();
ozy 0:99b49fd71085 131 wait(0.2);
ozy 0:99b49fd71085 132 }
ozy 0:99b49fd71085 133 // Guard if user presses button C
ozy 3:1d99b6ad4f9e 134 while (buttonC.read() == 1) { // we use while not if here because user would hold to guard
ozy 3:1d99b6ad4f9e 135 fighter.guard(lcd); // draw kick on same coordinates as the sprite
ozy 0:99b49fd71085 136 lcd.refresh();
ozy 0:99b49fd71085 137 wait(0.2);
ozy 0:99b49fd71085 138 }
ozy 0:99b49fd71085 139 }
ozy 3:1d99b6ad4f9e 140
ozy 1:3bdadf6f6dbd 141 else if(x > 0.52) { // joystick moved to the left
ozy 3:1d99b6ad4f9e 142 fighter.add_x(-4); // decrement left
ozy 3:1d99b6ad4f9e 143 fighter.move_left(lcd);
ozy 1:3bdadf6f6dbd 144 lcd.refresh();
ozy 1:3bdadf6f6dbd 145 wait(0.1);
ozy 3:1d99b6ad4f9e 146 fighter.move_left2(lcd);
ozy 0:99b49fd71085 147 lcd.refresh();
ozy 0:99b49fd71085 148 wait(0.08);
ozy 0:99b49fd71085 149
ozy 3:1d99b6ad4f9e 150
ozy 3:1d99b6ad4f9e 151 // kick if user presses button A
ozy 3:1d99b6ad4f9e 152 if (buttonA.read() == 1) {
ozy 3:1d99b6ad4f9e 153 fighter.kick_left(lcd);
ozy 0:99b49fd71085 154 lcd.refresh();
ozy 0:99b49fd71085 155 wait(0.2);
ozy 0:99b49fd71085 156 }
ozy 3:1d99b6ad4f9e 157 if (buttonB.read() == 1) {
ozy 3:1d99b6ad4f9e 158 fighter.punch_left(lcd); // draw punch on same coordinates as the sprite
ozy 0:99b49fd71085 159 lcd.refresh();
ozy 0:99b49fd71085 160 wait(0.2);
ozy 0:99b49fd71085 161 }
ozy 0:99b49fd71085 162 // Guard if user presses button C
ozy 3:1d99b6ad4f9e 163 if (buttonC.read() == 1) {
ozy 3:1d99b6ad4f9e 164 fighter.guard(lcd); // draw guard on same coordinates as the sprite
ozy 0:99b49fd71085 165 lcd.refresh();
ozy 0:99b49fd71085 166 wait(0.2);
ozy 0:99b49fd71085 167 }
ozy 3:1d99b6ad4f9e 168 }
ozy 0:99b49fd71085 169 }
ozy 0:99b49fd71085 170
ozy 1:3bdadf6f6dbd 171 void move_enemy() {
ozy 3:1d99b6ad4f9e 172 // read each of the pins
ozy 1:3bdadf6f6dbd 173 float x = joy_h.read();
ozy 1:3bdadf6f6dbd 174 float y = joy_v.read();
ozy 1:3bdadf6f6dbd 175 printf("x = %.2f | y = %.2f \n",x,y);
ozy 3:1d99b6ad4f9e 176
ozy 1:3bdadf6f6dbd 177 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
ozy 1:3bdadf6f6dbd 178 enemy.draw(lcd, x+33, GROUND); // draw standing fighter and add 30 print it on middle of lcd screen
ozy 1:3bdadf6f6dbd 179 // ... and the y-coordinate to place sprite exactly on ground is 34 ( 46 - 12(height of sprite) = 34 )
ozy 1:3bdadf6f6dbd 180 lcd.refresh();
ozy 1:3bdadf6f6dbd 181 wait(0.2);
ozy 3:1d99b6ad4f9e 182 // Preform kick move if user presses button A
ozy 1:3bdadf6f6dbd 183 while (buttonA.read() == 1) {
ozy 1:3bdadf6f6dbd 184 enemy.kick_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite
ozy 1:3bdadf6f6dbd 185 lcd.refresh();
ozy 1:3bdadf6f6dbd 186 wait(0.2);
ozy 1:3bdadf6f6dbd 187 }
ozy 1:3bdadf6f6dbd 188 // Preform punch move if user presses button B
ozy 1:3bdadf6f6dbd 189 while (buttonB.read() == 1) {
ozy 1:3bdadf6f6dbd 190 enemy.sword_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite
ozy 1:3bdadf6f6dbd 191 lcd.refresh();
ozy 1:3bdadf6f6dbd 192 wait(0.2);
ozy 1:3bdadf6f6dbd 193 }
ozy 1:3bdadf6f6dbd 194 }
ozy 1:3bdadf6f6dbd 195 else if(x < 0.48) { // joystick moved to the right
ozy 1:3bdadf6f6dbd 196 // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!)
ozy 1:3bdadf6f6dbd 197 enemy.move_right(lcd, SPEED*x+70, GROUND); // draw standing fighter, multiply by speed -69 (negative to correct for direction!)
ozy 1:3bdadf6f6dbd 198 lcd.refresh();
ozy 1:3bdadf6f6dbd 199 wait(0.1);
ozy 1:3bdadf6f6dbd 200 enemy.move_right2(lcd, SPEED*x+70, GROUND);
ozy 1:3bdadf6f6dbd 201 lcd.refresh();
ozy 1:3bdadf6f6dbd 202 wait(0.08);
ozy 3:1d99b6ad4f9e 203
ozy 3:1d99b6ad4f9e 204 // kick if user presses button A
ozy 1:3bdadf6f6dbd 205 while (buttonA.read() == 1) {
ozy 3:1d99b6ad4f9e 206 enemy.kick_right(lcd, SPEED*x+70, GROUND);
ozy 1:3bdadf6f6dbd 207 lcd.refresh();
ozy 1:3bdadf6f6dbd 208 wait(0.2);
ozy 1:3bdadf6f6dbd 209 }
ozy 1:3bdadf6f6dbd 210 // Preform punch move if user presses button B
ozy 1:3bdadf6f6dbd 211 while (buttonB.read() == 1) {
ozy 1:3bdadf6f6dbd 212 enemy.sword_right(lcd, SPEED*x+70, GROUND); // draw kick on same coordinates as the sprite
ozy 1:3bdadf6f6dbd 213 lcd.refresh();
ozy 1:3bdadf6f6dbd 214 wait(0.2);
ozy 1:3bdadf6f6dbd 215 }
ozy 1:3bdadf6f6dbd 216 }
ozy 1:3bdadf6f6dbd 217 else if(x > 0.52) { // joystick moved to the left
ozy 1:3bdadf6f6dbd 218 enemy.move_left(lcd, SPEED*x+71, GROUND);
ozy 1:3bdadf6f6dbd 219 lcd.refresh();
ozy 1:3bdadf6f6dbd 220 wait(0.1);
ozy 1:3bdadf6f6dbd 221 enemy.move_left2(lcd, SPEED*x+71, 34);
ozy 1:3bdadf6f6dbd 222 lcd.refresh();
ozy 1:3bdadf6f6dbd 223 wait(0.08);
ozy 1:3bdadf6f6dbd 224 }
ozy 3:1d99b6ad4f9e 225
ozy 3:1d99b6ad4f9e 226 // kick if user presses button A
ozy 1:3bdadf6f6dbd 227 while (buttonA.read() == 1) {
ozy 3:1d99b6ad4f9e 228 enemy.kick_left(lcd, SPEED*x+71, GROUND);
ozy 1:3bdadf6f6dbd 229 lcd.refresh();
ozy 1:3bdadf6f6dbd 230 wait(0.2);
ozy 1:3bdadf6f6dbd 231 }
ozy 1:3bdadf6f6dbd 232 while (buttonB.read() == 1) {
ozy 1:3bdadf6f6dbd 233 enemy.sword_left(lcd, SPEED*x+71, GROUND); // draw kick on same coordinates as the sprite
ozy 1:3bdadf6f6dbd 234 lcd.refresh();
ozy 1:3bdadf6f6dbd 235 wait(0.2);
ozy 1:3bdadf6f6dbd 236 }
ozy 0:99b49fd71085 237 }
ozy 0:99b49fd71085 238
ozy 2:1703eb2a68f8 239
ozy 2:1703eb2a68f8 240