Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
main.cpp@4:6b1de03716d6, 2021-04-19 (annotated)
- Committer:
- ozy
- Date:
- Mon Apr 19 20:04:44 2021 +0000
- Revision:
- 4:6b1de03716d6
- Parent:
- 3:1d99b6ad4f9e
- Child:
- 5:889ad974b64d
Code before draw function trial
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 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 | 4:6b1de03716d6 | 81 | |
ozy | 4:6b1de03716d6 | 82 | int x_pos = fighter.get_x(); |
ozy | 4:6b1de03716d6 | 83 | printf("fighter x pos = %i \n", x_pos); |
ozy | 4:6b1de03716d6 | 84 | |
ozy | 3:1d99b6ad4f9e | 85 | /* |
ozy | 3:1d99b6ad4f9e | 86 | Code idea: |
ozy | 0:99b49fd71085 | 87 | if joystick is not moved , display standing sprite |
ozy | 0:99b49fd71085 | 88 | if joystick is moved right, toggle between runright and midrunright |
ozy | 0:99b49fd71085 | 89 | if joystick is moved left, toggle between runleft and midrunleft |
ozy | 0:99b49fd71085 | 90 | */ |
ozy | 3:1d99b6ad4f9e | 91 | 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 | 92 | fighter.draw(lcd); // draw standing fighter |
ozy | 0:99b49fd71085 | 93 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 94 | wait(0.3); |
ozy | 3:1d99b6ad4f9e | 95 | |
ozy | 3:1d99b6ad4f9e | 96 | // Preform kick move if user presses button A |
ozy | 3:1d99b6ad4f9e | 97 | if (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 98 | fighter.kick_right(lcd); // draw kick on same coordinates as the standing sprite |
ozy | 0:99b49fd71085 | 99 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 100 | wait(0.3); |
ozy | 0:99b49fd71085 | 101 | } |
ozy | 0:99b49fd71085 | 102 | // Preform punch move if user presses button B |
ozy | 3:1d99b6ad4f9e | 103 | if (buttonB.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 104 | fighter.punch_right(lcd); // draw kick |
ozy | 0:99b49fd71085 | 105 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 106 | wait(0.3); |
ozy | 0:99b49fd71085 | 107 | } |
ozy | 0:99b49fd71085 | 108 | // Guard if user presses button C |
ozy | 0:99b49fd71085 | 109 | while (buttonC.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 110 | fighter.guard(lcd); // draw guard move frame |
ozy | 0:99b49fd71085 | 111 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 112 | wait(0.3); |
ozy | 0:99b49fd71085 | 113 | } |
ozy | 0:99b49fd71085 | 114 | } |
ozy | 1:3bdadf6f6dbd | 115 | else if(x < 0.48) { // joystick moved to the right |
ozy | 0:99b49fd71085 | 116 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 4:6b1de03716d6 | 117 | fighter.add_x(5); // increment by 5 |
ozy | 3:1d99b6ad4f9e | 118 | fighter.move_right(lcd); |
ozy | 0:99b49fd71085 | 119 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 120 | wait(0.1); |
ozy | 3:1d99b6ad4f9e | 121 | fighter.move_right2(lcd); |
ozy | 0:99b49fd71085 | 122 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 123 | wait(0.08); |
ozy | 4:6b1de03716d6 | 124 | |
ozy | 4:6b1de03716d6 | 125 | if (x_pos >= 65) { // code to stop fighter moving out of lcd screen |
ozy | 4:6b1de03716d6 | 126 | fighter.add_x(-5); |
ozy | 4:6b1de03716d6 | 127 | } |
ozy | 4:6b1de03716d6 | 128 | |
ozy | 4:6b1de03716d6 | 129 | |
ozy | 3:1d99b6ad4f9e | 130 | // kick if user presses button A |
ozy | 3:1d99b6ad4f9e | 131 | if (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 132 | fighter.kick_right(lcd); |
ozy | 0:99b49fd71085 | 133 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 134 | wait(0.3); |
ozy | 0:99b49fd71085 | 135 | } |
ozy | 0:99b49fd71085 | 136 | // Preform punch move if user presses button B |
ozy | 3:1d99b6ad4f9e | 137 | if (buttonB.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 138 | fighter.punch_right(lcd); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 139 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 140 | wait(0.3); |
ozy | 0:99b49fd71085 | 141 | } |
ozy | 0:99b49fd71085 | 142 | // Guard if user presses button C |
ozy | 3:1d99b6ad4f9e | 143 | while (buttonC.read() == 1) { // we use while not if here because user would hold to guard |
ozy | 3:1d99b6ad4f9e | 144 | fighter.guard(lcd); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 145 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 146 | wait(0.3); |
ozy | 0:99b49fd71085 | 147 | } |
ozy | 0:99b49fd71085 | 148 | } |
ozy | 3:1d99b6ad4f9e | 149 | |
ozy | 1:3bdadf6f6dbd | 150 | else if(x > 0.52) { // joystick moved to the left |
ozy | 4:6b1de03716d6 | 151 | fighter.add_x(-5); // decrement left by 5 |
ozy | 3:1d99b6ad4f9e | 152 | fighter.move_left(lcd); |
ozy | 1:3bdadf6f6dbd | 153 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 154 | wait(0.1); |
ozy | 3:1d99b6ad4f9e | 155 | fighter.move_left2(lcd); |
ozy | 0:99b49fd71085 | 156 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 157 | wait(0.08); |
ozy | 4:6b1de03716d6 | 158 | |
ozy | 4:6b1de03716d6 | 159 | if (x_pos <= 6) { // code to stop fighter moving out of lcd screen |
ozy | 4:6b1de03716d6 | 160 | fighter.add_x(5); |
ozy | 4:6b1de03716d6 | 161 | } |
ozy | 3:1d99b6ad4f9e | 162 | |
ozy | 3:1d99b6ad4f9e | 163 | // kick if user presses button A |
ozy | 3:1d99b6ad4f9e | 164 | if (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 165 | fighter.kick_left(lcd); |
ozy | 0:99b49fd71085 | 166 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 167 | wait(0.3); |
ozy | 0:99b49fd71085 | 168 | } |
ozy | 3:1d99b6ad4f9e | 169 | if (buttonB.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 170 | fighter.punch_left(lcd); // draw punch on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 171 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 172 | wait(0.3); |
ozy | 0:99b49fd71085 | 173 | } |
ozy | 0:99b49fd71085 | 174 | // Guard if user presses button C |
ozy | 3:1d99b6ad4f9e | 175 | if (buttonC.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 176 | fighter.guard(lcd); // draw guard on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 177 | lcd.refresh(); |
ozy | 4:6b1de03716d6 | 178 | wait(0.3); |
ozy | 0:99b49fd71085 | 179 | } |
ozy | 3:1d99b6ad4f9e | 180 | } |
ozy | 0:99b49fd71085 | 181 | } |
ozy | 0:99b49fd71085 | 182 | |
ozy | 1:3bdadf6f6dbd | 183 | void move_enemy() { |
ozy | 3:1d99b6ad4f9e | 184 | // read each of the pins |
ozy | 1:3bdadf6f6dbd | 185 | float x = joy_h.read(); |
ozy | 1:3bdadf6f6dbd | 186 | float y = joy_v.read(); |
ozy | 1:3bdadf6f6dbd | 187 | printf("x = %.2f | y = %.2f \n",x,y); |
ozy | 3:1d99b6ad4f9e | 188 | |
ozy | 4:6b1de03716d6 | 189 | 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 | 1:3bdadf6f6dbd | 190 | enemy.draw(lcd, x+33, GROUND); // draw standing fighter and add 30 print it on middle of lcd screen |
ozy | 1:3bdadf6f6dbd | 191 | // ... and the y-coordinate to place sprite exactly on ground is 34 ( 46 - 12(height of sprite) = 34 ) |
ozy | 1:3bdadf6f6dbd | 192 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 193 | wait(0.2); |
ozy | 3:1d99b6ad4f9e | 194 | // Preform kick move if user presses button A |
ozy | 1:3bdadf6f6dbd | 195 | while (buttonA.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 196 | enemy.kick_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite |
ozy | 1:3bdadf6f6dbd | 197 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 198 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 199 | } |
ozy | 1:3bdadf6f6dbd | 200 | // Preform punch move if user presses button B |
ozy | 1:3bdadf6f6dbd | 201 | while (buttonB.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 202 | enemy.sword_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite |
ozy | 1:3bdadf6f6dbd | 203 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 204 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 205 | } |
ozy | 1:3bdadf6f6dbd | 206 | } |
ozy | 1:3bdadf6f6dbd | 207 | else if(x < 0.48) { // joystick moved to the right |
ozy | 1:3bdadf6f6dbd | 208 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 1:3bdadf6f6dbd | 209 | enemy.move_right(lcd, SPEED*x+70, GROUND); // draw standing fighter, multiply by speed -69 (negative to correct for direction!) |
ozy | 1:3bdadf6f6dbd | 210 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 211 | wait(0.1); |
ozy | 1:3bdadf6f6dbd | 212 | enemy.move_right2(lcd, SPEED*x+70, GROUND); |
ozy | 1:3bdadf6f6dbd | 213 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 214 | wait(0.08); |
ozy | 3:1d99b6ad4f9e | 215 | |
ozy | 3:1d99b6ad4f9e | 216 | // kick if user presses button A |
ozy | 1:3bdadf6f6dbd | 217 | while (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 218 | enemy.kick_right(lcd, SPEED*x+70, GROUND); |
ozy | 1:3bdadf6f6dbd | 219 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 220 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 221 | } |
ozy | 1:3bdadf6f6dbd | 222 | // Preform punch move if user presses button B |
ozy | 1:3bdadf6f6dbd | 223 | while (buttonB.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 224 | enemy.sword_right(lcd, SPEED*x+70, GROUND); // draw kick on same coordinates as the sprite |
ozy | 1:3bdadf6f6dbd | 225 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 226 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 227 | } |
ozy | 1:3bdadf6f6dbd | 228 | } |
ozy | 1:3bdadf6f6dbd | 229 | else if(x > 0.52) { // joystick moved to the left |
ozy | 1:3bdadf6f6dbd | 230 | enemy.move_left(lcd, SPEED*x+71, GROUND); |
ozy | 1:3bdadf6f6dbd | 231 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 232 | wait(0.1); |
ozy | 1:3bdadf6f6dbd | 233 | enemy.move_left2(lcd, SPEED*x+71, 34); |
ozy | 1:3bdadf6f6dbd | 234 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 235 | wait(0.08); |
ozy | 1:3bdadf6f6dbd | 236 | } |
ozy | 3:1d99b6ad4f9e | 237 | |
ozy | 3:1d99b6ad4f9e | 238 | // kick if user presses button A |
ozy | 1:3bdadf6f6dbd | 239 | while (buttonA.read() == 1) { |
ozy | 3:1d99b6ad4f9e | 240 | enemy.kick_left(lcd, SPEED*x+71, GROUND); |
ozy | 1:3bdadf6f6dbd | 241 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 242 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 243 | } |
ozy | 1:3bdadf6f6dbd | 244 | while (buttonB.read() == 1) { |
ozy | 1:3bdadf6f6dbd | 245 | enemy.sword_left(lcd, SPEED*x+71, GROUND); // draw kick on same coordinates as the sprite |
ozy | 1:3bdadf6f6dbd | 246 | lcd.refresh(); |
ozy | 1:3bdadf6f6dbd | 247 | wait(0.2); |
ozy | 1:3bdadf6f6dbd | 248 | } |
ozy | 0:99b49fd71085 | 249 | } |
ozy | 0:99b49fd71085 | 250 | |
ozy | 2:1703eb2a68f8 | 251 | |
ozy | 2:1703eb2a68f8 | 252 |