/** Meteor Defense by Liam Peixoto In this program, players control an orbital spaceship defending the Earth from falling debris from a recent meteor strike on the Moon. Players control their ship by using WSAD and Spacebar to fire a projectile. **/ float shipInitialStart = 0; //Initial start of ship float shipCurrentPos; //Ship position float bulletX; float bulletY; float bulletWidth; float bulletHeight; float randomEnemyInitial = 0; //Asteroid initial start float enemyY = 185; float enemyWidth = 0; float enemyHeight = 0; float randomGen; //Random number generator int counter; boolean shipExist = false; //Boolean for spawning the ship boolean bulletExist = false; //Boolean for creating a bullet boolean enemyExist = false; //Boolean for creating an asteroid void setup() { size(400, 400); frameRate(60); } void draw() { background(0); bGStars(); bGPattern(); planet(); if (enemyExist == false); { enemy(); } if (enemyExist == true) { enemyMove(); } if (bulletExist == true) { bulletTravel(); } if (shipExist == false) { spaceShipStart(); shipExist = true; } if (shipExist == true) { spaceShip(); } } /////////////////////////////////////////////// ///////////////FUNCTIONS/////////////////////// /////////////////////////////////////////////// void enemyMove() //This function moves the enemy towards the ship slowly. Resets the enemy if it moves to a certain Y value { fill(50); ellipse(randomEnemyInitial, enemyY+0.2, enemyWidth, enemyHeight); enemyY = enemyY+0.2; enemyWidth = enemyWidth+0.2; enemyHeight = enemyHeight+0.2; if (enemyY >= 350) { resetEnemy(); } } /////////////////////////////////////////////// void resetEnemy() //Resets the enemy to a new random spawn location { enemyExist = false; randomEnemyInitial = 0; enemyY = 185; enemyWidth =0; enemyHeight = 0; counter = 0; } /////////////////////////////////////////////// void enemy() //Ditermines the initial spawn point of the enemy and adds a 5 second delay to spawning an enemy { if (randomEnemyInitial == 0) { randomGen = random(50, 351); counter = millis(); } if (counter >= 5000) { randomEnemyInitial = randomGen; enemyExist = true; } } /////////////////////////////////////////////// void bulletTravel() //Function for bullet movement and collision. Is called when space is pressed. Fires a bullet upwards which shrinks as it moves away. { if (bulletWidth <= 0) { bulletExist = false; } if ((int)bulletY <= (int)enemyY) { if (bulletX <= (randomEnemyInitial+(enemyWidth/2)) && bulletX >= (randomEnemyInitial-(enemyWidth/2))) { resetEnemy(); bulletExist = false; } } fill(#d60000); stroke(#ff0000); strokeWeight(2); ellipse(bulletX, bulletY-2, bulletWidth-0.5, bulletHeight-0.5); bulletY = bulletY-2; bulletWidth = bulletWidth-0.4; bulletHeight = bulletHeight-0.4; } /////////////////////////////////////////////// void keyPressed() //Contains various key pressed functions for the A, D, and SPACEBAR keys. A and D move the ship, while space fires a bullet { //Called when keys are pressed if (key == 'd') { shipCurrentPos = shipCurrentPos+15; if (shipCurrentPos >= 380) { shipCurrentPos = 380; } } if (key == 'a') { shipCurrentPos = shipCurrentPos-15; if (shipCurrentPos <= 20) { shipCurrentPos = 20; } } if (key == ' ') { bulletX = shipCurrentPos; bulletY = 305; bulletWidth = 20; bulletHeight = 20; bulletExist = true; } } /////////////////////////////////////////////// void spaceShip() //This function creates all graphic aspects of the ship and adds a slight animation to them { rectMode(CENTER); ellipseMode(CENTER); fill(#470000); stroke(#7c0101); strokeWeight(3); quad((shipCurrentPos-20), 290+3*(sin(radians(frameCount))), (shipCurrentPos-10), 280+3*(sin(radians(frameCount))), (shipCurrentPos+10), 280+3*(sin(radians(frameCount))), (shipCurrentPos+20), 290+3*(sin(radians(frameCount)))); fill(100); strokeWeight(1); noStroke(); rect(shipCurrentPos, 305+3*(sin(radians(frameCount))), 75, 30); fill(110); triangle((shipCurrentPos-36.5), 320+3*(sin(radians(frameCount))), (shipCurrentPos-100), 315+3*(sin(radians(frameCount))), (shipCurrentPos-36.5), 290+3*(sin(radians(frameCount)))); triangle((shipCurrentPos+36.5), 320+3*(sin(radians(frameCount))), (shipCurrentPos+100), 315+3*(sin(radians(frameCount))), (shipCurrentPos+36.5), 290+3*(sin(radians(frameCount)))); fill(50); ellipse((shipCurrentPos-18.75), 305+3*(sin(radians(frameCount))), 30, 30); ellipse((shipCurrentPos+18.75), 305+3*(sin(radians(frameCount))), 30, 30); fill(#ff6b21); ellipse((shipCurrentPos-18.75), 305+3*(sin(radians(frameCount))), 23+(sin(radians(frameCount*80))), 23+(sin(radians(frameCount*80)))); ellipse((shipCurrentPos+18.75), 305+3*(sin(radians(frameCount))), 23+(sin(radians(frameCount*80))), 23+(sin(radians(frameCount*80)))); fill(#ffe182); ellipse((shipCurrentPos-18.75), 305+3*(sin(radians(frameCount))), 15+(sin(radians(frameCount*80))), 15+(sin(radians(frameCount*80)))); ellipse((shipCurrentPos+18.75), 305+3*(sin(radians(frameCount))), 15+(sin(radians(frameCount*80))), 15+(sin(radians(frameCount*80)))); } /////////////////////////////////////////////// void spaceShipStart() //Figures out the initial start of the ship and stores it's location { if (shipInitialStart == 0) { shipInitialStart = random(175, 226); } fill(100); shipCurrentPos = shipInitialStart; } /////////////////////////////////////////////// void planet() //Creates the Panet beneath the ship { fill(#102657); noStroke(); ellipse(200, 450, 800, 300); fill(#263e7e); ellipse(200, 460, 800, 300); fill(#2b49a1); ellipse(200, 470, 800, 300); fill(#004c06); quad(0, 400, 0, 385, 50, 355, 50, 400); quad(50, 355, 50, 400, 75, 400, 75, 380); quad(75, 400, 75, 380, 125, 400, 125, 410); } /////////////////////////////////////////////// void bGPattern() //Creates the moon and meteor impact visible in the background { fill(100); noStroke(); ellipse(450, 100, 400, 400); fill(75); ellipse(375,175,75,75); ellipse(325,25,65,65); for (int i=0; i<300; i++) { fill(#d36200); noStroke(); ellipse(i, i/3, 60, 60); } for (int j=0; j<300; j++) { fill(#ffdb66); noStroke(); ellipse(j, j/3, 40, 40); } for(int k=0;k<300;k++) { fill(#ffecb2); noStroke(); ellipse(k,k/3,20,20); } fill(#d36200); ellipse(300,100,120+7*(sin(radians(frameCount/4))),120+7*(sin(radians(frameCount/4)))); fill(#ffdb66); ellipse(300,100,90+5*(sin(radians(frameCount/3))),90+5*(sin(radians(frameCount/3)))); fill(#ffecb2); ellipse(300,100,60+5*(sin(radians(frameCount/3))),60+5*(sin(radians(frameCount/3)))); } /////////////////////////////////////////////// void bGStars() //Creates the stars in the background. { fill(255); stroke(255); point(189, 354); point(337, 225); point(153, 43); point(104, 225); point(262, 382); point(73, 77); point(115, 38); stroke(#ffe03d); point(129, 252); point(52, 209); point(278, 248); point(72, 374); point(252, 27); stroke(#bc0000); point(209, 136); point(117, 60); point(204, 103); point(147, 197); point(91, 348); point(113, 156); point(350, 200); }