Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Committer:
ozy
Date:
Sat Apr 17 12:35:18 2021 +0000
Revision:
2:1703eb2a68f8
Parent:
1:3bdadf6f6dbd
Child:
3:1d99b6ad4f9e
Code AFTER interrupts (not very efficient)

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 0:99b49fd71085 11 ///////////// objects ///////////////////
ozy 0:99b49fd71085 12 Fighter fighter;
ozy 0:99b49fd71085 13 Menu menu;
ozy 1:3bdadf6f6dbd 14 Enemy enemy;
ozy 2:1703eb2a68f8 15 InterruptIn buttonA(p29);
ozy 2:1703eb2a68f8 16 InterruptIn buttonB(p28);
ozy 2:1703eb2a68f8 17 InterruptIn buttonC(p27);
ozy 2:1703eb2a68f8 18 InterruptIn buttonD(p26);
ozy 0:99b49fd71085 19 AnalogIn joy_v(p20);
ozy 0:99b49fd71085 20 AnalogIn joy_h(p19);
ozy 0:99b49fd71085 21 N5110 lcd(p14,p8,p9,p10,p11,p13,p21);
ozy 0:99b49fd71085 22 BusOut leds(LED4,LED3,LED2,LED1);
ozy 0:99b49fd71085 23 Serial pc(USBTX, USBRX);
ozy 0:99b49fd71085 24 //////////// functions ////////////////////
ozy 0:99b49fd71085 25 void init();
ozy 0:99b49fd71085 26 void menu_screen();
ozy 0:99b49fd71085 27 void move_sprite();
ozy 0:99b49fd71085 28 void draw_background();
ozy 1:3bdadf6f6dbd 29 void move_enemy();
ozy 1:3bdadf6f6dbd 30 int get_user_input(int &input);
ozy 2:1703eb2a68f8 31 void buttonA_isr();
ozy 2:1703eb2a68f8 32 void buttonB_isr();
ozy 2:1703eb2a68f8 33 void buttonC_isr();
ozy 2:1703eb2a68f8 34 void buttonD_isr();
ozy 2:1703eb2a68f8 35 void button_init();
ozy 2:1703eb2a68f8 36 void check_interrupt();
ozy 2:1703eb2a68f8 37 //////////// initializations ////////////////////
ozy 2:1703eb2a68f8 38 volatile int g_buttonA_flag = 0;
ozy 2:1703eb2a68f8 39 volatile int g_buttonB_flag = 0;
ozy 2:1703eb2a68f8 40 volatile int g_buttonC_flag = 0;
ozy 2:1703eb2a68f8 41 volatile int g_buttonD_flag = 0;
ozy 0:99b49fd71085 42
ozy 0:99b49fd71085 43
ozy 0:99b49fd71085 44 int main() {
ozy 0:99b49fd71085 45 init();
ozy 1:3bdadf6f6dbd 46 menu_screen();
ozy 0:99b49fd71085 47 while(1) {
ozy 2:1703eb2a68f8 48 lcd.clear();
ozy 1:3bdadf6f6dbd 49 // draw_background();
ozy 1:3bdadf6f6dbd 50 // move_enemy();
ozy 1:3bdadf6f6dbd 51 // move_sprite();
ozy 2:1703eb2a68f8 52 check_interrupt();
ozy 2:1703eb2a68f8 53 // put the MCU to sleep until an interrupt wakes it up
ozy 2:1703eb2a68f8 54 sleep();
ozy 0:99b49fd71085 55 }
ozy 0:99b49fd71085 56 }
ozy 0:99b49fd71085 57
ozy 0:99b49fd71085 58 void init() { // initialize all devices
ozy 0:99b49fd71085 59 lcd.init();
ozy 0:99b49fd71085 60 lcd.setContrast(0.5);
ozy 2:1703eb2a68f8 61 button_init();
ozy 0:99b49fd71085 62 }
ozy 0:99b49fd71085 63
ozy 0:99b49fd71085 64 void menu_screen() {
ozy 0:99b49fd71085 65 menu.main_menu(lcd); // printing the menu screen
ozy 0:99b49fd71085 66 wait(3.5f);
ozy 0:99b49fd71085 67 menu.created_by(lcd); // 2nd menu screen
ozy 0:99b49fd71085 68 wait(3.5f);
ozy 1:3bdadf6f6dbd 69 menu.homescreen(lcd); // print homescreen
ozy 1:3bdadf6f6dbd 70 menu.menu_selection(lcd, buttonA, buttonB, buttonC, buttonD); // function to ask user to select menu item
ozy 0:99b49fd71085 71 }
ozy 0:99b49fd71085 72
ozy 0:99b49fd71085 73
ozy 0:99b49fd71085 74 void draw_background() {
ozy 0:99b49fd71085 75 // lcd.drawLine(x1, y1, x2, y2, type);
ozy 0:99b49fd71085 76 lcd.drawLine(0,46,82,46,1); // draws ground platform
ozy 0:99b49fd71085 77 lcd.drawLine(0,46,0,25,1); // draws first side wall
ozy 0:99b49fd71085 78 lcd.drawLine(82,46,82,25,1); // draws second side wall
ozy 0:99b49fd71085 79 lcd.refresh();
ozy 0:99b49fd71085 80 }
ozy 0:99b49fd71085 81
ozy 0:99b49fd71085 82
ozy 0:99b49fd71085 83 void move_sprite() {
ozy 0:99b49fd71085 84 // getting joystick coordinates using read() function
ozy 0:99b49fd71085 85 // joystick centered at (0.50,0.50) with utmost left at (1.00,0.50) and utmost right being (0.00,0.50)
ozy 0:99b49fd71085 86
ozy 0:99b49fd71085 87 // read each of the pins
ozy 0:99b49fd71085 88 float x = joy_h.read();
ozy 0:99b49fd71085 89 float y = joy_v.read();
ozy 0:99b49fd71085 90 printf("x = %.2f | y = %.2f \n",x,y);
ozy 0:99b49fd71085 91 /*
ozy 0:99b49fd71085 92 Code idea:
ozy 0:99b49fd71085 93 if joystick is not moved , display standing sprite
ozy 0:99b49fd71085 94 if joystick is moved right, toggle between runright and midrunright
ozy 0:99b49fd71085 95 if joystick is moved left, toggle between runleft and midrunleft
ozy 0:99b49fd71085 96 */
ozy 1:3bdadf6f6dbd 97 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 0:99b49fd71085 98 fighter.draw(lcd, x+33, GROUND); // draw standing fighter and add 30 print it on middle of lcd screen
ozy 0:99b49fd71085 99 // ... and the y-coordinate to place sprite exactly on ground is 34 ( 46 - 12(height of sprite) = 34 )
ozy 0:99b49fd71085 100 lcd.refresh();
ozy 0:99b49fd71085 101 wait(0.2);
ozy 0:99b49fd71085 102
ozy 0:99b49fd71085 103 // Preform kick move if user presses button A
ozy 0:99b49fd71085 104 while (buttonA.read() == 1) {
ozy 0:99b49fd71085 105 fighter.kick_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite
ozy 0:99b49fd71085 106 lcd.refresh();
ozy 0:99b49fd71085 107 wait(0.2);
ozy 0:99b49fd71085 108 }
ozy 0:99b49fd71085 109 // Preform punch move if user presses button B
ozy 0:99b49fd71085 110 while (buttonB.read() == 1) {
ozy 0:99b49fd71085 111 fighter.punch_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite
ozy 0:99b49fd71085 112 lcd.refresh();
ozy 0:99b49fd71085 113 wait(0.2);
ozy 0:99b49fd71085 114 }
ozy 0:99b49fd71085 115 // Guard if user presses button C
ozy 0:99b49fd71085 116 while (buttonC.read() == 1) {
ozy 0:99b49fd71085 117 fighter.guard(lcd, x+33, GROUND); // draw guard move on same coordinates as the standing sprite
ozy 0:99b49fd71085 118 lcd.refresh();
ozy 0:99b49fd71085 119 wait(0.2);
ozy 0:99b49fd71085 120 }
ozy 0:99b49fd71085 121 // Jump if user presses button D
ozy 0:99b49fd71085 122 while (buttonD.read() == 1) {
ozy 0:99b49fd71085 123 fighter.draw(lcd, x+33, GROUND-15); // adding 15 the sprite y-coordinate to create jump move
ozy 0:99b49fd71085 124 lcd.refresh();
ozy 0:99b49fd71085 125 wait(0.2);
ozy 0:99b49fd71085 126 }
ozy 0:99b49fd71085 127 }
ozy 1:3bdadf6f6dbd 128 else if(x < 0.48) { // joystick moved to the right
ozy 0:99b49fd71085 129 // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!)
ozy 0:99b49fd71085 130 fighter.move_right(lcd, SPEED*x+70, GROUND); // draw standing fighter, multiply by speed -69 (negative to correct for direction!)
ozy 0:99b49fd71085 131 lcd.refresh();
ozy 0:99b49fd71085 132 wait(0.1);
ozy 0:99b49fd71085 133 fighter.move_right2(lcd, SPEED*x+70, GROUND);
ozy 0:99b49fd71085 134 lcd.refresh();
ozy 0:99b49fd71085 135 wait(0.08);
ozy 0:99b49fd71085 136
ozy 0:99b49fd71085 137 // kick if user presses button A
ozy 0:99b49fd71085 138 while (buttonA.read() == 1) {
ozy 0:99b49fd71085 139 fighter.kick_right(lcd, SPEED*x+70, GROUND);
ozy 0:99b49fd71085 140 lcd.refresh();
ozy 0:99b49fd71085 141 wait(0.2);
ozy 0:99b49fd71085 142 }
ozy 0:99b49fd71085 143 // Preform punch move if user presses button B
ozy 0:99b49fd71085 144 while (buttonB.read() == 1) {
ozy 0:99b49fd71085 145 fighter.punch_right(lcd, SPEED*x+70, GROUND); // draw kick on same coordinates as the sprite
ozy 0:99b49fd71085 146 lcd.refresh();
ozy 0:99b49fd71085 147 wait(0.2);
ozy 0:99b49fd71085 148 }
ozy 0:99b49fd71085 149 // Guard if user presses button C
ozy 0:99b49fd71085 150 while (buttonC.read() == 1) {
ozy 0:99b49fd71085 151 fighter.guard(lcd, SPEED*x+70, GROUND); // draw kick on same coordinates as the sprite
ozy 0:99b49fd71085 152 lcd.refresh();
ozy 0:99b49fd71085 153 wait(0.2);
ozy 0:99b49fd71085 154 }
ozy 0:99b49fd71085 155 }
ozy 1:3bdadf6f6dbd 156 else if(x > 0.52) { // joystick moved to the left
ozy 0:99b49fd71085 157 fighter.move_left(lcd, SPEED*x+71, GROUND);
ozy 1:3bdadf6f6dbd 158 lcd.refresh();
ozy 1:3bdadf6f6dbd 159 wait(0.1);
ozy 1:3bdadf6f6dbd 160 fighter.move_left2(lcd, SPEED*x+71, 34);
ozy 0:99b49fd71085 161 lcd.refresh();
ozy 0:99b49fd71085 162 wait(0.08);
ozy 0:99b49fd71085 163 }
ozy 0:99b49fd71085 164
ozy 0:99b49fd71085 165 // kick if user presses button A
ozy 0:99b49fd71085 166 while (buttonA.read() == 1) {
ozy 0:99b49fd71085 167 fighter.kick_left(lcd, SPEED*x+71, GROUND);
ozy 0:99b49fd71085 168 lcd.refresh();
ozy 0:99b49fd71085 169 wait(0.2);
ozy 0:99b49fd71085 170 }
ozy 0:99b49fd71085 171 while (buttonB.read() == 1) {
ozy 0:99b49fd71085 172 fighter.punch_left(lcd, SPEED*x+71, GROUND); // draw kick on same coordinates as the sprite
ozy 0:99b49fd71085 173 lcd.refresh();
ozy 0:99b49fd71085 174 wait(0.2);
ozy 0:99b49fd71085 175 }
ozy 0:99b49fd71085 176 // Guard if user presses button C
ozy 0:99b49fd71085 177 while (buttonC.read() == 1) {
ozy 0:99b49fd71085 178 fighter.guard(lcd, SPEED*x+71, GROUND); // draw kick on same coordinates as the sprite
ozy 0:99b49fd71085 179 lcd.refresh();
ozy 0:99b49fd71085 180 wait(0.2);
ozy 0:99b49fd71085 181 }
ozy 0:99b49fd71085 182 }
ozy 0:99b49fd71085 183
ozy 1:3bdadf6f6dbd 184 void move_enemy() {
ozy 1:3bdadf6f6dbd 185 // read each of the pins
ozy 1:3bdadf6f6dbd 186 float x = joy_h.read();
ozy 1:3bdadf6f6dbd 187 float y = joy_v.read();
ozy 1:3bdadf6f6dbd 188 printf("x = %.2f | y = %.2f \n",x,y);
ozy 1:3bdadf6f6dbd 189
ozy 1:3bdadf6f6dbd 190 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 191 enemy.draw(lcd, x+33, GROUND); // draw standing fighter and add 30 print it on middle of lcd screen
ozy 1:3bdadf6f6dbd 192 // ... and the y-coordinate to place sprite exactly on ground is 34 ( 46 - 12(height of sprite) = 34 )
ozy 1:3bdadf6f6dbd 193 lcd.refresh();
ozy 1:3bdadf6f6dbd 194 wait(0.2);
ozy 1:3bdadf6f6dbd 195 // Preform kick move if user presses button A
ozy 1:3bdadf6f6dbd 196 while (buttonA.read() == 1) {
ozy 1:3bdadf6f6dbd 197 enemy.kick_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite
ozy 1:3bdadf6f6dbd 198 lcd.refresh();
ozy 1:3bdadf6f6dbd 199 wait(0.2);
ozy 1:3bdadf6f6dbd 200 }
ozy 1:3bdadf6f6dbd 201 // Preform punch move if user presses button B
ozy 1:3bdadf6f6dbd 202 while (buttonB.read() == 1) {
ozy 1:3bdadf6f6dbd 203 enemy.sword_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite
ozy 1:3bdadf6f6dbd 204 lcd.refresh();
ozy 1:3bdadf6f6dbd 205 wait(0.2);
ozy 1:3bdadf6f6dbd 206 }
ozy 1:3bdadf6f6dbd 207 }
ozy 1:3bdadf6f6dbd 208 else if(x < 0.48) { // joystick moved to the right
ozy 1:3bdadf6f6dbd 209 // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!)
ozy 1:3bdadf6f6dbd 210 enemy.move_right(lcd, SPEED*x+70, GROUND); // draw standing fighter, multiply by speed -69 (negative to correct for direction!)
ozy 1:3bdadf6f6dbd 211 lcd.refresh();
ozy 1:3bdadf6f6dbd 212 wait(0.1);
ozy 1:3bdadf6f6dbd 213 enemy.move_right2(lcd, SPEED*x+70, GROUND);
ozy 1:3bdadf6f6dbd 214 lcd.refresh();
ozy 1:3bdadf6f6dbd 215 wait(0.08);
ozy 1:3bdadf6f6dbd 216
ozy 1:3bdadf6f6dbd 217 // kick if user presses button A
ozy 1:3bdadf6f6dbd 218 while (buttonA.read() == 1) {
ozy 1:3bdadf6f6dbd 219 enemy.kick_right(lcd, SPEED*x+70, GROUND);
ozy 1:3bdadf6f6dbd 220 lcd.refresh();
ozy 1:3bdadf6f6dbd 221 wait(0.2);
ozy 1:3bdadf6f6dbd 222 }
ozy 1:3bdadf6f6dbd 223 // Preform punch move if user presses button B
ozy 1:3bdadf6f6dbd 224 while (buttonB.read() == 1) {
ozy 1:3bdadf6f6dbd 225 enemy.sword_right(lcd, SPEED*x+70, GROUND); // draw kick on same coordinates as the sprite
ozy 1:3bdadf6f6dbd 226 lcd.refresh();
ozy 1:3bdadf6f6dbd 227 wait(0.2);
ozy 1:3bdadf6f6dbd 228 }
ozy 1:3bdadf6f6dbd 229 }
ozy 1:3bdadf6f6dbd 230 else if(x > 0.52) { // joystick moved to the left
ozy 1:3bdadf6f6dbd 231 enemy.move_left(lcd, SPEED*x+71, GROUND);
ozy 1:3bdadf6f6dbd 232 lcd.refresh();
ozy 1:3bdadf6f6dbd 233 wait(0.1);
ozy 1:3bdadf6f6dbd 234 enemy.move_left2(lcd, SPEED*x+71, 34);
ozy 1:3bdadf6f6dbd 235 lcd.refresh();
ozy 1:3bdadf6f6dbd 236 wait(0.08);
ozy 1:3bdadf6f6dbd 237 }
ozy 1:3bdadf6f6dbd 238
ozy 1:3bdadf6f6dbd 239 // kick if user presses button A
ozy 1:3bdadf6f6dbd 240 while (buttonA.read() == 1) {
ozy 1:3bdadf6f6dbd 241 enemy.kick_left(lcd, SPEED*x+71, GROUND);
ozy 1:3bdadf6f6dbd 242 lcd.refresh();
ozy 1:3bdadf6f6dbd 243 wait(0.2);
ozy 1:3bdadf6f6dbd 244 }
ozy 1:3bdadf6f6dbd 245 while (buttonB.read() == 1) {
ozy 1:3bdadf6f6dbd 246 enemy.sword_left(lcd, SPEED*x+71, GROUND); // draw kick on same coordinates as the sprite
ozy 1:3bdadf6f6dbd 247 lcd.refresh();
ozy 1:3bdadf6f6dbd 248 wait(0.2);
ozy 1:3bdadf6f6dbd 249 }
ozy 0:99b49fd71085 250 }
ozy 0:99b49fd71085 251
ozy 1:3bdadf6f6dbd 252 int get_user_input(int &input) {
ozy 1:3bdadf6f6dbd 253 while (1) {
ozy 1:3bdadf6f6dbd 254 if (buttonA.read() == 1) {
ozy 1:3bdadf6f6dbd 255 input = 1;
ozy 1:3bdadf6f6dbd 256 break;
ozy 1:3bdadf6f6dbd 257 }
ozy 1:3bdadf6f6dbd 258 if (buttonB.read() == 1) {
ozy 1:3bdadf6f6dbd 259 input = 2;
ozy 1:3bdadf6f6dbd 260 break;
ozy 1:3bdadf6f6dbd 261 }
ozy 1:3bdadf6f6dbd 262 if (buttonC.read() == 1) {
ozy 1:3bdadf6f6dbd 263 input = 3;
ozy 1:3bdadf6f6dbd 264 break;
ozy 1:3bdadf6f6dbd 265 }
ozy 1:3bdadf6f6dbd 266 if (buttonD.read() == 1) {
ozy 1:3bdadf6f6dbd 267 input = 4;
ozy 1:3bdadf6f6dbd 268 break;
ozy 1:3bdadf6f6dbd 269 }
ozy 1:3bdadf6f6dbd 270 }
ozy 1:3bdadf6f6dbd 271 return input;
ozy 0:99b49fd71085 272 }
ozy 1:3bdadf6f6dbd 273
ozy 2:1703eb2a68f8 274 // Button A B C D event-triggered interrupt
ozy 2:1703eb2a68f8 275 void buttonA_isr() {
ozy 2:1703eb2a68f8 276 g_buttonA_flag = 1; // set flag in ISR
ozy 2:1703eb2a68f8 277 }
ozy 2:1703eb2a68f8 278 void buttonB_isr() {
ozy 2:1703eb2a68f8 279 g_buttonB_flag = 1; // set flag in ISR
ozy 2:1703eb2a68f8 280 }
ozy 1:3bdadf6f6dbd 281
ozy 2:1703eb2a68f8 282 void buttonC_isr() {
ozy 2:1703eb2a68f8 283 g_buttonC_flag = 1; // set flag in ISR
ozy 2:1703eb2a68f8 284 }
ozy 2:1703eb2a68f8 285
ozy 2:1703eb2a68f8 286 void buttonD_isr() {
ozy 2:1703eb2a68f8 287 g_buttonD_flag = 1; // set flag in ISR
ozy 2:1703eb2a68f8 288 }
ozy 2:1703eb2a68f8 289
ozy 2:1703eb2a68f8 290 void button_init() {
ozy 2:1703eb2a68f8 291 buttonA.rise(&buttonA_isr);
ozy 2:1703eb2a68f8 292 buttonB.rise(&buttonB_isr);
ozy 2:1703eb2a68f8 293 buttonC.rise(&buttonC_isr);
ozy 2:1703eb2a68f8 294 buttonD.rise(&buttonD_isr);
ozy 2:1703eb2a68f8 295 buttonA.mode(PullNone);
ozy 2:1703eb2a68f8 296 buttonB.mode(PullNone);
ozy 2:1703eb2a68f8 297 buttonC.mode(PullNone);
ozy 2:1703eb2a68f8 298 buttonD.mode(PullNone);
ozy 2:1703eb2a68f8 299 }
ozy 2:1703eb2a68f8 300
ozy 2:1703eb2a68f8 301 void check_interrupt() {
ozy 2:1703eb2a68f8 302 int input;
ozy 2:1703eb2a68f8 303 get_user_input(input);
ozy 2:1703eb2a68f8 304 // check if flag i.e. interrupt has occured
ozy 2:1703eb2a68f8 305 if (g_buttonA_flag == 1) {
ozy 2:1703eb2a68f8 306 g_buttonA_flag = 0;
ozy 2:1703eb2a68f8 307 printf("user input is %i ", input);
ozy 2:1703eb2a68f8 308 wait(0.1);
ozy 2:1703eb2a68f8 309 }
ozy 2:1703eb2a68f8 310 else if (g_buttonB_flag == 1) {
ozy 2:1703eb2a68f8 311 g_buttonB_flag = 0;
ozy 2:1703eb2a68f8 312 printf("user input is %i ", input);
ozy 2:1703eb2a68f8 313 wait(0.1);
ozy 2:1703eb2a68f8 314 }
ozy 2:1703eb2a68f8 315 else if (g_buttonC_flag == 1) {
ozy 2:1703eb2a68f8 316 g_buttonC_flag = 0;
ozy 2:1703eb2a68f8 317 printf("user input is %i ", input);
ozy 2:1703eb2a68f8 318 wait(0.1);
ozy 2:1703eb2a68f8 319 }
ozy 2:1703eb2a68f8 320 else if (g_buttonD_flag == 1) {
ozy 2:1703eb2a68f8 321 g_buttonD_flag = 0;
ozy 2:1703eb2a68f8 322 printf("user input is %i ", input);
ozy 2:1703eb2a68f8 323 wait(0.1);
ozy 2:1703eb2a68f8 324 }
ozy 2:1703eb2a68f8 325 }