Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
Fighter.cpp@15:7fd2d34f3be5, 2021-04-26 (annotated)
- Committer:
- ozy
- Date:
- Mon Apr 26 23:58:35 2021 +0000
- Revision:
- 15:7fd2d34f3be5
- Parent:
- 11:b344a64b75bf
- Child:
- 17:928edcdd1058
Code after jump move D
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ozy | 0:99b49fd71085 | 1 | #include "Fighter.h" |
ozy | 0:99b49fd71085 | 2 | #include "mbed.h" |
ozy | 9:8c840b08a633 | 3 | #include <stdlib.h> |
ozy | 0:99b49fd71085 | 4 | |
ozy | 0:99b49fd71085 | 5 | Fighter::Fighter() {} |
ozy | 0:99b49fd71085 | 6 | |
ozy | 3:1d99b6ad4f9e | 7 | int Fighter::get_x() { |
ozy | 3:1d99b6ad4f9e | 8 | return _x; |
ozy | 3:1d99b6ad4f9e | 9 | } |
ozy | 3:1d99b6ad4f9e | 10 | |
ozy | 3:1d99b6ad4f9e | 11 | int Fighter::get_y() { |
ozy | 3:1d99b6ad4f9e | 12 | return _y; |
ozy | 3:1d99b6ad4f9e | 13 | } |
ozy | 3:1d99b6ad4f9e | 14 | |
ozy | 7:737fb0c3dbef | 15 | void Fighter::init() { |
ozy | 7:737fb0c3dbef | 16 | // initialize fighter position |
ozy | 7:737fb0c3dbef | 17 | set_x(15); |
ozy | 10:e83899f11e8a | 18 | set_y(34); |
ozy | 7:737fb0c3dbef | 19 | } |
ozy | 7:737fb0c3dbef | 20 | |
ozy | 3:1d99b6ad4f9e | 21 | void Fighter::set_x(int x) { |
ozy | 3:1d99b6ad4f9e | 22 | _x = x; |
ozy | 3:1d99b6ad4f9e | 23 | } |
ozy | 3:1d99b6ad4f9e | 24 | |
ozy | 3:1d99b6ad4f9e | 25 | void Fighter::set_y(int y) { |
ozy | 3:1d99b6ad4f9e | 26 | _y = y; |
ozy | 3:1d99b6ad4f9e | 27 | } |
ozy | 3:1d99b6ad4f9e | 28 | |
ozy | 7:737fb0c3dbef | 29 | void Fighter::add_x(int x) { |
ozy | 3:1d99b6ad4f9e | 30 | _x += x; |
ozy | 3:1d99b6ad4f9e | 31 | } |
ozy | 3:1d99b6ad4f9e | 32 | |
ozy | 7:737fb0c3dbef | 33 | void Fighter::add_y(int y) { |
ozy | 3:1d99b6ad4f9e | 34 | _y += y; |
ozy | 3:1d99b6ad4f9e | 35 | } |
ozy | 3:1d99b6ad4f9e | 36 | |
ozy | 9:8c840b08a633 | 37 | |
ozy | 7:737fb0c3dbef | 38 | void Fighter::draw(N5110 &lcd) { // drawing standing Fighter |
ozy | 0:99b49fd71085 | 39 | |
ozy | 0:99b49fd71085 | 40 | const int standsprite[12][10] = { |
ozy | 0:99b49fd71085 | 41 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 42 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 43 | { 0,0,1,1,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 44 | { 0,0,1,0,1,1,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 45 | { 0,1,1,0,1,1,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 46 | { 0,1,0,0,1,1,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 47 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 48 | { 0,0,1,1,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 49 | { 0,0,1,0,0,0,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 50 | { 0,1,1,0,0,0,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 51 | { 0,1,0,0,0,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 52 | { 0,1,0,0,0,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 53 | }; |
ozy | 7:737fb0c3dbef | 54 | |
ozy | 7:737fb0c3dbef | 55 | lcd.drawSprite(_x,_y,12,10,(int *)standsprite); |
ozy | 7:737fb0c3dbef | 56 | } |
ozy | 7:737fb0c3dbef | 57 | |
ozy | 7:737fb0c3dbef | 58 | // ************************************************************************ |
ozy | 7:737fb0c3dbef | 59 | |
ozy | 7:737fb0c3dbef | 60 | void Fighter::move_left(N5110 &lcd){ |
ozy | 0:99b49fd71085 | 61 | const int run_left[12][10] = { |
ozy | 0:99b49fd71085 | 62 | { 0,0,1,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 63 | { 0,0,1,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 64 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 65 | { 0,0,1,0,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 66 | { 1,1,1,0,1,1,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 67 | { 0,0,0,0,1,1,0,0,0,1 }, |
ozy | 0:99b49fd71085 | 68 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 69 | { 0,0,1,1,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 70 | { 0,1,1,0,0,0,1,1,1,1 }, |
ozy | 0:99b49fd71085 | 71 | { 0,0,1,1,0,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 72 | { 0,0,0,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 73 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 74 | }; |
ozy | 7:737fb0c3dbef | 75 | |
ozy | 7:737fb0c3dbef | 76 | const int midrun_left[12][10] = { |
ozy | 0:99b49fd71085 | 77 | { 0,0,1,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 78 | { 0,0,1,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 79 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 80 | { 0,0,1,0,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 81 | { 1,1,1,0,1,1,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 82 | { 0,0,0,0,1,1,0,0,0,1 }, |
ozy | 0:99b49fd71085 | 83 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 84 | { 0,0,1,1,0,0,1,1,0,0 }, |
ozy | 1:3bdadf6f6dbd | 85 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 1:3bdadf6f6dbd | 86 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 1:3bdadf6f6dbd | 87 | { 0,0,0,0,1,0,1,0,0,0 }, |
ozy | 1:3bdadf6f6dbd | 88 | { 0,0,0,1,0,0,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 89 | }; |
ozy | 10:e83899f11e8a | 90 | |
ozy | 10:e83899f11e8a | 91 | // code to toggle between both move left animations (show some leg movement!) |
ozy | 10:e83899f11e8a | 92 | int x = rand() % 2; |
ozy | 10:e83899f11e8a | 93 | if (x == 0) { |
ozy | 10:e83899f11e8a | 94 | x = 1; |
ozy | 10:e83899f11e8a | 95 | lcd.drawSprite(_x,_y,12,10,(int *)run_left); |
ozy | 10:e83899f11e8a | 96 | } |
ozy | 10:e83899f11e8a | 97 | else if(x == 1) { |
ozy | 10:e83899f11e8a | 98 | x = 0; |
ozy | 10:e83899f11e8a | 99 | lcd.drawSprite(_x,_y,12,10,(int *)midrun_left); |
ozy | 10:e83899f11e8a | 100 | } |
ozy | 10:e83899f11e8a | 101 | |
ozy | 7:737fb0c3dbef | 102 | } |
ozy | 7:737fb0c3dbef | 103 | |
ozy | 10:e83899f11e8a | 104 | |
ozy | 7:737fb0c3dbef | 105 | // ************************************************************************ |
ozy | 7:737fb0c3dbef | 106 | |
ozy | 7:737fb0c3dbef | 107 | void Fighter::move_right(N5110 &lcd) { |
ozy | 7:737fb0c3dbef | 108 | |
ozy | 0:99b49fd71085 | 109 | const int run_right[12][10] = { |
ozy | 0:99b49fd71085 | 110 | { 0,0,0,0,0,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 111 | { 0,0,0,0,0,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 112 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 113 | { 0,0,1,1,1,1,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 114 | { 0,1,1,0,1,1,0,1,1,1 }, |
ozy | 0:99b49fd71085 | 115 | { 1,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 116 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 117 | { 0,0,1,1,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 118 | { 1,1,1,1,0,0,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 119 | { 0,0,0,0,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 120 | { 0,0,0,0,0,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 121 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 122 | }; |
ozy | 0:99b49fd71085 | 123 | |
ozy | 7:737fb0c3dbef | 124 | const int midrun_right[12][10] = { |
ozy | 0:99b49fd71085 | 125 | { 0,0,0,0,0,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 126 | { 0,0,0,0,0,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 127 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 128 | { 0,0,1,1,1,1,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 129 | { 0,1,1,0,1,1,0,1,1,1 }, |
ozy | 0:99b49fd71085 | 130 | { 1,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 131 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 132 | { 0,0,1,1,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 133 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 134 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 1:3bdadf6f6dbd | 135 | { 0,0,0,1,0,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 136 | { 0,0,1,0,0,0,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 137 | }; |
ozy | 10:e83899f11e8a | 138 | |
ozy | 10:e83899f11e8a | 139 | // code to toggle between both move right animations |
ozy | 10:e83899f11e8a | 140 | int x = rand() % 2; |
ozy | 10:e83899f11e8a | 141 | if (x == 0) { |
ozy | 10:e83899f11e8a | 142 | x = 1; |
ozy | 10:e83899f11e8a | 143 | lcd.drawSprite(_x,_y,12,10,(int *)run_right); |
ozy | 10:e83899f11e8a | 144 | } |
ozy | 10:e83899f11e8a | 145 | else if(x == 1) { |
ozy | 10:e83899f11e8a | 146 | x = 0; |
ozy | 10:e83899f11e8a | 147 | lcd.drawSprite(_x,_y,12,10,(int *)midrun_right); |
ozy | 10:e83899f11e8a | 148 | } |
ozy | 10:e83899f11e8a | 149 | |
ozy | 7:737fb0c3dbef | 150 | } |
ozy | 7:737fb0c3dbef | 151 | |
ozy | 7:737fb0c3dbef | 152 | // ************************************************************************ |
ozy | 7:737fb0c3dbef | 153 | |
ozy | 7:737fb0c3dbef | 154 | void Fighter::kick_right(N5110 &lcd) { |
ozy | 7:737fb0c3dbef | 155 | const int kick[12][10] = { |
ozy | 0:99b49fd71085 | 156 | { 0,1,1,0,0,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 157 | { 0,1,1,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 158 | { 0,0,0,1,1,1,1,0,0,1 }, |
ozy | 0:99b49fd71085 | 159 | { 0,0,1,1,1,0,0,0,1,1 }, |
ozy | 0:99b49fd71085 | 160 | { 0,1,1,1,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 161 | { 0,1,0,0,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 162 | { 1,1,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 163 | { 0,0,0,0,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 164 | { 0,0,0,1,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 165 | { 0,0,0,1,0,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 166 | { 0,0,1,1,0,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 167 | { 0,0,1,1,0,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 168 | }; |
ozy | 7:737fb0c3dbef | 169 | lcd.drawSprite(_x,_y,12,10,(int *)kick); |
ozy | 7:737fb0c3dbef | 170 | } |
ozy | 7:737fb0c3dbef | 171 | |
ozy | 7:737fb0c3dbef | 172 | // ************************************************************************ |
ozy | 7:737fb0c3dbef | 173 | |
ozy | 7:737fb0c3dbef | 174 | void Fighter::kick_left(N5110 &lcd) { |
ozy | 7:737fb0c3dbef | 175 | const int kick[12][10] = { |
ozy | 0:99b49fd71085 | 176 | { 0,0,0,0,0,0,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 177 | { 0,0,0,0,1,1,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 178 | { 1,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 179 | { 1,1,0,0,0,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 180 | { 0,0,1,1,1,1,1,1,1,0 }, |
ozy | 0:99b49fd71085 | 181 | { 0,0,0,1,1,1,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 182 | { 0,0,0,0,1,1,0,0,1,1 }, |
ozy | 0:99b49fd71085 | 183 | { 0,0,0,0,0,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 184 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 185 | { 0,0,0,0,1,0,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 186 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 187 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 188 | }; |
ozy | 7:737fb0c3dbef | 189 | lcd.drawSprite(_x,_y,12,10,(int *)kick); |
ozy | 7:737fb0c3dbef | 190 | } |
ozy | 7:737fb0c3dbef | 191 | |
ozy | 7:737fb0c3dbef | 192 | // ************************************************************************ |
ozy | 7:737fb0c3dbef | 193 | |
ozy | 7:737fb0c3dbef | 194 | void Fighter::punch_right(N5110 &lcd) { |
ozy | 7:737fb0c3dbef | 195 | const int punch[12][10] = { |
ozy | 0:99b49fd71085 | 196 | { 0,0,0,0,1,1,0,0,0,1 }, |
ozy | 0:99b49fd71085 | 197 | { 0,0,0,0,1,1,0,0,1,1 }, |
ozy | 0:99b49fd71085 | 198 | { 0,0,1,1,1,1,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 199 | { 0,1,0,0,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 200 | { 0,1,1,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 201 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 202 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 203 | { 0,0,0,1,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 204 | { 0,0,1,1,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 205 | { 0,0,1,0,0,0,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 206 | { 0,1,1,0,0,0,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 207 | { 1,1,0,0,0,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 208 | }; |
ozy | 7:737fb0c3dbef | 209 | lcd.drawSprite(_x,_y,12,10,(int *)punch); |
ozy | 7:737fb0c3dbef | 210 | } |
ozy | 7:737fb0c3dbef | 211 | |
ozy | 7:737fb0c3dbef | 212 | // ************************************************************************ |
ozy | 7:737fb0c3dbef | 213 | |
ozy | 7:737fb0c3dbef | 214 | void Fighter::punch_left(N5110 &lcd) { |
ozy | 7:737fb0c3dbef | 215 | const int punch[12][10] = { |
ozy | 0:99b49fd71085 | 216 | { 1,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 217 | { 1,1,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 218 | { 0,1,1,0,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 219 | { 0,0,1,1,1,1,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 220 | { 0,0,0,0,1,1,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 221 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 222 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 223 | { 0,0,1,1,0,0,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 224 | { 0,0,1,1,0,0,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 225 | { 0,0,0,1,0,0,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 226 | { 0,0,0,1,0,0,0,1,1,0 }, |
ozy | 0:99b49fd71085 | 227 | { 0,0,0,1,1,0,0,0,1,1 }, |
ozy | 0:99b49fd71085 | 228 | }; |
ozy | 7:737fb0c3dbef | 229 | lcd.drawSprite(_x,_y,12,10,(int *)punch); |
ozy | 7:737fb0c3dbef | 230 | } |
ozy | 7:737fb0c3dbef | 231 | |
ozy | 7:737fb0c3dbef | 232 | // ************************************************************************ |
ozy | 7:737fb0c3dbef | 233 | |
ozy | 7:737fb0c3dbef | 234 | void Fighter::guard(N5110 &lcd) { |
ozy | 0:99b49fd71085 | 235 | const int guard[12][10] = { |
ozy | 0:99b49fd71085 | 236 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 237 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 238 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 239 | { 0,0,1,0,1,1,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 240 | { 0,0,1,0,1,1,0,1,0,0 }, |
ozy | 0:99b49fd71085 | 241 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 242 | { 0,0,0,0,1,1,0,0,0,0 }, |
ozy | 0:99b49fd71085 | 243 | { 0,0,0,1,1,1,1,0,0,0 }, |
ozy | 0:99b49fd71085 | 244 | { 0,0,1,1,1,1,1,1,0,0 }, |
ozy | 0:99b49fd71085 | 245 | { 0,1,0,0,0,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 246 | { 0,1,0,0,0,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 247 | { 0,1,0,0,0,0,0,0,1,0 }, |
ozy | 0:99b49fd71085 | 248 | }; |
ozy | 7:737fb0c3dbef | 249 | lcd.drawSprite(_x,_y,12,10,(int *)guard); |
ozy | 7:737fb0c3dbef | 250 | } |
ozy | 6:a1a7dc264fed | 251 | |
ozy | 7:737fb0c3dbef | 252 | void Fighter::move_fighter(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) { |
ozy | 6:a1a7dc264fed | 253 | // getting joystick coordinates using read() function |
ozy | 6:a1a7dc264fed | 254 | // joystick centered at (0.50,0.50) with utmost left at (1.00,0.50) and utmost right being (0.00,0.50) |
ozy | 6:a1a7dc264fed | 255 | // read each of the pins |
ozy | 7:737fb0c3dbef | 256 | float x = joy_h.read(); |
ozy | 7:737fb0c3dbef | 257 | float y = joy_v.read(); |
ozy | 8:e2e2eb4ea0ca | 258 | // printf("x = %.2f | y = %.2f \n",x,y); |
ozy | 7:737fb0c3dbef | 259 | |
ozy | 7:737fb0c3dbef | 260 | int x_pos = get_x(); |
ozy | 8:e2e2eb4ea0ca | 261 | // printf("fighter x pos = %i \n", x_pos); |
ozy | 7:737fb0c3dbef | 262 | |
ozy | 6:a1a7dc264fed | 263 | /* |
ozy | 6:a1a7dc264fed | 264 | Code idea: |
ozy | 6:a1a7dc264fed | 265 | if joystick is not moved , display standing sprite |
ozy | 6:a1a7dc264fed | 266 | if joystick is moved right, toggle between runright and midrunright |
ozy | 6:a1a7dc264fed | 267 | if joystick is moved left, toggle between runleft and midrunleft |
ozy | 6:a1a7dc264fed | 268 | */ |
ozy | 7:737fb0c3dbef | 269 | 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 | 7:737fb0c3dbef | 270 | draw(lcd); // draw standing fighter |
ozy | 6:a1a7dc264fed | 271 | |
ozy | 6:a1a7dc264fed | 272 | // Preform kick move if user presses button A |
ozy | 6:a1a7dc264fed | 273 | if (buttonA.read() == 1) { |
ozy | 7:737fb0c3dbef | 274 | kick_right(lcd); // draw kick on same coordinates as the standing sprite |
ozy | 6:a1a7dc264fed | 275 | } |
ozy | 6:a1a7dc264fed | 276 | // Preform punch move if user presses button B |
ozy | 6:a1a7dc264fed | 277 | if (buttonB.read() == 1) { |
ozy | 7:737fb0c3dbef | 278 | punch_right(lcd); // draw kick |
ozy | 6:a1a7dc264fed | 279 | } |
ozy | 6:a1a7dc264fed | 280 | // Guard if user presses button C |
ozy | 8:e2e2eb4ea0ca | 281 | if (buttonC.read() == 1) { |
ozy | 7:737fb0c3dbef | 282 | guard(lcd); // draw guard move frame |
ozy | 6:a1a7dc264fed | 283 | } |
ozy | 15:7fd2d34f3be5 | 284 | if (buttonD.read() == 0) { // code to return fighter to ground when user uses the jump button whilst moving right/left |
ozy | 15:7fd2d34f3be5 | 285 | int y_pos = get_y(); |
ozy | 15:7fd2d34f3be5 | 286 | if (y_pos != 34) { |
ozy | 15:7fd2d34f3be5 | 287 | set_y(34); |
ozy | 15:7fd2d34f3be5 | 288 | } |
ozy | 15:7fd2d34f3be5 | 289 | } |
ozy | 7:737fb0c3dbef | 290 | } |
ozy | 7:737fb0c3dbef | 291 | else if(x < 0.48) { // joystick moved to the right |
ozy | 6:a1a7dc264fed | 292 | // print the move_right animation, refresh, then print the 2nd move_right animation (toggling animations to create moving legs!) |
ozy | 10:e83899f11e8a | 293 | add_x(4); // increment by 5 |
ozy | 7:737fb0c3dbef | 294 | move_right(lcd); |
ozy | 7:737fb0c3dbef | 295 | |
ozy | 7:737fb0c3dbef | 296 | if (x_pos >= 65) { // code to stop fighter moving out of lcd screen |
ozy | 7:737fb0c3dbef | 297 | add_x(-5); |
ozy | 6:a1a7dc264fed | 298 | } |
ozy | 7:737fb0c3dbef | 299 | |
ozy | 7:737fb0c3dbef | 300 | |
ozy | 6:a1a7dc264fed | 301 | // kick if user presses button A |
ozy | 6:a1a7dc264fed | 302 | if (buttonA.read() == 1) { |
ozy | 7:737fb0c3dbef | 303 | kick_right(lcd); |
ozy | 7:737fb0c3dbef | 304 | } |
ozy | 6:a1a7dc264fed | 305 | // Preform punch move if user presses button B |
ozy | 6:a1a7dc264fed | 306 | if (buttonB.read() == 1) { |
ozy | 7:737fb0c3dbef | 307 | punch_right(lcd); // draw kick on same coordinates as the sprite |
ozy | 10:e83899f11e8a | 308 | } |
ozy | 6:a1a7dc264fed | 309 | // Guard if user presses button C |
ozy | 8:e2e2eb4ea0ca | 310 | if (buttonC.read() == 1) { // we use while not if here because user would hold to guard |
ozy | 7:737fb0c3dbef | 311 | guard(lcd); // draw kick on same coordinates as the sprite |
ozy | 6:a1a7dc264fed | 312 | } |
ozy | 15:7fd2d34f3be5 | 313 | if (buttonD.read() == 1) { // computing jump move |
ozy | 15:7fd2d34f3be5 | 314 | move_right(lcd); |
ozy | 15:7fd2d34f3be5 | 315 | add_y(-12); // add 12 to y position |
ozy | 15:7fd2d34f3be5 | 316 | add_x(5); |
ozy | 15:7fd2d34f3be5 | 317 | int y_pos = get_y(); |
ozy | 15:7fd2d34f3be5 | 318 | if (y_pos < 22) { // to avoid sprite jumping more than 12 everytime they click D |
ozy | 15:7fd2d34f3be5 | 319 | set_y(22); |
ozy | 15:7fd2d34f3be5 | 320 | } |
ozy | 15:7fd2d34f3be5 | 321 | if (x_pos >= 65) { // code to stop fighter moving out of lcd screen while jumping! |
ozy | 15:7fd2d34f3be5 | 322 | add_x(-5); |
ozy | 15:7fd2d34f3be5 | 323 | } |
ozy | 15:7fd2d34f3be5 | 324 | } |
ozy | 15:7fd2d34f3be5 | 325 | if (buttonD.read() == 0) { // code to return fighter to ground |
ozy | 15:7fd2d34f3be5 | 326 | int y_pos = get_y(); |
ozy | 15:7fd2d34f3be5 | 327 | if (y_pos != 34) { |
ozy | 15:7fd2d34f3be5 | 328 | set_y(34); |
ozy | 15:7fd2d34f3be5 | 329 | } |
ozy | 15:7fd2d34f3be5 | 330 | } |
ozy | 6:a1a7dc264fed | 331 | } |
ozy | 7:737fb0c3dbef | 332 | else if(x > 0.52) { // joystick moved to the left |
ozy | 10:e83899f11e8a | 333 | add_x(-4); // decrement left by 5 |
ozy | 7:737fb0c3dbef | 334 | move_left(lcd); |
ozy | 0:99b49fd71085 | 335 | |
ozy | 10:e83899f11e8a | 336 | if (x_pos <= 8) { // code to stop fighter moving out of lcd screen |
ozy | 7:737fb0c3dbef | 337 | add_x(5); |
ozy | 7:737fb0c3dbef | 338 | } |
ozy | 0:99b49fd71085 | 339 | |
ozy | 7:737fb0c3dbef | 340 | // kick if user presses button A |
ozy | 7:737fb0c3dbef | 341 | if (buttonA.read() == 1) { |
ozy | 7:737fb0c3dbef | 342 | kick_left(lcd); |
ozy | 7:737fb0c3dbef | 343 | } |
ozy | 7:737fb0c3dbef | 344 | if (buttonB.read() == 1) { |
ozy | 7:737fb0c3dbef | 345 | punch_left(lcd); // draw punch on same coordinates as the sprite |
ozy | 7:737fb0c3dbef | 346 | } |
ozy | 7:737fb0c3dbef | 347 | // Guard if user presses button C |
ozy | 8:e2e2eb4ea0ca | 348 | if (buttonC.read() == 1) { |
ozy | 8:e2e2eb4ea0ca | 349 | guard(lcd); |
ozy | 7:737fb0c3dbef | 350 | } |
ozy | 15:7fd2d34f3be5 | 351 | // code to make fighter jump |
ozy | 15:7fd2d34f3be5 | 352 | if (buttonD.read() == 1) { // computing jump move for left movement |
ozy | 15:7fd2d34f3be5 | 353 | move_left(lcd); |
ozy | 15:7fd2d34f3be5 | 354 | add_y(-12); // add 12 to y position |
ozy | 15:7fd2d34f3be5 | 355 | add_x(-5); |
ozy | 15:7fd2d34f3be5 | 356 | int y_pos = get_y(); |
ozy | 15:7fd2d34f3be5 | 357 | if (y_pos < 22) { // to avoid sprite jumping more than 12 everytime they click D |
ozy | 15:7fd2d34f3be5 | 358 | set_y(22); |
ozy | 15:7fd2d34f3be5 | 359 | } |
ozy | 15:7fd2d34f3be5 | 360 | if (x_pos <= 8) { // code to stop fighter moving out of lcd screen while jumping! |
ozy | 15:7fd2d34f3be5 | 361 | add_x(5); |
ozy | 15:7fd2d34f3be5 | 362 | } |
ozy | 15:7fd2d34f3be5 | 363 | } |
ozy | 15:7fd2d34f3be5 | 364 | if (buttonD.read() == 0) { // code to return fighter to ground |
ozy | 15:7fd2d34f3be5 | 365 | int y_pos = get_y(); |
ozy | 15:7fd2d34f3be5 | 366 | if (y_pos != 34) { |
ozy | 15:7fd2d34f3be5 | 367 | set_y(34); |
ozy | 15:7fd2d34f3be5 | 368 | } |
ozy | 15:7fd2d34f3be5 | 369 | } |
ozy | 7:737fb0c3dbef | 370 | } |
ozy | 7:737fb0c3dbef | 371 | } |