![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ECE 4180 @ Georgia Tech
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE MMA8452
main.cpp@10:6f15dcee0a72, 2020-04-30 (annotated)
- Committer:
- snorod
- Date:
- Thu Apr 30 01:01:47 2020 +0000
- Revision:
- 10:6f15dcee0a72
- Parent:
- 8:31e63caf37e2
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:cfcf73272647 | 1 | #include "mbed.h" |
4180_1 | 2:75727e89a717 | 2 | #include "uLCD_4DGL.h" |
snorod | 10:6f15dcee0a72 | 3 | #include "rtos.h" |
snorod | 10:6f15dcee0a72 | 4 | #include "MMA8452.h" |
4180_1 | 0:cfcf73272647 | 5 | |
snorod | 10:6f15dcee0a72 | 6 | #define LEFT_X 45 |
snorod | 10:6f15dcee0a72 | 7 | #define CENTER_X 65 |
snorod | 10:6f15dcee0a72 | 8 | #define RIGHT_X 85 |
snorod | 10:6f15dcee0a72 | 9 | |
snorod | 10:6f15dcee0a72 | 10 | #define JUMP_Y 96 |
snorod | 10:6f15dcee0a72 | 11 | #define CENTER_Y 108 |
snorod | 10:6f15dcee0a72 | 12 | #define DUCK_Y 120 |
snorod | 10:6f15dcee0a72 | 13 | |
snorod | 10:6f15dcee0a72 | 14 | #define RADIUS 4 |
snorod | 10:6f15dcee0a72 | 15 | |
snorod | 10:6f15dcee0a72 | 16 | #define SPEED 5 |
snorod | 10:6f15dcee0a72 | 17 | #define VOLUME 0.1 |
snorod | 10:6f15dcee0a72 | 18 | |
snorod | 10:6f15dcee0a72 | 19 | Serial pc(USBTX,USBRX); // included for debugging accelerometer |
snorod | 10:6f15dcee0a72 | 20 | MMA8452 acc(p28, p27, 100000); |
4180_1 | 2:75727e89a717 | 21 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
snorod | 10:6f15dcee0a72 | 22 | PwmOut jack(p23); |
snorod | 10:6f15dcee0a72 | 23 | DigitalIn jump_button(p22); |
snorod | 10:6f15dcee0a72 | 24 | DigitalIn duck_button(p21); |
snorod | 10:6f15dcee0a72 | 25 | |
snorod | 10:6f15dcee0a72 | 26 | // mutex for writing to the uLCD |
snorod | 10:6f15dcee0a72 | 27 | Mutex LCD; |
snorod | 10:6f15dcee0a72 | 28 | |
snorod | 10:6f15dcee0a72 | 29 | volatile int score; |
snorod | 10:6f15dcee0a72 | 30 | volatile int high_score; |
snorod | 10:6f15dcee0a72 | 31 | volatile int steps; |
snorod | 10:6f15dcee0a72 | 32 | volatile int strikes; |
snorod | 10:6f15dcee0a72 | 33 | double acc_x; |
snorod | 10:6f15dcee0a72 | 34 | volatile int player_x; |
snorod | 10:6f15dcee0a72 | 35 | volatile int player_y; |
snorod | 10:6f15dcee0a72 | 36 | |
snorod | 10:6f15dcee0a72 | 37 | volatile int obstacle_x; |
snorod | 10:6f15dcee0a72 | 38 | volatile int obstacle_y; |
snorod | 10:6f15dcee0a72 | 39 | volatile int dodge_x; |
snorod | 10:6f15dcee0a72 | 40 | |
snorod | 10:6f15dcee0a72 | 41 | int ob_x_roots[] = {61, 65, 69}; |
snorod | 10:6f15dcee0a72 | 42 | const char* strikes_arr[4] = { " ", " X", " XX", "XXX" }; |
snorod | 10:6f15dcee0a72 | 43 | enum ob_type {dodge, duck, jump}; |
snorod | 10:6f15dcee0a72 | 44 | ob_type ob_type_arr[] = {dodge, duck, jump}; |
snorod | 10:6f15dcee0a72 | 45 | |
snorod | 10:6f15dcee0a72 | 46 | volatile ob_type obstacle_type; |
snorod | 10:6f15dcee0a72 | 47 | |
snorod | 10:6f15dcee0a72 | 48 | volatile bool play_leftright; |
snorod | 10:6f15dcee0a72 | 49 | volatile bool play_jump; |
snorod | 10:6f15dcee0a72 | 50 | volatile bool play_duck; |
snorod | 10:6f15dcee0a72 | 51 | volatile bool play_strikes; |
snorod | 10:6f15dcee0a72 | 52 | volatile bool play_dead; |
snorod | 10:6f15dcee0a72 | 53 | |
snorod | 10:6f15dcee0a72 | 54 | void read_sound(void const *argument) { |
snorod | 10:6f15dcee0a72 | 55 | while(1) { |
snorod | 10:6f15dcee0a72 | 56 | |
snorod | 10:6f15dcee0a72 | 57 | // different sounds in order of priority to audio output |
snorod | 10:6f15dcee0a72 | 58 | if (play_dead) { |
snorod | 10:6f15dcee0a72 | 59 | jack.period(1.0/440.0); |
snorod | 10:6f15dcee0a72 | 60 | jack = VOLUME/2.0; |
snorod | 10:6f15dcee0a72 | 61 | Thread::wait(750); |
snorod | 10:6f15dcee0a72 | 62 | jack = 0.0; |
snorod | 10:6f15dcee0a72 | 63 | Thread::wait(100); |
snorod | 10:6f15dcee0a72 | 64 | jack = VOLUME/2.0; |
snorod | 10:6f15dcee0a72 | 65 | Thread::wait(750); |
snorod | 10:6f15dcee0a72 | 66 | jack = 0.0; |
snorod | 10:6f15dcee0a72 | 67 | Thread::wait(100); |
snorod | 10:6f15dcee0a72 | 68 | jack = VOLUME/2.0; |
snorod | 10:6f15dcee0a72 | 69 | Thread::wait(750); |
snorod | 10:6f15dcee0a72 | 70 | jack = 0.0; |
snorod | 10:6f15dcee0a72 | 71 | play_dead = false; |
snorod | 10:6f15dcee0a72 | 72 | play_strikes = false; |
snorod | 10:6f15dcee0a72 | 73 | play_duck = false; |
snorod | 10:6f15dcee0a72 | 74 | play_jump = false; |
snorod | 10:6f15dcee0a72 | 75 | play_leftright = false; |
snorod | 10:6f15dcee0a72 | 76 | } else if (play_strikes) { |
snorod | 10:6f15dcee0a72 | 77 | jack.period(1.0/440.0); |
snorod | 10:6f15dcee0a72 | 78 | jack = VOLUME/2.0; |
snorod | 10:6f15dcee0a72 | 79 | Thread::wait(750); |
snorod | 10:6f15dcee0a72 | 80 | jack = 0.0; |
snorod | 10:6f15dcee0a72 | 81 | play_strikes = false; |
snorod | 10:6f15dcee0a72 | 82 | play_duck = false; |
snorod | 10:6f15dcee0a72 | 83 | play_jump = false; |
snorod | 10:6f15dcee0a72 | 84 | play_leftright = false; |
snorod | 10:6f15dcee0a72 | 85 | } else if (play_duck) { |
snorod | 10:6f15dcee0a72 | 86 | jack.period(1.0/130.8); |
snorod | 10:6f15dcee0a72 | 87 | jack = VOLUME/2.0; |
snorod | 10:6f15dcee0a72 | 88 | Thread::wait(1000); |
snorod | 10:6f15dcee0a72 | 89 | jack = 0.0; |
snorod | 10:6f15dcee0a72 | 90 | play_duck = false; |
snorod | 10:6f15dcee0a72 | 91 | play_jump = false; |
snorod | 10:6f15dcee0a72 | 92 | play_leftright = false; |
snorod | 10:6f15dcee0a72 | 93 | } else if (play_jump) { |
snorod | 10:6f15dcee0a72 | 94 | jack.period(1.0/261.6); |
snorod | 10:6f15dcee0a72 | 95 | jack = VOLUME/2.0; |
snorod | 10:6f15dcee0a72 | 96 | Thread::wait(150); |
snorod | 10:6f15dcee0a72 | 97 | jack.period(1.0/523.3); |
snorod | 10:6f15dcee0a72 | 98 | Thread::wait(300); |
snorod | 10:6f15dcee0a72 | 99 | jack = 0.0; |
snorod | 10:6f15dcee0a72 | 100 | play_jump = false; |
snorod | 10:6f15dcee0a72 | 101 | play_leftright = false; |
snorod | 10:6f15dcee0a72 | 102 | } else if (play_leftright) { |
snorod | 10:6f15dcee0a72 | 103 | jack.period(1.0/261.6); |
snorod | 10:6f15dcee0a72 | 104 | jack = VOLUME/2.0; |
snorod | 10:6f15dcee0a72 | 105 | Thread::wait(250); |
snorod | 10:6f15dcee0a72 | 106 | jack = 0.0; |
snorod | 10:6f15dcee0a72 | 107 | play_leftright = false; |
snorod | 10:6f15dcee0a72 | 108 | } |
snorod | 10:6f15dcee0a72 | 109 | } |
snorod | 10:6f15dcee0a72 | 110 | } |
snorod | 10:6f15dcee0a72 | 111 | |
snorod | 10:6f15dcee0a72 | 112 | void read_acc(void const *argument){ |
snorod | 10:6f15dcee0a72 | 113 | while(1) { |
snorod | 10:6f15dcee0a72 | 114 | acc.readXGravity(&acc_x); |
snorod | 10:6f15dcee0a72 | 115 | if (acc_x > 0.5) { |
snorod | 10:6f15dcee0a72 | 116 | if (player_x == CENTER_X) { |
snorod | 10:6f15dcee0a72 | 117 | player_x = LEFT_X; |
snorod | 10:6f15dcee0a72 | 118 | } else if (player_x == RIGHT_X) { |
snorod | 10:6f15dcee0a72 | 119 | player_x = CENTER_X; |
snorod | 10:6f15dcee0a72 | 120 | } |
snorod | 10:6f15dcee0a72 | 121 | play_leftright = true; |
snorod | 10:6f15dcee0a72 | 122 | Thread::wait(500); |
snorod | 10:6f15dcee0a72 | 123 | } else if (acc_x < -0.5) { |
snorod | 10:6f15dcee0a72 | 124 | if (player_x == CENTER_X) { |
snorod | 10:6f15dcee0a72 | 125 | player_x = RIGHT_X; |
snorod | 10:6f15dcee0a72 | 126 | } else if (player_x == LEFT_X) { |
snorod | 10:6f15dcee0a72 | 127 | player_x = CENTER_X; |
snorod | 10:6f15dcee0a72 | 128 | } |
snorod | 10:6f15dcee0a72 | 129 | play_leftright = true; |
snorod | 10:6f15dcee0a72 | 130 | Thread::wait(500); |
snorod | 10:6f15dcee0a72 | 131 | } |
snorod | 10:6f15dcee0a72 | 132 | Thread::wait(250); |
snorod | 10:6f15dcee0a72 | 133 | } |
snorod | 10:6f15dcee0a72 | 134 | } |
snorod | 10:6f15dcee0a72 | 135 | |
snorod | 10:6f15dcee0a72 | 136 | void read_score(void const *argument) { |
snorod | 10:6f15dcee0a72 | 137 | while(1) { |
snorod | 10:6f15dcee0a72 | 138 | score = score + 10; |
snorod | 10:6f15dcee0a72 | 139 | LCD.lock(); |
snorod | 10:6f15dcee0a72 | 140 | uLCD.locate(11, 0); |
snorod | 10:6f15dcee0a72 | 141 | uLCD.printf("%7D", score); // maximum 7-digit score, cap at 9,999,900 |
snorod | 10:6f15dcee0a72 | 142 | LCD.unlock(); |
snorod | 10:6f15dcee0a72 | 143 | Thread::wait(1000); |
snorod | 10:6f15dcee0a72 | 144 | } |
snorod | 10:6f15dcee0a72 | 145 | } |
snorod | 10:6f15dcee0a72 | 146 | |
snorod | 10:6f15dcee0a72 | 147 | void read_high_score(void const *argument) { |
snorod | 10:6f15dcee0a72 | 148 | while(1) { |
snorod | 10:6f15dcee0a72 | 149 | LCD.lock(); |
snorod | 10:6f15dcee0a72 | 150 | uLCD.locate(0, 0); |
snorod | 10:6f15dcee0a72 | 151 | uLCD.printf("HIGH:\n%D", high_score); |
snorod | 10:6f15dcee0a72 | 152 | LCD.unlock(); |
snorod | 10:6f15dcee0a72 | 153 | Thread::wait(1500); |
snorod | 10:6f15dcee0a72 | 154 | } |
snorod | 10:6f15dcee0a72 | 155 | } |
snorod | 10:6f15dcee0a72 | 156 | |
snorod | 10:6f15dcee0a72 | 157 | void read_strikes(void const *argument) { |
snorod | 10:6f15dcee0a72 | 158 | while(1) { |
snorod | 10:6f15dcee0a72 | 159 | LCD.lock(); |
snorod | 10:6f15dcee0a72 | 160 | uLCD.locate(15, 1); |
snorod | 10:6f15dcee0a72 | 161 | uLCD.printf(strikes_arr[strikes % 4]); |
snorod | 10:6f15dcee0a72 | 162 | LCD.unlock(); |
snorod | 10:6f15dcee0a72 | 163 | Thread::wait(1000); |
snorod | 10:6f15dcee0a72 | 164 | } |
snorod | 10:6f15dcee0a72 | 165 | } |
snorod | 10:6f15dcee0a72 | 166 | |
snorod | 10:6f15dcee0a72 | 167 | // check for button press |
snorod | 10:6f15dcee0a72 | 168 | void read_duck(void const *argument) { |
snorod | 10:6f15dcee0a72 | 169 | while(1) { |
snorod | 10:6f15dcee0a72 | 170 | if (!duck_button) { |
snorod | 10:6f15dcee0a72 | 171 | player_y = DUCK_Y; |
snorod | 10:6f15dcee0a72 | 172 | play_duck = true; |
snorod | 10:6f15dcee0a72 | 173 | Thread::wait(1000); |
snorod | 10:6f15dcee0a72 | 174 | player_y = CENTER_Y; |
snorod | 10:6f15dcee0a72 | 175 | } |
snorod | 10:6f15dcee0a72 | 176 | } |
snorod | 10:6f15dcee0a72 | 177 | } |
snorod | 10:6f15dcee0a72 | 178 | |
snorod | 10:6f15dcee0a72 | 179 | // check for button press |
snorod | 10:6f15dcee0a72 | 180 | void read_jump(void const *argument) { |
snorod | 10:6f15dcee0a72 | 181 | while(1) { |
snorod | 10:6f15dcee0a72 | 182 | if (!jump_button && player_y != DUCK_Y) { |
snorod | 10:6f15dcee0a72 | 183 | player_y = JUMP_Y; |
snorod | 10:6f15dcee0a72 | 184 | play_jump = true; |
snorod | 10:6f15dcee0a72 | 185 | Thread::wait(1000); |
snorod | 10:6f15dcee0a72 | 186 | if (player_y == JUMP_Y) { |
snorod | 10:6f15dcee0a72 | 187 | player_y = CENTER_Y; |
snorod | 10:6f15dcee0a72 | 188 | } |
snorod | 10:6f15dcee0a72 | 189 | } |
snorod | 10:6f15dcee0a72 | 190 | } |
snorod | 10:6f15dcee0a72 | 191 | } |
snorod | 10:6f15dcee0a72 | 192 | |
snorod | 10:6f15dcee0a72 | 193 | // check if it's game over |
snorod | 10:6f15dcee0a72 | 194 | void read_alive(void const *argument) { |
snorod | 10:6f15dcee0a72 | 195 | while(1) { |
snorod | 10:6f15dcee0a72 | 196 | if (strikes >= 3 || score >= 9999900) { |
snorod | 10:6f15dcee0a72 | 197 | if (score > high_score) { |
snorod | 10:6f15dcee0a72 | 198 | high_score = score; |
snorod | 10:6f15dcee0a72 | 199 | } |
snorod | 10:6f15dcee0a72 | 200 | score = 0; |
snorod | 10:6f15dcee0a72 | 201 | strikes = 0; |
snorod | 10:6f15dcee0a72 | 202 | player_x = CENTER_X; |
snorod | 10:6f15dcee0a72 | 203 | player_y = CENTER_Y; |
snorod | 10:6f15dcee0a72 | 204 | } |
snorod | 10:6f15dcee0a72 | 205 | } |
snorod | 10:6f15dcee0a72 | 206 | } |
snorod | 10:6f15dcee0a72 | 207 | |
snorod | 10:6f15dcee0a72 | 208 | void read_position(void const *argument) { |
snorod | 10:6f15dcee0a72 | 209 | while(1) { |
snorod | 10:6f15dcee0a72 | 210 | LCD.lock(); |
snorod | 10:6f15dcee0a72 | 211 | |
snorod | 10:6f15dcee0a72 | 212 | // cover up positions on screen where there's no player or obstacle |
snorod | 10:6f15dcee0a72 | 213 | if (player_x != LEFT_X) { |
snorod | 10:6f15dcee0a72 | 214 | if (obstacle_y != JUMP_Y) { |
snorod | 10:6f15dcee0a72 | 215 | uLCD.filled_circle(LEFT_X, JUMP_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 216 | } |
snorod | 10:6f15dcee0a72 | 217 | if (obstacle_y != CENTER_Y) { |
snorod | 10:6f15dcee0a72 | 218 | uLCD.filled_circle(LEFT_X, CENTER_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 219 | } |
snorod | 10:6f15dcee0a72 | 220 | if (obstacle_y != DUCK_Y) { |
snorod | 10:6f15dcee0a72 | 221 | uLCD.filled_circle(LEFT_X, DUCK_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 222 | } |
snorod | 10:6f15dcee0a72 | 223 | } |
snorod | 10:6f15dcee0a72 | 224 | if (player_x != CENTER_X) { |
snorod | 10:6f15dcee0a72 | 225 | if (obstacle_y != JUMP_Y) { |
snorod | 10:6f15dcee0a72 | 226 | uLCD.filled_circle(CENTER_X, JUMP_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 227 | } |
snorod | 10:6f15dcee0a72 | 228 | if (obstacle_y != CENTER_Y) { |
snorod | 10:6f15dcee0a72 | 229 | uLCD.filled_circle(CENTER_X, CENTER_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 230 | } |
snorod | 10:6f15dcee0a72 | 231 | if (obstacle_y != DUCK_Y) { |
snorod | 10:6f15dcee0a72 | 232 | uLCD.filled_circle(CENTER_X, DUCK_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 233 | } |
snorod | 10:6f15dcee0a72 | 234 | } |
snorod | 10:6f15dcee0a72 | 235 | if (player_x != RIGHT_X) { |
snorod | 10:6f15dcee0a72 | 236 | if (obstacle_y != JUMP_Y) { |
snorod | 10:6f15dcee0a72 | 237 | uLCD.filled_circle(RIGHT_X, JUMP_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 238 | } |
snorod | 10:6f15dcee0a72 | 239 | if (obstacle_y != CENTER_Y) { |
snorod | 10:6f15dcee0a72 | 240 | uLCD.filled_circle(RIGHT_X, CENTER_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 241 | } |
snorod | 10:6f15dcee0a72 | 242 | if (obstacle_y != DUCK_Y) { |
snorod | 10:6f15dcee0a72 | 243 | uLCD.filled_circle(RIGHT_X, DUCK_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 244 | } |
snorod | 10:6f15dcee0a72 | 245 | } |
snorod | 10:6f15dcee0a72 | 246 | if (player_y != JUMP_Y && obstacle_y != JUMP_Y) { |
snorod | 10:6f15dcee0a72 | 247 | if (player_x == RIGHT_X) { |
snorod | 10:6f15dcee0a72 | 248 | uLCD.filled_circle(player_x, JUMP_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 249 | } else if (player_x == LEFT_X) { |
snorod | 10:6f15dcee0a72 | 250 | uLCD.filled_circle(player_x, JUMP_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 251 | } else if (player_x == CENTER_X) { |
snorod | 10:6f15dcee0a72 | 252 | uLCD.filled_circle(player_x, JUMP_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 253 | } |
snorod | 10:6f15dcee0a72 | 254 | } |
snorod | 10:6f15dcee0a72 | 255 | if (player_y != CENTER_Y && obstacle_y != CENTER_Y) { |
snorod | 10:6f15dcee0a72 | 256 | uLCD.filled_circle(player_x, CENTER_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 257 | } |
snorod | 10:6f15dcee0a72 | 258 | if (player_y != DUCK_Y && obstacle_y != DUCK_Y) { |
snorod | 10:6f15dcee0a72 | 259 | if (player_x == RIGHT_X) { |
snorod | 10:6f15dcee0a72 | 260 | uLCD.filled_circle(player_x, DUCK_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 261 | } else if (player_x == LEFT_X) { |
snorod | 10:6f15dcee0a72 | 262 | uLCD.filled_circle(player_x, DUCK_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 263 | } else if (player_x == CENTER_X) { |
snorod | 10:6f15dcee0a72 | 264 | uLCD.filled_circle(player_x, DUCK_Y, RADIUS, 0x003057); |
snorod | 10:6f15dcee0a72 | 265 | } |
snorod | 10:6f15dcee0a72 | 266 | } |
snorod | 10:6f15dcee0a72 | 267 | uLCD.filled_circle(player_x, player_y, RADIUS, 0xB3A369); |
snorod | 10:6f15dcee0a72 | 268 | LCD.unlock(); |
snorod | 10:6f15dcee0a72 | 269 | } |
snorod | 10:6f15dcee0a72 | 270 | } |
snorod | 10:6f15dcee0a72 | 271 | |
snorod | 10:6f15dcee0a72 | 272 | void read_obstacle(void const *argument) { |
snorod | 10:6f15dcee0a72 | 273 | while(1) { |
snorod | 10:6f15dcee0a72 | 274 | LCD.lock(); |
snorod | 10:6f15dcee0a72 | 275 | uLCD.filled_rectangle(obstacle_x - 10 - (steps * 2), obstacle_y + 7, obstacle_x + 10 + (steps * 2), obstacle_y - 7, 0x003057); |
snorod | 10:6f15dcee0a72 | 276 | steps++; |
snorod | 10:6f15dcee0a72 | 277 | obstacle_y = obstacle_y + 12; |
snorod | 10:6f15dcee0a72 | 278 | |
snorod | 10:6f15dcee0a72 | 279 | // move obstacle along slope down the track |
snorod | 10:6f15dcee0a72 | 280 | if (obstacle_type == dodge) { |
snorod | 10:6f15dcee0a72 | 281 | if (dodge_x >= ob_x_roots[2]) { |
snorod | 10:6f15dcee0a72 | 282 | dodge_x = dodge_x + 2; |
snorod | 10:6f15dcee0a72 | 283 | } else if (dodge_x <= ob_x_roots[0]) { |
snorod | 10:6f15dcee0a72 | 284 | dodge_x = dodge_x - 2; |
snorod | 10:6f15dcee0a72 | 285 | } |
snorod | 10:6f15dcee0a72 | 286 | } |
snorod | 10:6f15dcee0a72 | 287 | |
snorod | 10:6f15dcee0a72 | 288 | // reset obstacle if it has exited the screen |
snorod | 10:6f15dcee0a72 | 289 | if (obstacle_y >= 130) { |
snorod | 10:6f15dcee0a72 | 290 | obstacle_type = ob_type_arr[rand() % 3]; |
snorod | 10:6f15dcee0a72 | 291 | obstacle_x = ob_x_roots[1]; |
snorod | 10:6f15dcee0a72 | 292 | if (obstacle_type == dodge) { |
snorod | 10:6f15dcee0a72 | 293 | dodge_x = ob_x_roots[rand() % 3]; |
snorod | 10:6f15dcee0a72 | 294 | } |
snorod | 10:6f15dcee0a72 | 295 | obstacle_y = 12; |
snorod | 10:6f15dcee0a72 | 296 | steps = 0; |
snorod | 10:6f15dcee0a72 | 297 | } |
snorod | 10:6f15dcee0a72 | 298 | |
snorod | 10:6f15dcee0a72 | 299 | // draw dodge obstacle |
snorod | 10:6f15dcee0a72 | 300 | if (obstacle_type != dodge) { |
snorod | 10:6f15dcee0a72 | 301 | uLCD.filled_rectangle(obstacle_x - 10 - (steps * 2), obstacle_y + 7, obstacle_x + 10 + (steps * 2), obstacle_y - 7, 0xEAAA00); |
snorod | 10:6f15dcee0a72 | 302 | } else { |
snorod | 10:6f15dcee0a72 | 303 | if (dodge_x >= ob_x_roots[2]) { |
snorod | 10:6f15dcee0a72 | 304 | // draw rectangle on left side, dodge on the right |
snorod | 10:6f15dcee0a72 | 305 | uLCD.filled_rectangle(obstacle_x - 10 - (steps * 2), obstacle_y + 7, dodge_x - 10, obstacle_y - 7, 0xEAAA00); |
snorod | 10:6f15dcee0a72 | 306 | } else if (dodge_x <= ob_x_roots[0]) { |
snorod | 10:6f15dcee0a72 | 307 | // draw rectangle on right side, dodge on the left |
snorod | 10:6f15dcee0a72 | 308 | uLCD.filled_rectangle(dodge_x + 10, obstacle_y + 7, obstacle_x + 10 + (steps * 2), obstacle_y - 7, 0xEAAA00); |
snorod | 10:6f15dcee0a72 | 309 | } else { |
snorod | 10:6f15dcee0a72 | 310 | // draw two rectangles on either side, dodge in the middle |
snorod | 10:6f15dcee0a72 | 311 | uLCD.filled_rectangle(ob_x_roots[0] - 5 - (steps * 2), obstacle_y + 7, ob_x_roots[0] - steps, obstacle_y - 7, 0xEAAA00); |
snorod | 10:6f15dcee0a72 | 312 | uLCD.filled_rectangle(ob_x_roots[2] + steps, obstacle_y + 7, ob_x_roots[2] + 5 + (steps * 2), obstacle_y - 7, 0xEAAA00); |
snorod | 10:6f15dcee0a72 | 313 | } |
snorod | 10:6f15dcee0a72 | 314 | } |
snorod | 10:6f15dcee0a72 | 315 | |
snorod | 10:6f15dcee0a72 | 316 | // draw duck/jump obstacle |
snorod | 10:6f15dcee0a72 | 317 | if (obstacle_type == duck) { |
snorod | 10:6f15dcee0a72 | 318 | uLCD.filled_rectangle(obstacle_x - 8 - (steps * 2), obstacle_y + 7, obstacle_x + 8 + (steps * 2), obstacle_y - 5, 0x003057); |
snorod | 10:6f15dcee0a72 | 319 | } else if (obstacle_type == jump) { |
snorod | 10:6f15dcee0a72 | 320 | uLCD.filled_rectangle(obstacle_x - 8 - (steps * 2), obstacle_y + 5, obstacle_x + 8 + (steps * 2), obstacle_y - 7, 0x003057); |
snorod | 10:6f15dcee0a72 | 321 | } |
snorod | 10:6f15dcee0a72 | 322 | |
snorod | 10:6f15dcee0a72 | 323 | // check for collisions, increment strikes if so |
snorod | 10:6f15dcee0a72 | 324 | if (obstacle_y == CENTER_Y) { |
snorod | 10:6f15dcee0a72 | 325 | if ((obstacle_type == duck && player_y != DUCK_Y) || (obstacle_type == jump && player_y != JUMP_Y) || (obstacle_type == dodge && dodge_x != player_x)) { |
snorod | 10:6f15dcee0a72 | 326 | strikes++; |
snorod | 10:6f15dcee0a72 | 327 | if (strikes >= 3) { |
snorod | 10:6f15dcee0a72 | 328 | play_dead = true; |
snorod | 10:6f15dcee0a72 | 329 | } else { |
snorod | 10:6f15dcee0a72 | 330 | play_strikes = true; |
snorod | 10:6f15dcee0a72 | 331 | } |
snorod | 10:6f15dcee0a72 | 332 | } |
snorod | 10:6f15dcee0a72 | 333 | } |
snorod | 10:6f15dcee0a72 | 334 | LCD.unlock(); |
snorod | 10:6f15dcee0a72 | 335 | Thread::wait(SPEED); |
snorod | 10:6f15dcee0a72 | 336 | } |
snorod | 10:6f15dcee0a72 | 337 | } |
4180_1 | 0:cfcf73272647 | 338 | |
4180_1 | 2:75727e89a717 | 339 | int main() |
4180_1 | 2:75727e89a717 | 340 | { |
snorod | 10:6f15dcee0a72 | 341 | uLCD.background_color(0x003057); |
4180_1 | 7:7bd7397ab89f | 342 | uLCD.cls(); |
snorod | 10:6f15dcee0a72 | 343 | uLCD.text_width(4); |
snorod | 10:6f15dcee0a72 | 344 | uLCD.text_height(4); |
snorod | 10:6f15dcee0a72 | 345 | uLCD.textbackground_color(0x003057); |
snorod | 10:6f15dcee0a72 | 346 | uLCD.color(0xB3A369); |
snorod | 10:6f15dcee0a72 | 347 | uLCD.printf("\nBuzz Run"); |
4180_1 | 7:7bd7397ab89f | 348 | |
snorod | 10:6f15dcee0a72 | 349 | wait(2); |
4180_1 | 3:454d1f4c8fd7 | 350 | uLCD.cls(); |
snorod | 10:6f15dcee0a72 | 351 | uLCD.baudrate(3000000); // high baud rate for fast refresh |
snorod | 10:6f15dcee0a72 | 352 | |
snorod | 10:6f15dcee0a72 | 353 | // Left-side guardrails |
snorod | 10:6f15dcee0a72 | 354 | uLCD.line(23, 130, 43, 10, 0xFFFFFF); |
snorod | 10:6f15dcee0a72 | 355 | uLCD.line(24, 130, 44, 10, 0xB3A369); |
snorod | 10:6f15dcee0a72 | 356 | uLCD.line(25, 130, 45, 10, 0xB3A369); |
snorod | 10:6f15dcee0a72 | 357 | uLCD.line(26, 130, 46, 10, 0xB3A369); |
snorod | 10:6f15dcee0a72 | 358 | uLCD.line(27, 130, 47, 10, 0xFFFFFF); |
snorod | 10:6f15dcee0a72 | 359 | |
snorod | 10:6f15dcee0a72 | 360 | // Right-side guardrails |
snorod | 10:6f15dcee0a72 | 361 | uLCD.line(103, 130, 83, 10, 0xFFFFFF); |
snorod | 10:6f15dcee0a72 | 362 | uLCD.line(104, 130, 84, 10, 0xB3A369); |
snorod | 10:6f15dcee0a72 | 363 | uLCD.line(105, 130, 85, 10, 0xB3A369); |
snorod | 10:6f15dcee0a72 | 364 | uLCD.line(106, 130, 86, 10, 0xB3A369); |
snorod | 10:6f15dcee0a72 | 365 | uLCD.line(107, 130, 87, 10, 0xFFFFFF); |
snorod | 10:6f15dcee0a72 | 366 | |
snorod | 10:6f15dcee0a72 | 367 | score = 0; |
snorod | 10:6f15dcee0a72 | 368 | high_score = 0; |
snorod | 10:6f15dcee0a72 | 369 | strikes = 0; |
snorod | 10:6f15dcee0a72 | 370 | steps = 0; |
snorod | 10:6f15dcee0a72 | 371 | |
snorod | 10:6f15dcee0a72 | 372 | // make sure sounds are off |
snorod | 10:6f15dcee0a72 | 373 | play_leftright = false; |
snorod | 10:6f15dcee0a72 | 374 | play_jump = false; |
snorod | 10:6f15dcee0a72 | 375 | play_duck = false; |
snorod | 10:6f15dcee0a72 | 376 | play_strikes = false; |
snorod | 10:6f15dcee0a72 | 377 | play_dead = false; |
snorod | 10:6f15dcee0a72 | 378 | |
snorod | 10:6f15dcee0a72 | 379 | // initialize player position |
snorod | 10:6f15dcee0a72 | 380 | player_x = CENTER_X; |
snorod | 10:6f15dcee0a72 | 381 | player_y = CENTER_Y; |
snorod | 10:6f15dcee0a72 | 382 | |
snorod | 10:6f15dcee0a72 | 383 | // initialize obstacle coordinates |
snorod | 10:6f15dcee0a72 | 384 | obstacle_x = ob_x_roots[1]; |
snorod | 10:6f15dcee0a72 | 385 | dodge_x = ob_x_roots[rand() % 3]; |
snorod | 10:6f15dcee0a72 | 386 | obstacle_y = 12; |
snorod | 10:6f15dcee0a72 | 387 | obstacle_type = ob_type_arr[rand() % 3]; |
snorod | 10:6f15dcee0a72 | 388 | |
snorod | 10:6f15dcee0a72 | 389 | // pull up push_buttons |
snorod | 10:6f15dcee0a72 | 390 | jump_button.mode(PullUp); |
snorod | 10:6f15dcee0a72 | 391 | duck_button.mode(PullUp); |
snorod | 10:6f15dcee0a72 | 392 | |
snorod | 10:6f15dcee0a72 | 393 | // start threads |
snorod | 10:6f15dcee0a72 | 394 | Thread acc_thread(read_acc); |
snorod | 10:6f15dcee0a72 | 395 | Thread score_thread(read_score); |
snorod | 10:6f15dcee0a72 | 396 | Thread duck_thread(read_duck); |
snorod | 10:6f15dcee0a72 | 397 | Thread jump_thread(read_jump); |
snorod | 10:6f15dcee0a72 | 398 | Thread alive_thread(read_alive); |
snorod | 10:6f15dcee0a72 | 399 | Thread position_thread(read_position); |
snorod | 10:6f15dcee0a72 | 400 | Thread obstacle_thread(read_obstacle); |
snorod | 10:6f15dcee0a72 | 401 | Thread strikes_thread(read_strikes); |
snorod | 10:6f15dcee0a72 | 402 | Thread sound_thread(read_sound); |
snorod | 10:6f15dcee0a72 | 403 | Thread high_score_thread(read_high_score); |
snorod | 10:6f15dcee0a72 | 404 | |
snorod | 10:6f15dcee0a72 | 405 | while(1) { |
snorod | 10:6f15dcee0a72 | 406 | // loop forever |
4180_1 | 3:454d1f4c8fd7 | 407 | } |
snorod | 10:6f15dcee0a72 | 408 | |
4180_1 | 6:f752accd632c | 409 | } |