Space Game

Dependencies:   N5110 mbed

Committer:
el14af
Date:
Thu May 05 10:21:22 2016 +0000
Revision:
0:10704407da46
final awesome

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14af 0:10704407da46 1 //
el14af 0:10704407da46 2 // Elec 2645
el14af 0:10704407da46 3 // Embedded Systems Project
el14af 0:10704407da46 4 // 5 March 2016
el14af 0:10704407da46 5 // Aleksander Filipiak
el14af 0:10704407da46 6
el14af 0:10704407da46 7 #include "ESP.h"
el14af 0:10704407da46 8
el14af 0:10704407da46 9
el14af 0:10704407da46 10 // ---------------------------------------------------------------------------MAIN()-----------------------------------------------------------------------------------------------------
el14af 0:10704407da46 11
el14af 0:10704407da46 12 int main()
el14af 0:10704407da46 13 {
el14af 0:10704407da46 14 // initialise buttons
el14af 0:10704407da46 15 speed_button.mode(PullDown);
el14af 0:10704407da46 16 start_button.mode(PullDown);
el14af 0:10704407da46 17 pause_button.mode(PullDown);
el14af 0:10704407da46 18 pause_button.rise(&pause_isr);
el14af 0:10704407da46 19
el14af 0:10704407da46 20 //initialise display
el14af 0:10704407da46 21 lcd.init();
el14af 0:10704407da46 22
el14af 0:10704407da46 23 //calibrate joystick
el14af 0:10704407da46 24 calibrateJoystick();
el14af 0:10704407da46 25
el14af 0:10704407da46 26 // print border
el14af 0:10704407da46 27 print_border(0,83,0,47);
el14af 0:10704407da46 28
el14af 0:10704407da46 29 // position in menu, 0 = start game, 1 = help
el14af 0:10704407da46 30 bool position = 0;
el14af 0:10704407da46 31
el14af 0:10704407da46 32 // attach joystick
el14af 0:10704407da46 33 pollJoystick.attach(&updateJoystick,0.05);
el14af 0:10704407da46 34
el14af 0:10704407da46 35
el14af 0:10704407da46 36 // menu
el14af 0:10704407da46 37 while (1) {
el14af 0:10704407da46 38
el14af 0:10704407da46 39 // select menu item: start game
el14af 0:10704407da46 40 if (joystick.direction == UP) {
el14af 0:10704407da46 41 position = 0;
el14af 0:10704407da46 42 }
el14af 0:10704407da46 43 // help
el14af 0:10704407da46 44 else if (joystick. direction == DOWN) {
el14af 0:10704407da46 45 position = 1;
el14af 0:10704407da46 46 }
el14af 0:10704407da46 47
el14af 0:10704407da46 48 //clear lcd
el14af 0:10704407da46 49 lcd.clear();
el14af 0:10704407da46 50
el14af 0:10704407da46 51 // print menu items
el14af 0:10704407da46 52 lcd.printString("START GAME",20,1);
el14af 0:10704407da46 53 lcd.printString("HELP",20,3);
el14af 0:10704407da46 54
el14af 0:10704407da46 55 // print cursor at appropriate position
el14af 0:10704407da46 56 if (!position) {
el14af 0:10704407da46 57 lcd.drawRect(15,10,2,2,0);
el14af 0:10704407da46 58 } else {
el14af 0:10704407da46 59 lcd.drawRect(15,25,2,2,0);
el14af 0:10704407da46 60 }
el14af 0:10704407da46 61
el14af 0:10704407da46 62 // refresh border
el14af 0:10704407da46 63 print_border(0,83,0,47);
el14af 0:10704407da46 64 // smaller ornamental border
el14af 0:10704407da46 65 print_border(2,81,2,45);
el14af 0:10704407da46 66
el14af 0:10704407da46 67 // refresh screen
el14af 0:10704407da46 68 lcd.refresh();
el14af 0:10704407da46 69
el14af 0:10704407da46 70 // if either button is pressed and the help menu function is selected bring up the help menu
el14af 0:10704407da46 71 if ((start_button || speed_button) && position) {
el14af 0:10704407da46 72
el14af 0:10704407da46 73 // outout variable
el14af 0:10704407da46 74 int output;
el14af 0:10704407da46 75
el14af 0:10704407da46 76 // create array for counter
el14af 0:10704407da46 77 int help_counter [5] = {0,1,2,3,4};
el14af 0:10704407da46 78
el14af 0:10704407da46 79 // state variable
el14af 0:10704407da46 80 int state = 0;
el14af 0:10704407da46 81
el14af 0:10704407da46 82 // help loop
el14af 0:10704407da46 83 while (1) {
el14af 0:10704407da46 84
el14af 0:10704407da46 85 // check which state the counter is in and see which the next state should be next,
el14af 0:10704407da46 86 // depending on direction
el14af 0:10704407da46 87 switch(state) {
el14af 0:10704407da46 88 case 0:
el14af 0:10704407da46 89 switch(joystick.direction) {
el14af 0:10704407da46 90 case RIGHT:
el14af 0:10704407da46 91 state = 0;
el14af 0:10704407da46 92 break;
el14af 0:10704407da46 93 case LEFT:
el14af 0:10704407da46 94 state = 1;
el14af 0:10704407da46 95 break;
el14af 0:10704407da46 96 }
el14af 0:10704407da46 97 break;
el14af 0:10704407da46 98 //
el14af 0:10704407da46 99 case 1:
el14af 0:10704407da46 100
el14af 0:10704407da46 101 switch(joystick.direction) {
el14af 0:10704407da46 102 case RIGHT:
el14af 0:10704407da46 103 state = 0;
el14af 0:10704407da46 104 break;
el14af 0:10704407da46 105 case LEFT:
el14af 0:10704407da46 106 state = 2;
el14af 0:10704407da46 107 break;
el14af 0:10704407da46 108 }
el14af 0:10704407da46 109 break;
el14af 0:10704407da46 110 case 2:
el14af 0:10704407da46 111
el14af 0:10704407da46 112 switch(joystick.direction) {
el14af 0:10704407da46 113 case RIGHT:
el14af 0:10704407da46 114 state = 1;
el14af 0:10704407da46 115 break;
el14af 0:10704407da46 116 case LEFT:
el14af 0:10704407da46 117 state = 3;
el14af 0:10704407da46 118 break;
el14af 0:10704407da46 119 }
el14af 0:10704407da46 120 break;
el14af 0:10704407da46 121 case 3:
el14af 0:10704407da46 122
el14af 0:10704407da46 123 switch(joystick.direction) {
el14af 0:10704407da46 124 case RIGHT:
el14af 0:10704407da46 125 state = 0;
el14af 0:10704407da46 126 break;
el14af 0:10704407da46 127 case LEFT:
el14af 0:10704407da46 128 state = 4;
el14af 0:10704407da46 129 break;
el14af 0:10704407da46 130 }
el14af 0:10704407da46 131 // last state brings the user back to the main menu
el14af 0:10704407da46 132 case 4:
el14af 0:10704407da46 133 break;
el14af 0:10704407da46 134 }
el14af 0:10704407da46 135
el14af 0:10704407da46 136 // outout current state
el14af 0:10704407da46 137 output = help_counter[state];
el14af 0:10704407da46 138
el14af 0:10704407da46 139 lcd.clear();
el14af 0:10704407da46 140
el14af 0:10704407da46 141 // refresh border
el14af 0:10704407da46 142 print_border(0,83,0,47);
el14af 0:10704407da46 143 // smaller ornamental border
el14af 0:10704407da46 144 print_border(2,81,2,45);
el14af 0:10704407da46 145
el14af 0:10704407da46 146 // print instructions based on counter output
el14af 0:10704407da46 147 if (output ==0) {
el14af 0:10704407da46 148
el14af 0:10704407da46 149 // instructions
el14af 0:10704407da46 150 lcd.printString("USE",10,1);
el14af 0:10704407da46 151 lcd.printString("JOYSTICK",10,2);
el14af 0:10704407da46 152 lcd.printString("TO MOVE",10,3);
el14af 0:10704407da46 153 lcd.printString("SHIP",10,4);
el14af 0:10704407da46 154 } else if (output ==1) {
el14af 0:10704407da46 155
el14af 0:10704407da46 156 // instructions
el14af 0:10704407da46 157 lcd.printString("START BUTTON:",5,1);
el14af 0:10704407da46 158 lcd.printString("PAUSE",10,2);
el14af 0:10704407da46 159 lcd.printString("SPEED BUTTON:",5,3);
el14af 0:10704407da46 160 lcd.printString("FASTER SHIP",10,4);
el14af 0:10704407da46 161 } else if (output ==2) {
el14af 0:10704407da46 162
el14af 0:10704407da46 163 // instructions
el14af 0:10704407da46 164 lcd.drawRect(5,10,2,2,0);
el14af 0:10704407da46 165 lcd.printString("+ 8 seconds",15,1);
el14af 0:10704407da46 166 lcd.drawLine(5,27,7,27,1);
el14af 0:10704407da46 167 lcd.drawLine(6,26,6,28,1);
el14af 0:10704407da46 168 lcd.printString("- 5 seconds",15,3);
el14af 0:10704407da46 169
el14af 0:10704407da46 170 } else if (output ==3) {
el14af 0:10704407da46 171
el14af 0:10704407da46 172 // instructions
el14af 0:10704407da46 173 lcd.printString("DON'T LET",10,1);
el14af 0:10704407da46 174 lcd.printString("THE TIMER",10,2);
el14af 0:10704407da46 175 lcd.printString("RUN OUT!",10,3);
el14af 0:10704407da46 176 // 5th state breaks the loop and leads back to menu
el14af 0:10704407da46 177 } else if (output ==4) {
el14af 0:10704407da46 178
el14af 0:10704407da46 179 break;
el14af 0:10704407da46 180 }
el14af 0:10704407da46 181 lcd.refresh();
el14af 0:10704407da46 182
el14af 0:10704407da46 183 //short delay
el14af 0:10704407da46 184 wait(0.3);
el14af 0:10704407da46 185
el14af 0:10704407da46 186 }
el14af 0:10704407da46 187 }
el14af 0:10704407da46 188
el14af 0:10704407da46 189 // if the button has been pressed and Start Game is selected, break the menu loop
el14af 0:10704407da46 190 // and continue to game initialisation
el14af 0:10704407da46 191 if ((start_button || speed_button) && !position) {
el14af 0:10704407da46 192 break;
el14af 0:10704407da46 193 }
el14af 0:10704407da46 194
el14af 0:10704407da46 195 wait(0.1);
el14af 0:10704407da46 196
el14af 0:10704407da46 197 }
el14af 0:10704407da46 198
el14af 0:10704407da46 199 lcd.clear();
el14af 0:10704407da46 200
el14af 0:10704407da46 201 // print game border
el14af 0:10704407da46 202 print_border(0,83,8,47);
el14af 0:10704407da46 203
el14af 0:10704407da46 204 // setup
el14af 0:10704407da46 205 setup_game();
el14af 0:10704407da46 206
el14af 0:10704407da46 207 lcd.refresh();
el14af 0:10704407da46 208
el14af 0:10704407da46 209 // flashing press start
el14af 0:10704407da46 210 while(1) {
el14af 0:10704407da46 211
el14af 0:10704407da46 212 lcd.printString ("PRESS START",10,2);
el14af 0:10704407da46 213
el14af 0:10704407da46 214 lcd.refresh();
el14af 0:10704407da46 215
el14af 0:10704407da46 216 wait(0.5);
el14af 0:10704407da46 217
el14af 0:10704407da46 218 lcd.printString (" ",10,2);
el14af 0:10704407da46 219
el14af 0:10704407da46 220 wait(0.3);
el14af 0:10704407da46 221
el14af 0:10704407da46 222 // break loop when start button is pressed
el14af 0:10704407da46 223 if (start_button == 1) {
el14af 0:10704407da46 224 break;
el14af 0:10704407da46 225 }
el14af 0:10704407da46 226 }
el14af 0:10704407da46 227
el14af 0:10704407da46 228 wait(0.5);
el14af 0:10704407da46 229
el14af 0:10704407da46 230 // attach tickers:
el14af 0:10704407da46 231
el14af 0:10704407da46 232 // ship
el14af 0:10704407da46 233 ship_mover.attach(&speed_change,0.03); // ship mover
el14af 0:10704407da46 234 //turrets
el14af 0:10704407da46 235 turret_mover.attach(&update_turrets,0.07); // turret mover
el14af 0:10704407da46 236 shooter.attach(&shoot,0.5); // shooter
el14af 0:10704407da46 237 // missles
el14af 0:10704407da46 238 missle_mover.attach(&update_missles,0.04); // missle mover
el14af 0:10704407da46 239 hit_checker.attach(&check_hit,0.04); // hit checker
el14af 0:10704407da46 240 // collectibles
el14af 0:10704407da46 241 collect_printer.attach(&print_collect,5.0); // collectible printer
el14af 0:10704407da46 242 collect_checker.attach(&check_collect,0.05); // collectible checker
el14af 0:10704407da46 243 collect_updater.attach(&update_collect,0.1); // collectible updater
el14af 0:10704407da46 244 collect_timer.attach(&collect_timeout,10.0); // collectible timer
el14af 0:10704407da46 245 // timer
el14af 0:10704407da46 246 timer.attach(&time_left,1.0);
el14af 0:10704407da46 247
el14af 0:10704407da46 248 // reset timer
el14af 0:10704407da46 249 g_time_left = 30;
el14af 0:10704407da46 250
el14af 0:10704407da46 251 // game loop
el14af 0:10704407da46 252 while(1) {
el14af 0:10704407da46 253
el14af 0:10704407da46 254
el14af 0:10704407da46 255 // pause loop
el14af 0:10704407da46 256 // if pause button is pressed
el14af 0:10704407da46 257 if (g_pause_flag) {
el14af 0:10704407da46 258
el14af 0:10704407da46 259 g_pause_flag = 0;
el14af 0:10704407da46 260
el14af 0:10704407da46 261 // detach tickers to stop gameplay
el14af 0:10704407da46 262 //ship
el14af 0:10704407da46 263 ship_mover.detach(); // ship mover
el14af 0:10704407da46 264 //turrets
el14af 0:10704407da46 265 turret_mover.detach(); // turret mover
el14af 0:10704407da46 266 shooter.detach(); // shooter
el14af 0:10704407da46 267 // missles
el14af 0:10704407da46 268 missle_mover.detach(); // missle mover
el14af 0:10704407da46 269 hit_checker.detach(); // hit checker
el14af 0:10704407da46 270 // collectibles
el14af 0:10704407da46 271 collect_printer.detach();// collectible printer
el14af 0:10704407da46 272 collect_checker.detach();// collectible checker
el14af 0:10704407da46 273 collect_updater.detach();// collectible updater
el14af 0:10704407da46 274 collect_timer.detach(); // collectible timer
el14af 0:10704407da46 275 // timer
el14af 0:10704407da46 276 timer.detach();
el14af 0:10704407da46 277
el14af 0:10704407da46 278 while(1) {
el14af 0:10704407da46 279
el14af 0:10704407da46 280
el14af 0:10704407da46 281 // flashing pause
el14af 0:10704407da46 282 lcd.printString(" ",25,2);
el14af 0:10704407da46 283
el14af 0:10704407da46 284 wait(0.3);
el14af 0:10704407da46 285
el14af 0:10704407da46 286 lcd.printString("PAUSE",25,2);
el14af 0:10704407da46 287
el14af 0:10704407da46 288 wait(0.5);
el14af 0:10704407da46 289
el14af 0:10704407da46 290 lcd.refresh();
el14af 0:10704407da46 291
el14af 0:10704407da46 292 // if the start button is pressed again game starts again
el14af 0:10704407da46 293 if (g_pause_flag) {
el14af 0:10704407da46 294 break;
el14af 0:10704407da46 295 }
el14af 0:10704407da46 296
el14af 0:10704407da46 297 }
el14af 0:10704407da46 298 // reset flag
el14af 0:10704407da46 299 g_pause_flag = 0;
el14af 0:10704407da46 300
el14af 0:10704407da46 301 // clear pause
el14af 0:10704407da46 302 lcd.printString(" ",25,2);
el14af 0:10704407da46 303
el14af 0:10704407da46 304 // re-attach tickers to restart gameplay
el14af 0:10704407da46 305 // ship
el14af 0:10704407da46 306 ship_mover.attach(&speed_change,0.03); // ship mover
el14af 0:10704407da46 307 //turrets
el14af 0:10704407da46 308 turret_mover.attach(&update_turrets,0.07); // turret mover
el14af 0:10704407da46 309 shooter.attach(&shoot,0.5); // shooter
el14af 0:10704407da46 310 // missles
el14af 0:10704407da46 311 missle_mover.attach(&update_missles,0.04); // missle mover
el14af 0:10704407da46 312 hit_checker.attach(&check_hit,0.04); // hit checker
el14af 0:10704407da46 313 // collectibles
el14af 0:10704407da46 314 collect_printer.attach(&print_collect,5.0); // collectible printer
el14af 0:10704407da46 315 collect_checker.attach(&check_collect,0.05); // collectible checker
el14af 0:10704407da46 316 collect_updater.attach(&update_collect,0.1); // collectible updater
el14af 0:10704407da46 317 collect_timer.attach(&collect_timeout,10.0); // collectible timer
el14af 0:10704407da46 318 // timer
el14af 0:10704407da46 319 timer.attach(&time_left,1.0);
el14af 0:10704407da46 320 }
el14af 0:10704407da46 321
el14af 0:10704407da46 322 // print game border
el14af 0:10704407da46 323 print_border(0,83,8,47);
el14af 0:10704407da46 324
el14af 0:10704407da46 325 // if missle hits
el14af 0:10704407da46 326 if(g_hit_flag == 1) {
el14af 0:10704407da46 327 // reset missle flags in case missle hits again
el14af 0:10704407da46 328 missle_one.hit_flag = 0;
el14af 0:10704407da46 329 missle_two.hit_flag = 0;
el14af 0:10704407da46 330 missle_three.hit_flag = 0;
el14af 0:10704407da46 331 missle_four.hit_flag = 0;
el14af 0:10704407da46 332 missle_five.hit_flag = 0;
el14af 0:10704407da46 333 missle_six.hit_flag = 0;
el14af 0:10704407da46 334
el14af 0:10704407da46 335 //take 5 seconds off the timer
el14af 0:10704407da46 336 g_time_left = g_time_left - 5;
el14af 0:10704407da46 337
el14af 0:10704407da46 338 // print new time left
el14af 0:10704407da46 339 char buffer[14];
el14af 0:10704407da46 340 int length = sprintf(buffer,"Time: %2d ",g_time_left);
el14af 0:10704407da46 341 if (length <= 14) // if string will fit on display
el14af 0:10704407da46 342 lcd.printString(buffer,16,0);
el14af 0:10704407da46 343
el14af 0:10704407da46 344 // reset flag
el14af 0:10704407da46 345 g_hit_flag = 0;
el14af 0:10704407da46 346 }
el14af 0:10704407da46 347 // if collectible is collected
el14af 0:10704407da46 348 if(g_collect_flag) {
el14af 0:10704407da46 349
el14af 0:10704407da46 350 // add 8 seconds to timer
el14af 0:10704407da46 351 g_time_left = g_time_left +8;
el14af 0:10704407da46 352
el14af 0:10704407da46 353 // print new time left
el14af 0:10704407da46 354 char buffer[14];
el14af 0:10704407da46 355 int length = sprintf(buffer,"Time: %2d ",g_time_left);
el14af 0:10704407da46 356 if (length <= 14) // if string will fit on display
el14af 0:10704407da46 357 lcd.printString(buffer,16,0);
el14af 0:10704407da46 358
el14af 0:10704407da46 359 // reset flag
el14af 0:10704407da46 360 g_collect_flag = 0;
el14af 0:10704407da46 361 }
el14af 0:10704407da46 362
el14af 0:10704407da46 363 //refresh screen
el14af 0:10704407da46 364 lcd.refresh();
el14af 0:10704407da46 365
el14af 0:10704407da46 366 // break loop if timer runs out
el14af 0:10704407da46 367 if (g_time_left < 0) {
el14af 0:10704407da46 368
el14af 0:10704407da46 369 break;
el14af 0:10704407da46 370
el14af 0:10704407da46 371 }
el14af 0:10704407da46 372 }
el14af 0:10704407da46 373 // detach all tickers
el14af 0:10704407da46 374
el14af 0:10704407da46 375 // ship
el14af 0:10704407da46 376 ship_mover.detach(); // ship mover
el14af 0:10704407da46 377 //turrets
el14af 0:10704407da46 378 turret_mover.detach(); // turret mover
el14af 0:10704407da46 379 shooter.detach(); // shooter
el14af 0:10704407da46 380 // missles
el14af 0:10704407da46 381 missle_mover.detach(); // missle mover
el14af 0:10704407da46 382 hit_checker.detach(); // hit checker
el14af 0:10704407da46 383 // collectibles
el14af 0:10704407da46 384 collect_printer.detach();// collectible printer
el14af 0:10704407da46 385 collect_checker.detach();// collectible checker
el14af 0:10704407da46 386 collect_updater.detach();// collectible updater
el14af 0:10704407da46 387 collect_timer.detach(); // collectible timer
el14af 0:10704407da46 388 // timer
el14af 0:10704407da46 389 timer.detach();
el14af 0:10704407da46 390
el14af 0:10704407da46 391 lcd.clear();
el14af 0:10704407da46 392
el14af 0:10704407da46 393 // screen border
el14af 0:10704407da46 394 print_border(0,83,0,47);
el14af 0:10704407da46 395 // smaller ornamental border
el14af 0:10704407da46 396 print_border(2,81,2,45);
el14af 0:10704407da46 397
el14af 0:10704407da46 398 // print score
el14af 0:10704407da46 399 lcd.printString("SCORE: ",23,2);
el14af 0:10704407da46 400 char buffer[14];
el14af 0:10704407da46 401 int length = sprintf(buffer," %2d ",g_score);
el14af 0:10704407da46 402 lcd.printString(buffer,23,3);
el14af 0:10704407da46 403
el14af 0:10704407da46 404 }
el14af 0:10704407da46 405
el14af 0:10704407da46 406 // ---------------------------------------------------------------------------FUNCTION PROTOTYPES-----------------------------------------------------------------------------------------
el14af 0:10704407da46 407
el14af 0:10704407da46 408 void pause_isr ()
el14af 0:10704407da46 409 {
el14af 0:10704407da46 410
el14af 0:10704407da46 411 g_pause_flag = 1;
el14af 0:10704407da46 412 }
el14af 0:10704407da46 413
el14af 0:10704407da46 414 // read default positions of the joystick to calibrate later readings
el14af 0:10704407da46 415 void calibrateJoystick()
el14af 0:10704407da46 416 {
el14af 0:10704407da46 417 //button.mode(PullDown);
el14af 0:10704407da46 418 // must not move during calibration
el14af 0:10704407da46 419 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el14af 0:10704407da46 420 joystick.y0 = yPot;
el14af 0:10704407da46 421 }
el14af 0:10704407da46 422 // read and update position of the joystick
el14af 0:10704407da46 423 void updateJoystick()
el14af 0:10704407da46 424 {
el14af 0:10704407da46 425 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el14af 0:10704407da46 426 joystick.x = xPot - joystick.x0;
el14af 0:10704407da46 427 joystick.y = yPot - joystick.y0;
el14af 0:10704407da46 428 // read button state
el14af 0:10704407da46 429 //joystick.button = button;
el14af 0:10704407da46 430
el14af 0:10704407da46 431 // calculate direction depending on x,y values
el14af 0:10704407da46 432 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el14af 0:10704407da46 433 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14af 0:10704407da46 434 joystick.direction = CENTRE;
el14af 0:10704407da46 435 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14af 0:10704407da46 436 joystick.direction = UP;
el14af 0:10704407da46 437 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14af 0:10704407da46 438 joystick.direction = DOWN;
el14af 0:10704407da46 439 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14af 0:10704407da46 440 joystick.direction = RIGHT;
el14af 0:10704407da46 441 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14af 0:10704407da46 442 joystick.direction = LEFT;
el14af 0:10704407da46 443 } else {
el14af 0:10704407da46 444 joystick.direction = UNKNOWN;
el14af 0:10704407da46 445 }
el14af 0:10704407da46 446
el14af 0:10704407da46 447 // set flag for printing
el14af 0:10704407da46 448 printFlag = 1;
el14af 0:10704407da46 449 }
el14af 0:10704407da46 450
el14af 0:10704407da46 451 // function for creating border
el14af 0:10704407da46 452 void print_border(int Xstart, int Xend, int Ystart, int Yend)
el14af 0:10704407da46 453 {
el14af 0:10704407da46 454
el14af 0:10704407da46 455 for (int i=Xstart; i<Xend+1; i++) {
el14af 0:10704407da46 456 lcd.setPixel(i,Ystart);
el14af 0:10704407da46 457 lcd.setPixel(i,Yend);
el14af 0:10704407da46 458 }
el14af 0:10704407da46 459 for (int j=Ystart; j<Yend+1; j++) {
el14af 0:10704407da46 460 lcd.setPixel(Xstart,j);
el14af 0:10704407da46 461 lcd.setPixel(Xend,j);
el14af 0:10704407da46 462 }
el14af 0:10704407da46 463 }
el14af 0:10704407da46 464
el14af 0:10704407da46 465 // setup game screen for current level
el14af 0:10704407da46 466 void setup_game()
el14af 0:10704407da46 467 {
el14af 0:10704407da46 468
el14af 0:10704407da46 469 // print ship in the middle of screen pointing up
el14af 0:10704407da46 470
el14af 0:10704407da46 471 // set up default x/y coordinate of the centre pixel of the ship, in the middle
el14af 0:10704407da46 472 // of the display, with the dwfault direction of UP. properties are defined within
el14af 0:10704407da46 473 // the Ship struct.
el14af 0:10704407da46 474 ship.x = 42;
el14af 0:10704407da46 475 ship.y = 35;
el14af 0:10704407da46 476 ship.direction = UP;
el14af 0:10704407da46 477
el14af 0:10704407da46 478 //print ship around the x/y centre coordinate
el14af 0:10704407da46 479
el14af 0:10704407da46 480 //centre
el14af 0:10704407da46 481 lcd.drawLine(ship.x,ship.y-2,ship.x,ship.y+1,1);
el14af 0:10704407da46 482 // centre left
el14af 0:10704407da46 483 lcd.drawLine(ship.x-1,ship.y-1,ship.x-1,ship.y+2,1);
el14af 0:10704407da46 484 // left engine
el14af 0:10704407da46 485 lcd.drawLine(ship.x-2,ship.y+1,ship.x-2,ship.y+2,1);
el14af 0:10704407da46 486 // centre right
el14af 0:10704407da46 487 lcd.drawLine(ship.x+1,ship.y-1,ship.x+1,ship.y+2,1);
el14af 0:10704407da46 488 // right engine
el14af 0:10704407da46 489 lcd.drawLine(ship.x+2,ship.y+1,ship.x+2,ship.y+2,1);
el14af 0:10704407da46 490
el14af 0:10704407da46 491
el14af 0:10704407da46 492 // turret setup
el14af 0:10704407da46 493 // sets the default parameters for the current level
el14af 0:10704407da46 494
el14af 0:10704407da46 495 // top turret setup
el14af 0:10704407da46 496 top_turret.position = 62;
el14af 0:10704407da46 497 top_turret.direction = LEFT;
el14af 0:10704407da46 498
el14af 0:10704407da46 499 // print turret at new position
el14af 0:10704407da46 500 lcd.drawRect(top_turret.position-2,9,4,2,1);
el14af 0:10704407da46 501 lcd.clearPixel(top_turret.position,11);
el14af 0:10704407da46 502
el14af 0:10704407da46 503
el14af 0:10704407da46 504 // bottom turret setup
el14af 0:10704407da46 505 bottom_turret.position = 21;
el14af 0:10704407da46 506 bottom_turret.direction = RIGHT;
el14af 0:10704407da46 507
el14af 0:10704407da46 508 // print turret at new position
el14af 0:10704407da46 509 lcd.drawRect(bottom_turret.position - 2,44,4,2,1);
el14af 0:10704407da46 510 lcd.clearPixel(bottom_turret.position,44);
el14af 0:10704407da46 511
el14af 0:10704407da46 512
el14af 0:10704407da46 513 // left turret setup
el14af 0:10704407da46 514 left_turret.position = 16;
el14af 0:10704407da46 515 left_turret.direction = DOWN;
el14af 0:10704407da46 516
el14af 0:10704407da46 517 // print turret at new position
el14af 0:10704407da46 518 lcd.drawRect(1,left_turret.position-2,2,4,1);
el14af 0:10704407da46 519 lcd.clearPixel(3,left_turret.position);
el14af 0:10704407da46 520
el14af 0:10704407da46 521
el14af 0:10704407da46 522
el14af 0:10704407da46 523 // right turret setup
el14af 0:10704407da46 524 right_turret.position = 31;
el14af 0:10704407da46 525 right_turret.direction = UP;
el14af 0:10704407da46 526
el14af 0:10704407da46 527 // print turret at new position
el14af 0:10704407da46 528 lcd.drawRect(80,right_turret.position-2,2,4,1);
el14af 0:10704407da46 529 lcd.clearPixel(80,right_turret.position);
el14af 0:10704407da46 530
el14af 0:10704407da46 531
el14af 0:10704407da46 532 }
el14af 0:10704407da46 533
el14af 0:10704407da46 534
el14af 0:10704407da46 535 void speed_change()
el14af 0:10704407da46 536 {
el14af 0:10704407da46 537 // increment the ship counter every time the ship moves
el14af 0:10704407da46 538 g_ship_counter ++;
el14af 0:10704407da46 539
el14af 0:10704407da46 540 // if speed button is not pressed and counter number is divisible by 3
el14af 0:10704407da46 541 if (speed_button == 0 && g_ship_counter % 3 == 0) {
el14af 0:10704407da46 542
el14af 0:10704407da46 543 update_ship();
el14af 0:10704407da46 544 }
el14af 0:10704407da46 545 // else if speed_button is pressed
el14af 0:10704407da46 546 else if (speed_button == 1) {
el14af 0:10704407da46 547
el14af 0:10704407da46 548 // update ship with every repetition
el14af 0:10704407da46 549 update_ship();
el14af 0:10704407da46 550
el14af 0:10704407da46 551 }
el14af 0:10704407da46 552 }
el14af 0:10704407da46 553
el14af 0:10704407da46 554 // update ship's position based on jostick controls
el14af 0:10704407da46 555 void update_ship ()
el14af 0:10704407da46 556 {
el14af 0:10704407da46 557
el14af 0:10704407da46 558
el14af 0:10704407da46 559 // clear ship at current position of x/y coordinates. since the ship has symetrical
el14af 0:10704407da46 560
el14af 0:10704407da46 561 lcd.drawRect(ship.x-2,ship.y-2,4,4,2);
el14af 0:10704407da46 562
el14af 0:10704407da46 563
el14af 0:10704407da46 564
el14af 0:10704407da46 565 // update ship direction variable in struct based on position of joystick
el14af 0:10704407da46 566 if (joystick.direction == UP) {
el14af 0:10704407da46 567
el14af 0:10704407da46 568 ship.direction = UP;
el14af 0:10704407da46 569 }
el14af 0:10704407da46 570 if (joystick.direction == DOWN) {
el14af 0:10704407da46 571
el14af 0:10704407da46 572 ship.direction = DOWN;
el14af 0:10704407da46 573 }
el14af 0:10704407da46 574 if (joystick.direction == RIGHT) {
el14af 0:10704407da46 575
el14af 0:10704407da46 576 ship.direction = RIGHT;
el14af 0:10704407da46 577 }
el14af 0:10704407da46 578 if (joystick.direction == LEFT) {
el14af 0:10704407da46 579
el14af 0:10704407da46 580 ship.direction = LEFT;
el14af 0:10704407da46 581 }
el14af 0:10704407da46 582 // don't update position if joystick position is unchanged, or is unknown.
el14af 0:10704407da46 583 // i.e. ship continues to fly in a straight line
el14af 0:10704407da46 584 if (joystick.direction == CENTRE || joystick.direction == UNKNOWN) {
el14af 0:10704407da46 585
el14af 0:10704407da46 586 ship.direction = ship.direction;
el14af 0:10704407da46 587 }
el14af 0:10704407da46 588
el14af 0:10704407da46 589 // Print ship in new position based on the four possible directions that the ship could be traveling in
el14af 0:10704407da46 590 // i.e. UP, DOWN, LEFT or RIGHT. The manitude of the shift of the x/y centre of the ship depends on
el14af 0:10704407da46 591 // the state of the speed_button: shift by 1 pixel for standard movement and by 3 pixels for faster
el14af 0:10704407da46 592 // movement. The newly printed ship points in the direction of travel.
el14af 0:10704407da46 593
el14af 0:10704407da46 594 if (ship.direction == UP) {
el14af 0:10704407da46 595
el14af 0:10704407da46 596 // print ship 1 pixel higher
el14af 0:10704407da46 597 ship.y = ship.y - 1;
el14af 0:10704407da46 598
el14af 0:10704407da46 599 // print new ship
el14af 0:10704407da46 600
el14af 0:10704407da46 601 //centre
el14af 0:10704407da46 602 lcd.drawLine(ship.x,ship.y-2,ship.x,ship.y+1,1);
el14af 0:10704407da46 603 // centre left
el14af 0:10704407da46 604 lcd.drawLine(ship.x-1,ship.y-1,ship.x-1,ship.y+2,1);
el14af 0:10704407da46 605 // left engine
el14af 0:10704407da46 606 lcd.drawLine(ship.x-2,ship.y+1,ship.x-2,ship.y+2,1);
el14af 0:10704407da46 607 // centre right
el14af 0:10704407da46 608 lcd.drawLine(ship.x+1,ship.y-1,ship.x+1,ship.y+2,1);
el14af 0:10704407da46 609 // right engine
el14af 0:10704407da46 610 lcd.drawLine(ship.x+2,ship.y+1,ship.x+2,ship.y+2,1);
el14af 0:10704407da46 611 }
el14af 0:10704407da46 612
el14af 0:10704407da46 613 if (ship.direction == DOWN) {
el14af 0:10704407da46 614
el14af 0:10704407da46 615 // print ship 1 pixel lower
el14af 0:10704407da46 616 ship.y = ship.y + 1;
el14af 0:10704407da46 617
el14af 0:10704407da46 618 //print new ship
el14af 0:10704407da46 619
el14af 0:10704407da46 620 // centre
el14af 0:10704407da46 621 lcd.drawLine(ship.x,ship.y-1,ship.x,ship.y+2,1);
el14af 0:10704407da46 622 // centre left
el14af 0:10704407da46 623 lcd.drawLine(ship.x-1,ship.y+1,ship.x-1,ship.y-2,1);
el14af 0:10704407da46 624 // left engine
el14af 0:10704407da46 625 lcd.drawLine(ship.x-2,ship.y-1,ship.x-2,ship.y-2,1);
el14af 0:10704407da46 626 // centre right
el14af 0:10704407da46 627 lcd.drawLine(ship.x+1,ship.y+1,ship.x+1,ship.y-2,1);
el14af 0:10704407da46 628 // right engine
el14af 0:10704407da46 629 lcd.drawLine(ship.x+2,ship.y-1,ship.x+2,ship.y-2,1);
el14af 0:10704407da46 630
el14af 0:10704407da46 631 }
el14af 0:10704407da46 632
el14af 0:10704407da46 633 if (ship.direction == LEFT) {
el14af 0:10704407da46 634
el14af 0:10704407da46 635 // print ship 1 pixel to the left
el14af 0:10704407da46 636 ship.x = ship.x + 1;
el14af 0:10704407da46 637
el14af 0:10704407da46 638 //print new ship
el14af 0:10704407da46 639
el14af 0:10704407da46 640 // centre
el14af 0:10704407da46 641 lcd.drawLine(ship.x-1,ship.y,ship.x+2,ship.y,1);
el14af 0:10704407da46 642 // upper centre
el14af 0:10704407da46 643 lcd.drawLine(ship.x-2,ship.y-1,ship.x+1,ship.y-1,1);
el14af 0:10704407da46 644 // top engine
el14af 0:10704407da46 645 lcd.drawLine(ship.x-2,ship.y-2,ship.x-1,ship.y-2,1);
el14af 0:10704407da46 646 // lower centre
el14af 0:10704407da46 647 lcd.drawLine(ship.x-2,ship.y+1,ship.x+1,ship.y+1,1);
el14af 0:10704407da46 648 // bottom engine
el14af 0:10704407da46 649 lcd.drawLine(ship.x-2,ship.y+2,ship.x-1,ship.y+2,1);
el14af 0:10704407da46 650
el14af 0:10704407da46 651 }
el14af 0:10704407da46 652
el14af 0:10704407da46 653 if (ship.direction == RIGHT) {
el14af 0:10704407da46 654
el14af 0:10704407da46 655 // print ship 1 pixel right
el14af 0:10704407da46 656 ship.x = ship.x - 1;
el14af 0:10704407da46 657
el14af 0:10704407da46 658
el14af 0:10704407da46 659 //print new ship
el14af 0:10704407da46 660
el14af 0:10704407da46 661 // centre
el14af 0:10704407da46 662 lcd.drawLine(ship.x-2,ship.y,ship.x+1,ship.y,1);
el14af 0:10704407da46 663 // upper centre
el14af 0:10704407da46 664 lcd.drawLine(ship.x-1,ship.y-1,ship.x+2,ship.y-1,1);
el14af 0:10704407da46 665 // top engine
el14af 0:10704407da46 666 lcd.drawLine(ship.x+1,ship.y-2,ship.x+2,ship.y-2,1);
el14af 0:10704407da46 667 // lower centre
el14af 0:10704407da46 668 lcd.drawLine(ship.x-1,ship.y+1,ship.x+2,ship.y+1,1);
el14af 0:10704407da46 669 // bottom engine
el14af 0:10704407da46 670 lcd.drawLine(ship.x+1,ship.y+2,ship.x+2,ship.y+2,1);
el14af 0:10704407da46 671
el14af 0:10704407da46 672 }
el14af 0:10704407da46 673 // limit ship position to the game borders:
el14af 0:10704407da46 674 // X MIN
el14af 0:10704407da46 675 if (ship.x < 2) {
el14af 0:10704407da46 676 ship.x = 2;
el14af 0:10704407da46 677 }
el14af 0:10704407da46 678 // X MAX
el14af 0:10704407da46 679 if (ship.x > 81) {
el14af 0:10704407da46 680 ship.x = 81;
el14af 0:10704407da46 681 }
el14af 0:10704407da46 682 // Y MIN
el14af 0:10704407da46 683 if (ship.y < 10) {
el14af 0:10704407da46 684 ship.y = 10;
el14af 0:10704407da46 685 }
el14af 0:10704407da46 686 // Y MAX
el14af 0:10704407da46 687 if (ship.y > 45) {
el14af 0:10704407da46 688 ship.y = 45;
el14af 0:10704407da46 689 }
el14af 0:10704407da46 690 }
el14af 0:10704407da46 691
el14af 0:10704407da46 692 // update position of top turret
el14af 0:10704407da46 693 void update_turrets ()
el14af 0:10704407da46 694 {
el14af 0:10704407da46 695
el14af 0:10704407da46 696 // update top turret
el14af 0:10704407da46 697
el14af 0:10704407da46 698 // clear turret at current position
el14af 0:10704407da46 699 lcd.drawRect(top_turret.position-2,9,4,2,2);
el14af 0:10704407da46 700
el14af 0:10704407da46 701 // if the turret has reached the screen boundry, change the direction
el14af 0:10704407da46 702 if (top_turret.position == 3) {
el14af 0:10704407da46 703 top_turret.direction = RIGHT;
el14af 0:10704407da46 704 }
el14af 0:10704407da46 705 if (top_turret.position == 80) {
el14af 0:10704407da46 706 top_turret.direction = LEFT;
el14af 0:10704407da46 707 }
el14af 0:10704407da46 708
el14af 0:10704407da46 709 // shift the position of the centre pixel of the turret in the dirction of movement
el14af 0:10704407da46 710 if (top_turret.direction == LEFT) {
el14af 0:10704407da46 711 top_turret.position -- ;
el14af 0:10704407da46 712 } else if (top_turret.direction == RIGHT) {
el14af 0:10704407da46 713 top_turret.position ++ ;
el14af 0:10704407da46 714 }
el14af 0:10704407da46 715
el14af 0:10704407da46 716 // print turret at new position
el14af 0:10704407da46 717 lcd.drawRect(top_turret.position-2,9,4,2,1);
el14af 0:10704407da46 718 lcd.clearPixel(top_turret.position,11);
el14af 0:10704407da46 719
el14af 0:10704407da46 720
el14af 0:10704407da46 721
el14af 0:10704407da46 722 //update bottom turret
el14af 0:10704407da46 723
el14af 0:10704407da46 724 //clear turret at current position
el14af 0:10704407da46 725 lcd.drawRect(bottom_turret.position - 2,44,4,2,2);
el14af 0:10704407da46 726
el14af 0:10704407da46 727 // if the turret has reached the screen boundry, change the direction
el14af 0:10704407da46 728 if (bottom_turret.position == 3) {
el14af 0:10704407da46 729 bottom_turret.direction = RIGHT;
el14af 0:10704407da46 730 }
el14af 0:10704407da46 731 if (bottom_turret.position == 80) {
el14af 0:10704407da46 732 bottom_turret.direction = LEFT;
el14af 0:10704407da46 733 }
el14af 0:10704407da46 734
el14af 0:10704407da46 735 // shift the position of the centre pixel of the turret in the dirction of movement
el14af 0:10704407da46 736 if (bottom_turret.direction == LEFT) {
el14af 0:10704407da46 737 bottom_turret.position -- ;
el14af 0:10704407da46 738 } else if(bottom_turret.direction == RIGHT) {
el14af 0:10704407da46 739 bottom_turret.position ++ ;
el14af 0:10704407da46 740 }
el14af 0:10704407da46 741
el14af 0:10704407da46 742 //print turret at new position
el14af 0:10704407da46 743 lcd.drawRect(bottom_turret.position - 2,44,4,2,1);
el14af 0:10704407da46 744 lcd.clearPixel(bottom_turret.position,44);
el14af 0:10704407da46 745
el14af 0:10704407da46 746
el14af 0:10704407da46 747
el14af 0:10704407da46 748 // update left turret
el14af 0:10704407da46 749
el14af 0:10704407da46 750 // clear turret at current position
el14af 0:10704407da46 751 lcd.drawRect(1,left_turret.position-2,2,4,2);
el14af 0:10704407da46 752
el14af 0:10704407da46 753 // if the turret has reached the screen boundry, change the direction
el14af 0:10704407da46 754
el14af 0:10704407da46 755 if (left_turret.position == 10) {
el14af 0:10704407da46 756 left_turret.direction = DOWN;
el14af 0:10704407da46 757 }
el14af 0:10704407da46 758 if (left_turret.position == 44) {
el14af 0:10704407da46 759 left_turret.direction = UP;
el14af 0:10704407da46 760 }
el14af 0:10704407da46 761
el14af 0:10704407da46 762 // shift the position of the centre pixel of the turret in the dirction of movement
el14af 0:10704407da46 763 if (left_turret.direction == UP) {
el14af 0:10704407da46 764 left_turret.position -- ;
el14af 0:10704407da46 765 } else if (left_turret.direction == DOWN) {
el14af 0:10704407da46 766 left_turret.position ++ ;
el14af 0:10704407da46 767 }
el14af 0:10704407da46 768
el14af 0:10704407da46 769 // print turret at new position
el14af 0:10704407da46 770 lcd.drawRect(1,left_turret.position-2,2,4,1);
el14af 0:10704407da46 771 lcd.clearPixel(3,left_turret.position);
el14af 0:10704407da46 772
el14af 0:10704407da46 773
el14af 0:10704407da46 774
el14af 0:10704407da46 775 // update right turret
el14af 0:10704407da46 776
el14af 0:10704407da46 777 // clear turret at current position
el14af 0:10704407da46 778 lcd.drawRect(80,right_turret.position-2,2,4,2);
el14af 0:10704407da46 779
el14af 0:10704407da46 780 // if the turret has reached the screen boundry, change the direction
el14af 0:10704407da46 781
el14af 0:10704407da46 782 if (right_turret.position == 10) {
el14af 0:10704407da46 783 right_turret.direction = DOWN;
el14af 0:10704407da46 784 }
el14af 0:10704407da46 785 if (right_turret.position == 44) {
el14af 0:10704407da46 786 right_turret.direction = UP;
el14af 0:10704407da46 787 }
el14af 0:10704407da46 788
el14af 0:10704407da46 789 // shift the position of the centre pixel of the turret in the dirction of movement
el14af 0:10704407da46 790 if (right_turret.direction == UP) {
el14af 0:10704407da46 791 right_turret.position -- ;
el14af 0:10704407da46 792 } else if (right_turret.direction == DOWN) {
el14af 0:10704407da46 793 right_turret.position ++ ;
el14af 0:10704407da46 794 }
el14af 0:10704407da46 795
el14af 0:10704407da46 796 // print turret at new position
el14af 0:10704407da46 797 lcd.drawRect(80,right_turret.position-2,2,4,1);
el14af 0:10704407da46 798 lcd.clearPixel(80,right_turret.position);
el14af 0:10704407da46 799
el14af 0:10704407da46 800
el14af 0:10704407da46 801 lcd.refresh();
el14af 0:10704407da46 802
el14af 0:10704407da46 803 }
el14af 0:10704407da46 804
el14af 0:10704407da46 805 //function for firing missles
el14af 0:10704407da46 806 void shoot()
el14af 0:10704407da46 807 {
el14af 0:10704407da46 808
el14af 0:10704407da46 809 // generates a pseudo-random value between 0 and 99 for each turret.
el14af 0:10704407da46 810 srand(time(NULL));
el14af 0:10704407da46 811 int top_shoot = rand() %100+1;
el14af 0:10704407da46 812 int bottom_shoot = rand() %100+1;
el14af 0:10704407da46 813 int left_shoot = rand() %100+1;
el14af 0:10704407da46 814 int right_shoot = rand() %100+1;
el14af 0:10704407da46 815
el14af 0:10704407da46 816 // 20% chance for each turret to fire a missle. if random value is less than 20 the turret fires
el14af 0:10704407da46 817 // and the turret's shoot property is set to 1. the process is the same for each of the 4 turrets.
el14af 0:10704407da46 818 if (top_shoot < 20 ) {
el14af 0:10704407da46 819
el14af 0:10704407da46 820 //change shoot varialbe in turret struct
el14af 0:10704407da46 821 top_turret.shoot = 1;
el14af 0:10704407da46 822
el14af 0:10704407da46 823 // search for an inactive missle struct to assign the missle to. if none of the missle
el14af 0:10704407da46 824 // struct as available, i.e. there are already 6 missles on screen, the turret does not
el14af 0:10704407da46 825 // fire.
el14af 0:10704407da46 826 if (!missle_one.active) { // if missle is available
el14af 0:10704407da46 827 missle_one.active = 1; // set missle to active
el14af 0:10704407da46 828 missle_one.x = top_turret.position; // set the x-axis position of the missle equal to the turret's position
el14af 0:10704407da46 829 missle_one.y = 10; // the Y coordinate is set to 8 to account for game border and turret
el14af 0:10704407da46 830 missle_one.direction = DOWN; // set the direction the missle should travel in
el14af 0:10704407da46 831 }
el14af 0:10704407da46 832 // steps are repeated in case of other missle structs being used
el14af 0:10704407da46 833 else if (!missle_two.active) {
el14af 0:10704407da46 834 missle_two.active = 1;
el14af 0:10704407da46 835 missle_two.x = top_turret.position;
el14af 0:10704407da46 836 missle_two.y = 10;
el14af 0:10704407da46 837 missle_two.direction = DOWN;
el14af 0:10704407da46 838 } else if (!missle_three.active) {
el14af 0:10704407da46 839 missle_three.active = 1;
el14af 0:10704407da46 840 missle_three.x = top_turret.position;
el14af 0:10704407da46 841 missle_three.y = 10;
el14af 0:10704407da46 842 missle_three.direction = DOWN;
el14af 0:10704407da46 843 } else if (!missle_four.active) {
el14af 0:10704407da46 844 missle_four.active = 1;
el14af 0:10704407da46 845 missle_four.x = top_turret.position;
el14af 0:10704407da46 846 missle_four.y = 10;
el14af 0:10704407da46 847 missle_four.direction = DOWN;
el14af 0:10704407da46 848 } else if (!missle_five.active) {
el14af 0:10704407da46 849 missle_five.active = 1;
el14af 0:10704407da46 850 missle_five.x = top_turret.position;
el14af 0:10704407da46 851 missle_five.y = 10;
el14af 0:10704407da46 852 missle_five.direction = DOWN;
el14af 0:10704407da46 853 } else if (!missle_six.active) {
el14af 0:10704407da46 854 missle_six.active = 1;
el14af 0:10704407da46 855 missle_six.x = top_turret.position;
el14af 0:10704407da46 856 missle_six.y = 10;
el14af 0:10704407da46 857 missle_six.direction = DOWN;
el14af 0:10704407da46 858 } else {
el14af 0:10704407da46 859 top_turret.shoot = 0;
el14af 0:10704407da46 860 }
el14af 0:10704407da46 861
el14af 0:10704407da46 862 }
el14af 0:10704407da46 863 // whole process is identical for the other three turrets. the only changes are the direction assigned
el14af 0:10704407da46 864 // to the missle when it fires, and whether the position of the turret dictates the x or y starting
el14af 0:10704407da46 865 // position of the missle
el14af 0:10704407da46 866
el14af 0:10704407da46 867 // bottom turret
el14af 0:10704407da46 868 if (bottom_shoot < 20 ) {
el14af 0:10704407da46 869
el14af 0:10704407da46 870 bottom_turret.shoot = 1;
el14af 0:10704407da46 871
el14af 0:10704407da46 872 if (!missle_one.active) { // if missle is available
el14af 0:10704407da46 873 missle_one.active = 1; // set missle to active
el14af 0:10704407da46 874 missle_one.x = bottom_turret.position; // set the X-axis position of the missle equal to the turret's position
el14af 0:10704407da46 875 missle_one.y = 45; // the Y coordinate is set to 45 to account for game border and turret
el14af 0:10704407da46 876 missle_one.direction = UP; // set the direction the missle should travel in
el14af 0:10704407da46 877 }
el14af 0:10704407da46 878 // steps are repeated in case of other missle structs being used
el14af 0:10704407da46 879 else if (!missle_two.active) {
el14af 0:10704407da46 880 missle_two.active = 1;
el14af 0:10704407da46 881 missle_two.x = bottom_turret.position;
el14af 0:10704407da46 882 missle_two.y = 45;
el14af 0:10704407da46 883 missle_two.direction = UP;
el14af 0:10704407da46 884 } else if (!missle_three.active) {
el14af 0:10704407da46 885 missle_three.active = 1;
el14af 0:10704407da46 886 missle_three.x = bottom_turret.position;
el14af 0:10704407da46 887 missle_three.y = 45;
el14af 0:10704407da46 888 missle_three.direction = UP;
el14af 0:10704407da46 889 } else if (!missle_four.active) {
el14af 0:10704407da46 890 missle_four.active = 1;
el14af 0:10704407da46 891 missle_four.x = bottom_turret.position;
el14af 0:10704407da46 892 missle_four.y = 45;
el14af 0:10704407da46 893 missle_four.direction = UP;
el14af 0:10704407da46 894 } else if (!missle_five.active) {
el14af 0:10704407da46 895 missle_five.active = 1;
el14af 0:10704407da46 896 missle_five.x = bottom_turret.position;
el14af 0:10704407da46 897 missle_five.y = 45;
el14af 0:10704407da46 898 missle_five.direction = UP;
el14af 0:10704407da46 899 } else if (!missle_six.active) {
el14af 0:10704407da46 900 missle_six.active = 1;
el14af 0:10704407da46 901 missle_six.x = bottom_turret.position;
el14af 0:10704407da46 902 missle_six.y = 45;
el14af 0:10704407da46 903 missle_six.direction = UP;
el14af 0:10704407da46 904 } else {
el14af 0:10704407da46 905 bottom_turret.shoot = 0;
el14af 0:10704407da46 906 }
el14af 0:10704407da46 907
el14af 0:10704407da46 908 }
el14af 0:10704407da46 909 // left turret
el14af 0:10704407da46 910 if (left_shoot < 20 ) {
el14af 0:10704407da46 911
el14af 0:10704407da46 912 left_turret.shoot = 1;
el14af 0:10704407da46 913
el14af 0:10704407da46 914 if (!missle_one.active) {
el14af 0:10704407da46 915 missle_one.active = 1;
el14af 0:10704407da46 916 missle_one.x = 3; // the X coordinate is set to 3 to account for game border and turret
el14af 0:10704407da46 917 missle_two.y = left_turret.position; // set the Y-axis position of the missle equal to the turret's position
el14af 0:10704407da46 918
el14af 0:10704407da46 919 missle_one.direction = RIGHT;
el14af 0:10704407da46 920 } else if (!missle_two.active) {
el14af 0:10704407da46 921 missle_two.active = 1;
el14af 0:10704407da46 922 missle_two.x = 3;
el14af 0:10704407da46 923 missle_two.y = left_turret.position;
el14af 0:10704407da46 924 missle_two.direction = RIGHT;
el14af 0:10704407da46 925 } else if (!missle_three.active) {
el14af 0:10704407da46 926 missle_three.active = 1;
el14af 0:10704407da46 927 missle_three.x = 3;
el14af 0:10704407da46 928 missle_three.y = left_turret.position;
el14af 0:10704407da46 929 missle_three.direction = RIGHT;
el14af 0:10704407da46 930 } else if (!missle_four.active) {
el14af 0:10704407da46 931 missle_four.active = 1;
el14af 0:10704407da46 932 missle_four.x = 3;
el14af 0:10704407da46 933 missle_four.y = left_turret.position;
el14af 0:10704407da46 934 missle_four.direction = RIGHT;
el14af 0:10704407da46 935 } else if (!missle_five.active) {
el14af 0:10704407da46 936 missle_five.active = 1;
el14af 0:10704407da46 937 missle_five.x = 3;
el14af 0:10704407da46 938 missle_five.y = left_turret.position;
el14af 0:10704407da46 939 missle_five.direction = RIGHT;
el14af 0:10704407da46 940 } else if (!missle_six.active) {
el14af 0:10704407da46 941 missle_six.active = 1;
el14af 0:10704407da46 942 missle_six.x = 3;
el14af 0:10704407da46 943 missle_six.y = left_turret.position;
el14af 0:10704407da46 944 missle_six.direction = RIGHT;
el14af 0:10704407da46 945 } else {
el14af 0:10704407da46 946 left_turret.shoot = 0;
el14af 0:10704407da46 947 }
el14af 0:10704407da46 948
el14af 0:10704407da46 949 }
el14af 0:10704407da46 950 // right turret
el14af 0:10704407da46 951 if (right_shoot < 20 ) {
el14af 0:10704407da46 952
el14af 0:10704407da46 953 left_turret.shoot = 1;
el14af 0:10704407da46 954
el14af 0:10704407da46 955 if (!missle_one.active) {
el14af 0:10704407da46 956 missle_one.active = 1;
el14af 0:10704407da46 957 missle_one.x = 81; // the X coordinate is set to 81 to account for game border and turret
el14af 0:10704407da46 958 missle_one.y = right_turret.position; // set the Y-axis position of the missle equal to the turret's position
el14af 0:10704407da46 959 missle_one.direction = LEFT;
el14af 0:10704407da46 960 } else if (!missle_two.active) {
el14af 0:10704407da46 961 missle_two.active = 1;
el14af 0:10704407da46 962 missle_two.x = 81;
el14af 0:10704407da46 963 missle_two.y = right_turret.position;
el14af 0:10704407da46 964 missle_two.direction = LEFT;
el14af 0:10704407da46 965 } else if (!missle_three.active) {
el14af 0:10704407da46 966 missle_three.active = 1;
el14af 0:10704407da46 967 missle_three.x = 81;
el14af 0:10704407da46 968 missle_three.y = right_turret.position;
el14af 0:10704407da46 969 missle_three.direction = LEFT;
el14af 0:10704407da46 970 } else if (!missle_four.active) {
el14af 0:10704407da46 971 missle_four.active = 1;
el14af 0:10704407da46 972 missle_four.x = 81;
el14af 0:10704407da46 973 missle_four.y = right_turret.position;
el14af 0:10704407da46 974 missle_four.direction = LEFT;
el14af 0:10704407da46 975 } else if (!missle_five.active) {
el14af 0:10704407da46 976 missle_five.active = 1;
el14af 0:10704407da46 977 missle_five.x = 81;
el14af 0:10704407da46 978 missle_five.y = right_turret.position;
el14af 0:10704407da46 979 missle_five.direction = LEFT;
el14af 0:10704407da46 980 } else if (!missle_six.active) {
el14af 0:10704407da46 981 missle_six.active = 1;
el14af 0:10704407da46 982 missle_six.x = 81;
el14af 0:10704407da46 983 missle_six.y = right_turret.position;
el14af 0:10704407da46 984 missle_six.direction = LEFT;
el14af 0:10704407da46 985 } else {
el14af 0:10704407da46 986 right_turret.shoot = 0;
el14af 0:10704407da46 987 }
el14af 0:10704407da46 988
el14af 0:10704407da46 989 }
el14af 0:10704407da46 990
el14af 0:10704407da46 991 }
el14af 0:10704407da46 992
el14af 0:10704407da46 993 // track position of currently flying missles, update their position and detect hits.
el14af 0:10704407da46 994 void update_missles ()
el14af 0:10704407da46 995 {
el14af 0:10704407da46 996 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 997 if (missle_one.active) {
el14af 0:10704407da46 998
el14af 0:10704407da46 999 //clear missle at current position
el14af 0:10704407da46 1000 // centre
el14af 0:10704407da46 1001 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1002
el14af 0:10704407da46 1003 // set new position of missle based on direction of travel
el14af 0:10704407da46 1004 if (missle_one.direction == UP) {
el14af 0:10704407da46 1005 missle_one.y -- ;
el14af 0:10704407da46 1006 }
el14af 0:10704407da46 1007 if (missle_one.direction == DOWN) {
el14af 0:10704407da46 1008 missle_one.y ++ ;
el14af 0:10704407da46 1009 }
el14af 0:10704407da46 1010 if (missle_one.direction == LEFT) {
el14af 0:10704407da46 1011 missle_one.x -- ;
el14af 0:10704407da46 1012 }
el14af 0:10704407da46 1013 if (missle_one.direction == RIGHT) {
el14af 0:10704407da46 1014 missle_one.x ++ ;
el14af 0:10704407da46 1015 }
el14af 0:10704407da46 1016
el14af 0:10704407da46 1017 //draw missle at new position
el14af 0:10704407da46 1018 lcd.drawLine(missle_one.x - 1,missle_one.y,missle_one.x + 1,missle_one.y,1);
el14af 0:10704407da46 1019 lcd.drawLine(missle_one.x,missle_one.y - 1,missle_one.x,missle_one.y + 1,1);
el14af 0:10704407da46 1020
el14af 0:10704407da46 1021
el14af 0:10704407da46 1022 }
el14af 0:10704407da46 1023
el14af 0:10704407da46 1024 // the function then repeats process for any other active missles
el14af 0:10704407da46 1025
el14af 0:10704407da46 1026 if (missle_two.active) {
el14af 0:10704407da46 1027 //clear missle at current position
el14af 0:10704407da46 1028 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1029
el14af 0:10704407da46 1030 // set new position of missle based on direction of travel
el14af 0:10704407da46 1031 if (missle_two.direction == UP) {
el14af 0:10704407da46 1032 missle_two.y -- ;
el14af 0:10704407da46 1033 }
el14af 0:10704407da46 1034 if (missle_two.direction == DOWN) {
el14af 0:10704407da46 1035 missle_two.y ++ ;
el14af 0:10704407da46 1036 }
el14af 0:10704407da46 1037 if (missle_two.direction == LEFT) {
el14af 0:10704407da46 1038 missle_two.x -- ;
el14af 0:10704407da46 1039 }
el14af 0:10704407da46 1040 if (missle_two.direction == RIGHT) {
el14af 0:10704407da46 1041 missle_two.x ++ ;
el14af 0:10704407da46 1042 }
el14af 0:10704407da46 1043
el14af 0:10704407da46 1044 //draw missle at new position
el14af 0:10704407da46 1045 lcd.drawLine(missle_two.x - 1,missle_two.y,missle_two.x + 1,missle_two.y,1);
el14af 0:10704407da46 1046 lcd.drawLine(missle_two.x,missle_two.y - 1,missle_two.x,missle_two.y + 1,1);
el14af 0:10704407da46 1047
el14af 0:10704407da46 1048
el14af 0:10704407da46 1049 }
el14af 0:10704407da46 1050 if (missle_three.active) {
el14af 0:10704407da46 1051 //clear missle at current position
el14af 0:10704407da46 1052 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1053
el14af 0:10704407da46 1054 // set new position of missle based on direction of travel
el14af 0:10704407da46 1055 if (missle_three.direction == UP) {
el14af 0:10704407da46 1056 missle_three.y -- ;
el14af 0:10704407da46 1057 }
el14af 0:10704407da46 1058 if (missle_three.direction == DOWN) {
el14af 0:10704407da46 1059 missle_three.y ++ ;
el14af 0:10704407da46 1060 }
el14af 0:10704407da46 1061 if (missle_three.direction == LEFT) {
el14af 0:10704407da46 1062 missle_three.x -- ;
el14af 0:10704407da46 1063 }
el14af 0:10704407da46 1064 if (missle_three.direction == RIGHT) {
el14af 0:10704407da46 1065 missle_three.x ++ ;
el14af 0:10704407da46 1066 }
el14af 0:10704407da46 1067
el14af 0:10704407da46 1068 //draw missle at new position
el14af 0:10704407da46 1069 lcd.drawLine(missle_three.x - 1,missle_three.y,missle_three.x + 1,missle_three.y,1);
el14af 0:10704407da46 1070 lcd.drawLine(missle_three.x,missle_three.y - 1,missle_three.x,missle_three.y + 1,1);
el14af 0:10704407da46 1071
el14af 0:10704407da46 1072 }
el14af 0:10704407da46 1073 if (missle_four.active) {
el14af 0:10704407da46 1074 //clear missle at current position
el14af 0:10704407da46 1075 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1076
el14af 0:10704407da46 1077 // set new position of missle based on direction of travel
el14af 0:10704407da46 1078 if (missle_four.direction == UP) {
el14af 0:10704407da46 1079 missle_four.y -- ;
el14af 0:10704407da46 1080 }
el14af 0:10704407da46 1081 if (missle_four.direction == DOWN) {
el14af 0:10704407da46 1082 missle_four.y ++ ;
el14af 0:10704407da46 1083 }
el14af 0:10704407da46 1084 if (missle_four.direction == LEFT) {
el14af 0:10704407da46 1085 missle_four.x -- ;
el14af 0:10704407da46 1086 }
el14af 0:10704407da46 1087 if (missle_four.direction == RIGHT) {
el14af 0:10704407da46 1088 missle_four.x ++ ;
el14af 0:10704407da46 1089 }
el14af 0:10704407da46 1090
el14af 0:10704407da46 1091 //draw missle at new position
el14af 0:10704407da46 1092 lcd.drawLine(missle_four.x - 1,missle_four.y,missle_four.x + 1,missle_four.y,1);
el14af 0:10704407da46 1093 lcd.drawLine(missle_four.x,missle_four.y - 1,missle_four.x,missle_four.y + 1,1);
el14af 0:10704407da46 1094
el14af 0:10704407da46 1095 }
el14af 0:10704407da46 1096 if (missle_five.active) {
el14af 0:10704407da46 1097 //clear missle at current position
el14af 0:10704407da46 1098 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1099
el14af 0:10704407da46 1100 // set new position of missle based on direction of travel
el14af 0:10704407da46 1101 if (missle_five.direction == UP) {
el14af 0:10704407da46 1102 missle_five.y -- ;
el14af 0:10704407da46 1103 }
el14af 0:10704407da46 1104 if (missle_five.direction == DOWN) {
el14af 0:10704407da46 1105 missle_five.y ++ ;
el14af 0:10704407da46 1106 }
el14af 0:10704407da46 1107 if (missle_five.direction == LEFT) {
el14af 0:10704407da46 1108 missle_five.x -- ;
el14af 0:10704407da46 1109 }
el14af 0:10704407da46 1110 if (missle_five.direction == RIGHT) {
el14af 0:10704407da46 1111 missle_five.x ++ ;
el14af 0:10704407da46 1112 }
el14af 0:10704407da46 1113
el14af 0:10704407da46 1114 //draw missle at new position
el14af 0:10704407da46 1115 lcd.drawLine(missle_five.x - 1,missle_five.y,missle_five.x + 1,missle_five.y,1);
el14af 0:10704407da46 1116 lcd.drawLine(missle_five.x,missle_five.y - 1,missle_five.x,missle_five.y + 1,1);
el14af 0:10704407da46 1117
el14af 0:10704407da46 1118
el14af 0:10704407da46 1119 }
el14af 0:10704407da46 1120 if (missle_six.active) {
el14af 0:10704407da46 1121 //clear missle at current position
el14af 0:10704407da46 1122 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1123
el14af 0:10704407da46 1124 // set new position of missle based on direction of travel
el14af 0:10704407da46 1125 if (missle_six.direction == UP) {
el14af 0:10704407da46 1126 missle_six.y -- ;
el14af 0:10704407da46 1127 }
el14af 0:10704407da46 1128 if (missle_six.direction == DOWN) {
el14af 0:10704407da46 1129 missle_six.y ++ ;
el14af 0:10704407da46 1130 }
el14af 0:10704407da46 1131 if (missle_six.direction == LEFT) {
el14af 0:10704407da46 1132 missle_six.x -- ;
el14af 0:10704407da46 1133 }
el14af 0:10704407da46 1134 if (missle_six.direction == RIGHT) {
el14af 0:10704407da46 1135 missle_six.x ++ ;
el14af 0:10704407da46 1136 }
el14af 0:10704407da46 1137
el14af 0:10704407da46 1138 //draw missle at new position
el14af 0:10704407da46 1139 lcd.drawLine(missle_six.x - 1,missle_six.y,missle_six.x + 1,missle_six.y,1);
el14af 0:10704407da46 1140 lcd.drawLine(missle_six.x,missle_six.y - 1,missle_six.x,missle_six.y + 1,1);
el14af 0:10704407da46 1141
el14af 0:10704407da46 1142
el14af 0:10704407da46 1143
el14af 0:10704407da46 1144 }
el14af 0:10704407da46 1145 }
el14af 0:10704407da46 1146
el14af 0:10704407da46 1147 // detect hits missle expires
el14af 0:10704407da46 1148 void check_hit ()
el14af 0:10704407da46 1149 {
el14af 0:10704407da46 1150 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1151 if (missle_one.active) {
el14af 0:10704407da46 1152
el14af 0:10704407da46 1153
el14af 0:10704407da46 1154 if (ship.direction == UP) {
el14af 0:10704407da46 1155 // check if the missle is within the x_axis constraints of the hit box
el14af 0:10704407da46 1156 // the structure : if missle x coordinate is greater than the top right pixel of hit box...
el14af 0:10704407da46 1157 // AND
el14af 0:10704407da46 1158 // missle x coordinate is less than top right pixel of hit box + width of the hit box...
el14af 0:10704407da46 1159 // then the missle is in the x constraints.
el14af 0:10704407da46 1160 if ( (missle_one.x > ship.x - 2 ) && (missle_one.x < ship.x + 2) ) {
el14af 0:10704407da46 1161 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1162 if ( (missle_one.y > ship.y - 2 ) && (missle_one.y < ship.y + 3) ) {
el14af 0:10704407da46 1163 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1164 missle_one.active = 0;
el14af 0:10704407da46 1165 // set hit flag for missle 1.
el14af 0:10704407da46 1166 missle_one.hit_flag = 1;
el14af 0:10704407da46 1167 //clear missle
el14af 0:10704407da46 1168 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1169 }
el14af 0:10704407da46 1170 }
el14af 0:10704407da46 1171 }
el14af 0:10704407da46 1172 // the process is repeated for the different directions of travel, with a different hitbox shape:
el14af 0:10704407da46 1173 // down
el14af 0:10704407da46 1174 if (ship.direction == DOWN) {
el14af 0:10704407da46 1175 if ( (missle_one.x > ship.x - 2 ) && (missle_one.x < ship.x + 2 ) ) {
el14af 0:10704407da46 1176 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1177 if ( (missle_one.y > ship.y - 3 ) && (missle_one.y < ship.y + 2) ) {
el14af 0:10704407da46 1178 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1179 missle_one.active = 0;
el14af 0:10704407da46 1180 // set hit flag for missle 1.
el14af 0:10704407da46 1181 missle_one.hit_flag = 1;
el14af 0:10704407da46 1182 //clear missle
el14af 0:10704407da46 1183 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1184 }
el14af 0:10704407da46 1185 }
el14af 0:10704407da46 1186 }
el14af 0:10704407da46 1187 // left
el14af 0:10704407da46 1188 if (ship.direction == LEFT) {
el14af 0:10704407da46 1189 if ( (missle_one.x > ship.x - 3 ) && (missle_one.x < ship.x + 2) ) {
el14af 0:10704407da46 1190 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1191 if ( (missle_one.y > ship.y - 2 ) && (missle_one.y < ship.y + 2) ) {
el14af 0:10704407da46 1192 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1193 missle_one.active = 0;
el14af 0:10704407da46 1194 // set hit flag for missle 1.
el14af 0:10704407da46 1195 missle_one.hit_flag = 1;
el14af 0:10704407da46 1196 //clear missle
el14af 0:10704407da46 1197 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1198 }
el14af 0:10704407da46 1199 }
el14af 0:10704407da46 1200 }
el14af 0:10704407da46 1201 // right
el14af 0:10704407da46 1202 if (ship.direction == RIGHT) {
el14af 0:10704407da46 1203 if ( (missle_one.x > ship.x - 2 ) && (missle_one.x < ship.x + 3) ) {
el14af 0:10704407da46 1204 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1205 if ( (missle_one.y > ship.y - 2 ) && (missle_one.y < ship.y + 2) ) {
el14af 0:10704407da46 1206 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1207 missle_one.active = 0;
el14af 0:10704407da46 1208 // set hit flag for missle 1.
el14af 0:10704407da46 1209 missle_one.hit_flag = 1;
el14af 0:10704407da46 1210 //clear missle
el14af 0:10704407da46 1211 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1212 }
el14af 0:10704407da46 1213 }
el14af 0:10704407da46 1214 }
el14af 0:10704407da46 1215
el14af 0:10704407da46 1216 // detect missle expires
el14af 0:10704407da46 1217 // depending on the direction of flight, if the missle reaches the border without coming in contact with the ship.
el14af 0:10704407da46 1218 // the missle is set to inactive
el14af 0:10704407da46 1219
el14af 0:10704407da46 1220 if (missle_one.direction == UP) {
el14af 0:10704407da46 1221 if (missle_one.y == 10) {
el14af 0:10704407da46 1222 missle_one.active = 0;
el14af 0:10704407da46 1223
el14af 0:10704407da46 1224 //clear missle
el14af 0:10704407da46 1225 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1226 }
el14af 0:10704407da46 1227
el14af 0:10704407da46 1228 }
el14af 0:10704407da46 1229 if (missle_one.direction == DOWN) {
el14af 0:10704407da46 1230 if (missle_one.y == 45) {
el14af 0:10704407da46 1231 missle_one.active = 0;
el14af 0:10704407da46 1232
el14af 0:10704407da46 1233 //clear missle
el14af 0:10704407da46 1234 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1235 }
el14af 0:10704407da46 1236
el14af 0:10704407da46 1237 }
el14af 0:10704407da46 1238 if (missle_one.direction == LEFT) {
el14af 0:10704407da46 1239 if (missle_one.x == 2 ) {
el14af 0:10704407da46 1240 missle_one.active = 0;
el14af 0:10704407da46 1241
el14af 0:10704407da46 1242 //clear missle
el14af 0:10704407da46 1243 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1244 }
el14af 0:10704407da46 1245
el14af 0:10704407da46 1246 }
el14af 0:10704407da46 1247 if (missle_one.direction == RIGHT) {
el14af 0:10704407da46 1248 if (missle_one.x == 80 ) {
el14af 0:10704407da46 1249 missle_one.active = 0;
el14af 0:10704407da46 1250
el14af 0:10704407da46 1251 //clear missle
el14af 0:10704407da46 1252 lcd.drawRect(missle_one.x-1,missle_one.y-1,2,2,2);
el14af 0:10704407da46 1253 }
el14af 0:10704407da46 1254
el14af 0:10704407da46 1255 }
el14af 0:10704407da46 1256 }
el14af 0:10704407da46 1257 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1258 if (missle_two.active) {
el14af 0:10704407da46 1259 if (ship.direction == UP) {
el14af 0:10704407da46 1260 //check if the ship is within the x_axis constraints of the hit box
el14af 0:10704407da46 1261 // the structure : if missle x coordinate is greater than the top right pixel of hit box...
el14af 0:10704407da46 1262 // AND
el14af 0:10704407da46 1263 // missle x coordinate is less than top right pixel of hit box + width of the hit box...
el14af 0:10704407da46 1264 // then the missle is in the x constraints.
el14af 0:10704407da46 1265 if ( (missle_two.x > ship.x - 2 ) && (missle_two.x < ship.x + 2) ) {
el14af 0:10704407da46 1266 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1267 if ( (missle_two.y > ship.y - 2 ) && (missle_two.y < ship.y + 3) ) {
el14af 0:10704407da46 1268 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1269 missle_two.active = 0;
el14af 0:10704407da46 1270 // set hit flag for missle 1.
el14af 0:10704407da46 1271 missle_two.hit_flag = 1;
el14af 0:10704407da46 1272 //clear missle
el14af 0:10704407da46 1273 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1274 }
el14af 0:10704407da46 1275 }
el14af 0:10704407da46 1276 }
el14af 0:10704407da46 1277 // the process is repeated for the different directions of travel, with a different hitbox shape
el14af 0:10704407da46 1278 if (ship.direction == DOWN) {
el14af 0:10704407da46 1279 if ( (missle_two.x > ship.x - 2 ) && (missle_two.x < ship.x + 2 ) ) {
el14af 0:10704407da46 1280 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1281 if ( (missle_two.y > ship.y - 3 ) && (missle_two.y < ship.y + 2) ) {
el14af 0:10704407da46 1282 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1283 missle_two.active = 0;
el14af 0:10704407da46 1284 // set hit flag for missle 1.
el14af 0:10704407da46 1285 missle_two.hit_flag = 1;
el14af 0:10704407da46 1286 //clear missle
el14af 0:10704407da46 1287 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1288 }
el14af 0:10704407da46 1289 }
el14af 0:10704407da46 1290 }
el14af 0:10704407da46 1291 if (ship.direction == LEFT) {
el14af 0:10704407da46 1292 if ( (missle_two.x > ship.x - 2 ) && (missle_two.x < ship.x + 3) ) {
el14af 0:10704407da46 1293 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1294 if ( (missle_two.y > ship.y - 2 ) && (missle_two.y < ship.y + 2) ) {
el14af 0:10704407da46 1295 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1296 missle_two.active = 0;
el14af 0:10704407da46 1297 // set hit flag for missle 1.
el14af 0:10704407da46 1298 missle_two.hit_flag = 1;
el14af 0:10704407da46 1299 //clear missle
el14af 0:10704407da46 1300 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1301 }
el14af 0:10704407da46 1302 }
el14af 0:10704407da46 1303 }
el14af 0:10704407da46 1304 if (ship.direction == RIGHT) {
el14af 0:10704407da46 1305 if ( (missle_two.x > ship.x - 3 ) && (missle_two.x < ship.x + 2) ) {
el14af 0:10704407da46 1306 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1307 if ( (missle_two.y > ship.y - 2 ) && (missle_two.y < ship.y + 2) ) {
el14af 0:10704407da46 1308 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1309 missle_two.active = 0;
el14af 0:10704407da46 1310 // set hit flag for missle 1.
el14af 0:10704407da46 1311 missle_two.hit_flag = 1;
el14af 0:10704407da46 1312 //clear missle
el14af 0:10704407da46 1313 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1314 }
el14af 0:10704407da46 1315 }
el14af 0:10704407da46 1316 }
el14af 0:10704407da46 1317
el14af 0:10704407da46 1318 // detect missle expires
el14af 0:10704407da46 1319 // depending on the direction of flight, if the missle reaches the border without coming in contact with the ship.
el14af 0:10704407da46 1320 // the missle is set to inactive
el14af 0:10704407da46 1321
el14af 0:10704407da46 1322 if (missle_two.direction == UP) {
el14af 0:10704407da46 1323 if (missle_two.y == 10) {
el14af 0:10704407da46 1324 missle_two.active = 0;
el14af 0:10704407da46 1325
el14af 0:10704407da46 1326 //clear missle
el14af 0:10704407da46 1327 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1328 }
el14af 0:10704407da46 1329
el14af 0:10704407da46 1330 }
el14af 0:10704407da46 1331 if (missle_two.direction == DOWN) {
el14af 0:10704407da46 1332 if (missle_two.y == 44) {
el14af 0:10704407da46 1333 missle_two.active = 0;
el14af 0:10704407da46 1334
el14af 0:10704407da46 1335 //clear missle
el14af 0:10704407da46 1336 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1337 }
el14af 0:10704407da46 1338
el14af 0:10704407da46 1339 }
el14af 0:10704407da46 1340 if (missle_two.direction == LEFT) {
el14af 0:10704407da46 1341 if (missle_two.x == 2 ) {
el14af 0:10704407da46 1342 missle_two.active = 0;
el14af 0:10704407da46 1343
el14af 0:10704407da46 1344 //clear missle
el14af 0:10704407da46 1345 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1346 }
el14af 0:10704407da46 1347
el14af 0:10704407da46 1348 }
el14af 0:10704407da46 1349 if (missle_two.direction == RIGHT) {
el14af 0:10704407da46 1350 if (missle_two.x == 80 ) {
el14af 0:10704407da46 1351 missle_two.active = 0;
el14af 0:10704407da46 1352
el14af 0:10704407da46 1353 //clear missle
el14af 0:10704407da46 1354 lcd.drawRect(missle_two.x-1,missle_two.y-1,2,2,2);
el14af 0:10704407da46 1355 }
el14af 0:10704407da46 1356
el14af 0:10704407da46 1357 }
el14af 0:10704407da46 1358 }
el14af 0:10704407da46 1359 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1360 if (missle_three.active) {
el14af 0:10704407da46 1361
el14af 0:10704407da46 1362 if (ship.direction == UP) {
el14af 0:10704407da46 1363 //check if the ship is within the x_axis constraints of the hit box
el14af 0:10704407da46 1364 // the structure : if missle x coordinate is greater than the top right pixel of hit box...
el14af 0:10704407da46 1365 // AND
el14af 0:10704407da46 1366 // missle x coordinate is less than top right pixel of hit box + width of the hit box...
el14af 0:10704407da46 1367 // then the missle is in the x constraints.
el14af 0:10704407da46 1368 if ( (missle_three.x > ship.x - 2 ) && (missle_three.x < ship.x + 2) ) {
el14af 0:10704407da46 1369 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1370 if ( (missle_three.y > ship.y - 2 ) && (missle_three.y < ship.y + 3) ) {
el14af 0:10704407da46 1371 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1372 missle_three.active = 0;
el14af 0:10704407da46 1373 // set hit flag for missle 1.
el14af 0:10704407da46 1374 missle_three.hit_flag = 1;
el14af 0:10704407da46 1375 //clear missle
el14af 0:10704407da46 1376 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1377 }
el14af 0:10704407da46 1378 }
el14af 0:10704407da46 1379 }
el14af 0:10704407da46 1380 // the process is repeated for the different directions of travel, with a different hitbox shape
el14af 0:10704407da46 1381 if (ship.direction == DOWN) {
el14af 0:10704407da46 1382 if ( (missle_three.x > ship.x - 2 ) && (missle_three.x < ship.x + 2 ) ) {
el14af 0:10704407da46 1383 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1384 if ( (missle_three.y > ship.y - 3 ) && (missle_three.y < ship.y + 2) ) {
el14af 0:10704407da46 1385 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1386 missle_three.active = 0;
el14af 0:10704407da46 1387 // set hit flag for missle 1.
el14af 0:10704407da46 1388 missle_three.hit_flag = 1;
el14af 0:10704407da46 1389 //clear missle
el14af 0:10704407da46 1390 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1391 }
el14af 0:10704407da46 1392 }
el14af 0:10704407da46 1393 }
el14af 0:10704407da46 1394 if (ship.direction == LEFT) {
el14af 0:10704407da46 1395 if ( (missle_three.x > ship.x - 2 ) && (missle_three.x < ship.x + 3) ) {
el14af 0:10704407da46 1396 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1397 if ( (missle_three.y > ship.y - 2 ) && (missle_three.y < ship.y + 2) ) {
el14af 0:10704407da46 1398 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1399 missle_three.active = 0;
el14af 0:10704407da46 1400 // set hit flag for missle 1.
el14af 0:10704407da46 1401 missle_three.hit_flag = 1;
el14af 0:10704407da46 1402 //clear missle
el14af 0:10704407da46 1403 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1404 }
el14af 0:10704407da46 1405 }
el14af 0:10704407da46 1406 }
el14af 0:10704407da46 1407 if (ship.direction == RIGHT) {
el14af 0:10704407da46 1408 if ( (missle_three.x > ship.x - 3 ) && (missle_three.x < ship.x + 2) ) {
el14af 0:10704407da46 1409 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1410 if ( (missle_three.y > ship.y - 2) && (missle_three.y < ship.y + 2) ) {
el14af 0:10704407da46 1411 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1412 missle_three.active = 0;
el14af 0:10704407da46 1413 // set hit flag for missle 1.
el14af 0:10704407da46 1414 missle_three.hit_flag = 1;
el14af 0:10704407da46 1415 //clear missle
el14af 0:10704407da46 1416 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1417 }
el14af 0:10704407da46 1418 }
el14af 0:10704407da46 1419 }
el14af 0:10704407da46 1420
el14af 0:10704407da46 1421 // detect missle expires
el14af 0:10704407da46 1422 // depending on the direction of flight, if the missle reaches the border without coming in contact with the ship.
el14af 0:10704407da46 1423 // the missle is set to inactive
el14af 0:10704407da46 1424
el14af 0:10704407da46 1425 if (missle_three.direction == UP) {
el14af 0:10704407da46 1426 if (missle_three.y == 10) {
el14af 0:10704407da46 1427 missle_three.active = 0;
el14af 0:10704407da46 1428
el14af 0:10704407da46 1429 //clear missle
el14af 0:10704407da46 1430 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1431 }
el14af 0:10704407da46 1432
el14af 0:10704407da46 1433 }
el14af 0:10704407da46 1434 if (missle_three.direction == DOWN) {
el14af 0:10704407da46 1435 if (missle_three.y == 44) {
el14af 0:10704407da46 1436 missle_three.active = 0;
el14af 0:10704407da46 1437
el14af 0:10704407da46 1438 //clear missle
el14af 0:10704407da46 1439 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1440 }
el14af 0:10704407da46 1441
el14af 0:10704407da46 1442 }
el14af 0:10704407da46 1443 if (missle_three.direction == LEFT) {
el14af 0:10704407da46 1444 if (missle_three.x == 2 ) {
el14af 0:10704407da46 1445 missle_three.active = 0;
el14af 0:10704407da46 1446
el14af 0:10704407da46 1447 //clear missle
el14af 0:10704407da46 1448 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1449 }
el14af 0:10704407da46 1450
el14af 0:10704407da46 1451 }
el14af 0:10704407da46 1452 if (missle_three.direction == RIGHT) {
el14af 0:10704407da46 1453 if (missle_three.x == 80 ) {
el14af 0:10704407da46 1454 missle_three.active = 0;
el14af 0:10704407da46 1455
el14af 0:10704407da46 1456 //clear missle
el14af 0:10704407da46 1457 lcd.drawRect(missle_three.x-1,missle_three.y-1,2,2,2);
el14af 0:10704407da46 1458 }
el14af 0:10704407da46 1459
el14af 0:10704407da46 1460 }
el14af 0:10704407da46 1461
el14af 0:10704407da46 1462 }
el14af 0:10704407da46 1463 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1464 if (missle_four.active) {
el14af 0:10704407da46 1465 if (ship.direction == UP) {
el14af 0:10704407da46 1466 //check if the ship is within the x_axis constraints of the hit box
el14af 0:10704407da46 1467 // the structure : if missle x coordinate is greater than the top right pixel of hit box...
el14af 0:10704407da46 1468 // AND
el14af 0:10704407da46 1469 // missle x coordinate is less than top right pixel of hit box + width of the hit box...
el14af 0:10704407da46 1470 // then the missle is in the x constraints.
el14af 0:10704407da46 1471 if ( (missle_four.x > ship.x - 2 ) && (missle_four.x < ship.x + 2) ) {
el14af 0:10704407da46 1472 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1473 if ( (missle_four.y > ship.y - 2 ) && (missle_four.y < ship.y + 3) ) {
el14af 0:10704407da46 1474 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1475 missle_four.active = 0;
el14af 0:10704407da46 1476 // set hit flag for missle 1.
el14af 0:10704407da46 1477 missle_four.hit_flag = 1;
el14af 0:10704407da46 1478 //clear missle
el14af 0:10704407da46 1479 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1480 }
el14af 0:10704407da46 1481 }
el14af 0:10704407da46 1482 }
el14af 0:10704407da46 1483 // the process is repeated for the different directions of travel, with a different hitbox shape
el14af 0:10704407da46 1484 if (ship.direction == DOWN) {
el14af 0:10704407da46 1485 if ( (missle_four.x > ship.x - 2 ) && (missle_four.x < ship.x + 2 ) ) {
el14af 0:10704407da46 1486 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1487 if ( (missle_four.y > ship.y - 3 ) && (missle_four.y < ship.y + 2) ) {
el14af 0:10704407da46 1488 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1489 missle_four.active = 0;
el14af 0:10704407da46 1490 // set hit flag for missle 1.
el14af 0:10704407da46 1491 missle_four.hit_flag = 1;
el14af 0:10704407da46 1492 //clear missle
el14af 0:10704407da46 1493 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1494 }
el14af 0:10704407da46 1495 }
el14af 0:10704407da46 1496 }
el14af 0:10704407da46 1497 if (ship.direction == LEFT) {
el14af 0:10704407da46 1498 if ( (missle_four.x > ship.x - 2 ) && (missle_four.x < ship.x + 3) ) {
el14af 0:10704407da46 1499 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1500 if ( (missle_four.y > ship.y - 2) && (missle_four.y < ship.y + 2) ) {
el14af 0:10704407da46 1501 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1502 missle_four.active = 0;
el14af 0:10704407da46 1503 // set hit flag for missle 1.
el14af 0:10704407da46 1504 missle_four.hit_flag = 1;
el14af 0:10704407da46 1505 //clear missle
el14af 0:10704407da46 1506 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1507 }
el14af 0:10704407da46 1508 }
el14af 0:10704407da46 1509 }
el14af 0:10704407da46 1510 if (ship.direction == RIGHT) {
el14af 0:10704407da46 1511 if ( (missle_four.x > ship.x - 3 ) && (missle_four.x < ship.x + 2) ) {
el14af 0:10704407da46 1512 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1513 if ( (missle_four.y > ship.y - 2 ) && (missle_four.y < ship.y + 2) ) {
el14af 0:10704407da46 1514 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1515 missle_four.active = 0;
el14af 0:10704407da46 1516 // set hit flag for missle 1.
el14af 0:10704407da46 1517 missle_four.hit_flag = 1;
el14af 0:10704407da46 1518 //clear missle
el14af 0:10704407da46 1519 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1520 }
el14af 0:10704407da46 1521 }
el14af 0:10704407da46 1522 }
el14af 0:10704407da46 1523
el14af 0:10704407da46 1524 // detect missle expires
el14af 0:10704407da46 1525 // depending on the direction of flight, if the missle reaches the border without coming in contact with the ship.
el14af 0:10704407da46 1526 // the missle is set to inactive
el14af 0:10704407da46 1527
el14af 0:10704407da46 1528 if (missle_four.direction == UP) {
el14af 0:10704407da46 1529 if (missle_four.y == 10) {
el14af 0:10704407da46 1530 missle_four.active = 0;
el14af 0:10704407da46 1531
el14af 0:10704407da46 1532 //clear missle
el14af 0:10704407da46 1533 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1534 }
el14af 0:10704407da46 1535
el14af 0:10704407da46 1536 }
el14af 0:10704407da46 1537 if (missle_four.direction == DOWN) {
el14af 0:10704407da46 1538 if (missle_four.y == 44) {
el14af 0:10704407da46 1539 missle_four.active = 0;
el14af 0:10704407da46 1540
el14af 0:10704407da46 1541 //clear missle
el14af 0:10704407da46 1542 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1543 }
el14af 0:10704407da46 1544
el14af 0:10704407da46 1545 }
el14af 0:10704407da46 1546 if (missle_four.direction == LEFT) {
el14af 0:10704407da46 1547 if (missle_four.x == 2 ) {
el14af 0:10704407da46 1548 missle_four.active = 0;
el14af 0:10704407da46 1549
el14af 0:10704407da46 1550 //clear missle
el14af 0:10704407da46 1551 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1552 }
el14af 0:10704407da46 1553
el14af 0:10704407da46 1554 }
el14af 0:10704407da46 1555 if (missle_four.direction == RIGHT) {
el14af 0:10704407da46 1556 if (missle_four.x == 80 ) {
el14af 0:10704407da46 1557 missle_four.active = 0;
el14af 0:10704407da46 1558
el14af 0:10704407da46 1559 //clear missle
el14af 0:10704407da46 1560 lcd.drawRect(missle_four.x-1,missle_four.y-1,2,2,2);
el14af 0:10704407da46 1561 }
el14af 0:10704407da46 1562
el14af 0:10704407da46 1563 }
el14af 0:10704407da46 1564 }
el14af 0:10704407da46 1565 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1566 if (missle_five.active) {
el14af 0:10704407da46 1567 if (ship.direction == UP) {
el14af 0:10704407da46 1568 //check if the ship is within the x_axis constraints of the hit box
el14af 0:10704407da46 1569 // the structure : if missle x coordinate is greater than the top right pixel of hit box...
el14af 0:10704407da46 1570 // AND
el14af 0:10704407da46 1571 // missle x coordinate is less than top right pixel of hit box + width of the hit box...
el14af 0:10704407da46 1572 // then the missle is in the x constraints.
el14af 0:10704407da46 1573 if ( (missle_five.x > ship.x - 2 ) && (missle_five.x < ship.x + 2) ) {
el14af 0:10704407da46 1574 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1575 if ( (missle_five.y > ship.y - 2 ) && (missle_five.y < ship.y + 3) ) {
el14af 0:10704407da46 1576 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1577 missle_five.active = 0;
el14af 0:10704407da46 1578 // set hit flag for missle 1.
el14af 0:10704407da46 1579 missle_five.hit_flag = 1;
el14af 0:10704407da46 1580 //clear missle
el14af 0:10704407da46 1581 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1582 }
el14af 0:10704407da46 1583 }
el14af 0:10704407da46 1584 }
el14af 0:10704407da46 1585 // the process is repeated for the different directions of travel, with a different hitbox shape
el14af 0:10704407da46 1586 if (ship.direction == DOWN) {
el14af 0:10704407da46 1587 if ( (missle_five.x > ship.x - 2 ) && (missle_five.x < ship.x + 2 ) ) {
el14af 0:10704407da46 1588 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1589 if ( (missle_five.y > ship.y - 3 ) && (missle_five.y < ship.y + 2) ) {
el14af 0:10704407da46 1590 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1591 missle_five.active = 0;
el14af 0:10704407da46 1592 // set hit flag for missle 1.
el14af 0:10704407da46 1593 missle_five.hit_flag = 1;
el14af 0:10704407da46 1594 //clear missle
el14af 0:10704407da46 1595 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1596 }
el14af 0:10704407da46 1597 }
el14af 0:10704407da46 1598 }
el14af 0:10704407da46 1599 if (ship.direction == LEFT) {
el14af 0:10704407da46 1600 if ( (missle_five.x > ship.x - 2 ) && (missle_five.x < ship.x + 3) ) {
el14af 0:10704407da46 1601 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1602 if ( (missle_five.y > ship.y - 2 ) && (missle_five.y < ship.y + 2) ) {
el14af 0:10704407da46 1603 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1604 missle_five.active = 0;
el14af 0:10704407da46 1605 // set hit flag for missle 1.
el14af 0:10704407da46 1606 missle_five.hit_flag = 1;
el14af 0:10704407da46 1607 //clear missle
el14af 0:10704407da46 1608 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1609 }
el14af 0:10704407da46 1610 }
el14af 0:10704407da46 1611 }
el14af 0:10704407da46 1612 if (ship.direction == RIGHT) {
el14af 0:10704407da46 1613 if ( (missle_five.x > ship.x - 3 ) && (missle_five.x < ship.x + 2) ) {
el14af 0:10704407da46 1614 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1615 if ( (missle_five.y > ship.y - 2 ) && (missle_five.y < ship.y + 2) ) {
el14af 0:10704407da46 1616 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1617 missle_five.active = 0;
el14af 0:10704407da46 1618 // set hit flag for missle 1.
el14af 0:10704407da46 1619 missle_five.hit_flag = 1;
el14af 0:10704407da46 1620 //clear missle
el14af 0:10704407da46 1621 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1622 }
el14af 0:10704407da46 1623 }
el14af 0:10704407da46 1624 }
el14af 0:10704407da46 1625
el14af 0:10704407da46 1626 // detect missle expires
el14af 0:10704407da46 1627 // depending on the direction of flight, if the missle reaches the border without coming in contact with the ship.
el14af 0:10704407da46 1628 // the missle is set to inactive
el14af 0:10704407da46 1629
el14af 0:10704407da46 1630 if (missle_five.direction == UP) {
el14af 0:10704407da46 1631 if (missle_five.y == 10) {
el14af 0:10704407da46 1632 missle_five.active = 0;
el14af 0:10704407da46 1633
el14af 0:10704407da46 1634 //clear missle
el14af 0:10704407da46 1635 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1636 }
el14af 0:10704407da46 1637
el14af 0:10704407da46 1638 }
el14af 0:10704407da46 1639 if (missle_five.direction == DOWN) {
el14af 0:10704407da46 1640 if (missle_five.y == 44) {
el14af 0:10704407da46 1641 missle_five.active = 0;
el14af 0:10704407da46 1642
el14af 0:10704407da46 1643 //clear missle
el14af 0:10704407da46 1644 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1645 }
el14af 0:10704407da46 1646
el14af 0:10704407da46 1647 }
el14af 0:10704407da46 1648 if (missle_five.direction == LEFT) {
el14af 0:10704407da46 1649 if (missle_five.x == 2 ) {
el14af 0:10704407da46 1650 missle_five.active = 0;
el14af 0:10704407da46 1651
el14af 0:10704407da46 1652 //clear missle
el14af 0:10704407da46 1653 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1654 }
el14af 0:10704407da46 1655
el14af 0:10704407da46 1656 }
el14af 0:10704407da46 1657 if (missle_five.direction == RIGHT) {
el14af 0:10704407da46 1658 if (missle_five.x == 80 ) {
el14af 0:10704407da46 1659 missle_five.active = 0;
el14af 0:10704407da46 1660
el14af 0:10704407da46 1661 //clear missle
el14af 0:10704407da46 1662 lcd.drawRect(missle_five.x-1,missle_five.y-1,2,2,2);
el14af 0:10704407da46 1663 }
el14af 0:10704407da46 1664
el14af 0:10704407da46 1665 }
el14af 0:10704407da46 1666
el14af 0:10704407da46 1667 }
el14af 0:10704407da46 1668 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1669 if (missle_six.active) {
el14af 0:10704407da46 1670 if (ship.direction == UP) {
el14af 0:10704407da46 1671 //check if the ship is within the x_axis constraints of the hit box
el14af 0:10704407da46 1672 // the structure : if missle x coordinate is greater than the top right pixel of hit box...
el14af 0:10704407da46 1673 // AND
el14af 0:10704407da46 1674 // missle x coordinate is less than top right pixel of hit box + width of the hit box...
el14af 0:10704407da46 1675 // then the missle is in the x constraints.
el14af 0:10704407da46 1676 if ( (missle_six.x > ship.x - 2 ) && (missle_six.x < ship.x + 2) ) {
el14af 0:10704407da46 1677 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1678 if ( (missle_six.y > ship.y - 2 ) && (missle_six.y < ship.y + 3) ) {
el14af 0:10704407da46 1679 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1680 missle_six.active = 0;
el14af 0:10704407da46 1681 // set hit flag for missle 1.
el14af 0:10704407da46 1682 missle_six.hit_flag = 1;
el14af 0:10704407da46 1683 //clear missle
el14af 0:10704407da46 1684 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1685 }
el14af 0:10704407da46 1686 }
el14af 0:10704407da46 1687 }
el14af 0:10704407da46 1688 // the process is repeated for the different directions of travel, with a different hitbox shape
el14af 0:10704407da46 1689 if (ship.direction == DOWN) {
el14af 0:10704407da46 1690 if ( (missle_six.x > ship.x - 2) && (missle_six.x < ship.x + 2 ) ) {
el14af 0:10704407da46 1691 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1692 if ( (missle_six.y > ship.y - 3 ) && (missle_six.y < ship.y + 2) ) {
el14af 0:10704407da46 1693 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1694 missle_six.active = 0;
el14af 0:10704407da46 1695 // set hit flag for missle 1.
el14af 0:10704407da46 1696 missle_six.hit_flag = 1;
el14af 0:10704407da46 1697 //clear missle
el14af 0:10704407da46 1698 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1699 }
el14af 0:10704407da46 1700 }
el14af 0:10704407da46 1701 }
el14af 0:10704407da46 1702 if (ship.direction == LEFT) {
el14af 0:10704407da46 1703 if ( (missle_six.x > ship.x - 2 ) && (missle_six.x < ship.x + 3) ) {
el14af 0:10704407da46 1704 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1705 if ( (missle_six.y > ship.y - 2 ) && (missle_six.y < ship.y + 2) ) {
el14af 0:10704407da46 1706 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1707 missle_six.active = 0;
el14af 0:10704407da46 1708 // set hit flag for missle 1.
el14af 0:10704407da46 1709 missle_six.hit_flag = 1;
el14af 0:10704407da46 1710 //clear missle
el14af 0:10704407da46 1711 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1712 }
el14af 0:10704407da46 1713 }
el14af 0:10704407da46 1714 }
el14af 0:10704407da46 1715 if (ship.direction == RIGHT) {
el14af 0:10704407da46 1716 if ( (missle_six.x > ship.x - 3 ) && (missle_six.x < ship.x + 2) ) {
el14af 0:10704407da46 1717 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1718 if ( (missle_six.y > ship.y - 2 ) && (missle_six.y < ship.y + 2) ) {
el14af 0:10704407da46 1719 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1720 missle_six.active = 0;
el14af 0:10704407da46 1721 // set hit flag for missle 1.
el14af 0:10704407da46 1722 missle_six.hit_flag = 1;
el14af 0:10704407da46 1723 //clear missle
el14af 0:10704407da46 1724 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1725 }
el14af 0:10704407da46 1726 }
el14af 0:10704407da46 1727 }
el14af 0:10704407da46 1728
el14af 0:10704407da46 1729 // detect missle expires
el14af 0:10704407da46 1730 // depending on the direction of flight, if the missle reaches the border without coming in contact with the ship.
el14af 0:10704407da46 1731 // the missle is set to inactive
el14af 0:10704407da46 1732
el14af 0:10704407da46 1733 if (missle_six.direction == UP) {
el14af 0:10704407da46 1734 if (missle_six.y == 10) {
el14af 0:10704407da46 1735 missle_six.active = 0;
el14af 0:10704407da46 1736
el14af 0:10704407da46 1737 //clear missle
el14af 0:10704407da46 1738 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1739 }
el14af 0:10704407da46 1740
el14af 0:10704407da46 1741 }
el14af 0:10704407da46 1742 if (missle_six.direction == DOWN) {
el14af 0:10704407da46 1743 if (missle_six.y == 44) {
el14af 0:10704407da46 1744 missle_six.active = 0;
el14af 0:10704407da46 1745
el14af 0:10704407da46 1746 //clear missle
el14af 0:10704407da46 1747 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1748 }
el14af 0:10704407da46 1749
el14af 0:10704407da46 1750 }
el14af 0:10704407da46 1751 if (missle_six.direction == LEFT) {
el14af 0:10704407da46 1752 if (missle_six.x == 2 ) {
el14af 0:10704407da46 1753 missle_six.active = 0;
el14af 0:10704407da46 1754
el14af 0:10704407da46 1755 //clear missle
el14af 0:10704407da46 1756 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1757 }
el14af 0:10704407da46 1758
el14af 0:10704407da46 1759 }
el14af 0:10704407da46 1760 if (missle_six.direction == RIGHT) {
el14af 0:10704407da46 1761 if (missle_six.x == 80 ) {
el14af 0:10704407da46 1762 missle_six.active = 0;
el14af 0:10704407da46 1763
el14af 0:10704407da46 1764 //clear missle
el14af 0:10704407da46 1765 lcd.drawRect(missle_six.x-1,missle_six.y-1,2,2,2);
el14af 0:10704407da46 1766 }
el14af 0:10704407da46 1767
el14af 0:10704407da46 1768 }
el14af 0:10704407da46 1769 }
el14af 0:10704407da46 1770
el14af 0:10704407da46 1771 //if any of the missles have hit a flag is raised
el14af 0:10704407da46 1772 if (missle_one.hit_flag == 1) {
el14af 0:10704407da46 1773 g_hit_flag = 1;
el14af 0:10704407da46 1774 }
el14af 0:10704407da46 1775 if (missle_two.hit_flag == 1) {
el14af 0:10704407da46 1776 g_hit_flag = 1;
el14af 0:10704407da46 1777 }
el14af 0:10704407da46 1778 if (missle_three.hit_flag == 1) {
el14af 0:10704407da46 1779 g_hit_flag = 1;
el14af 0:10704407da46 1780 }
el14af 0:10704407da46 1781 if (missle_four.hit_flag == 1) {
el14af 0:10704407da46 1782 g_hit_flag = 1;
el14af 0:10704407da46 1783 }
el14af 0:10704407da46 1784 if (missle_five.hit_flag == 1) {
el14af 0:10704407da46 1785 g_hit_flag = 1;
el14af 0:10704407da46 1786 }
el14af 0:10704407da46 1787 if (missle_six.hit_flag == 1) {
el14af 0:10704407da46 1788 g_hit_flag = 1;
el14af 0:10704407da46 1789 }
el14af 0:10704407da46 1790
el14af 0:10704407da46 1791 }
el14af 0:10704407da46 1792 // raises flag for collectible timeout
el14af 0:10704407da46 1793 void collect_timeout ()
el14af 0:10704407da46 1794 {
el14af 0:10704407da46 1795 g_collect_timeout_flag = 1;
el14af 0:10704407da46 1796
el14af 0:10704407da46 1797 }
el14af 0:10704407da46 1798 //prints collectible
el14af 0:10704407da46 1799 void print_collect ()
el14af 0:10704407da46 1800 {
el14af 0:10704407da46 1801 // function first determines what collectible is free to be allocated, and if either collectible has expired
el14af 0:10704407da46 1802
el14af 0:10704407da46 1803 bool collect_free; // variable for determining which collectible struct is free, 0 for collec_one and 1 for collect_two.
el14af 0:10704407da46 1804 // determined by if statments below
el14af 0:10704407da46 1805
el14af 0:10704407da46 1806 // if both collectibles are active
el14af 0:10704407da46 1807 if (collect_one.active && collect_two.active) {
el14af 0:10704407da46 1808
el14af 0:10704407da46 1809 // if either collectible has expired
el14af 0:10704407da46 1810 if (g_collect_timeout_flag == 1) {
el14af 0:10704407da46 1811
el14af 0:10704407da46 1812 // if collect_one has expired
el14af 0:10704407da46 1813 if (!g_what_expired) {
el14af 0:10704407da46 1814 // set to free
el14af 0:10704407da46 1815 collect_free = 0;
el14af 0:10704407da46 1816
el14af 0:10704407da46 1817 // reset collect_one
el14af 0:10704407da46 1818 collect_one.active = 0;
el14af 0:10704407da46 1819
el14af 0:10704407da46 1820 //clear collectible
el14af 0:10704407da46 1821 lcd.drawRect(collect_one.x-1,collect_one.y-1,2,2,2);
el14af 0:10704407da46 1822
el14af 0:10704407da46 1823 //reset timeout flag
el14af 0:10704407da46 1824 g_collect_timeout_flag = 0;
el14af 0:10704407da46 1825
el14af 0:10704407da46 1826 //change the next collectible to expire
el14af 0:10704407da46 1827 g_what_expired = 1;
el14af 0:10704407da46 1828 }
el14af 0:10704407da46 1829 // if collect_two has expired
el14af 0:10704407da46 1830 else {
el14af 0:10704407da46 1831 // set to free
el14af 0:10704407da46 1832 collect_free = 1;
el14af 0:10704407da46 1833
el14af 0:10704407da46 1834 // reset collect_two
el14af 0:10704407da46 1835 collect_two.active = 0;
el14af 0:10704407da46 1836
el14af 0:10704407da46 1837 //clear collectible
el14af 0:10704407da46 1838 lcd.drawRect(collect_two.x-1,collect_two.y-1,2,2,2);
el14af 0:10704407da46 1839
el14af 0:10704407da46 1840 // reset timeout flag
el14af 0:10704407da46 1841 g_collect_timeout_flag = 0;
el14af 0:10704407da46 1842
el14af 0:10704407da46 1843 //change the next collectible to expire
el14af 0:10704407da46 1844 g_what_expired = 0;
el14af 0:10704407da46 1845 }
el14af 0:10704407da46 1846 }
el14af 0:10704407da46 1847 }
el14af 0:10704407da46 1848 // if collect_two has been collected, or has not been allocated yet
el14af 0:10704407da46 1849 else if (collect_one.active && !collect_two.active) {
el14af 0:10704407da46 1850
el14af 0:10704407da46 1851 // set collect_two to free
el14af 0:10704407da46 1852 collect_free = 1;
el14af 0:10704407da46 1853
el14af 0:10704407da46 1854 }
el14af 0:10704407da46 1855 // if collect_one has been collected, or has not been allocated yet
el14af 0:10704407da46 1856 else if (!collect_one.active && collect_two.active) {
el14af 0:10704407da46 1857
el14af 0:10704407da46 1858 // set_collect_one to free
el14af 0:10704407da46 1859 collect_free = 0;
el14af 0:10704407da46 1860
el14af 0:10704407da46 1861 }
el14af 0:10704407da46 1862 // if neither collect_one or collect_two have been assigned, or both have been collected
el14af 0:10704407da46 1863 else if (!collect_one.active && !collect_two.active) {
el14af 0:10704407da46 1864
el14af 0:10704407da46 1865 // set collect_one to free
el14af 0:10704407da46 1866 collect_free = 0;
el14af 0:10704407da46 1867
el14af 0:10704407da46 1868 }
el14af 0:10704407da46 1869
el14af 0:10704407da46 1870 // reset rand() seed
el14af 0:10704407da46 1871 srand(time(NULL));
el14af 0:10704407da46 1872
el14af 0:10704407da46 1873 // the function then allocates a new position to whatever collectible is free
el14af 0:10704407da46 1874
el14af 0:10704407da46 1875 // if collec_one is free
el14af 0:10704407da46 1876 if (!collect_free) {
el14af 0:10704407da46 1877
el14af 0:10704407da46 1878 // sect collect_one to active
el14af 0:10704407da46 1879 collect_one.active = 1;
el14af 0:10704407da46 1880
el14af 0:10704407da46 1881 // assign random x/y coordinates within the bounds of the game edges, takes into account the way drawRect works
el14af 0:10704407da46 1882 collect_one.x = rand ()%80 + 1;
el14af 0:10704407da46 1883 collect_one.y = rand ()%44 + 10;
el14af 0:10704407da46 1884
el14af 0:10704407da46 1885 // draw the collectible
el14af 0:10704407da46 1886 lcd.drawRect(collect_one.x-1,collect_one.y-1,2,2,0);
el14af 0:10704407da46 1887
el14af 0:10704407da46 1888 }// if collec_two is free
el14af 0:10704407da46 1889 else if (collect_free) {
el14af 0:10704407da46 1890
el14af 0:10704407da46 1891 // sect collect_one to active
el14af 0:10704407da46 1892 collect_two.active = 1;
el14af 0:10704407da46 1893
el14af 0:10704407da46 1894 // assign random x/y coordinates within the bounds of the game edges, takes into account the way drawRect works
el14af 0:10704407da46 1895 collect_two.x = rand ()%80 + 1;
el14af 0:10704407da46 1896 collect_two.y = rand ()%44 + 10;
el14af 0:10704407da46 1897
el14af 0:10704407da46 1898 // draw the collectible
el14af 0:10704407da46 1899 lcd.drawRect(collect_two.x-1,collect_two.y-1,2,2,0);
el14af 0:10704407da46 1900 }
el14af 0:10704407da46 1901
el14af 0:10704407da46 1902
el14af 0:10704407da46 1903
el14af 0:10704407da46 1904 }
el14af 0:10704407da46 1905 // redraw collectible in case it disappeared
el14af 0:10704407da46 1906 void update_collect ()
el14af 0:10704407da46 1907 {
el14af 0:10704407da46 1908
el14af 0:10704407da46 1909 if (collect_one.active) {
el14af 0:10704407da46 1910 // draw the collectible
el14af 0:10704407da46 1911 lcd.drawRect(collect_one.x-1,collect_one.y-1,2,2,0);
el14af 0:10704407da46 1912
el14af 0:10704407da46 1913 }
el14af 0:10704407da46 1914 if (collect_two.active) {
el14af 0:10704407da46 1915 // draw the collectible
el14af 0:10704407da46 1916 lcd.drawRect(collect_two.x-1,collect_two.y-1,2,2,0);
el14af 0:10704407da46 1917 }
el14af 0:10704407da46 1918 }
el14af 0:10704407da46 1919
el14af 0:10704407da46 1920 // check if collectible has been collected
el14af 0:10704407da46 1921 void check_collect ()
el14af 0:10704407da46 1922 {
el14af 0:10704407da46 1923
el14af 0:10704407da46 1924 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1925 if (collect_one.active) {
el14af 0:10704407da46 1926
el14af 0:10704407da46 1927 // ship pointing up
el14af 0:10704407da46 1928 if (ship.direction == UP) {
el14af 0:10704407da46 1929 // check if the collectible is within the ship's the hit box
el14af 0:10704407da46 1930 if ( (collect_one.x > ship.x - 2 ) && (collect_one.x < ship.x + 2) ) {
el14af 0:10704407da46 1931 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1932 if ( (collect_one.y > ship.y - 2 ) && (collect_one.y < ship.y + 3) ) {
el14af 0:10704407da46 1933 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1934 collect_one.active = 0;
el14af 0:10704407da46 1935 // set hit flag for missle 1.
el14af 0:10704407da46 1936 g_collect_flag = 1;
el14af 0:10704407da46 1937 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 1938 g_what_expired = 1;
el14af 0:10704407da46 1939 }
el14af 0:10704407da46 1940 }
el14af 0:10704407da46 1941 }
el14af 0:10704407da46 1942 // ship pointing down
el14af 0:10704407da46 1943 if (ship.direction == DOWN) {
el14af 0:10704407da46 1944 if ( (collect_one.x > ship.x - 2 ) && (collect_one.x < ship.x + 2 ) ) {
el14af 0:10704407da46 1945 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1946 if ( (collect_one.y > ship.y - 3 ) && (collect_one.y < ship.y + 2) ) {
el14af 0:10704407da46 1947 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1948 collect_one.active = 0;
el14af 0:10704407da46 1949 // set hit flag for missle 1.
el14af 0:10704407da46 1950 g_collect_flag = 1;
el14af 0:10704407da46 1951 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 1952 g_what_expired = 1;
el14af 0:10704407da46 1953 }
el14af 0:10704407da46 1954 }
el14af 0:10704407da46 1955 }
el14af 0:10704407da46 1956 // ship pointing left
el14af 0:10704407da46 1957 if (ship.direction == LEFT) {
el14af 0:10704407da46 1958 if ( (collect_one.x > ship.x - 3 ) && (collect_one.x < ship.x + 2) ) {
el14af 0:10704407da46 1959 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1960 if ( (collect_one.y > ship.y - 2 ) && (collect_one.y < ship.y + 2) ) {
el14af 0:10704407da46 1961 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1962 collect_one.active = 0;
el14af 0:10704407da46 1963 // set hit flag for missle 1.
el14af 0:10704407da46 1964 g_collect_flag= 1;
el14af 0:10704407da46 1965 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 1966 g_what_expired = 1;
el14af 0:10704407da46 1967 }
el14af 0:10704407da46 1968 }
el14af 0:10704407da46 1969 }
el14af 0:10704407da46 1970 // ship pointing right
el14af 0:10704407da46 1971 if (ship.direction == RIGHT) {
el14af 0:10704407da46 1972 if ( (collect_one.x > ship.x - 2 ) && (collect_one.x < ship.x + 3) ) {
el14af 0:10704407da46 1973 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1974 if ( (collect_one.y > ship.y - 2 ) && (collect_one.y < ship.y + 2) ) {
el14af 0:10704407da46 1975 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1976 collect_one.active = 0;
el14af 0:10704407da46 1977 // set hit flag for missle 1.
el14af 0:10704407da46 1978 g_collect_flag = 1;
el14af 0:10704407da46 1979 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 1980 g_what_expired = 1;
el14af 0:10704407da46 1981 }
el14af 0:10704407da46 1982 }
el14af 0:10704407da46 1983 }
el14af 0:10704407da46 1984 }
el14af 0:10704407da46 1985
el14af 0:10704407da46 1986 // only preform operations if missle is active for efficiency
el14af 0:10704407da46 1987 if (collect_two.active) {
el14af 0:10704407da46 1988
el14af 0:10704407da46 1989 // ship pointing up
el14af 0:10704407da46 1990 if (ship.direction == UP) {
el14af 0:10704407da46 1991 // check if the collectible is within the ship's the hit box
el14af 0:10704407da46 1992 if ( (collect_two.x > ship.x - 2 ) && (collect_two.x < ship.x + 2) ) {
el14af 0:10704407da46 1993 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 1994 if ( (collect_two.y > ship.y - 2 ) && (collect_one.y < ship.y + 3) ) {
el14af 0:10704407da46 1995 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 1996 collect_two.active = 0;
el14af 0:10704407da46 1997 // set hit flag for missle 1.
el14af 0:10704407da46 1998 g_collect_flag = 1;
el14af 0:10704407da46 1999 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 2000 g_what_expired = 0;
el14af 0:10704407da46 2001 }
el14af 0:10704407da46 2002 }
el14af 0:10704407da46 2003 }
el14af 0:10704407da46 2004 // ship pointing down
el14af 0:10704407da46 2005 if (ship.direction == DOWN) {
el14af 0:10704407da46 2006 if ( (collect_two.x > ship.x - 2 ) && (collect_two.x < ship.x + 2 ) ) {
el14af 0:10704407da46 2007 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 2008 if ( (collect_two.y > ship.y - 3 ) && (collect_two.y < ship.y + 2) ) {
el14af 0:10704407da46 2009 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 2010 collect_two.active = 0;
el14af 0:10704407da46 2011 // set hit flag for missle 1.
el14af 0:10704407da46 2012 g_collect_flag = 1;
el14af 0:10704407da46 2013 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 2014 g_what_expired = 0;
el14af 0:10704407da46 2015 }
el14af 0:10704407da46 2016 }
el14af 0:10704407da46 2017 }
el14af 0:10704407da46 2018 // ship pointing left
el14af 0:10704407da46 2019 if (ship.direction == LEFT) {
el14af 0:10704407da46 2020 if ( (collect_two.x > ship.x - 3 ) && (collect_two.x < ship.x + 2) ) {
el14af 0:10704407da46 2021 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 2022 if ( (collect_two.y > ship.y - 2 ) && (collect_two.y < ship.y + 2) ) {
el14af 0:10704407da46 2023 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 2024 collect_two.active = 0;
el14af 0:10704407da46 2025 // set hit flag for missle 1.
el14af 0:10704407da46 2026 g_collect_flag= 1;
el14af 0:10704407da46 2027 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 2028 g_what_expired = 0;
el14af 0:10704407da46 2029 }
el14af 0:10704407da46 2030 }
el14af 0:10704407da46 2031 }
el14af 0:10704407da46 2032 // ship pointing right
el14af 0:10704407da46 2033 if (ship.direction == RIGHT) {
el14af 0:10704407da46 2034 if ( (collect_two.x > ship.x - 2 ) && (collect_two.x < ship.x + 3) ) {
el14af 0:10704407da46 2035 // same structure for y-axis of hit box. uses height instead of width
el14af 0:10704407da46 2036 if ( (collect_two.y > ship.y - 2 ) && (collect_two.y < ship.y + 2) ) {
el14af 0:10704407da46 2037 // set missle to inactive, i.e. missle explodes
el14af 0:10704407da46 2038 collect_two.active = 0;
el14af 0:10704407da46 2039 // set hit flag for missle 1.
el14af 0:10704407da46 2040 g_collect_flag = 1;
el14af 0:10704407da46 2041 // since collect_one has been reset, collect_two will expire next
el14af 0:10704407da46 2042 g_what_expired = 0;
el14af 0:10704407da46 2043 }
el14af 0:10704407da46 2044 }
el14af 0:10704407da46 2045 }
el14af 0:10704407da46 2046
el14af 0:10704407da46 2047
el14af 0:10704407da46 2048
el14af 0:10704407da46 2049 }
el14af 0:10704407da46 2050 }
el14af 0:10704407da46 2051 // time left
el14af 0:10704407da46 2052 void time_left ()
el14af 0:10704407da46 2053 {
el14af 0:10704407da46 2054 //with every second decrement time left by 1
el14af 0:10704407da46 2055 g_time_left --;
el14af 0:10704407da46 2056
el14af 0:10704407da46 2057 //increases score by 10 every second
el14af 0:10704407da46 2058 g_score = g_score + 10;
el14af 0:10704407da46 2059
el14af 0:10704407da46 2060 // print timer
el14af 0:10704407da46 2061 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
el14af 0:10704407da46 2062 // so can display a string of a maximum 14 characters in length
el14af 0:10704407da46 2063 // or create formatted strings - ensure they aren't more than 14 characters long
el14af 0:10704407da46 2064 int length = sprintf(buffer,"Time: %2d ",g_time_left); // print formatted data to buffer
el14af 0:10704407da46 2065 // it is important the format specifier ensures the length will fit in the buffer
el14af 0:10704407da46 2066 if (length <= 14) // if string will fit on display
el14af 0:10704407da46 2067 lcd.printString(buffer,16,0); // display on screen
el14af 0:10704407da46 2068
el14af 0:10704407da46 2069 }