Components and supplies
Male/Female Jumper Wires
Breadboard (generic)
Arduino UNO
Resistor 1k ohm
General Purpose Transistor NPN
Passive piezo buzzer
Atari Compatible Joystick 9Pin
Jumper wires (generic)
Male Plug Screw Nut Connector DB9-G1-01 9 Pin Terminal Breakout Board
MAX7219 Dot Matrix Module 4 in One Display
Project description
Code
UFO Attack code
c_cpp
1/*UFO ATTACK! - Written by Anthony Clarke. */ 2/*Some specific lines of code were lifted from open source example programs. These are labled in comments.*/ 3 4#include <SPI.h> 5#include <Adafruit_GFX.h> 6#include <Max72xxPanel.h> 7 8const int pinCS = 10; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI ) 9const int numberOfHorizontalDisplays = 4; 10const int numberOfVerticalDisplays = 1; 11 12Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays); 13 14// 'ship', 8x8px 15const unsigned char ship [] PROGMEM = { 16 0x80, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 17}; 18// 'UFO', 8x8px 19const unsigned char ufo [] PROGMEM = { 20 0x40, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 21}; 22// 'explosion', 8x8px 23const unsigned char explosion [] PROGMEM = { 24 0xe0, 0xe0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00 25}; 26// 'Bouncer', 8x8px 27const unsigned char bouncer [] PROGMEM = { 28 0x60, 0xc0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00 29}; 30// 'Boss1', 8x8px 31const unsigned char boss [] PROGMEM = { 32 0x18, 0x24, 0x42, 0xff, 0x24, 0xff, 0x7e, 0x3c 33}; 34// 'Faller', 8x8px 35const unsigned char faller [] PROGMEM = { 36 0x80, 0xc0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00 37}; 38// 'Riser', 8x8px 39const unsigned char riser [] PROGMEM = { 40 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 41}; 42// 'LT', 8x8px 43const unsigned char LT [] PROGMEM = { 44 0x78, 0xfc, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00 45}; 46// 'UFO laser', 8x8px 47const unsigned char ufoLaser [] PROGMEM = { 48 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 49}; 50// 'BossBoom', 8x8px 51const unsigned char bossBoom1 [] PROGMEM = { 52 0xaa, 0x55, 0xa2, 0x51, 0x8a, 0x45, 0xaa, 0x55 53}; 54// 'BossBoom2', 8x8px 55const unsigned char bossBoom2 [] PROGMEM = { 56 0x55, 0xaa, 0x45, 0x8a, 0x51, 0xa2, 0x55, 0xaa 57}; 58 59 60 61//Game started variable 62bool gameStart = false; 63 64// Defining joystick control pins 65int up = 2; 66int down = 3; 67int left = 4; 68int right = 5; 69int trigger = 6; 70 71//Title ticker variables..Shamlessly stolen from the ticker example :P 72String tape = "UFO ATTACK! - Press fire to start "; 73int wait = 40; // In milliseconds 74int spacer = 1; 75int width = 5 + spacer; // The font width is 5 pixels 76 77//Ship and laser position variables 78int shipx = 0; 79int shipy = 0; 80int lasx; 81int lasy; 82//Lives counter 83int lives = 5; 84//laser existense variable 85bool lasExist; 86//UFO laser position variables 87int ufoLas1x; 88int ufoLas1y; 89int ufoLas2x; 90int ufoLas2y; 91int ufoLas3x; 92int ufoLas3y; 93int ufoLas4x; 94int ufoLas4y; 95//UFO laser existence variable 96bool ufoLasExist1 = false; 97bool ufoLasExist2 = false; 98bool ufoLasExist3 = false; 99bool ufoLasExist4 = false; 100//UFO laser selection variable 101int selection; 102int lastSelection; 103//Boss Minion selection variable 104int minionSelection; 105//UFO position variables 106int ufo1x; 107int ufo1y; 108int ufo2x; 109int ufo2y; 110int ufo3x; 111int ufo3y; 112//Bouncer position variables 113int bouncer1x; 114int bouncer1y; 115int bouncer2x; 116int bouncer2y; 117int bouncer3x; 118int bouncer3y; 119int bouncer4x; 120int bouncer4y; 121//Faller position variables 122int faller1x; 123int faller1y; 124int faller2x; 125int faller2y; 126int faller3x; 127int faller3y; 128int faller4x; 129int faller4y; 130//Faller speed variables 131int faller1Speedx; 132int faller1Speedy; 133int faller2Speedx; 134int faller2Speedy; 135int faller3Speedx; 136int faller3Speedy; 137int faller4Speedx; 138int faller4Speedy; 139int fallerxSpeedTarget = 2; 140int fallerySpeedTarget = 2; 141//Riser position variables 142int riser1x; 143int riser1y; 144int riser2x; 145int riser2y; 146int riser3x; 147int riser3y; 148int riser4x; 149int riser4y; 150//Riser speed variables 151int riser1Speedx; 152int riser1Speedy; 153int riser2Speedx; 154int riser2Speedy; 155int riser3Speedx; 156int riser3Speedy; 157int riser4Speedx; 158int riser4Speedy; 159//LT position variable 160int ltx; 161int lty; 162//LT health variable 163int ltHealth = 3; 164//LT speed variable 165int ltxSpeed = 0; 166int ltxSpeedTarget = 2; 167//Boss position variable 168int bossx = 34; 169int bossy = 0; 170//Boss movement speed variables 171int bossxSpeed = 0; 172int bossySpeed = 0; 173int bossxSpeedTarget = 3; 174int bossySpeedTarget = 1; 175//UFO existence variables 176bool ufo1Exist = false; 177bool ufo2Exist = false; 178bool ufo3Exist = false; 179//Bouncer existence variables 180bool bouncer1Exist = false; 181bool bouncer2Exist = false; 182bool bouncer3Exist = false; 183bool bouncer4Exist = false; 184//Faller existence variables 185bool faller1Exist = false; 186bool faller2Exist = false; 187bool faller3Exist = false; 188bool faller4Exist = false; 189//Riser existence variables 190bool riser1Exist = false; 191bool riser2Exist = false; 192bool riser3Exist = false; 193bool riser4Exist = false; 194//LT existence variable 195bool ltExist = false; 196//Boss1 Existence variable 197bool boss1Exist = false; 198//Boss health variable 199int bossHealth; 200 201 202//Bouncer top or bottom variable 203int bouncerOrigin; 204//Bouncer gap space variable 205int bouncerGap = 6; 206//Bouncer direction variables 207bool bouncer1Direction; 208bool bouncer2Direction; 209bool bouncer3Direction; 210bool bouncer4Direction; 211int bouncer1xSpeed; 212int bouncer2xSpeed; 213int bouncer3xSpeed; 214int bouncer4xSpeed; 215int bouncer1ySpeed; 216int bouncer2ySpeed; 217int bouncer3ySpeed; 218int bouncer4ySpeed; 219int ySpeed = 1; 220int xSpeed = 2; 221 222 223//spawn timer and spawn time variables for UFO enemies 224unsigned long startTime;//setting up timer variables 225unsigned long currentTime; 226unsigned long spawnTime = 3000; //Spawn timer for UFO's 227//spawn timer and spawn time for bouncer enemies 228unsigned long startTime2; 229unsigned long currentTime2; 230unsigned long spawnTime2 = 10000; //Spawn timer for bouncers 231//Spawn timer for LT mini boss 232unsigned long startTime3; 233unsigned long currentTime3; 234unsigned long spawnTime3; //Spawn timer for LT 235//spawn timer for UFO lasers 236unsigned long bossStartTime; 237unsigned long bossCurrentTime; 238unsigned long bossStartTime2; 239unsigned long bossCurrentTime2; 240unsigned long bossLaserTimer = 1000; 241unsigned long bossMinionTimer = 6000; 242//Spawn enabler variable, needed to temporarily stop spawning for boss battle 243bool spawnEnabled; 244 245//Level variable 246int levelNumber = 1; 247//Score variable 248int score = 0; 249//Level progression variables 250int kills; 251int killsTarget; 252//Boss battle engaged variable 253bool bossBattleStarted = false; 254 255//Player ship collision detection variable 256bool collisionDetection = true; 257//Coin flip for UFOLaser 4 selection 258int coinFlip; 259 260//Sound variables 261int buzzerPin = 9; 262//Boss siren variables 263float sinVal; // Define a variable to save sine value 264int toneVal; // Define a variable to save sound frequency 265 266 267 268void setup() 269{ 270 //Serial.begin(9600); 271 //Setting up sound pin 272 pinMode(buzzerPin, OUTPUT); // Set the buzzer pin to output mode 273 //setting up spawntimers 274 275 276 //setting pin modes for joystick operation 277 pinMode(up, INPUT_PULLUP); //UP pin 278 pinMode(down, INPUT_PULLUP); //DOWN pin 279 pinMode(left, INPUT_PULLUP); //LEFT pin 280 pinMode(right, INPUT_PULLUP); //RIGHT pin 281 pinMode(trigger, INPUT_PULLUP); //FIRE pin 282 283 284 285 //setting led intensity 286 matrix.setIntensity(0); 287 288// Adjust to your own needs 289 matrix.setPosition(0, 3, 0); // The first display is at <0, 0> 290 matrix.setPosition(1, 2, 0); // The second display is at <1, 0> 291 matrix.setPosition(2, 1, 0); // The third display is at <2, 0> 292 matrix.setPosition(3, 0, 0); // And the last display is at <3, 0> 293// ... 294 matrix.setRotation(0, 3); // The first display is position upside down 295 matrix.setRotation(1, 3); 296 matrix.setRotation(2, 3); 297 matrix.setRotation(3, 3); // The same hold for the last display 298 matrix.fillScreen(LOW); 299 300 301} 302void loop() 303{ 304 if (gameStart == false) 305 { 306 Ticker(true); 307 } 308 309 SpawnTimer(); 310 JoystickControl(); 311 UfoControl(); 312 BouncerControl(); 313 LaserControl(); 314 ScreenLimit(); 315 BossBattle(); 316 BossControl(); 317 UFOLaserControl(); 318 LTControl(); 319 FallerControl(); 320 RiserControl(); 321 ShipCollision(); 322 //Serial.println(ufoLas1x); 323 324 325 326 327 matrix.write();//draw the screen 328 delay(50);//speed delay 329 matrix.fillScreen(LOW);//clear screen, must be done every frame. 330 331 332} 333 334void Level(int level) 335{ 336 if (level == 1) 337 { 338 spawnEnabled = true; //Enable spawning 339 spawnTime = 3000; //Spawn timer for UFO'a 340 spawnTime2 = 10000000; //Spawn timer for bouncers set so high they'll never appear 341 spawnTime3 = 10000000; //Spawn timer for LT set so high so they'll never appear 342 killsTarget = 10; //Sets kill target. When target reached boss should be spawned 343 bossHealth = 10; 344 bossLaserTimer = 1000; 345 } 346 if (level == 2) 347 { 348 spawnEnabled = true; 349 spawnTime = 3000; 350 spawnTime2 = 8000; 351 spawnTime3 = 10000000; 352 killsTarget = 20; 353 bossHealth = 15; 354 bossLaserTimer = 900; 355 } 356 if (level == 3) 357 { 358 spawnEnabled = true; 359 spawnTime = 2000; 360 spawnTime2 = 6000; 361 spawnTime3 = 8000; 362 killsTarget = 30; 363 bossHealth = 20; 364 bossLaserTimer = 800; 365 } 366 if (level == 4) 367 { 368 spawnEnabled = true; 369 spawnTime = 2000; 370 spawnTime2 = 5000; 371 spawnTime3 = 7000; 372 killsTarget = 40; 373 bossHealth = 25; 374 bossLaserTimer = 700; 375 bossMinionTimer = 6000; 376 } 377 if (level == 5) 378 { 379 spawnEnabled = true; 380 spawnTime = 1800; 381 spawnTime2 = 4500; 382 spawnTime3 = 6000; 383 killsTarget = 50; 384 bossHealth = 30; 385 bossLaserTimer = 600; 386 bossMinionTimer = 5500; 387 } 388 if (level == 6) 389 { 390 spawnEnabled = true; 391 spawnTime = 2000; 392 spawnTime2 = 5000; 393 spawnTime3 = 7000; 394 killsTarget = 60; 395 bossHealth = 35; 396 bossLaserTimer = 500; 397 bossMinionTimer = 5000; 398 } 399 if (level == 7) 400 { 401 spawnEnabled = true; 402 spawnTime = 2000; 403 spawnTime2 = 5000; 404 spawnTime3 = 7000; 405 killsTarget = 70; 406 bossHealth = 40; 407 bossLaserTimer = 400; 408 bossMinionTimer = 4000; 409 } 410 if (level == 8) 411 { 412 spawnEnabled = true; 413 spawnTime = 2000; 414 spawnTime2 = 5000; 415 spawnTime3 = 7000; 416 killsTarget = 80; 417 bossHealth = 45; 418 bossLaserTimer = 300; 419 bossMinionTimer = 3000; 420 } 421 if (level == 9) 422 { 423 spawnEnabled = true; 424 spawnTime = 2000; 425 spawnTime2 = 5000; 426 spawnTime3 = 7000; 427 killsTarget = 90; 428 bossHealth = 50; 429 bossLaserTimer = 200; 430 bossMinionTimer = 2000; 431 } 432 if (level == 10) 433 { 434 spawnEnabled = true; 435 spawnTime = 2000; 436 spawnTime2 = 5000; 437 spawnTime3 = 7000; 438 killsTarget = 100; 439 bossHealth = 55; 440 bossLaserTimer = 100; 441 bossMinionTimer = 1000; 442 } 443 444 445 //Need to add congratulation message for getting past level 10 446} 447 448 449 450void Ticker(bool resetKills) //Ticker code shamlessly stolen from the Ticker example. Used for Title and score and gameover 451{ 452 while (gameStart == false) 453 { 454 455 456 for ( int i = 0 ; i < width * tape.length() + matrix.width() - 1 - spacer; i++ ) 457 { 458 459 matrix.fillScreen(LOW); 460 461 int letter = i / width; 462 int x = (matrix.width() - 1) - i % width; 463 int y = (matrix.height() - 8) / 2; // center the text vertically 464 465 while ( x + width - spacer >= 0 && letter >= 0 ) 466 { 467 468 if ( letter < tape.length() ) { 469 matrix.drawChar(x, y, tape[letter], HIGH, LOW, 1); 470 if (digitalRead(trigger)== 0) 471 { 472 gameStart = true; 473 } 474 } 475 476 letter--; 477 x -= width; 478 } 479 if (digitalRead(trigger)== 0) //If trigger pressed then start the game 480 { 481 bossBattleStarted = false; 482 gameStart = true; 483 startTime = millis(); //Setting up spawn timer for UFO 484 startTime2 = millis(); //setting up spawn timer for Bouncer 485 startTime3 = millis(); //Setting up spawn timer for LT 486 currentTime = startTime;//Setting up start timers for SpawnTimer function 487 currentTime2 = startTime2; 488 currentTime3 = startTime3; 489 spawnEnabled = true;//Enabling spawnTimer 490 Level(levelNumber);//Calls level function to set level variables 491 shipx = 0; 492 shipy = 3; 493 if (resetKills == true) 494 { 495 kills = 0; 496 } 497 else if (resetKills == false) 498 { 499 kills = kills; 500 } 501 delay(500); 502 return; 503 } 504 matrix.write(); // Send bitmap to display 505 506 delay(wait); 507 } 508 509 510 } 511} 512 513 514void JoystickControl() 515{ 516 //draw ship at ship coords 517 matrix.drawBitmap(shipx,shipy,ship,3,2,1); 518 519 if (digitalRead(up)== 0)//if joystick up then move ship coords up 520 { 521 shipy--; 522 } 523 if (digitalRead(down)== 0)//if joystick down then move ship coords down 524 { 525 shipy++; 526 } 527 if (digitalRead(left)== 0)//if joystick left then move ship coords left 528 { 529 shipx--; 530 } 531 if (digitalRead(right)== 0)//if joystick right then move ship coords right 532 { 533 shipx++; 534 } 535 if (digitalRead(trigger)== 0 && lasExist == false)//if trigger pressed then draw laser and mark it as existing 536 { 537 ShootLaser(); 538 } 539 540} 541 542void ShootLaser() 543{ 544 if (lasExist == false) 545 { 546 lasx=shipx+3; 547 lasy=shipy+1; 548 lasExist=true; 549 tone(buzzerPin, 400,75);//play laser sound 550 551 } 552} 553 554void LaserControl() //Contains control code for the player laser and collision detection routines 555{ 556 //If the laser exists then move the laser dot across the screen 557 if (lasExist == true) 558 { 559 matrix.drawPixel(lasx,lasy,1); 560 lasx++; 561 //collision detection for UFO's 562 //Each IF statement covers every pixel with an extra one shadowing the top pixel because the laser can sometimes go through. 563 if (ufo1Exist == true) 564 { 565 566 for (int a = 0; a < 2; a++) 567 { 568 if (lasx == ufo1x+a && lasy == ufo1y+1) 569 { 570 ufo1Exist = false; 571 lasExist = false; 572 Boom(ufo1x,ufo1y,true,1); 573 ufo1x = 34; 574 } 575 } 576 for (int a = 1; a < 3; a++) 577 { 578 if (lasx == ufo1x+a && lasy == ufo1y) 579 { 580 ufo1Exist = false; 581 lasExist = false; 582 Boom(ufo1x,ufo1y,true,1); 583 ufo1x = 34; 584 } 585 } 586 } 587 //collision detection for UFO2 588 if (ufo2Exist == true) 589 { 590 591 for (int a = 0; a < 3; a++) 592 { 593 if (lasx == ufo2x+a && lasy == ufo2y+1) 594 { 595 ufo2Exist = false; 596 lasExist = false; 597 Boom(ufo2x,ufo2y,true,1); 598 ufo2x = 34; 599 } 600 } 601 for (int a = 1; a < 3; a++) 602 { 603 if (lasx == ufo2x+a && lasy == ufo2y) 604 { 605 ufo2Exist = false; 606 lasExist = false; 607 Boom(ufo2x,ufo2y,true,1); 608 ufo2x = 34; 609 } 610 } 611 } 612 613 //Collision detection for UFO3 614 if (ufo3Exist == true) 615 { 616 617 for (int a = 0; a < 3; a++) 618 { 619 if (lasx == ufo3x+a && lasy == ufo3y+1) 620 { 621 ufo3Exist = false; 622 lasExist = false; 623 Boom(ufo3x,ufo3y,true,1); 624 ufo3x = 34; 625 } 626 } 627 for (int a = 1; a < 3; a++) 628 { 629 if (lasx == ufo3x+a && lasy == ufo3y) 630 { 631 ufo3Exist = false; 632 lasExist = false; 633 Boom(ufo3x,ufo3y,true,1); 634 ufo3x = 34; 635 } 636 } 637 } 638 639 640 //Collision detection for Bouncer1 641 if (bouncer1Exist == true) 642 { 643 for (int a = 1; a < 2; a++) 644 { 645 if (lasx == bouncer1x+a && lasy == bouncer1y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 646 { 647 bouncer1Exist = false; 648 lasExist=false; 649 Boom(bouncer1x,bouncer1y,true,2); 650 bouncer1x = 34; 651 } 652 } 653 for (int a = 0; a < 2; a++) 654 { 655 if (lasx == bouncer1x+a && lasy == bouncer1y+1) 656 { 657 bouncer1Exist = false; 658 lasExist=false; 659 Boom(bouncer1x,bouncer1y,true,2); 660 bouncer1x = 34; 661 } 662 } 663 for (int a = 1; a < 2; a++) 664 { 665 if (lasx == bouncer1x+a && lasy == bouncer1y+2) 666 { 667 bouncer1Exist = false; 668 lasExist=false; 669 Boom(bouncer1x,bouncer1y,true,2); 670 bouncer1x = 34; 671 } 672 } 673 } 674 //Collision detection for Bouncer 2 675 if (bouncer2Exist == true) 676 { 677 for (int a = 1; a < 2; a++) 678 { 679 if (lasx == bouncer2x+a && lasy == bouncer2y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 680 { 681 bouncer2Exist = false; 682 lasExist=false; 683 Boom(bouncer2x,bouncer2y,true,2); 684 bouncer2x = 34; 685 } 686 } 687 for (int a = 0; a < 2; a++) 688 { 689 if (lasx == bouncer2x+a && lasy == bouncer2y+1) 690 { 691 bouncer2Exist = false; 692 lasExist=false; 693 Boom(bouncer2x,bouncer2y,true,2); 694 bouncer2x = 34; 695 } 696 } 697 for (int a = 1; a < 2; a++) 698 { 699 if (lasx == bouncer2x+a && lasy == bouncer2y+2) 700 { 701 bouncer2Exist = false; 702 lasExist=false; 703 Boom(bouncer2x,bouncer2y,true,2); 704 bouncer2x = 34; 705 } 706 } 707 } 708 709 710 711 //Collision detection for Bouncer 3 712 if (bouncer3Exist == true) 713 { 714 for (int a = 1; a < 2; a++) 715 { 716 if (lasx == bouncer3x+a && lasy == bouncer3y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 717 { 718 bouncer3Exist = false; 719 lasExist=false; 720 Boom(bouncer3x,bouncer3y,true,2); 721 bouncer3x = 34; 722 } 723 } 724 for (int a = 0; a < 2; a++) 725 { 726 if (lasx == bouncer3x+a && lasy == bouncer3y+1) 727 { 728 bouncer3Exist = false; 729 lasExist=false; 730 Boom(bouncer3x,bouncer3y,true,2); 731 bouncer3x = 34; 732 } 733 } 734 for (int a = 1; a < 2; a++) 735 { 736 if (lasx == bouncer3x+a && lasy == bouncer3y+2) 737 { 738 bouncer3Exist = false; 739 lasExist=false; 740 Boom(bouncer3x,bouncer3y,true,2); 741 bouncer3x = 34; 742 } 743 } 744 } 745 746 //Collision detection for Bouncer 4 747 if (bouncer4Exist == true) 748 { 749 for (int a = 1; a < 2; a++) 750 { 751 if (lasx == bouncer4x+a && lasy == bouncer4y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 752 { 753 bouncer4Exist = false; 754 lasExist=false; 755 Boom(bouncer4x,bouncer4y,true,2); 756 bouncer4x = 34; 757 } 758 } 759 for (int a = 0; a < 2; a++) 760 { 761 if (lasx == bouncer4x+a && lasy == bouncer4y+1) 762 { 763 bouncer4Exist = false; 764 lasExist=false; 765 Boom(bouncer4x,bouncer4y,true,2); 766 bouncer4x = 34; 767 } 768 } 769 for (int a = 1; a < 2; a++) 770 { 771 if (lasx == bouncer4x+a && lasy == bouncer4y+2) 772 { 773 bouncer4Exist = false; 774 lasExist=false; 775 Boom(bouncer4x,bouncer4y,true,2); 776 bouncer4x = 34; 777 } 778 } 779 } 780 781 //Collision detection for Boss 782 if (boss1Exist == true) 783 { 784 for (int a = 3; a < 5; a++ ) //Top line 0 785 { 786 if (lasx == bossx+a && lasy == bossy) 787 { 788 bossHealth--; 789 lasExist=false; 790 Boom(lasx,lasy,false,0); 791 } 792 } 793 for (int a = 2; a < 5; a++) //Line 1 794 { 795 if (lasx == bossx+a && lasy == bossy+1) 796 { 797 bossHealth--; 798 lasExist=false; 799 Boom(lasx,lasy,false,0); 800 } 801 } 802 for (int a = 1; a < 6; a++) //Line 2 803 { 804 if (lasx == bossx+a && lasy == bossy+2) 805 { 806 bossHealth--; 807 lasExist=false; 808 Boom(lasx,lasy,false,0); 809 } 810 } 811 for (int a = 0; a < 7; a++) //Line 3 812 { 813 if (lasx == bossx+a && lasy == bossy+3) 814 { 815 bossHealth--; 816 lasExist=false; 817 Boom(lasx,lasy,false,0); 818 } 819 } 820 for (int a = 2; a < 6; a++) //Line 4 821 { 822 if (lasx == bossx+a && lasy == bossy+4) 823 { 824 bossHealth--; 825 lasExist=false; 826 Boom(lasx,lasy,false,0); 827 } 828 } 829 for (int a = 0; a < 7; a++) //Line 5 830 { 831 if (lasx == bossx+a && lasy == bossy+5) 832 { 833 bossHealth--; 834 lasExist=false; 835 Boom(lasx,lasy,false,0); 836 } 837 } 838 for (int a = 1; a < 6; a++) //Line 6 839 { 840 if (lasx == bossx+a && lasy == bossy+6) 841 { 842 bossHealth--; 843 lasExist=false; 844 Boom(lasx,lasy,false,0); 845 } 846 } 847 for (int a = 2; a < 5; a++) //Line 7 848 { 849 if (lasx == bossx+a && lasy == bossy+7) 850 { 851 bossHealth--; 852 lasExist=false; 853 Boom(lasx,lasy,false,0); 854 } 855 } 856 857 } 858 859 //Collision detection routines for LT 860 if (ltExist == true) 861 { 862 for (int a = 1; a < 4; a++) //Top line 0 863 { 864 if (lasx == ltx+a && lasy == lty) 865 { 866 ltHealth--; 867 lasExist=false; 868 Boom(lasx,lasy,false,0); 869 } 870 } 871 for (int a = 0; a < 5; a++) //Line 1 872 { 873 if (lasx == ltx+a && lasy == lty+1) 874 { 875 ltHealth--; 876 lasExist=false; 877 Boom(lasx,lasy,false,0); 878 } 879 } 880 for (int a = 2; a < 4; a++) //Line 2 881 { 882 if (lasx == ltx+a && lasy == lty+2) 883 { 884 ltHealth--; 885 lasExist=false; 886 Boom(lasx,lasy,false,0); 887 } 888 } 889 } 890//Collision detection for Faller 1 891 if (faller1Exist == true) 892 { 893 for (int a = 0; a < 2; a++) //Top Line 0 894 { 895 if (lasx == faller1x+a && lasy == faller1y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 896 { 897 faller1Exist = false; 898 lasExist=false; 899 Boom(faller1x,faller1y,false,0); 900 faller1x = 34; 901 } 902 } 903 for (int a = 0; a < 3; a++) //Line 1 904 { 905 if (lasx == faller1x+a && lasy == faller1y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 906 { 907 faller1Exist = false; 908 lasExist=false; 909 Boom(faller1x,faller1y,false,0); 910 faller1x = 34; 911 } 912 } 913 for (int a = 0; a < 3; a++) //Line 2 914 { 915 if (lasx == faller1x+a && lasy == faller1y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 916 { 917 faller1Exist = false; 918 lasExist=false; 919 Boom(faller1x,faller1y,false,0); 920 faller1x = 34; 921 } 922 } 923 } 924 //Collision detection for Faller 2 925 if (faller2Exist == true) 926 { 927 for (int a = 0; a < 2; a++) //Top Line 0 928 { 929 if (lasx == faller2x+a && lasy == faller2y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 930 { 931 faller2Exist = false; 932 lasExist=false; 933 Boom(faller2x,faller2y,false,0); 934 faller2x = 34; 935 } 936 } 937 for (int a = 0; a < 3; a++) //Line 1 938 { 939 if (lasx == faller2x+a && lasy == faller2y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 940 { 941 faller2Exist = false; 942 lasExist=false; 943 Boom(faller2x,faller2y,false,0); 944 faller2x = 34; 945 } 946 } 947 for (int a = 0; a < 3; a++) //Line 2 948 { 949 if (lasx == faller2x+a && lasy == faller2y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 950 { 951 faller2Exist = false; 952 lasExist=false; 953 Boom(faller2x,faller2y,false,0); 954 faller2x = 34; 955 } 956 } 957 } 958 //Collision detection for Faller 3 959 if (faller3Exist == true) 960 { 961 for (int a = 0; a < 2; a++) //Top Line 0 962 { 963 if (lasx == faller3x+a && lasy == faller3y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 964 { 965 faller3Exist = false; 966 lasExist=false; 967 Boom(faller3x,faller3y,false,0); 968 faller3x = 34; 969 } 970 } 971 for (int a = 0; a < 3; a++) //Line 1 972 { 973 if (lasx == faller3x+a && lasy == faller3y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 974 { 975 faller3Exist = false; 976 lasExist=false; 977 Boom(faller3x,faller3y,false,0); 978 faller3x = 34; 979 } 980 } 981 for (int a = 0; a < 3; a++) //Line 2 982 { 983 if (lasx == faller3x+a && lasy == faller3y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 984 { 985 faller3Exist = false; 986 lasExist=false; 987 Boom(faller3x,faller3y,false,0); 988 faller3x = 34; 989 } 990 } 991 } 992 //Collision detection for Faller 4 993 if (faller4Exist == true) 994 { 995 for (int a = 0; a < 2; a++) //Top Line 0 996 { 997 if (lasx == faller4x+a && lasy == faller4y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 998 { 999 faller4Exist = false; 1000 lasExist=false; 1001 Boom(faller4x,faller4y,false,0); 1002 faller4x = 34; 1003 } 1004 } 1005 for (int a = 0; a < 3; a++) //Line 1 1006 { 1007 if (lasx == faller4x+a && lasy == faller4y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1008 { 1009 faller4Exist = false; 1010 lasExist=false; 1011 Boom(faller4x,faller4y,false,0); 1012 faller4x = 34; 1013 } 1014 } 1015 for (int a = 0; a < 3; a++) //Line 2 1016 { 1017 if (lasx == faller4x+a && lasy == faller4y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1018 { 1019 faller4Exist = false; 1020 lasExist=false; 1021 Boom(faller4x,faller4y,false,0); 1022 faller4x = 34; 1023 } 1024 } 1025 } 1026 1027 //Collision detection for Riser 1 1028 if (riser1Exist == true) 1029 { 1030 for (int a = 0; a < 3; a++) //Top Line 0 1031 { 1032 if (lasx == riser1x+a && lasy == riser1y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1033 { 1034 riser1Exist = false; 1035 lasExist=false; 1036 Boom(riser1x,riser1y,false,0); 1037 riser1x = 34; 1038 } 1039 } 1040 for (int a = 0; a < 2; a++) //Line 1 1041 { 1042 if (lasx == riser1x+0 && lasy == riser1y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1043 { 1044 riser1Exist = false; 1045 lasExist=false; 1046 Boom(riser1x,riser1y,false,0); 1047 riser1x = 34; 1048 } 1049 } 1050 for (int a = 0; a < 1; a++) //Line 2 1051 { 1052 if (lasx == riser1x+a && lasy == riser1y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1053 { 1054 riser1Exist = false; 1055 lasExist=false; 1056 Boom(riser1x,riser1y,false,0); 1057 riser1x = 34; 1058 } 1059 } 1060 } 1061 1062 1063 //Collision detection for Riser 2 1064 if (riser2Exist == true) 1065 { 1066 for (int a = 0; a < 3; a++) //Top Line 0 1067 { 1068 if (lasx == riser2x+a && lasy == riser2y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1069 { 1070 riser2Exist = false; 1071 lasExist=false; 1072 Boom(riser2x,riser2y,false,0); 1073 riser2x = 34; 1074 } 1075 } 1076 for (int a = 0; a < 2; a++) //Line 1 1077 { 1078 if (lasx == riser2x+0 && lasy == riser2y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1079 { 1080 riser2Exist = false; 1081 lasExist=false; 1082 Boom(riser2x,riser2y,false,0); 1083 riser2x = 34; 1084 } 1085 } 1086 for (int a = 0; a < 1; a++) //Line 2 1087 { 1088 if (lasx == riser2x+a && lasy == riser2y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1089 { 1090 riser2Exist = false; 1091 lasExist=false; 1092 Boom(riser2x,riser2y,false,0); 1093 riser1x = 34; 1094 } 1095 } 1096 } 1097 1098 //Collision detection for Riser 3 1099 if (riser3Exist == true) 1100 { 1101 for (int a = 0; a < 3; a++) //Top Line 0 1102 { 1103 if (lasx == riser3x+a && lasy == riser3y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1104 { 1105 riser3Exist = false; 1106 lasExist=false; 1107 Boom(riser3x,riser3y,false,0); 1108 riser3x = 34; 1109 } 1110 } 1111 for (int a = 0; a < 2; a++) //Line 1 1112 { 1113 if (lasx == riser3x+0 && lasy == riser3y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1114 { 1115 riser3Exist = false; 1116 lasExist=false; 1117 Boom(riser3x,riser3y,false,0); 1118 riser3x = 34; 1119 } 1120 } 1121 for (int a = 0; a < 1; a++) //Line 2 1122 { 1123 if (lasx == riser3x+a && lasy == riser3y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1124 { 1125 riser3Exist = false; 1126 lasExist=false; 1127 Boom(riser3x,riser3y,false,0); 1128 riser3x = 34; 1129 } 1130 } 1131 } 1132 //Collision detection for Riser 4 1133 if (riser4Exist == true) 1134 { 1135 for (int a = 0; a < 3; a++) //Top Line 0 1136 { 1137 if (lasx == riser4x+a && lasy == riser4y) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1138 { 1139 riser4Exist = false; 1140 lasExist=false; 1141 Boom(riser4x,riser4y,false,0); 1142 riser4x = 34; 1143 } 1144 } 1145 for (int a = 0; a < 2; a++) //Line 1 1146 { 1147 if (lasx == riser4x+0 && lasy == riser4y+1) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1148 { 1149 riser4Exist = false; 1150 lasExist=false; 1151 Boom(riser4x,riser4y,false,0); 1152 riser4x = 34; 1153 } 1154 } 1155 for (int a = 0; a < 1; a++) //Line 2 1156 { 1157 if (lasx == riser4x+a && lasy == riser4y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 1158 { 1159 riser4Exist = false; 1160 lasExist=false; 1161 Boom(riser4x,riser4y,false,0); 1162 riser4x = 34; 1163 } 1164 } 1165 } 1166 } 1167} 1168 1169 1170 1171void Boom(int x, int y, bool addKill, int scoreType) //Spawns explosion bitmap at called coords. Then increments kill count 1172{ 1173 matrix.drawBitmap(x,y,explosion,3,3,1); //Draw explosion 1174 tone(buzzerPin, 75,75); 1175 if (addKill == true) 1176 { 1177 kills++; 1178 } 1179 if (scoreType == 1) //Score type for UFOs 1180 { 1181 score = score+10; 1182 } 1183 if (scoreType == 2) //Score type for bouncers 1184 { 1185 score = score+20; 1186 } 1187 if (scoreType == 3) //Scoretype for LT 1188 { 1189 score = score+70; 1190 } 1191 //if (scoreType == 4) //Score type for Risers and Fallers.. Currently not using it 1192 // { 1193 // score = score+30; 1194// } 1195 if (scoreType == 5) //Score type for Boss 1196 { 1197 score = score+(100*levelNumber); 1198 } 1199} 1200 1201 1202void ScreenLimit() 1203{ 1204 //setting boundries of the screen for ship 1205 if (shipx < 0) 1206 { 1207 shipx = 0; 1208 } 1209 if (shipx > 29) 1210 { 1211 shipx = 29; 1212 } 1213 1214 if (shipy < 0) 1215 { 1216 shipy = 0; 1217 } 1218 if (shipy > 6) 1219 { 1220 shipy = 6; 1221 } 1222 1223 if (lasExist == true) //When it goes off the screen then stop drawing it and mark it as not existing 1224 { 1225 1226 if (lasx > 31) 1227 { 1228 lasExist=false; 1229 } 1230 if (lasy < 0) 1231 { 1232 lasy = 0; 1233 } 1234 if (lasy > 7) 1235 { 1236 lasy = 7; 1237 } 1238 } 1239 1240 //UFO laser screen limit controls 1241 1242 if (ufoLas1x < -2) 1243 { 1244 ufoLasExist1 = false; 1245 } 1246 1247 1248 1249 if (ufoLas2x < -2) 1250 { 1251 ufoLasExist2 = false; 1252 } 1253 1254 1255 1256 if (ufoLas3x < -2) 1257 { 1258 ufoLasExist3 = false; 1259 } 1260 1261 1262 1263 if (ufoLas4x < -2) 1264 { 1265 ufoLasExist4 = false; 1266 } 1267 1268 1269 //UFO screen limit controls. Automatically respawning UFO's at a random Y coordinate on the right side if they go off the left side 1270 if (ufo1Exist == true && ufo1x < -3) 1271 { 1272 ufo1x = 33; //if UFO goes off the left side it gets moved to the right side again 1273 ufo1y = random(0,7); 1274 // going to need some sort of sorting code to stop ufo's overlapping 1275 } 1276 if (ufo2Exist == true && ufo2x < -3) 1277 { 1278 ufo2x = 33; //if UFO goes off the left side it gets moved to the right side again 1279 ufo2y = random(0,7); 1280 // going to need some sort of sorting code to stop ufo's overlapping 1281 } 1282 if (ufo3Exist == true && ufo3x < -3) 1283 { 1284 ufo3x = 33; //if UFO goes off the left side it gets moved to the right side again 1285 ufo3y = random(0,7); 1286 // going to need some sort of sorting code to stop ufo's overlapping 1287 } 1288 1289 //Bouncer screen limit controls. When bouncers hit top or bottom the direction variable changes. When off the left they are wiped from existence and respawned at a later time. 1290 1291 if (bouncer1Exist == true && bouncer1y > 5) 1292 { 1293 bouncer1Direction = false; 1294 } 1295 else if (bouncer1Exist == true && bouncer1y < 0) 1296 { 1297 bouncer1Direction = true; 1298 } 1299 1300 if (bouncer2Exist == true && bouncer2y > 5) 1301 { 1302 bouncer2Direction = false; 1303 } 1304 else if (bouncer2Exist == true && bouncer2y < 0) 1305 { 1306 bouncer2Direction = true; 1307 } 1308 1309 if (bouncer3Exist == true && bouncer3y > 5) 1310 { 1311 bouncer3Direction = false; 1312 } 1313 else if (bouncer3Exist == true && bouncer3y < 0) 1314 { 1315 bouncer3Direction = true; 1316 } 1317 1318 if (bouncer4Exist == true && bouncer4y > 5) 1319 { 1320 bouncer4Direction = false; 1321 } 1322 else if (bouncer4Exist == true && bouncer4y < 0) 1323 { 1324 bouncer4Direction = true; 1325 } 1326 // If bouncers go off the left side they disappear as despawned 1327 if (bouncer1x < 0) 1328 { 1329 bouncer1Exist = false; 1330 } 1331 1332 if (bouncer2x < 0) 1333 { 1334 bouncer2Exist = false; 1335 } 1336 1337 if (bouncer3x < 0) 1338 { 1339 bouncer3Exist = false; 1340 } 1341 1342 if (bouncer4x < 0) 1343 { 1344 bouncer4Exist = false; 1345 } 1346 //LT screen limit control 1347 if (ltExist == true && ltx < -6) 1348 { 1349 ltx = 33; //if LT goes off the left side it gets moved to the right side again 1350 lty = random(0,6); 1351 } 1352 //Screen limit controls for Risers 1353 if (faller1Exist == true && faller1y > 7) 1354 { 1355 faller1Exist = false; 1356 } 1357 if (faller2Exist == true && faller2y > 7) 1358 { 1359 faller2Exist = false; 1360 } 1361 if (faller3Exist == true && faller3y > 7) 1362 { 1363 faller3Exist = false; 1364 } 1365 if (faller4Exist == true && faller4y > 7) 1366 { 1367 faller4Exist = false; 1368 } 1369 //Screen limit controls for Risers 1370 if (riser1Exist == true && riser1y < -2) 1371 { 1372 riser1Exist = false; 1373 } 1374 if (riser2Exist == true && riser2y < -2) 1375 { 1376 riser2Exist = false; 1377 } 1378 if (riser3Exist == true && riser3y < -2) 1379 { 1380 riser3Exist = false; 1381 } 1382 if (riser4Exist == true && riser4y < -2) 1383 { 1384 riser4Exist = false; 1385 } 1386 1387} 1388 1389void SpawnTimer() 1390{ 1391 if (spawnEnabled == true) 1392 { 1393 //Counting spawn timer 1394 currentTime = millis(); //get the current time in milliseconds 1395 currentTime2 = millis();//same again for second timer 1396 currentTime3 = millis();//and again 1397 if (currentTime - startTime >= spawnTime) //Uses the currentTime, startTime and spawnTime variables to spawn enemies on set times 1398 { 1399 //spawn UFO 1400 1401 SpawnUFO(); 1402 //reset timer 1403 startTime = currentTime; //Saves the start time of the spawn so the if statement works 1404 } 1405 1406 if (currentTime2 - startTime2 >= spawnTime2) 1407 { 1408 //spawn bouncer squad 1409 1410 SpawnBouncer(); 1411 //reset timer 1412 startTime2 = currentTime2; //Saves the start time of the spawn so the if statement works 1413 } 1414 1415 if (currentTime3 - startTime3 >= spawnTime3) 1416 { 1417 //spawn LT 1418 1419 SpawnLT(); 1420 //reset timer 1421 startTime3 = currentTime3; //Saves the start time of the spawn so the if statement works 1422 } 1423 } 1424 1425} 1426 1427void SpawnUFO() 1428{ 1429 if (ufo1Exist == false) 1430 { 1431 1432 ufo1x = 32; //Spawn off the right side of the screen 1433 ufo1y = random(0,7); //Spawn at a random Y coord 1434 1435 ufo1Exist = true; 1436 1437 } 1438 else if (ufo2Exist == false) 1439 { 1440 ufo2x = 32; //Spawn off the right side of the screen 1441 ufo2y = random(0,7); //Spawn at a random Y coord 1442 1443 ufo2Exist = true; 1444 } 1445 else if (ufo3Exist == false) 1446 { 1447 ufo3x = 32; //Spawn off the right side of the screen 1448 ufo3y = random(0,7); //Spawn at a random Y coord 1449 1450 ufo3Exist = true; 1451 } 1452 1453} 1454 1455void UfoControl() 1456{ 1457 //If the UFO exists then draw its bitmap 1458 if (ufo1Exist == true) 1459 { 1460 ufo1x--; 1461 matrix.drawBitmap(ufo1x,ufo1y,ufo,3,2,1); 1462 } 1463 if (ufo2Exist == true) 1464 { 1465 ufo2x--; 1466 matrix.drawBitmap(ufo2x,ufo2y,ufo,3,2,1); 1467 } 1468 if (ufo3Exist == true) 1469 { 1470 ufo3x--; 1471 matrix.drawBitmap(ufo3x,ufo3y,ufo,3,2,1); 1472 } 1473} 1474 1475 1476void SpawnBouncer() 1477{ 1478 if (bouncer1Exist == false && bouncer2Exist == false && bouncer3Exist == false && bouncer4Exist == false) 1479 { 1480 1481 bouncer1x = 32; //Spawn off the right side of the screen 1482 bouncer2x = bouncer1x+bouncerGap;//spawn other bouncers spaced out on the x axis 1483 bouncer3x = bouncer2x+bouncerGap; 1484 bouncer4x = bouncer3x+bouncerGap; 1485 bouncerOrigin = random(0,2); 1486 if (bouncerOrigin = 0) 1487 { 1488 bouncer1y = 0; //Spawn at top of screen 1489 bouncer2y = 1; 1490 bouncer3y = 2; 1491 bouncer4y = 3; 1492 } 1493 if (bouncerOrigin = 1) 1494 { 1495 bouncer1y = 7; //Spawn at bottom 1496 bouncer2y = 6; 1497 bouncer3y = 5; 1498 bouncer4y = 4; 1499 } 1500 1501 1502 bouncer1Exist = true; 1503 bouncer2Exist = true; 1504 bouncer3Exist = true; 1505 bouncer4Exist = true; 1506 1507 } 1508} 1509 1510void BouncerControl() 1511{ 1512 //Bouncer move across to the left and up or down depending opn direction variable. That variable is changed in ScreenLimit() 1513 1514 if (bouncer1Exist == true) //If bouncer exists then increment bouncer x and y counters. These are used to slow the movement. 1515 { 1516 bouncer1xSpeed++; 1517 bouncer1ySpeed++; 1518 if (bouncer1xSpeed > xSpeed) //When the counter goes above the speed value then IF statement executes 1519 { 1520 bouncer1x--; 1521 bouncer1xSpeed = 0; 1522 } 1523 if (bouncer1Direction == true) 1524 { 1525 if (bouncer1ySpeed > ySpeed) 1526 { 1527 bouncer1y++; 1528 bouncer1ySpeed = 0; 1529 } 1530 } 1531 else if (bouncer1Direction == false) 1532 { 1533 if (bouncer1ySpeed > ySpeed) 1534 { 1535 bouncer1y--; 1536 bouncer1ySpeed = 0; 1537 } 1538 } 1539 matrix.drawBitmap(bouncer1x,bouncer1y,bouncer,2,3,1); 1540 } 1541 if (bouncer2Exist == true) 1542 { 1543 bouncer2xSpeed++; 1544 bouncer2ySpeed++; 1545 if (bouncer2xSpeed > xSpeed) 1546 { 1547 bouncer2x--; 1548 bouncer2xSpeed = 0; 1549 } 1550 if (bouncer2Direction == true) 1551 { 1552 if (bouncer2ySpeed > ySpeed) 1553 { 1554 bouncer2y++; 1555 bouncer2ySpeed = 0; 1556 } 1557 } 1558 else if (bouncer2Direction == false) 1559 { 1560 if (bouncer2ySpeed > 2) 1561 { 1562 bouncer2y--; 1563 bouncer2ySpeed = 0; 1564 } 1565 } 1566 matrix.drawBitmap(bouncer2x,bouncer2y,bouncer,2,3,1); 1567 } 1568 if (bouncer3Exist == true) 1569 { 1570 bouncer3xSpeed++; 1571 bouncer3ySpeed++; 1572 if (bouncer3xSpeed > xSpeed) 1573 { 1574 bouncer3x--; 1575 bouncer3xSpeed = 0; 1576 } 1577 if (bouncer3Direction == true) 1578 { 1579 if (bouncer3ySpeed > ySpeed) 1580 { 1581 bouncer3y++; 1582 bouncer3ySpeed = 0; 1583 } 1584 } 1585 else if (bouncer3Direction == false) 1586 { 1587 if (bouncer3ySpeed > 2) 1588 { 1589 bouncer3y--; 1590 bouncer3ySpeed = 0; 1591 } 1592 } 1593 matrix.drawBitmap(bouncer3x,bouncer3y,bouncer,2,3,1); 1594 } 1595 if (bouncer4Exist == true) 1596 { 1597 bouncer4xSpeed++; 1598 bouncer4ySpeed++; 1599 if (bouncer4xSpeed > xSpeed) 1600 { 1601 bouncer4x--; 1602 bouncer4xSpeed = 0; 1603 } 1604 if (bouncer4Direction == true) 1605 { 1606 if (bouncer4ySpeed > 2) 1607 { 1608 bouncer4y++; 1609 bouncer4ySpeed = 0; 1610 } 1611 } 1612 else if (bouncer4Direction == false) 1613 { 1614 if (bouncer4ySpeed > ySpeed) 1615 { 1616 bouncer4y--; 1617 bouncer4ySpeed = 0; 1618 } 1619 } 1620 matrix.drawBitmap(bouncer4x,bouncer4y,bouncer,2,3,1); 1621 } 1622} 1623 1624void SpawnFaller() 1625{ 1626 if (faller1Exist == false && faller2Exist == false && faller3Exist == false && faller4Exist == false) 1627 { 1628 1629 faller1x = 15; //Spawn above the boss off the top of the screen 1630 faller2x = faller1x+bouncerGap;//spawn other bouncers spaced out on the x axis 1631 faller3x = faller2x+bouncerGap; 1632 faller4x = faller3x+bouncerGap; 1633 faller1y = -1; //Spawn at top of screen 1634 faller2y = -2; 1635 faller3y = -3; 1636 faller4y = -4; 1637 1638 faller1Exist = true; 1639 faller2Exist = true; 1640 faller3Exist = true; 1641 faller4Exist = true; 1642 1643 } 1644} 1645 1646void FallerControl() 1647{ 1648 //Fallers are spawned by the Boss during battle after level 4. Timer for spawning is in the BossControl function. 1649 //Control routines for faller 1 1650 if (faller1Exist == true) //If faller exists then increment fallerSpeed x and y counters. These are used to slow the movement. 1651 { 1652 faller1Speedx++; 1653 faller1Speedy++; 1654 if (faller1Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1655 { 1656 faller1x--; 1657 faller1Speedx = 0; 1658 } 1659 1660 1661 if (faller1Speedy > fallerySpeedTarget) 1662 { 1663 faller1y++; 1664 faller1Speedy = 0; 1665 } 1666 1667 matrix.drawBitmap(faller1x,faller1y,faller,3,3,1); 1668 } 1669// Control routines for faller 2 1670 if (faller2Exist == true) //If faller exists then increment fallerSpeed x and y counters. These are used to slow the movement. 1671 { 1672 faller2Speedx++; 1673 faller2Speedy++; 1674 if (faller2Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1675 { 1676 faller2x--; 1677 faller2Speedx = 0; 1678 } 1679 1680 1681 if (faller2Speedy > fallerySpeedTarget) 1682 { 1683 faller2y++; 1684 faller2Speedy = 0; 1685 } 1686 1687 matrix.drawBitmap(faller2x,faller2y,faller,3,3,1); 1688 } 1689 1690 // Control routines for faller 3 1691 if (faller3Exist == true) //If faller exists then increment fallerSpeed x and y counters. These are used to slow the movement. 1692 { 1693 faller3Speedx++; 1694 faller3Speedy++; 1695 if (faller3Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1696 { 1697 faller3x--; 1698 faller3Speedx = 0; 1699 } 1700 1701 1702 if (faller3Speedy > fallerySpeedTarget) 1703 { 1704 faller3y++; 1705 faller3Speedy = 0; 1706 } 1707 1708 matrix.drawBitmap(faller3x,faller3y,faller,3,3,1); 1709 } 1710 1711 // Control routines for faller 4 1712 if (faller4Exist == true) //If faller exists then increment fallerSpeed x and y counters. These are used to slow the movement. 1713 { 1714 faller4Speedx++; 1715 faller4Speedy++; 1716 if (faller4Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1717 { 1718 faller4x--; 1719 faller4Speedx = 0; 1720 } 1721 1722 1723 if (faller4Speedy > fallerySpeedTarget) 1724 { 1725 faller4y++; 1726 faller4Speedy = 0; 1727 } 1728 1729 matrix.drawBitmap(faller4x,faller4y,faller,3,3,1); 1730 } 1731} 1732 1733 1734void SpawnRiser() 1735{ 1736 if (riser1Exist == false && riser2Exist == false && riser3Exist == false && riser4Exist == false) 1737 { 1738 1739 riser1x = 15; //Spawn above the boss off the top of the screen 1740 riser2x = riser1x+bouncerGap;//spawn other bouncers spaced out on the x axis 1741 riser3x = riser2x+bouncerGap; 1742 riser4x = riser3x+bouncerGap; 1743 riser1y = 7; //Spawn at Bottom of screen 1744 riser2y = 8; 1745 riser3y = 9; 1746 riser4y = 10; 1747 1748 riser1Exist = true; 1749 riser2Exist = true; 1750 riser3Exist = true; 1751 riser4Exist = true; 1752 1753 } 1754} 1755 1756void RiserControl() 1757{ 1758 //Risers are spawned by the Boss during battle after level 5. Timer for spawning is in the BossControl function. 1759 //Control routines for riser 1 1760 if (riser1Exist == true) //If riser exists then increment riserSpeed x and y counters. These are used to slow the movement. 1761 { 1762 riser1Speedx++; 1763 riser1Speedy++; 1764 if (riser1Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1765 { 1766 riser1x--; 1767 riser1Speedx = 0; 1768 } 1769 1770 1771 if (riser1Speedy > fallerySpeedTarget) 1772 { 1773 riser1y--; 1774 riser1Speedy = 0; 1775 } 1776 1777 matrix.drawBitmap(riser1x,riser1y,riser,3,3,1); 1778 } 1779 //Control routines for riser 2 1780 if (riser2Exist == true) //If riser exists then increment riserSpeed x and y counters. These are used to slow the movement. 1781 { 1782 riser2Speedx++; 1783 riser2Speedy++; 1784 if (riser2Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1785 { 1786 riser2x--; 1787 riser2Speedx = 0; 1788 } 1789 1790 1791 if (riser2Speedy > fallerySpeedTarget) 1792 { 1793 riser2y--; 1794 riser2Speedy = 0; 1795 } 1796 1797 matrix.drawBitmap(riser2x,riser2y,riser,3,3,1); 1798 } 1799 //Control routines for riser 3 1800 if (riser3Exist == true) //If riser exists then increment riserSpeed x and y counters. These are used to slow the movement. 1801 { 1802 riser3Speedx++; 1803 riser3Speedy++; 1804 if (riser3Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1805 { 1806 riser3x--; 1807 riser3Speedx = 0; 1808 } 1809 1810 1811 if (riser3Speedy > fallerySpeedTarget) 1812 { 1813 riser3y--; 1814 riser3Speedy = 0; 1815 } 1816 1817 matrix.drawBitmap(riser3x,riser3y,riser,3,3,1); 1818 } 1819 //Control routines for riser 4 1820 if (riser4Exist == true) //If riser exists then increment riserSpeed x and y counters. These are used to slow the movement. 1821 { 1822 riser4Speedx++; 1823 riser4Speedy++; 1824 if (riser4Speedx > fallerxSpeedTarget) //When the counter goes above the speed value then IF statement executes 1825 { 1826 riser4x--; 1827 riser4Speedx = 0; 1828 } 1829 1830 1831 if (riser4Speedy > fallerySpeedTarget) 1832 { 1833 riser4y--; 1834 riser4Speedy = 0; 1835 } 1836 1837 matrix.drawBitmap(riser4x,riser4y,riser,3,3,1); 1838 } 1839 1840} 1841 1842void SpawnLT() 1843{ 1844 if (ltExist == false) 1845 { 1846 1847 ltx = 32; //Spawn off the right side of the screen 1848 lty = random(0,6); //Spawn at a random Y coord 1849 1850 ltExist = true; 1851 } 1852} 1853 1854void LTControl() 1855{ 1856 //If the UFO exists then draw its bitmap 1857 if (ltExist == true) 1858 { 1859 ltxSpeed++; 1860 if (ltxSpeed == ltxSpeedTarget) 1861 { 1862 ltxSpeed = 0; 1863 ltx--; 1864 } 1865 matrix.drawBitmap(ltx, lty, LT, 6, 3, 1); 1866 } 1867 if (ltHealth <= 0) 1868 { 1869 ltExist = false; 1870 Boom(ltx,lty,false,3); //Calls Boom() function to add score. Kills are added manually below. 1871 kills=kills+3; 1872 ltHealth = 3; 1873 } 1874} 1875 1876 1877 1878void BossBattle()//Sounds the warning siren then spawns boss. Still need to finish movement 1879{ 1880 if (kills > killsTarget && bossBattleStarted == false) 1881 { 1882 bossBattleStarted = true; 1883 spawnEnabled = false; //Turns off spawning for other UFO types 1884 for(int i = 5; i > 0; i--) //Play alarm sound 1885 { 1886 for (int x = 0; x < 360; x++) // X from 0 degree->360 degree 1887 { 1888 sinVal = sin(x * (PI / 180)); // Calculate the sine of x 1889 toneVal = 2000 + sinVal * 500; // Calculate sound frequency according to the sine of x 1890 tone(buzzerPin, toneVal); // Output sound frequency to buzzerPin 1891 delay(1); 1892 } 1893 } 1894 tone(buzzerPin, 0, 5); //This is to stop the tone sound. It sticks around otherwise. 1895 if (boss1Exist == false) 1896 { 1897 matrix.drawBitmap(bossx, bossy, boss, 8 ,8 ,1); 1898 boss1Exist = true; 1899 bossStartTime = millis(); //Set start timer for boss weapons 1900 bossStartTime = millis(); //Set start timer for boss minions 1901 } 1902 1903 } 1904} 1905 1906void BossControl() 1907{ 1908 if (boss1Exist == true) 1909 { 1910 bossxSpeed++; 1911 if (bossxSpeed > bossxSpeedTarget && bossx > 24) //When the counter goes above the speed value then IF statement executes 1912 { 1913 bossx--; //Move the boss x cords to the left 1914 bossxSpeed = 0; 1915 } 1916 1917 matrix.drawBitmap(bossx, bossy, boss, 8, 8, 1); //Draw the boss bitmap at the new coords 1918 if (bossx == 24) 1919 { 1920 1921 //Counting shoot timer 1922 bossCurrentTime = millis(); //get the current time in milliseconds 1923 bossCurrentTime2 = millis(); 1924 1925 if (bossCurrentTime - bossStartTime >= bossLaserTimer) //startTimer is set in BossBattle function 1926 { 1927 //shoot laser 1928 selection = random(1,5);//Select laser cannon at random 1929 while (selection == lastSelection)//While loop to ensure that the same laser isnt fired twice in a row too quick. Prevents timing gaps 1930 { 1931 selection = random(1,5); 1932 } 1933 UFOLaserShoot(selection);//Call UFOLaserShoot function with cannon selection 1934 //reset timer 1935 bossStartTime = bossCurrentTime; //Saves the start time of the spawn so the if statement works 1936 } 1937 if (levelNumber >= 4) 1938 { 1939 if (bossCurrentTime2 - bossStartTime2 >= bossMinionTimer) //startTimer is set in BossBattle function 1940 { 1941 //Select minions and spawn them 1942 minionSelection = random(1,3); 1943 if (minionSelection == 1) 1944 { 1945 SpawnFaller(); 1946 } 1947 if (minionSelection == 2) 1948 { 1949 SpawnRiser(); 1950 } 1951 1952 1953 //reset timer 1954 bossStartTime2 = bossCurrentTime2; //Saves the start time of the spawn so the if statement works 1955 } 1956 } 1957 } 1958 //Boss health counter hits zero, trigger boss death and new level 1959 if (bossHealth <= 0) 1960 { 1961 boss1Exist = false; //Boss no longer exists 1962 Boom(bossx,bossy,false,5);//Calls Boom() function to add score 1963 matrix.fillScreen(LOW); //Clear screen 1964 for (int i = 5; i > 0; i--) 1965 { 1966 matrix.drawBitmap(bossx, bossy, bossBoom1 ,8 ,8, 1); //Boss explosion first frame 1967 tone(buzzerPin, 100,100); //Play sound 1968 delay(200); //Wait 1969 //matrix.fillScreen(LOW); //Clear screen 1970 matrix.drawBitmap(bossx, bossy, bossBoom2 ,8 ,8, 1); //Boss explosion second frame 1971 tone(buzzerPin, 200,100); //Play second sound 1972 delay(200); //Wait 1973 } 1974 tone(buzzerPin, 1, 1);//Stop beep 1975 1976 bossx=39; //Move bossx back to 32 1977 matrix.fillScreen(LOW); //clear the screen 1978 gameStart = false; //Mark game as stopped 1979 levelNumber++ ; //increment level 1980 if (levelNumber > 10) 1981 { 1982 GameOver(1); 1983 return; 1984 } 1985 tape = "Level "; //Prepare new tape for ticker function 1986 tape += levelNumber; //Adding level number to ticker string 1987 tape += " Score: "; 1988 tape += score; 1989 ufoLasExist1 = false; 1990 ufoLasExist2 = false; 1991 ufoLasExist3 = false; 1992 ufoLasExist4 = false; 1993 faller1Exist = false; 1994 faller2Exist = false; 1995 faller3Exist = false; 1996 faller4Exist = false; 1997 riser1Exist = false; 1998 riser2Exist = false; 1999 riser3Exist = false; 2000 riser4Exist = false; 2001 ufoLas1x=bossx; 2002 ufoLas2x=bossx; 2003 ufoLas3x=bossx; 2004 ufoLas4x=bossx; 2005 Ticker(true); //Call ticker function to show score and new level 2006 2007 } 2008 } 2009} 2010void UFOLaserShoot(int selection) 2011{ 2012 if (selection == 1) 2013 { 2014 if (ufoLasExist1 == false)//Check to see if the UFO laser already exists 2015 { 2016 ufoLas1x=bossx+1; //spawn laser in the middle 2017 ufoLas1y=bossy+4; 2018 ufoLasExist1=true; 2019 tone(buzzerPin, 200,100);//play laser sound 2020 } 2021 } 2022 if (selection == 2) 2023 { 2024 if (ufoLasExist2 == false) 2025 { 2026 ufoLas2x=bossx+2; //spawn laser at the top side 2027 ufoLas2y=bossy+1; 2028 ufoLasExist2=true; 2029 tone(buzzerPin, 300,100);//play laser sound 2030 } 2031 } 2032 if (selection == 3) 2033 { 2034 if (ufoLasExist3 == false) 2035 { 2036 ufoLas3x=bossx+2; //spawn laser at the bottom side 2037 ufoLas3y=bossy+7; 2038 ufoLasExist3=true; 2039 tone(buzzerPin, 400,100);//play laser sound 2040 } 2041 } 2042 if (selection == 4) 2043 { 2044 if (ufoLasExist4 == false)//Planning to make this one random 2045 { 2046 coinFlip = random(0,2); 2047 if (coinFlip == 0) 2048 { 2049 ufoLas4x=bossx+2; 2050 ufoLas4y=bossy+2; 2051 ufoLasExist4=true; 2052 tone(buzzerPin, 500,100);//play laser sound 2053 } 2054 if (coinFlip == 1) 2055 { 2056 ufoLas4x=bossx+2; 2057 ufoLas4y=bossy+6; 2058 ufoLasExist4=true; 2059 tone(buzzerPin, 500,100);//play laser sound 2060 } 2061 } 2062 } 2063} 2064 2065void UFOLaserControl() 2066{ 2067 if (ufoLasExist1 == true) 2068 { 2069 ufoLas1x--; 2070 matrix.drawBitmap(ufoLas1x, ufoLas1y, ufoLaser, 2, 1, 1); 2071 } 2072 if (ufoLasExist2 == true) 2073 { 2074 ufoLas2x--; 2075 matrix.drawBitmap(ufoLas2x, ufoLas2y, ufoLaser, 2, 1, 1); 2076 } 2077 if (ufoLasExist3 == true) 2078 { 2079 ufoLas3x--; 2080 matrix.drawBitmap(ufoLas3x, ufoLas3y, ufoLaser, 2, 1, 1); 2081 } 2082 if (ufoLasExist4 == true) 2083 { 2084 ufoLas4x--; 2085 matrix.drawBitmap(ufoLas4x, ufoLas4y, ufoLaser, 2, 1, 1); 2086 } 2087 2088 2089} 2090 2091void ShipCollision() 2092{ 2093 if (gameStart == true && collisionDetection == true) 2094 { 2095 //Collision detection for line 0 of ship 2096 //Collision detection for UFO1. Dont need line 0 of UFO because line 0 of ship cannot touch it anyway. 2097 if (ufo1Exist == true) 2098 { 2099 2100 for (int a = 0; a < 2; a++) 2101 { 2102 if (shipx == ufo1x+a && shipy == ufo1y+1) ///Line 1 2103 { 2104 ShipHit(); 2105 } 2106 } 2107 //Collision detection for line 1 of ship.................. 2108 //Contains extra for loop to loop through all the ship line 1 pixels 2109 //Collision detection for UFO1 2110 for (int b = 0; b < 1; b++) //Had b < 2 and the collision detection worked but looked wonky when moving towards ufos 2111 { 2112 for (int a = 0; a < 2; a++) 2113 { 2114 if (shipx+b == ufo1x+a && shipy+1 == ufo1y+1) //Line 1 2115 { 2116 ShipHit(); 2117 } 2118 } 2119 2120 for (int a = 1; a < 3; a++) 2121 { 2122 if (shipx+b == ufo1x+a && shipy+1 == ufo1y) //Line 0 2123 { 2124 ShipHit(); 2125 } 2126 } 2127 } 2128 } 2129 //Collision detection for line 0 of ship 2130 //collision detection for UFO2 2131 if (ufo2Exist == true) 2132 { 2133 2134 for (int a = 0; a < 2; a++) 2135 { 2136 if (shipx == ufo2x+a && shipy == ufo2y+1) ///Line 1 2137 { 2138 ShipHit(); 2139 } 2140 } 2141 //Collision detection for line 1 of ship 2142 //Collision detection for UFO2 2143 for (int b = 0; b < 1; b++) 2144 { 2145 for (int a = 0; a < 2; a++) 2146 { 2147 if (shipx+b == ufo2x+a && shipy+1 == ufo2y+1) //Line 1 2148 { 2149 ShipHit(); 2150 } 2151 } 2152 2153 for (int a = 1; a < 3; a++) 2154 { 2155 if (shipx+b == ufo2x+a && shipy+1 == ufo2y) //Line 0 2156 { 2157 ShipHit(); 2158 } 2159 } 2160 } 2161 } 2162 //Collision detection for line 0 of ship 2163 //Collision detection for UFO3 2164 if (ufo3Exist == true) 2165 { 2166 2167 for (int a = 0; a < 2; a++) 2168 { 2169 if (shipx == ufo3x+a && shipy == ufo3y+1) ///Line 1 2170 { 2171 ShipHit(); 2172 } 2173 } 2174 2175 //Contains extra for loop to loop through all the ship line 1 pixels 2176 //Collision detection for UFO3 2177 for (int b = 0; b < 1; b++) 2178 { 2179 for (int a = 0; a < 2; a++) 2180 { 2181 if (shipx+b == ufo3x+a && shipy+1 == ufo3y+1) //Line 1 2182 { 2183 ShipHit(); 2184 } 2185 } 2186 2187 for (int a = 1; a < 3; a++) 2188 { 2189 if (shipx+b == ufo3x+a && shipy+1 == ufo3y) //Line 0 2190 { 2191 ShipHit(); 2192 } 2193 } 2194 } 2195 } 2196 //Collision detection for line 0 of ship 2197 //Collision detection for Bouncer1. Don't need line 0 and 1 becuase line 0 and 1 of bouncers and line 0 of ship never meet 2198 if (bouncer1Exist == true) 2199 { 2200 for (int a = 1; a < 2; a++) 2201 { 2202 if (shipx == bouncer1x+a && shipy == bouncer1y+2) //Line 2 2203 { 2204 ShipHit(); 2205 } 2206 } 2207 2208 //Collision detection for Bouncer1 and Line 1 of ship 2209 for (int b = 0; b < 1; b++) 2210 { 2211 for (int a = 1; a < 2; a++) 2212 { 2213 if (shipx+b == bouncer1x+a && shipy+1 == bouncer1y) //Line 0 2214 { 2215 ShipHit(); 2216 } 2217 } 2218 for (int a = 0; a < 2; a++) 2219 { 2220 if (shipx+b == bouncer1x+a && shipy+1 == bouncer1y+1) //Line 1 2221 { 2222 ShipHit(); 2223 } 2224 } 2225 for (int a = 1; a < 2; a++) 2226 { 2227 if (shipx+b == bouncer1x+a && shipy+1 == bouncer1y+2) //Line 2 2228 { 2229 ShipHit(); 2230 } 2231 } 2232 } 2233 } 2234 //Collision detection for line 0 of ship 2235 //Collision detection for Bouncer2. Don't need line 0 and 1 because line 0 and 1 of bouncers and line 0 of ship never meet 2236 if (bouncer2Exist == true) 2237 { 2238 2239 for (int a = 1; a < 2; a++) 2240 { 2241 if (shipx == bouncer2x+a && shipy == bouncer2y+2) //Line 2 2242 { 2243 ShipHit(); 2244 } 2245 } 2246 2247 //Collision detection for Bouncer2 and Line 1 of ship 2248 2249 for (int b = 0; b < 1; b++) 2250 { 2251 for (int a = 1; a < 2; a++) 2252 { 2253 if (shipx+b == bouncer2x+a && shipy+1 == bouncer2y) //Line 0 2254 { 2255 ShipHit(); 2256 } 2257 } 2258 for (int a = 0; a < 2; a++) 2259 { 2260 if (shipx+b == bouncer2x+a && shipy+1 == bouncer2y+1) //Line 1 2261 { 2262 ShipHit(); 2263 } 2264 } 2265 for (int a = 1; a < 2; a++) 2266 { 2267 if (shipx+b == bouncer2x+a && shipy+1 == bouncer2y+2) //Line 2 2268 { 2269 ShipHit(); 2270 } 2271 } 2272 } 2273 } 2274 2275 2276 2277 //Collision detection for line 0 of ship 2278 //Collision detection for Bouncer3. Don't need line 0 and 1 becuase line 0 and 1 of bouncers and line 0 of ship never meet 2279 if (bouncer3Exist == true) 2280 { 2281 2282 for (int a = 1; a < 2; a++) 2283 { 2284 if (shipx == bouncer3x+a && shipy == bouncer3y+2) //Line 2 2285 { 2286 ShipHit(); 2287 } 2288 } 2289 2290 //Collision detection for Bouncer3 and Line 1 of ship 2291 for (int b = 0; b < 1; b++) 2292 { 2293 for (int a = 1; a < 2; a++) 2294 { 2295 if (shipx+b == bouncer3x+a && shipy+1 == bouncer3y) //Line 0 2296 { 2297 ShipHit(); 2298 } 2299 } 2300 for (int a = 0; a < 2; a++) 2301 { 2302 if (shipx+b == bouncer3x+a && shipy+1 == bouncer3y+1) //Line 1 2303 { 2304 ShipHit(); 2305 } 2306 } 2307 for (int a = 1; a < 2; a++) 2308 { 2309 if (shipx+b == bouncer3x+a && shipy+1 == bouncer3y+2) //Line 2 2310 { 2311 ShipHit(); 2312 } 2313 } 2314 } 2315 } 2316 2317 //Collision detection for line 0 of ship 2318 //Collision detection for Bouncer4. Don't need line 0 and 1 becuase line 0 and 1 of bouncers and line 0 of ship never meet 2319 if (bouncer4Exist == true) 2320 { 2321 2322 for (int a = 1; a < 2; a++) 2323 { 2324 if (shipx == bouncer4x+a && shipy == bouncer4y+2) //Line 2 2325 { 2326 ShipHit(); 2327 } 2328 } 2329 2330 //Collision detection for Bouncer4 and Line 1 of ship 2331 for (int b = 0; b < 1; b++) 2332 { 2333 for (int a = 1; a < 2; a++) 2334 { 2335 if (shipx+b == bouncer4x+a && shipy+1 == bouncer4y) //Line 0 2336 { 2337 ShipHit(); 2338 } 2339 } 2340 for (int a = 0; a < 2; a++) 2341 { 2342 if (shipx+b == bouncer4x+a && shipy+1 == bouncer4y+1) //Line 1 2343 { 2344 ShipHit(); 2345 } 2346 } 2347 for (int a = 1; a < 2; a++) 2348 { 2349 if (shipx+b == bouncer4x+a && shipy+1 == bouncer4y+2) //Line 2 2350 { 2351 ShipHit(); 2352 } 2353 } 2354 } 2355 } 2356 2357 2358 2359//Dont need collision detection for lines 0 and 1 of LT and line 0 of ship because they cant touch anyway 2360//Collision detection routines for LT 2361 if (ltExist == true) 2362 { 2363 2364 for (int a = 2; a < 4; a++) //Line 2 2365 { 2366 if (shipx == ltx+a && shipy == lty+2) 2367 { 2368 ShipHit(); 2369 } 2370 } 2371 2372 //Collision detection routines for LT and Ship line 1 2373 for (int b = 0; b < 2; b++) 2374 { 2375 for (int a = 1; a < 4; a++) //Top line 0 2376 { 2377 if (shipx+b == ltx+a && shipy+1 == lty) 2378 { 2379 ShipHit(); 2380 } 2381 } 2382 for (int a = 0; a < 5; a++) //Line 1 2383 { 2384 if (shipx+b == ltx+a && shipy+1 == lty+1) 2385 { 2386 ShipHit(); 2387 } 2388 } 2389 for (int a = 2; a < 4; a++) //Line 2 2390 { 2391 if (shipx+b == ltx+a && shipy+1 == lty+2) 2392 { 2393 ShipHit(); 2394 } 2395 } 2396 } 2397 } 2398 2399//Dont need collision detection between line 0 of ship and line 0 and 1 of fallers because they will never touch 2400//Collision detection for Faller 1 2401 if (faller1Exist == true) 2402 { 2403 2404 for (int a = 0; a < 3; a++) //Line 2 2405 { 2406 if (shipx == faller1x+a && shipy == faller1y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2407 { 2408 ShipHit(); 2409 } 2410 } 2411 2412 //Collision detection for Faller 1 and ship line 1 2413 for (int b = 0; b < 1; b++) 2414 { 2415 for (int a = 0; a < 2; a++) //Top Line 0 2416 { 2417 if (shipx+b == faller1x+a && shipy+1 == faller1y) 2418 { 2419 ShipHit(); 2420 } 2421 } 2422 for (int a = 0; a < 3; a++) //Line 1 2423 { 2424 if (shipx+b == faller1x+a && shipy+1 == faller1y+1) 2425 { 2426 ShipHit(); 2427 } 2428 } 2429 for (int a = 0; a < 3; a++) //Line 2 2430 { 2431 if (shipx+b == faller1x+a && shipy+1 == faller1y+2) 2432 { 2433 ShipHit(); 2434 } 2435 } 2436 } 2437 } 2438 //Dont need collision detection between line 0 of ship and line 0 and 1 of fallers because they will never touch 2439 //Collision detection for Faller 2 2440 if (faller2Exist == true) 2441 { 2442 2443 for (int a = 0; a < 3; a++) //Line 2 2444 { 2445 if (shipx == faller2x+a && shipy == faller2y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2446 { 2447 ShipHit(); 2448 } 2449 } 2450 2451 //Collision detection for Faller 2 and ship line 1 2452 for (int b = 0; b < 1; b++) 2453 { 2454 for (int a = 0; a < 2; a++) //Top Line 0 2455 { 2456 if (shipx+b == faller2x+a && shipy+1 == faller2y) 2457 { 2458 ShipHit(); 2459 } 2460 } 2461 for (int a = 0; a < 3; a++) //Line 1 2462 { 2463 if (shipx+b == faller2x+a && shipy+1 == faller2y+1) 2464 { 2465 ShipHit(); 2466 } 2467 } 2468 for (int a = 0; a < 3; a++) //Line 2 2469 { 2470 if (shipx+b == faller2x+a && shipy+1 == faller2y+2) 2471 { 2472 ShipHit(); 2473 } 2474 } 2475 } 2476 } 2477 //Dont need collision detection between line 0 of ship and line 0 and 1 of fallers because they will never touch 2478 //Collision detection for Faller 3 2479 if (faller3Exist == true) 2480 { 2481 2482 for (int a = 0; a < 3; a++) //Line 2 2483 { 2484 if (shipx == faller3x+a && shipy == faller3y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2485 { 2486 ShipHit(); 2487 } 2488 } 2489 2490 //Collision detection for Faller 3 and ship line 1 2491 for (int b = 0; b < 1; b++) 2492 { 2493 for (int a = 0; a < 2; a++) //Top Line 0 2494 { 2495 if (shipx+b == faller3x+a && shipy+1 == faller3y) 2496 { 2497 ShipHit(); 2498 } 2499 } 2500 for (int a = 0; a < 3; a++) //Line 1 2501 { 2502 if (shipx+b == faller3x+a && shipy+1 == faller3y+1) 2503 { 2504 ShipHit(); 2505 } 2506 } 2507 for (int a = 0; a < 3; a++) //Line 2 2508 { 2509 if (shipx+b == faller3x+a && shipy+1 == faller3y+2) 2510 { 2511 ShipHit(); 2512 } 2513 } 2514 } 2515 } 2516 //Dont need collision detection between line 0 of ship and line 0 and 1 of fallers because they will never touch 2517 //Collision detection for Faller 4 2518 if (faller4Exist == true) 2519 { 2520 2521 for (int a = 0; a < 3; a++) //Line 2 2522 { 2523 if (shipx == faller4x+a && shipy == faller4y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2524 { 2525 ShipHit(); 2526 } 2527 } 2528 2529 //Collision detection for Faller 4 and ship line 1 2530 for (int b = 0; b < 1; b++) 2531 { 2532 for (int a = 0; a < 2; a++) //Top Line 0 2533 { 2534 if (shipx+b == faller4x+a && shipy+1 == faller4y) 2535 { 2536 ShipHit(); 2537 } 2538 } 2539 for (int a = 0; a < 3; a++) //Line 1 2540 { 2541 if (shipx+b == faller4x+a && shipy+1 == faller4y+1) 2542 { 2543 ShipHit(); 2544 } 2545 } 2546 for (int a = 0; a < 3; a++) //Line 2 2547 { 2548 if (shipx+b == faller4x+a && shipy+1 == faller4y+2) 2549 { 2550 ShipHit(); 2551 } 2552 } 2553 } 2554 } 2555 //Dont need collision detection between line 0 of ship and lines 0 and 1 of risers because they'll never touch 2556 //Collision detection for Riser 1 2557 if (riser1Exist == true) 2558 { 2559 2560 for (int a = 0; a < 1; a++) //Line 2 2561 { 2562 if (shipx == riser1x+a && shipy == riser1y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2563 { 2564 ShipHit(); 2565 } 2566 } 2567 2568 //Collision detection for Riser 1 and Ship line 1 2569 for (int b = 0; b < 1; b++) 2570 { 2571 for (int a = 0; a < 3; a++) //Top Line 0 2572 { 2573 if (shipx+b == riser1x+a && shipy+1 == riser1y) 2574 { 2575 ShipHit(); 2576 } 2577 } 2578 for (int a = 0; a < 2; a++) //Line 1 2579 { 2580 if (shipx+b == riser1x+0 && shipy+1 == riser1y+1) 2581 { 2582 ShipHit(); 2583 } 2584 } 2585 for (int a = 0; a < 1; a++) //Line 2 2586 { 2587 if (shipx+b == riser1x+a && shipy+1 == riser1y+2) 2588 { 2589 ShipHit(); 2590 } 2591 } 2592 } 2593 } 2594 2595 //Dont need collision detection between line 0 of ship and lines 0 and 1 of risers because they'll never touch 2596 //Collision detection for Riser 2 2597 if (riser2Exist == true) 2598 { 2599 2600 for (int a = 0; a < 1; a++) //Line 2 2601 { 2602 if (shipx == riser2x+a && shipy == riser2y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2603 { 2604 ShipHit(); 2605 } 2606 } 2607 2608 //Collision detection for Riser 2 and Ship line 1 2609 for (int b = 0; b < 1; b++) 2610 { 2611 for (int a = 0; a < 3; a++) //Top Line 0 2612 { 2613 if (shipx+b == riser2x+a && shipy+1 == riser2y) 2614 { 2615 ShipHit(); 2616 } 2617 } 2618 for (int a = 0; a < 2; a++) //Line 1 2619 { 2620 if (shipx+b == riser2x+0 && shipy+1 == riser2y+1) 2621 { 2622 ShipHit(); 2623 } 2624 } 2625 for (int a = 0; a < 1; a++) //Line 2 2626 { 2627 if (shipx+b == riser2x+a && shipy+1 == riser2y+2) 2628 { 2629 ShipHit(); 2630 } 2631 } 2632 } 2633 } 2634//Dont need collision detection between line 0 of ship and lines 0 and 1 of risers because they'll never touch 2635 //Collision detection for Riser 3 2636 if (riser3Exist == true) 2637 { 2638 2639 for (int a = 0; a < 1; a++) //Line 2 2640 { 2641 if (shipx == riser3x+a && shipy == riser3y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2642 { 2643 ShipHit(); 2644 } 2645 } 2646 2647 //Collision detection for Riser 3 and Ship line 1 2648 for (int b = 0; b < 1; b++) 2649 { 2650 for (int a = 0; a < 3; a++) //Top Line 0 2651 { 2652 if (shipx+b == riser3x+a && shipy+1 == riser3y) 2653 { 2654 ShipHit(); 2655 } 2656 } 2657 for (int a = 0; a < 2; a++) //Line 1 2658 { 2659 if (shipx+b == riser3x+0 && shipy+1 == riser3y+1) 2660 { 2661 ShipHit(); 2662 } 2663 } 2664 for (int a = 0; a < 1; a++) //Line 2 2665 { 2666 if (shipx+b == riser3x+a && shipy+1 == riser3y+2) 2667 { 2668 ShipHit(); 2669 } 2670 } 2671 } 2672 } 2673 //Dont need collision detection between line 0 of ship and lines 0 and 1 of risers because they'll never touch 2674 //Collision detection for Riser 4 2675 if (riser4Exist == true) 2676 { 2677 2678 for (int a = 0; a < 1; a++) //Line 2 2679 { 2680 if (shipx == riser4x+a && shipy == riser4y+2) //Checks to see if laser coords match bouncer bitmap coords, including the pixels just behind them. 2681 { 2682 ShipHit(); 2683 } 2684 } 2685 2686 //Collision detection for Riser 4 and Ship line 1 2687 for (int b = 0; b < 1; b++) 2688 { 2689 for (int a = 0; a < 3; a++) //Top Line 0 2690 { 2691 if (shipx+b == riser4x+a && shipy+1 == riser4y) 2692 { 2693 ShipHit(); 2694 } 2695 } 2696 for (int a = 0; a < 2; a++) //Line 1 2697 { 2698 if (shipx+b == riser4x+0 && shipy+1 == riser4y+1) 2699 { 2700 ShipHit(); 2701 } 2702 } 2703 for (int a = 0; a < 1; a++) //Line 2 2704 { 2705 if (shipx+b == riser4x+a && shipy+1 == riser4y+2) 2706 { 2707 ShipHit(); 2708 } 2709 } 2710 } 2711 } 2712 2713 //No collision detection for line 0 of ship and boss because they'll never touch anyway. 2714 //Collision detection for Boss abd Ship line 1 2715 if (boss1Exist == true) 2716 { 2717 for (int b = 0; b < 1; b++) 2718 { 2719 for (int a = 3; a < 5; a++ ) //Top line 0 2720 { 2721 if (shipx+b == bossx+a && shipy+1 == bossy) 2722 { 2723 ShipHit(); 2724 } 2725 } 2726 for (int a = 2; a < 5; a++) //Line 1 2727 { 2728 if (shipx+b == bossx+a && shipy+1 == bossy+1) 2729 { 2730 ShipHit(); 2731 } 2732 } 2733 for (int a = 1; a < 6; a++) //Line 2 2734 { 2735 if (shipx+b == bossx+a && shipy+1 == bossy+2) 2736 { 2737 ShipHit(); 2738 } 2739 } 2740 for (int a = 0; a < 7; a++) //Line 3 2741 { 2742 if (shipx+b == bossx+a && shipy+1 == bossy+3) 2743 { 2744 ShipHit(); 2745 } 2746 } 2747 for (int a = 2; a < 6; a++) //Line 4 2748 { 2749 if (shipx+b == bossx+a && shipy+1 == bossy+4) 2750 { 2751 ShipHit(); 2752 } 2753 } 2754 for (int a = 0; a < 7; a++) //Line 5 2755 { 2756 if (shipx+b == bossx+a && shipy+1 == bossy+5) 2757 { 2758 ShipHit(); 2759 } 2760 } 2761 for (int a = 1; a < 6; a++) //Line 6 2762 { 2763 if (shipx+b == bossx+a && shipy+1 == bossy+6) 2764 { 2765 ShipHit(); 2766 } 2767 } 2768 for (int a = 2; a < 5; a++) //Line 7 2769 { 2770 if (shipx+b == bossx+a && shipy+1 == bossy+7) 2771 { 2772 ShipHit(); 2773 } 2774 } 2775 } 2776 } 2777 2778 //Collision detection for UFO Lasers. They only have line 0 2779 if (ufoLasExist1 == true) 2780 { 2781 2782 for (int a = 0; a < 1; a++) 2783 { 2784 if (shipx == ufoLas1x+a && shipy == ufoLas1y) ///Line 0 2785 { 2786 ShipHit(); 2787 } 2788 } 2789 for (int b = 0; b < 1; b++) 2790 { 2791 for (int a = 0; a < 1; a++) 2792 { 2793 if (shipx+b == ufoLas1x+a && shipy+1 == ufoLas1y) ///Line 1 2794 { 2795 ShipHit(); 2796 } 2797 } 2798 } 2799 } 2800 //Collision detection for UFO Lasers. They only have line 0 2801 if (ufoLasExist2 == true) 2802 { 2803 2804 for (int a = 0; a < 1; a++) 2805 { 2806 if (shipx == ufoLas2x+a && shipy == ufoLas2y) ///Line 0 2807 { 2808 ShipHit(); 2809 } 2810 } 2811 for (int b = 0; b < 1; b++) 2812 { 2813 for (int a = 0; a < 1; a++) 2814 { 2815 if (shipx+b == ufoLas2x+a && shipy+1 == ufoLas2y) ///Line 1 2816 { 2817 ShipHit(); 2818 } 2819 } 2820 } 2821 } 2822 //Collision detection for UFO Lasers. They only have line 0 2823 if (ufoLasExist3 == true) 2824 { 2825 2826 for (int a = 0; a < 1; a++) 2827 { 2828 if (shipx == ufoLas3x+a && shipy == ufoLas3y) ///Line 0 2829 { 2830 ShipHit(); 2831 } 2832 } 2833 for (int b = 0; b < 1; b++) 2834 { 2835 for (int a = 0; a < 1; a++) 2836 { 2837 if (shipx+b == ufoLas3x+a && shipy+1 == ufoLas3y) ///Line 1 2838 { 2839 ShipHit(); 2840 } 2841 } 2842 } 2843 } 2844 //Collision detection for UFO Lasers. They only have line 0 2845 if (ufoLasExist4 == true) 2846 { 2847 2848 for (int a = 0; a < 1; a++) 2849 { 2850 if (shipx == ufoLas4x+a && shipy == ufoLas4y) ///Line 0 2851 { 2852 ShipHit(); 2853 } 2854 } 2855 for (int b = 0; b < 1; b++) 2856 { 2857 for (int a = 0; a < 1; a++) 2858 { 2859 if (shipx+b == ufoLas4x+a && shipy+1 == ufoLas4y) ///Line 1 2860 { 2861 ShipHit(); 2862 } 2863 } 2864 } 2865 } 2866 } 2867} 2868void GameOver(int gameStatus) 2869{ 2870 spawnEnabled = false; 2871 if (gameStatus == 0) //Game over due to death 2872 { 2873 tape = "Game Over :( Your score was "; 2874 tape += score; 2875 // Ticker(true); 2876 tape = "UFO ATTACK! - Press fire to start "; 2877 lives = 5; 2878 score = 0; 2879 levelNumber = 1; 2880 } 2881 else if (gameStatus == 1)//Game over due to winning level 10 2882 { 2883 tape = "Congratulations! You defeated the alien invasion :) Your total score was "; 2884 tape += score; 2885 //Ticker(true); 2886 lives = 5; 2887 score = 0; 2888 levelNumber = 1; 2889 } 2890 else if (gameStatus == 2)//Life lost 2891 { 2892 tape = "Ship destroyed! "; 2893 tape += "Lives left: "; 2894 tape += lives; 2895 } 2896 2897 2898 ufo1Exist = false; 2899 ufo2Exist = false; 2900 ufo3Exist = false; 2901 bouncer1Exist = false; 2902 bouncer2Exist = false; 2903 bouncer3Exist = false; 2904 bouncer4Exist = false; 2905 faller1Exist = false; 2906 faller2Exist = false; 2907 faller3Exist = false; 2908 faller4Exist = false; 2909 riser1Exist = false; 2910 riser2Exist = false; 2911 riser3Exist = false; 2912 riser4Exist = false; 2913 ltExist = false; 2914 boss1Exist = false; 2915 gameStart = false; 2916 ufoLasExist1 = false; 2917 ufoLasExist2 = false; 2918 ufoLasExist3 = false; 2919 ufoLasExist4 = false; 2920 ufoLas1x = 34; 2921 ufoLas1y = 34; 2922 ufoLas2x = 34; 2923 ufoLas2y = 34; 2924 ufoLas3x = 34; 2925 ufoLas3y = 34; 2926 ufoLas4x = 34; 2927 ufoLas4y = 34; 2928 ufo1x = 34; 2929 ufo2x = 34; 2930 ufo3x = 34; 2931 bouncer1x = 34; 2932 bouncer2x = 34; 2933 bouncer3x = 34; 2934 bouncer4x = 34; 2935 faller1x = 34; 2936 faller2x = 34; 2937 faller3x = 34; 2938 faller4x = 34; 2939 riser1x = 34; 2940 riser2x = 34; 2941 riser3x = 34; 2942 riser4x = 34; 2943 ltx = 34; 2944 bossx = 34; 2945 shipx = 0; 2946 shipy = 3; 2947 lasExist = false; 2948 2949if (gameStatus == 2) 2950{ 2951 Ticker(false); 2952} 2953else if (gameStatus == 0 || gameStatus == 1) 2954{ 2955 Ticker(true); 2956} 2957} 2958 2959 2960void ShipHit() 2961{ 2962 lives--; 2963 for (int i = 5000; i > 1; i = i-5) 2964 { 2965 tone(buzzerPin, i, 1); 2966 delay(1); 2967 } 2968 if (lives > 0) 2969 { 2970 GameOver(2); 2971 } 2972 else if (lives <= 0) 2973 { 2974 GameOver(0); 2975 } 2976}
Downloadable files
UFO Attack Wiring
Wiring Schematic for UFO Attack game.
UFO Attack Wiring
UFO Attack Wiring
Wiring Schematic for UFO Attack game.
UFO Attack Wiring
Comments
Only logged in users can leave comments
Anonymous user
2 years ago
Anyone?
MrNonesuch
2 years ago
https://www.arduino.cc/en/guide/libraries You use the library manager to install any libraries as far as i know.
Anonymous user
2 years ago
thanks for you help,
Anonymous user
2 years ago
How can I load the Adafruit_GFX.h file? I got this: Arduino:1.8.5 (Mac OS X), 开发板:"Arduino/Genuino Uno" /Users/1701360/Desktop/UFO attack/ufo_attack_code/ufo_attack_code.ino:2:26: fatal error: Adafruit_GFX.h: No such file or directory #include <Adafruit_GFX.h> ^ compilation terminated. exit status 1 为开发板 Arduino/Genuino Uno 编译时出错。 在文件 -> 首选项开启 “编译过程中显示详细输出”选项 这份报告会包含更多信息。
tscjbddl
6 years ago
what kind of joystick should i buy? two button or one button
tscjbddl
6 years ago
My Joystick has problem? The words are on the screen, but I still can't play qwq qwq qwq
tscjbddl
6 years ago
Anyone?
tscjbddl
2 years ago
thanks for you help,
MrNonesuch
2 years ago
https://www.arduino.cc/en/guide/libraries You use the library manager to install any libraries as far as i know.
tscjbddl
6 years ago
How can I load the Adafruit_GFX.h file? I got this: Arduino:1.8.5 (Mac OS X), 开发板:"Arduino/Genuino Uno" /Users/1701360/Desktop/UFO attack/ufo_attack_code/ufo_attack_code.ino:2:26: fatal error: Adafruit_GFX.h: No such file or directory #include <Adafruit_GFX.h> ^ compilation terminated. exit status 1 为开发板 Arduino/Genuino Uno 编译时出错。 在文件 -> 首选项开启 “编译过程中显示详细输出”选项 这份报告会包含更多信息。
MrNonesuch
0 Followers
•1 Projects
7
11
UFO Attack! | Arduino Project Hub
Anonymous user
2 years ago
My Joystick has problem? The words are on the screen, but I still can't play qwq qwq qwq