Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.39 KB | None | 0 0
  1. //Project #33
  2.  
  3. //Intro to Computer Programing – JavaScript
  4.  
  5. //Space Invaders - Simulate the 1978 Taito arcade game by Tomohiro Nishikado.
  6.  
  7.  
  8.  
  9. //laser cannon
  10.  
  11. var xCannon = 100; //x position of base - upper left
  12.  
  13. var yCannon = 360; //y position of base - upper left
  14.  
  15. var wCannon = 30; //width of base
  16.  
  17. var hCannon = 12; //height of base
  18.  
  19. var iCannon = 1.5; //increment of laser cannon
  20.  
  21. var numCannon = 3; //number of lives for laser cannon
  22.  
  23.  
  24.  
  25. //missile from laser cannon
  26.  
  27. var missile = false; //if there is a missile being fired
  28.  
  29. var xMissile = 0; //x position of missile
  30.  
  31. var yMissile = 0; //y position of missile
  32.  
  33. var wMissile = 2; //width of missile
  34.  
  35. var hMissile = 10; //height of missile
  36.  
  37. var iMissile = 5.5; //increment of missile - speed
  38.  
  39.  
  40.  
  41. //collision detection
  42.  
  43. var x,y; //(x,y) position of upper left of current alien or laser cannon
  44.  
  45. var w,h; //width and height of current alien or laser cannon
  46.  
  47.  
  48.  
  49. //alien
  50.  
  51. var d = 30; //distance between beginning of one alien to another
  52.  
  53. var iAlien = -0.3; //increment of alien - speed/direction
  54.  
  55. var leftRight = -1; //negative-left and positive-right
  56.  
  57. var startNumAliens = 55; //starting number of aliens
  58.  
  59. var numAliens = startNumAliens; //number of aliens in play - 5 rows of 11 = 55
  60.  
  61. var alienState1 = true; //2 graphic states of alien - true=open | false=close
  62.  
  63.  
  64.  
  65. //bomb from alien
  66.  
  67. var bomb = false; //if there is a bomb from an alien
  68.  
  69. var xBomb = 0; //x position of bomb
  70.  
  71. var yBomb = 0; //y position of bomb
  72.  
  73. var wBomb = 2; //width of bomb
  74.  
  75. var hBomb = 10; //height of bomb
  76.  
  77. var iBomb = 2.0; //increment of bomb - speed
  78.  
  79. var cBomb = color(0,0,0); //color of the bomb - depends on alien color
  80.  
  81.  
  82.  
  83. var aliens = [ //creating an array of alien objects
  84.  
  85. //(x,y) upper left coordinate of alien
  86.  
  87. //w,h are width and height of alien
  88.  
  89. //a represents whether the alien is alive (true) or not (false)
  90.  
  91. //v represents point value of the alien
  92.  
  93. //c represents color of the alien
  94.  
  95. {x: 10+d* 0, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)}, //top row
  96.  
  97. {x: 10+d* 1, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  98.  
  99. {x: 10+d* 2, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  100.  
  101. {x: 10+d* 3, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  102.  
  103. {x: 10+d* 4, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  104.  
  105. {x: 10+d* 5, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  106.  
  107. {x: 10+d* 6, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  108.  
  109. {x: 10+d* 7, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  110.  
  111. {x: 10+d* 8, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  112.  
  113. {x: 10+d* 9, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  114.  
  115. {x: 10+d*10, y: 100, w: 26, h: 16, a: true, v: 30, c: color(255,0,0)},
  116.  
  117.  
  118.  
  119. {x: 10+d* 0, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)}, //2nd row
  120.  
  121. {x: 10+d* 1, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  122.  
  123. {x: 10+d* 2, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  124.  
  125. {x: 10+d* 3, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  126.  
  127. {x: 10+d* 4, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  128.  
  129. {x: 10+d* 5, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  130.  
  131. {x: 10+d* 6, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  132.  
  133. {x: 10+d* 7, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  134.  
  135. {x: 10+d* 8, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  136.  
  137. {x: 10+d* 9, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  138.  
  139. {x: 10+d*10, y: 130, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  140.  
  141.  
  142.  
  143. {x: 10+d* 0, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)}, //3rd row
  144.  
  145. {x: 10+d* 1, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  146.  
  147. {x: 10+d* 2, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  148.  
  149. {x: 10+d* 3, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  150.  
  151. {x: 10+d* 4, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  152.  
  153. {x: 10+d* 5, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  154.  
  155. {x: 10+d* 6, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  156.  
  157. {x: 10+d* 7, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  158.  
  159. {x: 10+d* 8, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  160.  
  161. {x: 10+d* 9, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  162.  
  163. {x: 10+d*10, y: 160, w: 26, h: 16, a: true, v: 20, c: color(0,255,0)},
  164.  
  165.  
  166.  
  167. {x: 10+d* 0, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)}, //4th row
  168.  
  169. {x: 10+d* 1, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  170.  
  171. {x: 10+d* 2, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  172.  
  173. {x: 10+d* 3, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  174.  
  175. {x: 10+d* 4, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  176.  
  177. {x: 10+d* 5, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  178.  
  179. {x: 10+d* 6, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  180.  
  181. {x: 10+d* 7, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  182.  
  183. {x: 10+d* 8, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  184.  
  185. {x: 10+d* 9, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  186.  
  187. {x: 10+d*10, y: 190, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  188.  
  189.  
  190.  
  191. {x: 10+d* 0, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)}, //5th row
  192.  
  193. {x: 10+d* 1, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  194.  
  195. {x: 10+d* 2, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  196.  
  197. {x: 10+d* 3, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  198.  
  199. {x: 10+d* 4, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  200.  
  201. {x: 10+d* 5, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  202.  
  203. {x: 10+d* 6, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  204.  
  205. {x: 10+d* 7, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  206.  
  207. {x: 10+d* 8, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  208.  
  209. {x: 10+d* 9, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  210.  
  211. {x: 10+d*10, y: 220, w: 26, h: 16, a: true, v: 10, c: color(0,0,255)},
  212.  
  213. ];
  214.  
  215.  
  216.  
  217. //Mothership variables
  218.  
  219. var mother = false; //if there is a Mothership flying by left to right
  220.  
  221. var wMother = 26; //width of Mothership
  222.  
  223. var xMother = 0-wMother; //x position of Mothership
  224.  
  225. var yMother = 50; //y position of Mothership
  226.  
  227. var hMother = 16; //height of Mothership
  228.  
  229. var cMother = color(255, 0, 255); //color of Mothership
  230.  
  231. var vMother = 50; //point value of Mothership
  232.  
  233. var iMother = 1.1; // speed of the Mothership
  234.  
  235. //pick random number of aliens that are not alive for Mothership to attack
  236.  
  237. var rMother = floor(random(20,30)); //each round will be different
  238.  
  239. var nMother = 1; //number of Motherships that have appeared
  240.  
  241.  
  242.  
  243. //game global variables
  244.  
  245. var score = 0; //score of the game
  246.  
  247. var gameOver = false; //indicates if the game is over
  248.  
  249. var attackWave = 1; //current alien attack number
  250.  
  251. var splashScreen = false; //turns true when user has viewed it
  252.  
  253.  
  254.  
  255. //time variables
  256.  
  257. var iTime = millis(); //initial starting time of program in milliseconds (ms)
  258.  
  259. var cTime = millis(); //current time of program in ms
  260.  
  261. var eTime = cTime - iTime; //ellapsed time in ms
  262.  
  263. var sTime = 1000; //1000 ms or 1 second - time between states of alien graphics
  264.  
  265.  
  266.  
  267. //------------------------------------------------------------------------------
  268.  
  269. var drawAlien = function(x,y,w,h,c,i,s) {
  270.  
  271. fill(c);
  272.  
  273. if (i>=0 && i<=10) { //top row aliens
  274.  
  275. if (s) { //s=alienState1 - true=open
  276.  
  277. rect(x+10,y,4,2);
  278.  
  279. rect(x+8,y+2,8,2);
  280.  
  281. rect(x+6,y+4,12,2);
  282.  
  283. rect(x+4,y+6,4,2);
  284.  
  285. rect(x+10,y+6,4,2);
  286.  
  287. rect(x+16,y+6,4,2);
  288.  
  289. rect(x+4,y+8,16,2);
  290.  
  291. rect(x+8,y+10,2,2);
  292.  
  293. rect(x+14,y+10,2,2);
  294.  
  295. rect(x+6,y+12,2,2);
  296.  
  297. rect(x+10,y+12,4,2);
  298.  
  299. rect(x+16,y+12,2,2);
  300.  
  301. rect(x+4,y+14,6,2);
  302.  
  303. rect(x+14,y+14,6,2);
  304.  
  305. } else { //close
  306.  
  307. rect(x+10,y,4,2);
  308.  
  309. rect(x+8,y+2,8,2);
  310.  
  311. rect(x+6,y+4,12,2);
  312.  
  313. rect(x+4,y+6,4,2);
  314.  
  315. rect(x+10,y+6,4,2);
  316.  
  317. rect(x+16,y+6,4,2);
  318.  
  319. rect(x+4,y+8,16,2);
  320.  
  321. rect(x+6,y+10,2,2);
  322.  
  323. rect(x+10,y+10,4,2);
  324.  
  325. rect(x+16,y+10,2,2);
  326.  
  327. rect(x+4,y+12,2,2);
  328.  
  329. rect(x+18,y+12,2,2);
  330.  
  331. rect(x+6,y+14,2,2);
  332.  
  333. rect(x+16,y+14,2,2);
  334.  
  335. }
  336.  
  337. } else if (i>=11 && i<=32) { //2nd-3rd row aliens
  338.  
  339. if (s) { //s=alienState1 - true=open
  340.  
  341. rect(x+6,y,2,2);
  342.  
  343. rect(x+18,y,2,2);
  344.  
  345. rect(x+8,y+2,2,2);
  346.  
  347. rect(x+16,y+2,2,2);
  348.  
  349. rect(x+6,y+4,14,2);
  350.  
  351. rect(x+4,y+6,4,2);
  352.  
  353. rect(x+10,y+6,6,2);
  354.  
  355. rect(x+18,y+6,4,2);
  356.  
  357. rect(x+2,y+8,22,2);
  358.  
  359. rect(x+2,y+10,2,2);
  360.  
  361. rect(x+6,y+10,14,2);
  362.  
  363. rect(x+22,y+10,2,2);
  364.  
  365. rect(x+2,y+12,2,2);
  366.  
  367. rect(x+6,y+12,2,2);
  368.  
  369. rect(x+18,y+12,2,2);
  370.  
  371. rect(x+22,y+12,2,2);
  372.  
  373. rect(x+8,y+14,4,2);
  374.  
  375. rect(x+14,y+14,4,2);
  376.  
  377. } else { //close
  378.  
  379. rect(x+6,y,2,2);
  380.  
  381. rect(x+18,y,2,2);
  382.  
  383. rect(x+2,y+2,2,2);
  384.  
  385. rect(x+8,y+2,2,2);
  386.  
  387. rect(x+16,y+2,2,2);
  388.  
  389. rect(x+22,y+2,2,2);
  390.  
  391. rect(x+2,y+4,2,2);
  392.  
  393. rect(x+6,y+4,14,2);
  394.  
  395. rect(x+22,y+4,2,2);
  396.  
  397. rect(x+2,y+6,6,2);
  398.  
  399. rect(x+10,y+6,6,2);
  400.  
  401. rect(x+18,y+6,6,2);
  402.  
  403. rect(x+2,y+8,22,2);
  404.  
  405. rect(x+6,y+10,14,2);
  406.  
  407. rect(x+6,y+12,2,2);
  408.  
  409. rect(x+18,y+12,2,2);
  410.  
  411. rect(x+4,y+14,2,2);
  412.  
  413. rect(x+20,y+14,2,2);
  414.  
  415. }
  416.  
  417. } else if (i>=33 && i<=54) { //4th-5th row aliens
  418.  
  419. if (s) { //s=alienState1 - true=open
  420.  
  421. rect(x+8,y,8,2);
  422.  
  423. rect(x+2,y+2,20,2);
  424.  
  425. rect(x,y+4,24,2);
  426.  
  427. rect(x,y+6,6,2);
  428.  
  429. rect(x+10,y+6,4,2);
  430.  
  431. rect(x+18,y+6,6,2);
  432.  
  433. rect(x,y+8,24,2);
  434.  
  435. rect(x+6,y+10,4,2);
  436.  
  437. rect(x+14,y+10,4,2);
  438.  
  439. rect(x+2,y+12,4,2);
  440.  
  441. rect(x+10,y+12,4,2);
  442.  
  443. rect(x+18,y+12,4,2);
  444.  
  445. rect(x,y+14,4,2);
  446.  
  447. rect(x+20,y+14,4,2);
  448.  
  449. } else { //close
  450.  
  451. rect(x+8,y,8,2);
  452.  
  453. rect(x+2,y+2,20,2);
  454.  
  455. rect(x,y+4,24,2);
  456.  
  457. rect(x,y+6,6,2);
  458.  
  459. rect(x+10,y+6,4,2);
  460.  
  461. rect(x+18,y+6,6,2);
  462.  
  463. rect(x,y+8,24,2);
  464.  
  465. rect(x+4,y+10,6,2);
  466.  
  467. rect(x+14,y+10,6,2);
  468.  
  469. rect(x+2,y+12,4,2);
  470.  
  471. rect(x+10,y+12,4,2);
  472.  
  473. rect(x+18,y+12,4,2);
  474.  
  475. rect(x+4,y+14,4,2);
  476.  
  477. rect(x+16,y+14,4,2);
  478.  
  479. }
  480.  
  481. }
  482.  
  483. };
  484.  
  485.  
  486.  
  487. //------------------------------------------------------------------------------
  488.  
  489. var drawSplashScreen = function() {
  490.  
  491. background(0,0,0);
  492.  
  493. fill(color(0,255,255)); //text color
  494.  
  495. textSize(22);
  496.  
  497. text("Play", 180, 60);
  498.  
  499. textSize(40);
  500.  
  501. text("Space Invaders", 60, 130);
  502.  
  503. textSize(22);
  504.  
  505. text("*Score Advance Table*", 90, 200);
  506.  
  507. text("= 50 Points", 180, 230);
  508.  
  509. text("= 30 Points", 180, 260);
  510.  
  511. text("= 20 Points", 180, 290);
  512.  
  513. text("= 10 Points", 180, 320);
  514.  
  515. textSize(16);
  516.  
  517. text("Press (p) to play...", 140, 360);
  518.  
  519. //mothership
  520.  
  521. x=148; y=213;
  522.  
  523. fill(255,0,255);
  524.  
  525. rect(x+10,y,4,2);
  526.  
  527. rect(x+6,y+2,12,2);
  528.  
  529. rect(x+4,y+4,16,2);
  530.  
  531. rect(x+2,y+6,20,2);
  532.  
  533. rect(x+2,y+8,2,2);
  534.  
  535. rect(x+6,y+8,2,2);
  536.  
  537. rect(x+10,y+8,4,2);
  538.  
  539. rect(x+16,y+8,2,2);
  540.  
  541. rect(x+20,y+8,2,2);
  542.  
  543. rect(x,y+10,24,2);
  544.  
  545. rect(x+2,y+12,6,2);
  546.  
  547. rect(x+10,y+12,4,2);
  548.  
  549. rect(x+16,y+12,6,2);
  550.  
  551. rect(x+4,y+14,2,2);
  552.  
  553. rect(x+18,y+14,2,2);
  554.  
  555. //draw top row alien
  556.  
  557. x=148; y=243;
  558.  
  559. fill(255, 0, 0);
  560.  
  561. rect(x+10,y,4,2);
  562.  
  563. rect(x+8,y+2,8,2);
  564.  
  565. rect(x+6,y+4,12,2);
  566.  
  567. rect(x+4,y+6,4,2);
  568.  
  569. rect(x+10,y+6,4,2);
  570.  
  571. rect(x+16,y+6,4,2);
  572.  
  573. rect(x+4,y+8,16,2);
  574.  
  575. rect(x+8,y+10,2,2);
  576.  
  577. rect(x+14,y+10,2,2);
  578.  
  579. rect(x+6,y+12,2,2);
  580.  
  581. rect(x+10,y+12,4,2);
  582.  
  583. rect(x+16,y+12,2,2);
  584.  
  585. rect(x+4,y+14,6,2);
  586.  
  587. rect(x+14,y+14,6,2);
  588.  
  589. //2nd/3rd row alien
  590.  
  591. x=146; y=273;
  592.  
  593. fill(0,255,0);
  594.  
  595. rect(x+6,y,2,2);
  596.  
  597. rect(x+18,y,2,2);
  598.  
  599. rect(x+8,y+2,2,2);
  600.  
  601. rect(x+16,y+2,2,2);
  602.  
  603. rect(x+6,y+4,14,2);
  604.  
  605. rect(x+4,y+6,4,2);
  606.  
  607. rect(x+10,y+6,6,2);
  608.  
  609. rect(x+18,y+6,4,2);
  610.  
  611. rect(x+2,y+8,22,2);
  612.  
  613. rect(x+2,y+10,2,2);
  614.  
  615. rect(x+6,y+10,14,2);
  616.  
  617. rect(x+22,y+10,2,2);
  618.  
  619. rect(x+2,y+12,2,2);
  620.  
  621. rect(x+6,y+12,2,2);
  622.  
  623. rect(x+18,y+12,2,2);
  624.  
  625. rect(x+22,y+12,2,2);
  626.  
  627. rect(x+8,y+14,4,2);
  628.  
  629. rect(x+14,y+14,4,2);
  630.  
  631. //4th/5th row alien
  632.  
  633. x=146; y=303;
  634.  
  635. fill(0,0,255);
  636.  
  637. rect(x+8,y,8,2);
  638.  
  639. rect(x+2,y+2,20,2);
  640.  
  641. rect(x,y+4,24,2);
  642.  
  643. rect(x,y+6,6,2);
  644.  
  645. rect(x+10,y+6,4,2);
  646.  
  647. rect(x+18,y+6,6,2);
  648.  
  649. rect(x,y+8,24,2);
  650.  
  651. rect(x+6,y+10,4,2);
  652.  
  653. rect(x+14,y+10,4,2);
  654.  
  655. rect(x+2,y+12,4,2);
  656.  
  657. rect(x+10,y+12,4,2);
  658.  
  659. rect(x+18,y+12,4,2);
  660.  
  661. rect(x,y+14,4,2);
  662.  
  663. rect(x+20,y+14,4,2);
  664.  
  665. };
  666.  
  667.  
  668.  
  669. noStroke(); //shapes do not have an outline
  670.  
  671.  
  672.  
  673. //------------------------------------------------------------------------------
  674.  
  675. var draw = function() {
  676.  
  677.  
  678.  
  679. if (!splashScreen) {
  680.  
  681. drawSplashScreen(); //draw the splash screen of information to user
  682.  
  683. }
  684.  
  685.  
  686.  
  687. if (keyIsPressed && key.toString() === 'p') {
  688.  
  689. splashScreen = true; //user has viewed splash screen
  690.  
  691. }
  692.  
  693.  
  694.  
  695. if (!gameOver && splashScreen) { //play until the game is over
  696.  
  697.  
  698.  
  699. //black background
  700.  
  701. background(0,0,0);
  702.  
  703.  
  704.  
  705. //--------------------------------------------------------------------------
  706.  
  707. //laser cannon
  708.  
  709. fill(30, 255, 30); //green laser cannon
  710.  
  711. rect(xCannon+wCannon/2,yCannon-8,2,10); //top of cannon
  712.  
  713. rect(xCannon,yCannon,wCannon,hCannon); //base of cannon
  714.  
  715.  
  716.  
  717. //left/right movement of the cannon
  718.  
  719. if (keyIsPressed && keyCode === RIGHT) {
  720.  
  721. if (xCannon < 400-wCannon) {
  722.  
  723. xCannon = xCannon + iCannon;
  724.  
  725. }
  726.  
  727. } else if (keyIsPressed && keyCode === LEFT) {
  728.  
  729. if (xCannon > 0) {
  730.  
  731. xCannon = xCannon - iCannon;
  732.  
  733. }
  734.  
  735. }
  736.  
  737.  
  738.  
  739. //--------------------------------------------------------------------------
  740.  
  741. //shoot a missile from the laser cannon
  742.  
  743. if (keyIsPressed && key.toString() === ' ' && !missile) {
  744.  
  745. playSound(getSound("retro/laser3")); //missile shot sound
  746.  
  747. missile = true;
  748.  
  749. xMissile = xCannon + wCannon/2;
  750.  
  751. yMissile = yCannon - 10;
  752.  
  753. }
  754.  
  755. if (missile) {
  756.  
  757. if (yMissile < 0) {
  758.  
  759. missile = false;
  760.  
  761. } else {
  762.  
  763. //draw missile
  764.  
  765. rect(xMissile,yMissile+hMissile,wMissile,hMissile);
  766.  
  767. yMissile-=iMissile;
  768.  
  769.  
  770.  
  771. //collision detection - did missile hit an alien (check all aliens)
  772.  
  773. for (var i = 0; i < aliens.length; i++) {
  774.  
  775. if (aliens[i].a) { //is the alien still alive?
  776.  
  777. if (i>=0 && i<=10) { //top row
  778.  
  779. x = aliens[i].x+4; //x coordinate of current alien
  780.  
  781. y = aliens[i].y; //y coordinate of current alien
  782.  
  783. w = 16; //width of current alien
  784.  
  785. h = aliens[i].h; //height of current alien
  786.  
  787. } else if (i>=11 && i<=32) { //2nd-3rd row
  788.  
  789. x = aliens[i].x+2; //x coordinate of current alien
  790.  
  791. y = aliens[i].y; //y coordinate of current alien
  792.  
  793. w = 22; //width of current alien
  794.  
  795. h = aliens[i].h; //height of current alien
  796.  
  797. } else if (i>=33 && i<=54) { //4th-5th row
  798.  
  799. x = aliens[i].x; //x coordinate of current alien
  800.  
  801. y = aliens[i].y; //y coordinate of current alien
  802.  
  803. w = 24; //width of current alien
  804.  
  805. h = aliens[i].h; //height of current alien
  806.  
  807. }
  808.  
  809. if ((((xMissile > x) && (xMissile < x+w)) ||
  810.  
  811. ((xMissile+wMissile > x) && (xMissile+wMissile < x+w))) &&
  812.  
  813. (((yMissile > y) && (yMissile < y+h)) ||
  814.  
  815. ((yMissile+hMissile > y) && (yMissile+hMissile < y+h)))) {
  816.  
  817. //alien has been hit
  818.  
  819. playSound(getSound("retro/hit2")); //sound of collision
  820.  
  821. numAliens -= 1; //remove 1 alien from play
  822.  
  823. aliens[i].a = false; //remove alien from play
  824.  
  825. score += aliens[i].v; //add alien type to the user score
  826.  
  827. missile = false; //remove missile
  828.  
  829. }
  830.  
  831. }
  832.  
  833. }
  834.  
  835. }
  836.  
  837. }
  838.  
  839.  
  840.  
  841. //--------------------------------------------------------------------------
  842.  
  843. //increment all alive aliens and then draw them
  844.  
  845. for (var i = 0; i < aliens.length; i++) {
  846.  
  847. if (aliens[i].a) { //is the alien still alive?
  848.  
  849. aliens[i].x += iAlien;
  850.  
  851.  
  852.  
  853. if (aliens[i].x < 0 || aliens[i].x > 400-aliens[i].w) {
  854.  
  855. //if (aliens[i].x < 0) {
  856.  
  857. for (var j = 0; j <= i; j++) { //move all previous aliens back
  858.  
  859. if (aliens[j].a) {
  860.  
  861. aliens[j].x -= iAlien; //reset the x value back
  862.  
  863. }
  864.  
  865. }
  866.  
  867.  
  868.  
  869. iAlien = iAlien * -1; //changes direction left/right
  870.  
  871.  
  872.  
  873. for (var i = 0; i < aliens.length; i++) { //lower all aliens
  874.  
  875. aliens[i].y += 8; //alien is lowering
  876.  
  877. }
  878.  
  879.  
  880.  
  881. break; //get out of loop - all aliens have lowered
  882.  
  883. }
  884.  
  885. }
  886.  
  887. }
  888.  
  889. //draw aliens
  890.  
  891. cTime = millis(); //current time
  892.  
  893. eTime = cTime - iTime; //ellapsed time
  894.  
  895. if (eTime > sTime) { //change alien graphic state after sTime
  896.  
  897. alienState1 = !alienState1;
  898.  
  899. iTime = millis(); //reset the intial time
  900.  
  901. //background sound of aliens moving across the screen
  902.  
  903. playSound(getSound("rpg/hit-thud"));
  904.  
  905. }
  906.  
  907. for (var i = 0; i < aliens.length; i++) {
  908.  
  909. if (aliens[i].a) { //is the alien still alive?
  910.  
  911. drawAlien(aliens[i].x,aliens[i].y,aliens[i].w,aliens[i].h,aliens[i].c,i,alienState1);
  912.  
  913. }
  914.  
  915. }
  916.  
  917. //increase alien speed based on the number of aliens left
  918.  
  919. if (iAlien > 0) {
  920.  
  921. leftRight = 1; //moving to the right
  922.  
  923. } else {
  924.  
  925. leftRight = -1; //moving to the left
  926.  
  927. }
  928.  
  929. if (numAliens === 32) {
  930.  
  931. iAlien = 0.8*leftRight;
  932.  
  933. sTime = 900;
  934.  
  935. } else if (numAliens === 16) {
  936.  
  937. iAlien = 1.4*leftRight;
  938.  
  939. sTime = 800;
  940.  
  941. } else if (numAliens === 8) {
  942.  
  943. iAlien = 2.1*leftRight;
  944.  
  945. sTime = 700;
  946.  
  947. } else if (numAliens === 4) {
  948.  
  949. iAlien = 2.9*leftRight;
  950.  
  951. sTime = 500;
  952.  
  953. } else if (numAliens === 2) {
  954.  
  955. iAlien = 3.8*leftRight;
  956.  
  957. sTime = 300;
  958.  
  959. } else if (numAliens === 1) {
  960.  
  961. iAlien = 4.8*leftRight;
  962.  
  963. sTime = 100;
  964.  
  965. }
  966.  
  967.  
  968.  
  969. //--------------------------------------------------------------------------
  970.  
  971. //collision detection - check if an alien hit the laser cannon
  972.  
  973. for (var i = 0; i < aliens.length; i++) {
  974.  
  975. x = aliens[i].x; //x coordinate of current alien
  976.  
  977. y = aliens[i].y; //y coordinate of current alien
  978.  
  979. w = aliens[i].w; //width of current alien
  980.  
  981. h = aliens[i].h; //height of current alien
  982.  
  983. //is alien alive and low enough to hit laser cannon?
  984.  
  985. if (aliens[i].a && (y+h > yCannon)) {
  986.  
  987. if ((((xCannon > x) && (xCannon < x+w)) ||
  988.  
  989. ((xCannon+wCannon > x) && (xCannon+wCannon < x+w))) &&
  990.  
  991. (((yCannon > y) && (yCannon < y+h)) ||
  992.  
  993. ((yCannon+hCannon > y) && (yCannon+hCannon < y+h)))) {
  994.  
  995. //laser cannon has been hit
  996.  
  997. playSound(getSound("rpg/hit-splat")); //sound of collision
  998.  
  999. numCannon -= 1; //remove 1 laser cannon life
  1000.  
  1001. if (numCannon === 0) {
  1002.  
  1003. gameOver = true;
  1004.  
  1005. }
  1006.  
  1007. numAliens -= 1; //remove 1 alien from play
  1008.  
  1009. aliens[i].a = false; //remove alien from play
  1010.  
  1011. }
  1012.  
  1013. }
  1014.  
  1015. }
  1016.  
  1017.  
  1018.  
  1019. //--------------------------------------------------------------------------
  1020.  
  1021. //aliens randomly drop bombs
  1022.  
  1023. if (bomb) {
  1024.  
  1025. if (yBomb > 400) { //remove bomb if not in play
  1026.  
  1027. bomb = false;
  1028.  
  1029. } else {
  1030.  
  1031. //draw bomb
  1032.  
  1033. fill(cBomb);
  1034.  
  1035. rect(xBomb,yBomb+hBomb,wBomb,hBomb);
  1036.  
  1037. yBomb+=iBomb;
  1038.  
  1039.  
  1040.  
  1041. //collision detection - did bomb hit the laser cannon
  1042.  
  1043. x = xCannon; //x coordinate of laser cannon
  1044.  
  1045. y = yCannon; //y coordinate of laser cannon
  1046.  
  1047. w = wCannon; //width of laser cannon
  1048.  
  1049. h = hCannon; //height of laser cannon
  1050.  
  1051. if ((((xBomb > x) && (xBomb < x+w)) ||
  1052.  
  1053. ((xBomb+wBomb > x) && (xBomb+wBomb < x+w))) &&
  1054.  
  1055. (((yBomb > y) && (yBomb < y+h)) ||
  1056.  
  1057. ((yBomb+hBomb > y) && (yBomb+hBomb < y+h)))) {
  1058.  
  1059. //laser cannon has been hit
  1060.  
  1061. playSound(getSound("rpg/hit-splat")); //sound of collision
  1062.  
  1063. numCannon -= 1; //remove 1 laser cannon life
  1064.  
  1065. if (numCannon === 0) {
  1066.  
  1067. gameOver = true;
  1068.  
  1069. }
  1070.  
  1071. bomb = false; //remove bomb
  1072.  
  1073. }
  1074.  
  1075. }
  1076.  
  1077. } else if (numAliens !== 0) { //check for any aliens alive
  1078.  
  1079. var i = floor(random(0,aliens.length)); //randomly pick one of the aliens
  1080.  
  1081. while (!aliens[i].a) { //make sure selected alien is alive
  1082.  
  1083. i = floor(random(0,aliens.length)); //keep picking
  1084.  
  1085. }
  1086.  
  1087. bomb = true;
  1088.  
  1089. cBomb = aliens[i].c;
  1090.  
  1091. xBomb = aliens[i].x+aliens[i].w/2;
  1092.  
  1093. yBomb = aliens[i].y+aliens[i].h;
  1094.  
  1095. }
  1096.  
  1097.  
  1098.  
  1099. //--------------------------------------------------------------------------
  1100.  
  1101. //Mothership will randomly appear once per attack wave
  1102.  
  1103. if (mother) {
  1104.  
  1105. if (xMother+wMother > 400) {
  1106.  
  1107. mother = false; //remove Mothership
  1108.  
  1109. } else {
  1110.  
  1111. //draw Mothership
  1112.  
  1113. fill(cMother);
  1114.  
  1115. x = xMother; //x coordinate of Mothership
  1116.  
  1117. y = yMother; //y coordinate of Mothership
  1118.  
  1119. rect(x+10,y,4,2);
  1120.  
  1121. rect(x+6,y+2,12,2);
  1122.  
  1123. rect(x+4,y+4,16,2);
  1124.  
  1125. rect(x+2,y+6,20,2);
  1126.  
  1127. rect(x+2,y+8,2,2);
  1128.  
  1129. rect(x+6,y+8,2,2);
  1130.  
  1131. rect(x+10,y+8,4,2);
  1132.  
  1133. rect(x+16,y+8,2,2);
  1134.  
  1135. rect(x+20,y+8,2,2);
  1136.  
  1137. rect(x,y+10,24,2);
  1138.  
  1139. rect(x+2,y+12,6,2);
  1140.  
  1141. rect(x+10,y+12,4,2);
  1142.  
  1143. rect(x+16,y+12,6,2);
  1144.  
  1145. rect(x+4,y+14,2,2);
  1146.  
  1147. rect(x+18,y+14,2,2);
  1148.  
  1149. xMother+=iMother;
  1150.  
  1151. playSound(getSound("retro/jump2")); //sound of Mothership
  1152.  
  1153.  
  1154. //collision detection - did missile hit the Mothership
  1155.  
  1156. w = wMother; //width of Mothership
  1157.  
  1158. h = hMother; //height of Mothership
  1159.  
  1160. if ((((xMissile > x) && (xMissile < x+w)) ||
  1161.  
  1162. ((xMissile+wMissile > x) && (xMissile+wMissile < x+w))) &&
  1163.  
  1164. (((yMissile > y) && (yMissile < y+h)) ||
  1165.  
  1166. ((yMissile+hMissile > y) && (yMissile+hMissile < y+h)))) {
  1167.  
  1168. //Mothership has been hit
  1169.  
  1170. playSound(getSound("retro/hit2")); //sound of collision
  1171.  
  1172. score += vMother; //add Mothership value to the user score
  1173.  
  1174. mother = false; //remove Mothership
  1175.  
  1176. missile = false; //remove missile
  1177.  
  1178. }
  1179.  
  1180. }
  1181.  
  1182. } else if (nMother !== attackWave && rMother === numAliens) {
  1183.  
  1184. mother = true; //a Mothership will appear
  1185.  
  1186. nMother++;
  1187.  
  1188. }
  1189.  
  1190.  
  1191.  
  1192. //--------------------------------------------------------------------------
  1193.  
  1194. //check to see if new alien attack wave is needed
  1195.  
  1196. if (numAliens === 0) { //if all aliens are not alive
  1197.  
  1198. //new attack wave needed - reset all variables
  1199.  
  1200. numAliens = startNumAliens; //restore the number of aliens
  1201.  
  1202. for (var i = 0; i < aliens.length; i++) { //set values back to original
  1203.  
  1204. aliens[i].x = 10+d*(i%11); //set back to original x coordinate
  1205.  
  1206. aliens[i].y = 100+30*floor(i/11); //set back to original y coordinate
  1207.  
  1208. aliens[i].a = true; //set all aliens to be alive
  1209.  
  1210. }
  1211.  
  1212. bomb = false; //get rid of any bomb
  1213.  
  1214. missile = false; //get rid of any missile
  1215.  
  1216. mother = false; //get rid of any Mothership
  1217.  
  1218. xMother = 0-wMother; //reset the x coordinate of the Mothership
  1219.  
  1220. //pick random number of aliens that are not alive for Mothership to attack
  1221.  
  1222. rMother = floor(random(20,30)); //each round will be different
  1223.  
  1224. attackWave += 1; //go to the next alien attack wave
  1225.  
  1226. iAlien = -0.3; //reset increment of alien - speed/direction
  1227.  
  1228. sTime = 1000; //reset the change of alien graphic state time
  1229.  
  1230. }
  1231.  
  1232.  
  1233.  
  1234. //--------------------------------------------------------------------------
  1235.  
  1236. //scoring and other information to display
  1237.  
  1238. fill(color(0,255,255)); //text color
  1239.  
  1240. textSize(14);
  1241.  
  1242. text("Score: "+score, 10, 30);
  1243.  
  1244. text("Attack Wave: "+attackWave, 160,30);
  1245.  
  1246. text("Lives: "+numCannon, 330, 30);
  1247.  
  1248. if (gameOver) {
  1249.  
  1250. textSize(30);
  1251.  
  1252. text("GAME OVER", 110, 200);
  1253.  
  1254. }
  1255.  
  1256.  
  1257.  
  1258. }//end of global if statement when game ends
  1259.  
  1260. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement