Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
main.cpp@5:889ad974b64d, 2021-04-20 (annotated)
- Committer:
- ozy
- Date:
- Tue Apr 20 09:01:35 2021 +0000
- Revision:
- 5:889ad974b64d
- Parent:
- 4:6b1de03716d6
- Child:
- 6:a1a7dc264fed
Code after new enemy class but before combining draw functions
Who changed what in which revision?
User | Revision | Line number | New 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 GROUND 34 |
ozy | 0:99b49fd71085 | 10 | ///////////// objects /////////////////// |
ozy | 3:1d99b6ad4f9e | 11 | Fighter fighter; |
ozy | 0:99b49fd71085 | 12 | Menu menu; |
ozy | 1:3bdadf6f6dbd | 13 | Enemy enemy; |
ozy | 3:1d99b6ad4f9e | 14 | DigitalIn buttonA(p29); |
ozy | 3:1d99b6ad4f9e | 15 | DigitalIn buttonB(p28); |
ozy | 3:1d99b6ad4f9e | 16 | DigitalIn buttonC(p27); |
ozy | 3:1d99b6ad4f9e | 17 | DigitalIn buttonD(p26); |
ozy | 0:99b49fd71085 | 18 | AnalogIn joy_v(p20); |
ozy | 0:99b49fd71085 | 19 | AnalogIn joy_h(p19); |
ozy | 0:99b49fd71085 | 20 | N5110 lcd(p14,p8,p9,p10,p11,p13,p21); |
ozy | 0:99b49fd71085 | 21 | BusOut leds(LED4,LED3,LED2,LED1); |
ozy | 0:99b49fd71085 | 22 | Serial pc(USBTX, USBRX); |
ozy | 0:99b49fd71085 | 23 | //////////// functions //////////////////// |
ozy | 0:99b49fd71085 | 24 | void init(); |
ozy | 0:99b49fd71085 | 25 | void menu_screen(); |
ozy | 0:99b49fd71085 | 26 | void move_sprite(); |
ozy | 0:99b49fd71085 | 27 | void draw_background(); |
ozy | 1:3bdadf6f6dbd | 28 | void move_enemy(); |
ozy | 3:1d99b6ad4f9e | 29 | |
ozy | 0:99b49fd71085 | 30 | |
ozy | 0:99b49fd71085 | 31 | |
ozy | 0:99b49fd71085 | 32 | int main() { |
ozy | 0:99b49fd71085 | 33 | init(); |
ozy | 3:1d99b6ad4f9e | 34 | // menu_screen(); |
ozy | 0:99b49fd71085 | 35 | while(1) { |
ozy | 2:1703eb2a68f8 | 36 | lcd.clear(); |
ozy | 3:1d99b6ad4f9e | 37 | draw_background(); |
ozy | 3:1d99b6ad4f9e | 38 | move_sprite(); |
ozy | 1:3bdadf6f6dbd | 39 | // move_enemy(); |
ozy | 0:99b49fd71085 | 40 | } |
ozy | 0:99b49fd71085 | 41 | } |
ozy | 0:99b49fd71085 | 42 | |
ozy | 0:99b49fd71085 | 43 | void init() { // initialize all devices |
ozy | 0:99b49fd71085 | 44 | lcd.init(); |
ozy | 0:99b49fd71085 | 45 | lcd.setContrast(0.5); |
ozy | 3:1d99b6ad4f9e | 46 | fighter.set_x(33); // setting initial position of fighter |
ozy | 3:1d99b6ad4f9e | 47 | fighter.set_y(GROUND); // placing fighter on ground |
ozy | 5:889ad974b64d | 48 | enemy.set_x(33); // setting initial position of fighter |
ozy | 5:889ad974b64d | 49 | enemy.set_y(GROUND); // placing fighter on ground |
ozy | 0:99b49fd71085 | 50 | } |
ozy | 0:99b49fd71085 | 51 | |
ozy | 3:1d99b6ad4f9e | 52 | void menu_screen() { |
ozy | 3:1d99b6ad4f9e | 53 | menu.main_menu(lcd); // printing the menu screen |
ozy | 0:99b49fd71085 | 54 | wait(3.5f); |
ozy | 0:99b49fd71085 | 55 | menu.created_by(lcd); // 2nd menu screen |
ozy | 0:99b49fd71085 | 56 | wait(3.5f); |
ozy | 1:3bdadf6f6dbd | 57 | menu.homescreen(lcd); // print homescreen |
ozy | 1:3bdadf6f6dbd | 58 | menu.menu_selection(lcd, buttonA, buttonB, buttonC, buttonD); // function to ask user to select menu item |
ozy | 0:99b49fd71085 | 59 | } |
ozy | 0:99b49fd71085 | 60 | |
ozy | 0:99b49fd71085 | 61 | |
ozy | 0:99b49fd71085 | 62 | void draw_background() { |
ozy | 0:99b49fd71085 | 63 | // lcd.drawLine(x1, y1, x2, y2, type); |
ozy | 0:99b49fd71085 | 64 | lcd.drawLine(0,46,82,46,1); // draws ground platform |
ozy | 0:99b49fd71085 | 65 | lcd.drawLine(0,46,0,25,1); // draws first side wall |
ozy | 0:99b49fd71085 | 66 | lcd.drawLine(82,46,82,25,1); // draws second side wall |
ozy | 0:99b49fd71085 | 67 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 68 | } |
ozy | 0:99b49fd71085 | 69 | |
ozy | 0:99b49fd71085 | 70 | |
ozy | 0:99b49fd71085 | 71 | void move_sprite() { |
ozy | 0:99b49fd71085 | 72 | // getting joystick coordinates using read() function |
ozy | 0:99b49fd71085 | 73 | // 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 | 74 | |
ozy | 3:1d99b6ad4f9e | 75 | // read each of the pins |
ozy | 0:99b49fd71085 | 76 | float x = joy_h.read(); |
ozy | 0:99b49fd71085 | 77 | float y = joy_v.read(); |
ozy | 0:99b49fd71085 | 78 | printf("x = %.2f | y = %.2f \n",x,y); |
ozy | 5:889ad974b64d | 79 | // get x position of fighter |
ozy | 4:6b1de03716d6 | 80 | int x_pos = fighter.get_x(); |
ozy | 4:6b1de03716d6 | 81 | printf("fighter x pos = %i \n", x_pos); |
ozy | 4:6b1de03716d6 | 82 | |
ozy | 3:1d99b6ad4f9e | 83 | /* |
ozy | 3:1d99b6ad4f9e | 84 | Code idea: |
ozy | 0:99b49fd71085 | 85 | if joystick is not moved , display standing sprite |
ozy | 0:99b49fd71085 | 86 | if joystick is moved right, toggle between runright and midrunright |
ozy | 0:99b49fd71085 | 87 | if joystick is moved left, toggle between runleft and midrunleft |
ozy | 0:99b49fd71085 | 88 | */ |
ozy | 3:1d99b6ad4f9e | 89 | 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 | 90 | fighter.draw(lcd); // draw standing fighter |
ozy | 0:99b49fd71085 | 91 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 92 | wait(0.3); |
ozy | 3:1d99b6ad4f9e | 93 | |
ozy | 3:1d99b6ad4f9e | 94 | // Preform kick move if user presses button A |
ozy | 3:1d99b6ad4f9e | 95 | if (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 96 | fighter.kick_right(lcd); // draw kick on same coordinates as the standing sprite |
ozy | 0:99b49fd71085 | 97 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 98 | wait(0.3); |
ozy | 0:99b49fd71085 | 99 | } |
ozy | 0:99b49fd71085 | 100 | // Preform punch move if user presses button B |
ozy | 3:1d99b6ad4f9e | 101 | if (buttonB.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 102 | fighter.punch_right(lcd); // draw kick |
ozy | 0:99b49fd71085 | 103 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 104 | wait(0.3); |
ozy | 0:99b49fd71085 | 105 | } |
ozy | 0:99b49fd71085 | 106 | // Guard if user presses button C |
ozy | 0:99b49fd71085 | 107 | while (buttonC.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 108 | fighter.guard(lcd); // draw guard move frame |
ozy | 0:99b49fd71085 | 109 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 110 | wait(0.3); |
ozy | 0:99b49fd71085 | 111 | } |
ozy | 0:99b49fd71085 | 112 | } |
ozy | 1:3bdadf6f6dbd | 113 | else if(x < 0.48) { // joystick moved to the right |
ozy | 0:99b49fd71085 | 114 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 4:6b1de03716d6 | 115 | fighter.add_x(5); // increment by 5 |
ozy | 3:1d99b6ad4f9e | 116 | fighter.move_right(lcd); |
ozy | 0:99b49fd71085 | 117 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 118 | wait(0.1); |
ozy | 3:1d99b6ad4f9e | 119 | fighter.move_right2(lcd); |
ozy | 0:99b49fd71085 | 120 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 121 | wait(0.08); |
ozy | 4:6b1de03716d6 | 122 | |
ozy | 4:6b1de03716d6 | 123 | if (x_pos >= 65) { // code to stop fighter moving out of lcd screen |
ozy | 4:6b1de03716d6 | 124 | fighter.add_x(-5); |
ozy | 4:6b1de03716d6 | 125 | } |
ozy | 4:6b1de03716d6 | 126 | |
ozy | 4:6b1de03716d6 | 127 | |
ozy | 3:1d99b6ad4f9e | 128 | // kick if user presses button A |
ozy | 3:1d99b6ad4f9e | 129 | if (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 130 | fighter.kick_right(lcd); |
ozy | 0:99b49fd71085 | 131 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 132 | wait(0.3); |
ozy | 0:99b49fd71085 | 133 | } |
ozy | 0:99b49fd71085 | 134 | // Preform punch move if user presses button B |
ozy | 3:1d99b6ad4f9e | 135 | if (buttonB.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 136 | fighter.punch_right(lcd); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 137 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 138 | wait(0.3); |
ozy | 0:99b49fd71085 | 139 | } |
ozy | 0:99b49fd71085 | 140 | // Guard if user presses button C |
ozy | 3:1d99b6ad4f9e | 141 | while (buttonC.read() == 1) { // we use while not if here because user would hold to guard |
ozy | 3:1d99b6ad4f9e | 142 | fighter.guard(lcd); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 143 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 144 | wait(0.3); |
ozy | 0:99b49fd71085 | 145 | } |
ozy | 0:99b49fd71085 | 146 | } |
ozy | 3:1d99b6ad4f9e | 147 | |
ozy | 1:3bdadf6f6dbd | 148 | else if(x > 0.52) { // joystick moved to the left |
ozy | 4:6b1de03716d6 | 149 | fighter.add_x(-5); // decrement left by 5 |
ozy | 3:1d99b6ad4f9e | 150 | fighter.move_left(lcd); |
ozy | 1:3bdadf6f6dbd | 151 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 152 | wait(0.1); |
ozy | 3:1d99b6ad4f9e | 153 | fighter.move_left2(lcd); |
ozy | 0:99b49fd71085 | 154 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 155 | wait(0.08); |
ozy | 4:6b1de03716d6 | 156 | |
ozy | 4:6b1de03716d6 | 157 | if (x_pos <= 6) { // code to stop fighter moving out of lcd screen |
ozy | 4:6b1de03716d6 | 158 | fighter.add_x(5); |
ozy | 4:6b1de03716d6 | 159 | } |
ozy | 3:1d99b6ad4f9e | 160 | |
ozy | 3:1d99b6ad4f9e | 161 | // kick if user presses button A |
ozy | 3:1d99b6ad4f9e | 162 | if (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 163 | fighter.kick_left(lcd); |
ozy | 0:99b49fd71085 | 164 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 165 | wait(0.3); |
ozy | 0:99b49fd71085 | 166 | } |
ozy | 3:1d99b6ad4f9e | 167 | if (buttonB.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 168 | fighter.punch_left(lcd); // draw punch on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 169 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 170 | wait(0.3); |
ozy | 0:99b49fd71085 | 171 | } |
ozy | 0:99b49fd71085 | 172 | // Guard if user presses button C |
ozy | 3:1d99b6ad4f9e | 173 | if (buttonC.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 174 | fighter.guard(lcd); // draw guard on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 175 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 176 | wait(0.3); |
ozy | 0:99b49fd71085 | 177 | } |
ozy | 3:1d99b6ad4f9e | 178 | } |
ozy | 0:99b49fd71085 | 179 | } |
ozy | 0:99b49fd71085 | 180 | |
ozy | 1:3bdadf6f6dbd | 181 | void move_enemy() { |
ozy | 3:1d99b6ad4f9e | 182 | // read each of the pins |
ozy | 1:3bdadf6f6dbd | 183 | float x = joy_h.read(); |
ozy | 1:3bdadf6f6dbd | 184 | float y = joy_v.read(); |
ozy | 1:3bdadf6f6dbd | 185 | printf("x = %.2f | y = %.2f \n",x,y); |
ozy | 5:889ad974b64d | 186 | // get x position of enemy |
ozy | 5:889ad974b64d | 187 | int x_pos = enemy.get_x(); |
ozy | 5:889ad974b64d | 188 | printf("enemy x pos = %i \n", x_pos); |
ozy | 3:1d99b6ad4f9e | 189 | |
ozy | 4:6b1de03716d6 | 190 | 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 | 5:889ad974b64d | 191 | enemy.draw(lcd); // draw enemy |
ozy | 1:3bdadf6f6dbd | 192 | lcd.refresh(); |
ozy | 5:889ad974b64d | 193 | wait(0.3); |
ozy | 3:1d99b6ad4f9e | 194 | // Preform kick move if user presses button A |
ozy | 5:889ad974b64d | 195 | if (buttonA.read() == 1) { |
ozy | 5:889ad974b64d | 196 | enemy.kick_right(lcd); // draw kick on same coordinates as the standing sprite |
ozy | 1:3bdadf6f6dbd | 197 | lcd.refresh(); |
ozy | 5:889ad974b64d | 198 | wait(0.3); |
ozy | 1:3bdadf6f6dbd | 199 | } |
ozy | 1:3bdadf6f6dbd | 200 | // Preform punch move if user presses button B |
ozy | 5:889ad974b64d | 201 | if (buttonB.read() == 1) { |
ozy | 5:889ad974b64d | 202 | enemy.sword_right(lcd); // draw sword move on same coordinates as the standing sprite |
ozy | 1:3bdadf6f6dbd | 203 | lcd.refresh(); |
ozy | 5:889ad974b64d | 204 | wait(0.3); |
ozy | 1:3bdadf6f6dbd | 205 | } |
ozy | 1:3bdadf6f6dbd | 206 | } |
ozy | 1:3bdadf6f6dbd | 207 | else if(x < 0.48) { // joystick moved to the right |
ozy | 5:889ad974b64d | 208 | enemy.add_x(5); // increment by 5 |
ozy | 1:3bdadf6f6dbd | 209 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 5:889ad974b64d | 210 | enemy.move_right(lcd); // 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 | 5:889ad974b64d | 213 | enemy.move_right2(lcd); |
ozy | 1:3bdadf6f6dbd | 214 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 215 | wait(0.08); |
ozy | 5:889ad974b64d | 216 | |
ozy | 5:889ad974b64d | 217 | if (x_pos >= 65) { // code to stop enemy moving out of lcd screen |
ozy | 5:889ad974b64d | 218 | enemy.add_x(-5); |
ozy | 5:889ad974b64d | 219 | } |
ozy | 5:889ad974b64d | 220 | |
ozy | 3:1d99b6ad4f9e | 221 | // kick if user presses button A |
ozy | 5:889ad974b64d | 222 | if (buttonA.read() == 1) { |
ozy | 5:889ad974b64d | 223 | enemy.kick_right(lcd); |
ozy | 5:889ad974b64d | 224 | lcd.refresh(); |
ozy | 5:889ad974b64d | 225 | wait(0.3); |
ozy | 5:889ad974b64d | 226 | } |
ozy | 1:3bdadf6f6dbd | 227 | // Preform punch move if user presses button B |
ozy | 5:889ad974b64d | 228 | if (buttonB.read() == 1) { |
ozy | 5:889ad974b64d | 229 | enemy.sword_right(lcd); // draw kick on same coordinates as the sprite |
ozy | 1:3bdadf6f6dbd | 230 | lcd.refresh(); |
ozy | 5:889ad974b64d | 231 | wait(0.3); |
ozy | 1:3bdadf6f6dbd | 232 | } |
ozy | 1:3bdadf6f6dbd | 233 | } |
ozy | 1:3bdadf6f6dbd | 234 | else if(x > 0.52) { // joystick moved to the left |
ozy | 5:889ad974b64d | 235 | enemy.add_x(-5); // decrement by 5 |
ozy | 5:889ad974b64d | 236 | enemy.move_left(lcd); |
ozy | 1:3bdadf6f6dbd | 237 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 238 | wait(0.1); |
ozy | 5:889ad974b64d | 239 | enemy.move_left2(lcd); |
ozy | 1:3bdadf6f6dbd | 240 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 241 | wait(0.08); |
ozy | 5:889ad974b64d | 242 | |
ozy | 5:889ad974b64d | 243 | |
ozy | 5:889ad974b64d | 244 | if (x_pos <= 6) { // code to stop enemy moving out of lcd screen |
ozy | 5:889ad974b64d | 245 | enemy.add_x(5); |
ozy | 5:889ad974b64d | 246 | } |
ozy | 3:1d99b6ad4f9e | 247 | |
ozy | 3:1d99b6ad4f9e | 248 | // kick if user presses button A |
ozy | 5:889ad974b64d | 249 | if (buttonA.read() == 1) { |
ozy | 5:889ad974b64d | 250 | enemy.kick_left(lcd); |
ozy | 1:3bdadf6f6dbd | 251 | lcd.refresh(); |
ozy | 5:889ad974b64d | 252 | wait(0.3); |
ozy | 1:3bdadf6f6dbd | 253 | } |
ozy | 5:889ad974b64d | 254 | if (buttonB.read() == 1) { |
ozy | 5:889ad974b64d | 255 | enemy.sword_left(lcd); // draw kick on same coordinates as the sprite |
ozy | 1:3bdadf6f6dbd | 256 | lcd.refresh(); |
ozy | 5:889ad974b64d | 257 | wait(0.3); |
ozy | 1:3bdadf6f6dbd | 258 | } |
ozy | 5:889ad974b64d | 259 | } |
ozy | 0:99b49fd71085 | 260 | } |
ozy | 0:99b49fd71085 | 261 | |
ozy | 2:1703eb2a68f8 | 262 | |
ozy | 2:1703eb2a68f8 | 263 |