Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Project #33
- //Intro to Computer Programing – JavaScript
- //Space Invaders - Simulate the 1978 Taito arcade game by Tomohiro Nishikado.
- //laser cannon
- var xCannon = 100; //x position of base - upper left
- var yCannon = 360; //y position of base - upper left
- var wCannon = 30; //width of base
- var hCannon = 12; //height of base
- var iCannon = 1.5; //increment of laser cannon
- var numCannon = 3; //number of lives for laser cannon
- //missile from laser cannon
- var missile = false; //if there is a missile being fired
- var xMissile = 0; //x position of missile
- var yMissile = 0; //y position of missile
- var wMissile = 2; //width of missile
- var hMissile = 10; //height of missile
- var iMissile = 5.5; //increment of missile - speed
- //collision detection
- var x,y; //(x,y) position of upper left of current alien or laser cannon
- var w,h; //width and height of current alien or laser cannon
- //alien
- var d = 30; //distance between beginning of one alien to another
- var iAlien = -0.3; //increment of alien - speed/direction
- var leftRight = -1; //negative-left and positive-right
- var startNumAliens = 55; //starting number of aliens
- var numAliens = startNumAliens; //number of aliens in play - 5 rows of 11 = 55
- var alienState1 = true; //2 graphic states of alien - true=open | false=close
- //bomb from alien
- var bomb = false; //if there is a bomb from an alien
- var xBomb = 0; //x position of bomb
- var yBomb = 0; //y position of bomb
- var wBomb = 2; //width of bomb
- var hBomb = 10; //height of bomb
- var iBomb = 2.0; //increment of bomb - speed
- var cBomb = color(0,0,0); //color of the bomb - depends on alien color
- var aliens = [ //creating an array of alien objects
- //(x,y) upper left coordinate of alien
- //w,h are width and height of alien
- //a represents whether the alien is alive (true) or not (false)
- //v represents point value of the alien
- //c represents color of the alien
- {x: 10+d* 0, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)}, //top row
- {x: 10+d* 1, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 2, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 3, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 4, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 5, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 6, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 7, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 8, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 9, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d*10, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
- {x: 10+d* 0, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)}, //2nd row
- {x: 10+d* 1, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 2, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 3, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 4, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 5, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 6, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 7, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 8, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 9, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d*10, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 0, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)}, //3rd row
- {x: 10+d* 1, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 2, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 3, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 4, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 5, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 6, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 7, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 8, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 9, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d*10, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
- {x: 10+d* 0, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)}, //4th row
- {x: 10+d* 1, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 2, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 3, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 4, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 5, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 6, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 7, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 8, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 9, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d*10, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 0, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)}, //5th row
- {x: 10+d* 1, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 2, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 3, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 4, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 5, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 6, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 7, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 8, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d* 9, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- {x: 10+d*10, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
- ];
- //Mothership variables
- var mother = false; //if there is a Mothership flying by left to right
- var wMother = 26; //width of Mothership
- var xMother = 0-wMother; //x position of Mothership
- var yMother = 50; //y position of Mothership
- var hMother = 16; //height of Mothership
- var cMother = color(255, 0, 255); //color of Mothership
- var vMother = 50; //point value of Mothership
- var iMother = 1.1; // speed of the Mothership
- //pick random number of aliens that are not alive for Mothership to attack
- var rMother = floor(random(20,30)); //each round will be different
- var nMother = 1; //number of Motherships that have appeared
- //game global variables
- var score = 0; //score of the game
- var gameOver = false; //indicates if the game is over
- var attackWave = 1; //current alien attack number
- var splashScreen = false; //turns true when user has viewed it
- //time variables
- var iTime = millis(); //initial starting time of program in milliseconds (ms)
- var cTime = millis(); //current time of program in ms
- var eTime = cTime - iTime; //ellapsed time in ms
- var sTime = 1000; //1000 ms or 1 second - time between states of alien graphics
- //------------------------------------------------------------------------------
- var drawAlien = function(x,y,w,h,c,i,s) {
- fill(c);
- if (i>=0 && i<=10) { //top row aliens
- if (s) { //s=alienState1 - true=open
- rect(x+10,y,4,2);
- rect(x+8,y+2,8,2);
- rect(x+6,y+4,12,2);
- rect(x+4,y+6,4,2);
- rect(x+10,y+6,4,2);
- rect(x+16,y+6,4,2);
- rect(x+4,y+8,16,2);
- rect(x+8,y+10,2,2);
- rect(x+14,y+10,2,2);
- rect(x+6,y+12,2,2);
- rect(x+10,y+12,4,2);
- rect(x+16,y+12,2,2);
- rect(x+4,y+14,6,2);
- rect(x+14,y+14,6,2);
- } else { //close
- rect(x+10,y,4,2);
- rect(x+8,y+2,8,2);
- rect(x+6,y+4,12,2);
- rect(x+4,y+6,4,2);
- rect(x+10,y+6,4,2);
- rect(x+16,y+6,4,2);
- rect(x+4,y+8,16,2);
- rect(x+6,y+10,2,2);
- rect(x+10,y+10,4,2);
- rect(x+16,y+10,2,2);
- rect(x+4,y+12,2,2);
- rect(x+18,y+12,2,2);
- rect(x+6,y+14,2,2);
- rect(x+16,y+14,2,2);
- }
- } else if (i>=11 && i<=32) { //2nd-3rd row aliens
- if (s) { //s=alienState1 - true=open
- rect(x+6,y,2,2);
- rect(x+18,y,2,2);
- rect(x+8,y+2,2,2);
- rect(x+16,y+2,2,2);
- rect(x+6,y+4,14,2);
- rect(x+4,y+6,4,2);
- rect(x+10,y+6,6,2);
- rect(x+18,y+6,4,2);
- rect(x+2,y+8,22,2);
- rect(x+2,y+10,2,2);
- rect(x+6,y+10,14,2);
- rect(x+22,y+10,2,2);
- rect(x+2,y+12,2,2);
- rect(x+6,y+12,2,2);
- rect(x+18,y+12,2,2);
- rect(x+22,y+12,2,2);
- rect(x+8,y+14,4,2);
- rect(x+14,y+14,4,2);
- } else { //close
- rect(x+6,y,2,2);
- rect(x+18,y,2,2);
- rect(x+2,y+2,2,2);
- rect(x+8,y+2,2,2);
- rect(x+16,y+2,2,2);
- rect(x+22,y+2,2,2);
- rect(x+2,y+4,2,2);
- rect(x+6,y+4,14,2);
- rect(x+22,y+4,2,2);
- rect(x+2,y+6,6,2);
- rect(x+10,y+6,6,2);
- rect(x+18,y+6,6,2);
- rect(x+2,y+8,22,2);
- rect(x+6,y+10,14,2);
- rect(x+6,y+12,2,2);
- rect(x+18,y+12,2,2);
- rect(x+4,y+14,2,2);
- rect(x+20,y+14,2,2);
- }
- } else if (i>=33 && i<=54) { //4th-5th row aliens
- if (s) { //s=alienState1 - true=open
- rect(x+8,y,8,2);
- rect(x+2,y+2,20,2);
- rect(x,y+4,24,2);
- rect(x,y+6,6,2);
- rect(x+10,y+6,4,2);
- rect(x+18,y+6,6,2);
- rect(x,y+8,24,2);
- rect(x+6,y+10,4,2);
- rect(x+14,y+10,4,2);
- rect(x+2,y+12,4,2);
- rect(x+10,y+12,4,2);
- rect(x+18,y+12,4,2);
- rect(x,y+14,4,2);
- rect(x+20,y+14,4,2);
- } else { //close
- rect(x+8,y,8,2);
- rect(x+2,y+2,20,2);
- rect(x,y+4,24,2);
- rect(x,y+6,6,2);
- rect(x+10,y+6,4,2);
- rect(x+18,y+6,6,2);
- rect(x,y+8,24,2);
- rect(x+4,y+10,6,2);
- rect(x+14,y+10,6,2);
- rect(x+2,y+12,4,2);
- rect(x+10,y+12,4,2);
- rect(x+18,y+12,4,2);
- rect(x+4,y+14,4,2);
- rect(x+16,y+14,4,2);
- }
- }
- };
- //------------------------------------------------------------------------------
- var drawSplashScreen = function() {
- background(0,0,0);
- fill(color(0,255,255)); //text color
- textSize(22);
- text("Play", 180, 60);
- textSize(40);
- text("Space Invaders", 60, 130);
- textSize(22);
- text("*Score Advance Table*", 90, 200);
- text("= 50 Points", 180, 230);
- text("= 30 Points", 180, 260);
- text("= 20 Points", 180, 290);
- text("= 10 Points", 180, 320);
- textSize(16);
- text("Press (p) to play...", 140, 360);
- //mothership
- x=148; y=213;
- fill(255,0,255);
- rect(x+10,y,4,2);
- rect(x+6,y+2,12,2);
- rect(x+4,y+4,16,2);
- rect(x+2,y+6,20,2);
- rect(x+2,y+8,2,2);
- rect(x+6,y+8,2,2);
- rect(x+10,y+8,4,2);
- rect(x+16,y+8,2,2);
- rect(x+20,y+8,2,2);
- rect(x,y+10,24,2);
- rect(x+2,y+12,6,2);
- rect(x+10,y+12,4,2);
- rect(x+16,y+12,6,2);
- rect(x+4,y+14,2,2);
- rect(x+18,y+14,2,2);
- //draw top row alien
- x=148; y=243;
- fill(255, 0, 0);
- rect(x+10,y,4,2);
- rect(x+8,y+2,8,2);
- rect(x+6,y+4,12,2);
- rect(x+4,y+6,4,2);
- rect(x+10,y+6,4,2);
- rect(x+16,y+6,4,2);
- rect(x+4,y+8,16,2);
- rect(x+8,y+10,2,2);
- rect(x+14,y+10,2,2);
- rect(x+6,y+12,2,2);
- rect(x+10,y+12,4,2);
- rect(x+16,y+12,2,2);
- rect(x+4,y+14,6,2);
- rect(x+14,y+14,6,2);
- //2nd/3rd row alien
- x=146; y=273;
- fill(0,255,0);
- rect(x+6,y,2,2);
- rect(x+18,y,2,2);
- rect(x+8,y+2,2,2);
- rect(x+16,y+2,2,2);
- rect(x+6,y+4,14,2);
- rect(x+4,y+6,4,2);
- rect(x+10,y+6,6,2);
- rect(x+18,y+6,4,2);
- rect(x+2,y+8,22,2);
- rect(x+2,y+10,2,2);
- rect(x+6,y+10,14,2);
- rect(x+22,y+10,2,2);
- rect(x+2,y+12,2,2);
- rect(x+6,y+12,2,2);
- rect(x+18,y+12,2,2);
- rect(x+22,y+12,2,2);
- rect(x+8,y+14,4,2);
- rect(x+14,y+14,4,2);
- //4th/5th row alien
- x=146; y=303;
- fill(0,0,255);
- rect(x+8,y,8,2);
- rect(x+2,y+2,20,2);
- rect(x,y+4,24,2);
- rect(x,y+6,6,2);
- rect(x+10,y+6,4,2);
- rect(x+18,y+6,6,2);
- rect(x,y+8,24,2);
- rect(x+6,y+10,4,2);
- rect(x+14,y+10,4,2);
- rect(x+2,y+12,4,2);
- rect(x+10,y+12,4,2);
- rect(x+18,y+12,4,2);
- rect(x,y+14,4,2);
- rect(x+20,y+14,4,2);
- };
- noStroke(); //shapes do not have an outline
- //------------------------------------------------------------------------------
- var draw = function() {
- if (!splashScreen) {
- drawSplashScreen(); //draw the splash screen of information to user
- }
- if (keyIsPressed && key.toString() === 'p') {
- splashScreen = true; //user has viewed splash screen
- }
- if (!gameOver && splashScreen) { //play until the game is over
- //black background
- background(0,0,0);
- //--------------------------------------------------------------------------
- //laser cannon
- fill(30, 255, 30); //green laser cannon
- rect(xCannon+wCannon/2,yCannon-8,2,10); //top of cannon
- rect(xCannon,yCannon,wCannon,hCannon); //base of cannon
- //left/right movement of the cannon
- if (keyIsPressed && keyCode === RIGHT) {
- if (xCannon < 400-wCannon) {
- xCannon = xCannon + iCannon;
- }
- } else if (keyIsPressed && keyCode === LEFT) {
- if (xCannon > 0) {
- xCannon = xCannon - iCannon;
- }
- }
- //--------------------------------------------------------------------------
- //shoot a missile from the laser cannon
- if (keyIsPressed && key.toString() === ' ' && !missile) {
- playSound(getSound("retro/laser3")); //missile shot sound
- missile = true;
- xMissile = xCannon + wCannon/2;
- yMissile = yCannon - 10;
- }
- if (missile) {
- if (yMissile < 0) {
- missile = false;
- } else {
- //draw missile
- rect(xMissile,yMissile+hMissile,wMissile,hMissile);
- yMissile-=iMissile;
- //collision detection - did missile hit an alien (check all aliens)
- for (var i = 0; i < aliens.length; i++) {
- if (aliens[i].a) { //is the alien still alive?
- if (i>=0 && i<=10) { //top row
- x = aliens[i].x+4; //x coordinate of current alien
- y = aliens[i].y; //y coordinate of current alien
- w = 16; //width of current alien
- h = aliens[i].h; //height of current alien
- } else if (i>=11 && i<=32) { //2nd-3rd row
- x = aliens[i].x+2; //x coordinate of current alien
- y = aliens[i].y; //y coordinate of current alien
- w = 22; //width of current alien
- h = aliens[i].h; //height of current alien
- } else if (i>=33 && i<=54) { //4th-5th row
- x = aliens[i].x; //x coordinate of current alien
- y = aliens[i].y; //y coordinate of current alien
- w = 24; //width of current alien
- h = aliens[i].h; //height of current alien
- }
- if ((((xMissile > x) && (xMissile < x+w)) ||
- ((xMissile+wMissile > x) && (xMissile+wMissile < x+w))) &&
- (((yMissile > y) && (yMissile < y+h)) ||
- ((yMissile+hMissile > y) && (yMissile+hMissile < y+h)))) {
- //alien has been hit
- playSound(getSound("retro/hit2")); //sound of collision
- numAliens -= 1; //remove 1 alien from play
- aliens[i].a = false; //remove alien from play
- score += aliens[i].v; //add alien type to the user score
- missile = false; //remove missile
- }
- }
- }
- }
- }
- //--------------------------------------------------------------------------
- //increment all alive aliens and then draw them
- for (var i = 0; i < aliens.length; i++) {
- if (aliens[i].a) { //is the alien still alive?
- aliens[i].x += iAlien;
- if (aliens[i].x < 0 || aliens[i].x > 400-aliens[i].w) {
- //if (aliens[i].x < 0) {
- for (var j = 0; j <= i; j++) { //move all previous aliens back
- if (aliens[j].a) {
- aliens[j].x -= iAlien; //reset the x value back
- }
- }
- iAlien = iAlien * -1; //changes direction left/right
- for (var i = 0; i < aliens.length; i++) { //lower all aliens
- aliens[i].y += 8; //alien is lowering
- }
- break; //get out of loop - all aliens have lowered
- }
- }
- }
- //draw aliens
- cTime = millis(); //current time
- eTime = cTime - iTime; //ellapsed time
- if (eTime > sTime) { //change alien graphic state after sTime
- alienState1 = !alienState1;
- iTime = millis(); //reset the intial time
- //background sound of aliens moving across the screen
- playSound(getSound("rpg/hit-thud"));
- }
- for (var i = 0; i < aliens.length; i++) {
- if (aliens[i].a) { //is the alien still alive?
- drawAlien(aliens[i].x,aliens[i].y,aliens[i].w,aliens[i].h,aliens[i].c,i,alienState1);
- }
- }
- //increase alien speed based on the number of aliens left
- if (iAlien > 0) {
- leftRight = 1; //moving to the right
- } else {
- leftRight = -1; //moving to the left
- }
- if (numAliens === 32) {
- iAlien = 0.8*leftRight;
- sTime = 900;
- } else if (numAliens === 16) {
- iAlien = 1.4*leftRight;
- sTime = 800;
- } else if (numAliens === 8) {
- iAlien = 2.1*leftRight;
- sTime = 700;
- } else if (numAliens === 4) {
- iAlien = 2.9*leftRight;
- sTime = 500;
- } else if (numAliens === 2) {
- iAlien = 3.8*leftRight;
- sTime = 300;
- } else if (numAliens === 1) {
- iAlien = 4.8*leftRight;
- sTime = 100;
- }
- //--------------------------------------------------------------------------
- //collision detection - check if an alien hit the laser cannon
- for (var i = 0; i < aliens.length; i++) {
- x = aliens[i].x; //x coordinate of current alien
- y = aliens[i].y; //y coordinate of current alien
- w = aliens[i].w; //width of current alien
- h = aliens[i].h; //height of current alien
- //is alien alive and low enough to hit laser cannon?
- if (aliens[i].a && (y+h > yCannon)) {
- if ((((xCannon > x) && (xCannon < x+w)) ||
- ((xCannon+wCannon > x) && (xCannon+wCannon < x+w))) &&
- (((yCannon > y) && (yCannon < y+h)) ||
- ((yCannon+hCannon > y) && (yCannon+hCannon < y+h)))) {
- //laser cannon has been hit
- playSound(getSound("rpg/hit-splat")); //sound of collision
- numCannon -= 1; //remove 1 laser cannon life
- if (numCannon === 0) {
- gameOver = true;
- }
- numAliens -= 1; //remove 1 alien from play
- aliens[i].a = false; //remove alien from play
- }
- }
- }
- //--------------------------------------------------------------------------
- //aliens randomly drop bombs
- if (bomb) {
- if (yBomb > 400) { //remove bomb if not in play
- bomb = false;
- } else {
- //draw bomb
- fill(cBomb);
- rect(xBomb,yBomb+hBomb,wBomb,hBomb);
- yBomb+=iBomb;
- //collision detection - did bomb hit the laser cannon
- x = xCannon; //x coordinate of laser cannon
- y = yCannon; //y coordinate of laser cannon
- w = wCannon; //width of laser cannon
- h = hCannon; //height of laser cannon
- if ((((xBomb > x) && (xBomb < x+w)) ||
- ((xBomb+wBomb > x) && (xBomb+wBomb < x+w))) &&
- (((yBomb > y) && (yBomb < y+h)) ||
- ((yBomb+hBomb > y) && (yBomb+hBomb < y+h)))) {
- //laser cannon has been hit
- playSound(getSound("rpg/hit-splat")); //sound of collision
- numCannon -= 1; //remove 1 laser cannon life
- if (numCannon === 0) {
- gameOver = true;
- }
- bomb = false; //remove bomb
- }
- }
- } else if (numAliens !== 0) { //check for any aliens alive
- var i = floor(random(0,aliens.length)); //randomly pick one of the aliens
- while (!aliens[i].a) { //make sure selected alien is alive
- i = floor(random(0,aliens.length)); //keep picking
- }
- bomb = true;
- cBomb = aliens[i].c;
- xBomb = aliens[i].x+aliens[i].w/2;
- yBomb = aliens[i].y+aliens[i].h;
- }
- //--------------------------------------------------------------------------
- //Mothership will randomly appear once per attack wave
- if (mother) {
- if (xMother+wMother > 400) {
- mother = false; //remove Mothership
- } else {
- //draw Mothership
- fill(cMother);
- x = xMother; //x coordinate of Mothership
- y = yMother; //y coordinate of Mothership
- rect(x+10,y,4,2);
- rect(x+6,y+2,12,2);
- rect(x+4,y+4,16,2);
- rect(x+2,y+6,20,2);
- rect(x+2,y+8,2,2);
- rect(x+6,y+8,2,2);
- rect(x+10,y+8,4,2);
- rect(x+16,y+8,2,2);
- rect(x+20,y+8,2,2);
- rect(x,y+10,24,2);
- rect(x+2,y+12,6,2);
- rect(x+10,y+12,4,2);
- rect(x+16,y+12,6,2);
- rect(x+4,y+14,2,2);
- rect(x+18,y+14,2,2);
- xMother+=iMother;
- playSound(getSound("retro/jump2")); //sound of Mothership
- //collision detection - did missile hit the Mothership
- w = wMother; //width of Mothership
- h = hMother; //height of Mothership
- if ((((xMissile > x) && (xMissile < x+w)) ||
- ((xMissile+wMissile > x) && (xMissile+wMissile < x+w))) &&
- (((yMissile > y) && (yMissile < y+h)) ||
- ((yMissile+hMissile > y) && (yMissile+hMissile < y+h)))) {
- //Mothership has been hit
- playSound(getSound("retro/hit2")); //sound of collision
- score += vMother; //add Mothership value to the user score
- mother = false; //remove Mothership
- missile = false; //remove missile
- }
- }
- } else if (nMother !== attackWave && rMother === numAliens) {
- mother = true; //a Mothership will appear
- nMother++;
- }
- //--------------------------------------------------------------------------
- //check to see if new alien attack wave is needed
- if (numAliens === 0) { //if all aliens are not alive
- //new attack wave needed - reset all variables
- numAliens = startNumAliens; //restore the number of aliens
- for (var i = 0; i < aliens.length; i++) { //set values back to original
- aliens[i].x = 10+d*(i%11); //set back to original x coordinate
- aliens[i].y = 100+30*floor(i/11); //set back to original y coordinate
- aliens[i].a = true; //set all aliens to be alive
- }
- bomb = false; //get rid of any bomb
- missile = false; //get rid of any missile
- mother = false; //get rid of any Mothership
- xMother = 0-wMother; //reset the x coordinate of the Mothership
- //pick random number of aliens that are not alive for Mothership to attack
- rMother = floor(random(20,30)); //each round will be different
- attackWave += 1; //go to the next alien attack wave
- iAlien = -0.3; //reset increment of alien - speed/direction
- sTime = 1000; //reset the change of alien graphic state time
- }
- //--------------------------------------------------------------------------
- //scoring and other information to display
- fill(color(0,255,255)); //text color
- textSize(14);
- text("Score: "+score, 10, 30);
- text("Attack Wave: "+attackWave, 160,30);
- text("Lives: "+numCannon, 330, 30);
- if (gameOver) {
- textSize(30);
- text("GAME OVER", 110, 200);
- }
- }//end of global if statement when game ends
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement