Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
main.cpp@0:99b49fd71085, 2021-04-13 (annotated)
- Committer:
- ozy
- Date:
- Tue Apr 13 10:06:02 2021 +0000
- Revision:
- 0:99b49fd71085
- Child:
- 1:3bdadf6f6dbd
Sprite moving and Debugging menu class
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 | 0:99b49fd71085 | 7 | ///////////// defines ///////////////////// |
ozy | 0:99b49fd71085 | 8 | #define SPEED -69 |
ozy | 0:99b49fd71085 | 9 | #define GROUND 34 |
ozy | 0:99b49fd71085 | 10 | ///////////// objects /////////////////// |
ozy | 0:99b49fd71085 | 11 | Fighter fighter; |
ozy | 0:99b49fd71085 | 12 | Menu menu; |
ozy | 0:99b49fd71085 | 13 | DigitalIn buttonA(p29); |
ozy | 0:99b49fd71085 | 14 | DigitalIn buttonB(p28); |
ozy | 0:99b49fd71085 | 15 | DigitalIn buttonC(p27); |
ozy | 0:99b49fd71085 | 16 | DigitalIn buttonD(p26); |
ozy | 0:99b49fd71085 | 17 | AnalogIn joy_v(p20); |
ozy | 0:99b49fd71085 | 18 | AnalogIn joy_h(p19); |
ozy | 0:99b49fd71085 | 19 | N5110 lcd(p14,p8,p9,p10,p11,p13,p21); |
ozy | 0:99b49fd71085 | 20 | BusOut leds(LED4,LED3,LED2,LED1); |
ozy | 0:99b49fd71085 | 21 | Serial pc(USBTX, USBRX); |
ozy | 0:99b49fd71085 | 22 | //////////// functions //////////////////// |
ozy | 0:99b49fd71085 | 23 | void init(); |
ozy | 0:99b49fd71085 | 24 | void menu_screen(); |
ozy | 0:99b49fd71085 | 25 | void move_sprite(); |
ozy | 0:99b49fd71085 | 26 | void draw_background(); |
ozy | 0:99b49fd71085 | 27 | void menu_select(); |
ozy | 0:99b49fd71085 | 28 | char get_user_input(); |
ozy | 0:99b49fd71085 | 29 | |
ozy | 0:99b49fd71085 | 30 | |
ozy | 0:99b49fd71085 | 31 | int main() { |
ozy | 0:99b49fd71085 | 32 | init(); |
ozy | 0:99b49fd71085 | 33 | // menu_screen(); |
ozy | 0:99b49fd71085 | 34 | while(1) { |
ozy | 0:99b49fd71085 | 35 | lcd.clear(); |
ozy | 0:99b49fd71085 | 36 | draw_background(); |
ozy | 0:99b49fd71085 | 37 | move_sprite(); |
ozy | 0:99b49fd71085 | 38 | } |
ozy | 0:99b49fd71085 | 39 | } |
ozy | 0:99b49fd71085 | 40 | |
ozy | 0:99b49fd71085 | 41 | void init() { // initialize all devices |
ozy | 0:99b49fd71085 | 42 | lcd.init(); |
ozy | 0:99b49fd71085 | 43 | lcd.setContrast(0.5); |
ozy | 0:99b49fd71085 | 44 | // since Button A has an external pull-down, we should disable to internal pull-down |
ozy | 0:99b49fd71085 | 45 | // resistor that is enabled by default |
ozy | 0:99b49fd71085 | 46 | // buttonA.mode(PullNone); |
ozy | 0:99b49fd71085 | 47 | // buttonB.mode(PullNone); |
ozy | 0:99b49fd71085 | 48 | // buttonC.mode(PullNone); |
ozy | 0:99b49fd71085 | 49 | // buttonD.mode(PullNone); |
ozy | 0:99b49fd71085 | 50 | } |
ozy | 0:99b49fd71085 | 51 | |
ozy | 0:99b49fd71085 | 52 | void menu_screen() { |
ozy | 0:99b49fd71085 | 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 | 0:99b49fd71085 | 57 | menu.A_to_start(lcd); // 3rd menu screen asking user to press A to start game |
ozy | 0:99b49fd71085 | 58 | wait(3.5f); |
ozy | 0:99b49fd71085 | 59 | |
ozy | 0:99b49fd71085 | 60 | while (1) { |
ozy | 0:99b49fd71085 | 61 | if (buttonA.read() == 1) { |
ozy | 0:99b49fd71085 | 62 | menu.homescreen(lcd); |
ozy | 0:99b49fd71085 | 63 | menu_select(); |
ozy | 0:99b49fd71085 | 64 | } |
ozy | 0:99b49fd71085 | 65 | } |
ozy | 0:99b49fd71085 | 66 | } |
ozy | 0:99b49fd71085 | 67 | |
ozy | 0:99b49fd71085 | 68 | |
ozy | 0:99b49fd71085 | 69 | void draw_background() { |
ozy | 0:99b49fd71085 | 70 | // lcd.drawLine(x1, y1, x2, y2, type); |
ozy | 0:99b49fd71085 | 71 | lcd.drawLine(0,46,82,46,1); // draws ground platform |
ozy | 0:99b49fd71085 | 72 | lcd.drawLine(0,46,0,25,1); // draws first side wall |
ozy | 0:99b49fd71085 | 73 | lcd.drawLine(82,46,82,25,1); // draws second side wall |
ozy | 0:99b49fd71085 | 74 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 75 | } |
ozy | 0:99b49fd71085 | 76 | |
ozy | 0:99b49fd71085 | 77 | |
ozy | 0:99b49fd71085 | 78 | void move_sprite() { |
ozy | 0:99b49fd71085 | 79 | // getting joystick coordinates using read() function |
ozy | 0:99b49fd71085 | 80 | // 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 | 81 | |
ozy | 0:99b49fd71085 | 82 | // read each of the pins |
ozy | 0:99b49fd71085 | 83 | float x = joy_h.read(); |
ozy | 0:99b49fd71085 | 84 | float y = joy_v.read(); |
ozy | 0:99b49fd71085 | 85 | printf("x = %.2f | y = %.2f \n",x,y); |
ozy | 0:99b49fd71085 | 86 | /* |
ozy | 0:99b49fd71085 | 87 | Code idea: |
ozy | 0:99b49fd71085 | 88 | if joystick is not moved , display standing sprite |
ozy | 0:99b49fd71085 | 89 | if joystick is moved right, toggle between runright and midrunright |
ozy | 0:99b49fd71085 | 90 | if joystick is moved left, toggle between runleft and midrunleft |
ozy | 0:99b49fd71085 | 91 | */ |
ozy | 0:99b49fd71085 | 92 | if(x > 0.45 && x < 0.55) { // joystick not moved - we use ± 0.05 to take account of fluctuation in joystick tolerance and noice on ADC |
ozy | 0:99b49fd71085 | 93 | fighter.draw(lcd, x+33, GROUND); // draw standing fighter and add 30 print it on middle of lcd screen |
ozy | 0:99b49fd71085 | 94 | // ... and the y-coordinate to place sprite exactly on ground is 34 ( 46 - 12(height of sprite) = 34 ) |
ozy | 0:99b49fd71085 | 95 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 96 | wait(0.2); |
ozy | 0:99b49fd71085 | 97 | |
ozy | 0:99b49fd71085 | 98 | // Preform kick move if user presses button A |
ozy | 0:99b49fd71085 | 99 | while (buttonA.read() == 1) { |
ozy | 0:99b49fd71085 | 100 | fighter.kick_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite |
ozy | 0:99b49fd71085 | 101 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 102 | wait(0.2); |
ozy | 0:99b49fd71085 | 103 | } |
ozy | 0:99b49fd71085 | 104 | // Preform punch move if user presses button B |
ozy | 0:99b49fd71085 | 105 | while (buttonB.read() == 1) { |
ozy | 0:99b49fd71085 | 106 | fighter.punch_right(lcd, x+33, GROUND); // draw kick on same coordinates as the standing sprite |
ozy | 0:99b49fd71085 | 107 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 108 | wait(0.2); |
ozy | 0:99b49fd71085 | 109 | } |
ozy | 0:99b49fd71085 | 110 | // Guard if user presses button C |
ozy | 0:99b49fd71085 | 111 | while (buttonC.read() == 1) { |
ozy | 0:99b49fd71085 | 112 | fighter.guard(lcd, x+33, GROUND); // draw guard move on same coordinates as the standing sprite |
ozy | 0:99b49fd71085 | 113 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 114 | wait(0.2); |
ozy | 0:99b49fd71085 | 115 | } |
ozy | 0:99b49fd71085 | 116 | // Jump if user presses button D |
ozy | 0:99b49fd71085 | 117 | while (buttonD.read() == 1) { |
ozy | 0:99b49fd71085 | 118 | fighter.draw(lcd, x+33, GROUND-15); // adding 15 the sprite y-coordinate to create jump move |
ozy | 0:99b49fd71085 | 119 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 120 | wait(0.2); |
ozy | 0:99b49fd71085 | 121 | } |
ozy | 0:99b49fd71085 | 122 | } |
ozy | 0:99b49fd71085 | 123 | else if(x < 0.45) { // joystick moved to the right |
ozy | 0:99b49fd71085 | 124 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 0:99b49fd71085 | 125 | fighter.move_right(lcd, SPEED*x+70, GROUND); // draw standing fighter, multiply by speed -69 (negative to correct for direction!) |
ozy | 0:99b49fd71085 | 126 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 127 | wait(0.1); |
ozy | 0:99b49fd71085 | 128 | fighter.move_right2(lcd, SPEED*x+70, GROUND); |
ozy | 0:99b49fd71085 | 129 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 130 | wait(0.08); |
ozy | 0:99b49fd71085 | 131 | |
ozy | 0:99b49fd71085 | 132 | // kick if user presses button A |
ozy | 0:99b49fd71085 | 133 | while (buttonA.read() == 1) { |
ozy | 0:99b49fd71085 | 134 | fighter.kick_right(lcd, SPEED*x+70, GROUND); |
ozy | 0:99b49fd71085 | 135 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 136 | wait(0.2); |
ozy | 0:99b49fd71085 | 137 | } |
ozy | 0:99b49fd71085 | 138 | // Preform punch move if user presses button B |
ozy | 0:99b49fd71085 | 139 | while (buttonB.read() == 1) { |
ozy | 0:99b49fd71085 | 140 | fighter.punch_right(lcd, SPEED*x+70, GROUND); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 141 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 142 | wait(0.2); |
ozy | 0:99b49fd71085 | 143 | } |
ozy | 0:99b49fd71085 | 144 | // Guard if user presses button C |
ozy | 0:99b49fd71085 | 145 | while (buttonC.read() == 1) { |
ozy | 0:99b49fd71085 | 146 | fighter.guard(lcd, SPEED*x+70, GROUND); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 147 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 148 | wait(0.2); |
ozy | 0:99b49fd71085 | 149 | } |
ozy | 0:99b49fd71085 | 150 | } |
ozy | 0:99b49fd71085 | 151 | else if(x > 0.55) { // joystick moved to the left |
ozy | 0:99b49fd71085 | 152 | // printing only the first move left animation for comparison |
ozy | 0:99b49fd71085 | 153 | fighter.move_left(lcd, SPEED*x+71, GROUND); |
ozy | 0:99b49fd71085 | 154 | //wait(0.01); |
ozy | 0:99b49fd71085 | 155 | //fighter.move_left2(lcd, -25*x+30, 34); |
ozy | 0:99b49fd71085 | 156 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 157 | wait(0.08); |
ozy | 0:99b49fd71085 | 158 | } |
ozy | 0:99b49fd71085 | 159 | |
ozy | 0:99b49fd71085 | 160 | // kick if user presses button A |
ozy | 0:99b49fd71085 | 161 | while (buttonA.read() == 1) { |
ozy | 0:99b49fd71085 | 162 | fighter.kick_left(lcd, SPEED*x+71, GROUND); |
ozy | 0:99b49fd71085 | 163 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 164 | wait(0.2); |
ozy | 0:99b49fd71085 | 165 | } |
ozy | 0:99b49fd71085 | 166 | while (buttonB.read() == 1) { |
ozy | 0:99b49fd71085 | 167 | fighter.punch_left(lcd, SPEED*x+71, GROUND); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 168 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 169 | wait(0.2); |
ozy | 0:99b49fd71085 | 170 | } |
ozy | 0:99b49fd71085 | 171 | // Guard if user presses button C |
ozy | 0:99b49fd71085 | 172 | while (buttonC.read() == 1) { |
ozy | 0:99b49fd71085 | 173 | fighter.guard(lcd, SPEED*x+71, GROUND); // draw kick on same coordinates as the sprite |
ozy | 0:99b49fd71085 | 174 | lcd.refresh(); |
ozy | 0:99b49fd71085 | 175 | wait(0.2); |
ozy | 0:99b49fd71085 | 176 | } |
ozy | 0:99b49fd71085 | 177 | } |
ozy | 0:99b49fd71085 | 178 | |
ozy | 0:99b49fd71085 | 179 | void menu_select() { |
ozy | 0:99b49fd71085 | 180 | char button = get_user_input(); |
ozy | 0:99b49fd71085 | 181 | switch (button) { |
ozy | 0:99b49fd71085 | 182 | case 'A': |
ozy | 0:99b49fd71085 | 183 | menu.play(lcd); |
ozy | 0:99b49fd71085 | 184 | break; |
ozy | 0:99b49fd71085 | 185 | case 'B': |
ozy | 0:99b49fd71085 | 186 | menu.tutorial(lcd); |
ozy | 0:99b49fd71085 | 187 | break; |
ozy | 0:99b49fd71085 | 188 | case 'C': |
ozy | 0:99b49fd71085 | 189 | menu.options_menu(lcd); |
ozy | 0:99b49fd71085 | 190 | break; |
ozy | 0:99b49fd71085 | 191 | default: |
ozy | 0:99b49fd71085 | 192 | menu.homescreen(lcd); |
ozy | 0:99b49fd71085 | 193 | break; |
ozy | 0:99b49fd71085 | 194 | } |
ozy | 0:99b49fd71085 | 195 | } |
ozy | 0:99b49fd71085 | 196 | |
ozy | 0:99b49fd71085 | 197 | char get_user_input() { |
ozy | 0:99b49fd71085 | 198 | char button; |
ozy | 0:99b49fd71085 | 199 | while (buttonA.read() == 1) {button = 'A';} |
ozy | 0:99b49fd71085 | 200 | while (buttonB.read() == 1) {button = 'B';} |
ozy | 0:99b49fd71085 | 201 | while (buttonC.read() == 1) {button = 'C';} |
ozy | 0:99b49fd71085 | 202 | while (buttonD.read() == 1) {button = 'D';} |
ozy | 0:99b49fd71085 | 203 | pc.printf("Value is %c", button); |
ozy | 0:99b49fd71085 | 204 | return button; |
ozy | 0:99b49fd71085 | 205 | } |