Advertisement
Guest User

Code

a guest
Nov 16th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.83 KB | None | 0 0
  1. var gameWidth = canvasShip.width;
  2. var gameHeight = canvasShip.height;
  3.  
  4. var canFire = true;
  5. var canFireInterval = 20;
  6. var canFireTimer = 20;
  7.  
  8. var bgY1 = 0;
  9. var bgY2 = gameHeight * -1;
  10. var enemy1SpawnInterval = 10;
  11. var enemy1SpawnTimer = 10;
  12. var canSpawn = true;
  13.  
  14. var missileSpawnInterval = 15;
  15. var missileSpawnTimer = 15;
  16.  
  17. var playerDead = false;
  18.  
  19. var blink = true;
  20. var blinkTimer = 20;
  21. var blinkInterval = 20;
  22.  
  23. var points = 0;
  24. var lives = 3;
  25.  
  26. var paused = false;
  27.  
  28. var hasSelected = false;
  29. var startSelect = 1;
  30.  
  31. var player = new ship();
  32. var enemies1 = [];
  33. var missiles = [];
  34. var bulletsGreen = [];
  35. var comets1 = [];
  36. var comets2 = [];
  37.  
  38. var inLoop1 = false;
  39.  
  40. var inStart = true;
  41.  
  42. var sprites = new Image();
  43. sprites.src = 'images/spriteSheet.png';
  44. var startScreen = new Image();
  45. startScreen.src = 'images/start_screen.png';
  46. var pauseScreen = new Image();
  47. pauseScreen.src = 'images/pause_screen.png';
  48. var deathScreen = new Image();
  49. deathScreen.src = 'images/death_screen.png';
  50. var background1 = new Image();
  51. background1.src = 'images/background1.png';
  52.  
  53. var canvasPlayer = document.getElementById('canvasShip');
  54. var ctxPlayer = canvasPlayer.getContext('2d');
  55.  
  56. var canvasPause = document.getElementById('canvasPause');
  57. var ctxPause = canvasPause.getContext('2d');
  58.  
  59. var canvasEnemy = document.getElementById('canvasEnemy');
  60. var ctxEnemy = canvasEnemy.getContext('2d');
  61.  
  62. var canvasBullets = document.getElementById('canvasBullets');
  63. var ctxBullets = canvasBullets.getContext('2d');
  64.  
  65. var canvasStart = document.getElementById('canvasStart');
  66. var ctxStart = canvasStart.getContext('2d');
  67.  
  68. var canvasHUD = document.getElementById('canvasHUD');
  69. var ctxHUD = canvasHUD.getContext('2d');
  70.  
  71. var canvasMissile = document.getElementById('canvasMissile');
  72. var ctxMissile = canvasMissile.getContext('2d');
  73.  
  74. var canvasEnd = document.getElementById('canvasEnd');
  75. var ctxEnd = canvasHUD.getContext('2d');
  76.  
  77. var canvasBg = document.getElementById('canvasBg');
  78. var ctxBg = canvasBg.getContext('2d');
  79.  
  80. var canvasComets = document.getElementById('canvasComets');
  81. var ctxComets = canvasComets.getContext('2d');
  82.  
  83.  
  84. window.requestAnimationFrame = function(){
  85. return (
  86. window.requestAnimationFrame ||
  87. window.webkitRequestAnimationFrame ||
  88. window.mozRequestAnimationFrame ||
  89. window.oRequestAnimationFrame ||
  90. window.msRequestAnimationFrame ||
  91. function(/* function */ callback){
  92. window.setTimeout(callback, 1000 / 60);
  93. }
  94. );
  95. }();
  96.  
  97. function init() {
  98. window.requestAnimationFrame(startingLoop);
  99. }
  100.  
  101. onload = init();
  102.  
  103. //Check if a key is pressed
  104. document.addEventListener('keydown',checkKeyDown,false);
  105. document.addEventListener('keyup',checkKeyUp,false);
  106.  
  107. //Is a key down variables
  108. var isRightKey = false;
  109. var isLeftKey = false;
  110. var isUpKey = false;
  111. var isDownKey = false;
  112. var isSpacebar = false;
  113. var isEnterKey = false;
  114. var isEscKey = false;
  115.  
  116. //Clear any canvas
  117. function clearCtx(canvas) {
  118. canvas.clearRect(0,0,gameWidth,gameHeight);
  119. }
  120.  
  121. //Check collision of two rectangles
  122. function rect_collision(x1, y1, w1, h1, x2, y2, h2, w2) {
  123. //var top1 = y1;
  124. //var bottom1 = y1 + h1;
  125. //var left1 = x1;
  126. //var right1 = x1 + w1;
  127. //var top2 = y2;
  128. //var bottom2 = y2 + h2;
  129. //var left2 = x2;
  130. //var right2 = x2 + w2;
  131. if (!(x1 + w1 -25 < x2) &&
  132. !(x2 + w2 < x1) &&
  133. !(y1 + h1 < y2) &&
  134. !(y2 + h2 < y1)) {
  135. //console.log("Top 1 " + top1 + " Bottom 1 " + bottom1 + " Top 2 " + top2 + " Bottom 2 " + bottom2 + " Left 1 " + left1 + " Right 1 " + right1 + " Left 2 " + left2 + " Right2 " + right2);
  136. return true;
  137.  
  138. }
  139.  
  140. return false;
  141. }
  142.  
  143.  
  144. //Ship object, the player
  145. function ship() {
  146. this.speed = 7;
  147. this.width = 100;
  148. this.height = 75;
  149. this.drawX = gameWidth/2 - this.width/2;
  150. this.drawY = gameHeight - this.height - 5;
  151. this.inGame = true;
  152. }
  153.  
  154. ship.prototype.draw = function() {
  155. if(this.inGame) {
  156. clearCtx(ctxPlayer);
  157. var srcX;
  158. var srcY;
  159. if(isRightKey && !isLeftKey) {
  160. srcX = 0;
  161. srcY = 75;
  162. } else if (isLeftKey && !isRightKey) {
  163. srcX = 0;
  164. srcY = 153;
  165. } else {
  166. srcX= 0;
  167. srcY = 0;
  168. }
  169. var translateX= this.drawX+(this.width/2);
  170. var translateY= this.drawY+(this.height/2);
  171.  
  172. ctxPlayer.drawImage(sprites,srcX,srcY,this.width,this.height,this.drawX,this.drawY, this.width, this.height);
  173. } else {
  174. clearCtx(ctxPlayer);
  175. }
  176. };
  177.  
  178.  
  179. function Enemy1() {
  180. this.height = 50;
  181. this.width = 100;
  182. this.inGame = true;
  183. this.drawX = (Math.random() * (gameWidth - this.width));
  184. this.drawY = this.height * -1;
  185. this.srcX = 100;
  186. this.srcY = 0;
  187.  
  188. }
  189.  
  190. function drawEnemies1() {
  191. clearCtx(ctxEnemy);
  192. for (i=0;i<enemies1.length;i++) {
  193. if(enemies1[i].inGame) {
  194. if(enemies1[i].drawY > gameHeight) {
  195. enemies1[i].inGame = false;
  196. }
  197. ctxEnemy.drawImage(sprites,100,0,enemies1[i].width,enemies1[i].height,enemies1[i].drawX,enemies1[i].drawY, enemies1[i].width, enemies1[i].height);
  198. enemies1[i].drawY += 5;
  199. }
  200. }
  201. }
  202.  
  203. function showEnemy1(x) {
  204. var spawned = 0;
  205. for(i=0;i<enemies1.length;i++) {
  206. if(!enemies1[i].inGame) {
  207. if(spawned<x) {
  208. enemies1[i] = new Enemy1();
  209. spawned++;
  210. }
  211. }
  212. }
  213. spawnEnemies1(x-spawned);
  214. }
  215.  
  216. function spawnEnemies1(x) {
  217. for (i=0;i<x;i++) {
  218. enemies1[enemies1.length] = new Enemy1();
  219.  
  220. }
  221. }
  222.  
  223. //Functions that execute things every frame.
  224. function pausedLoop(time) {
  225. if (isEscKey) {
  226. ctxPause.drawImage(pauseScreen,0,0);
  227. setTimeout(function () {
  228. window.requestAnimationFrame(pausedLoop);
  229. }, 10);
  230. return;
  231. } else {
  232. clearCtx(ctxPause);
  233. setTimeout(function () {
  234. if(inLoop1) window.requestAnimationFrame(loop1);
  235. }, 10);
  236. return;
  237. }
  238. setTimeout(function () {
  239. if(inLoop1) window.requestAnimationFrame(loop1);
  240. }, 10);
  241. }
  242. function loop1(time) {
  243. drawBg();
  244. if (isRightKey) {
  245. if(player.drawX + player.width <= gameWidth) {
  246. player.drawX+=player.speed;
  247. }
  248. }
  249. if (isLeftKey) {
  250. if(player.drawX >= 0) {
  251. player.drawX-=player.speed;
  252. }
  253. }
  254. if (isEscKey) {
  255. paused = true;
  256. }
  257.  
  258. //Timer to check if you can fire or not.
  259. if(!canFire) {
  260. canFireTimer++;
  261. if(canFireTimer >= canFireInterval) {
  262. canFire = true;
  263. }
  264. }
  265. //End timer
  266.  
  267. if (isSpacebar) {
  268. if(canFire) {
  269. if(player.inGame) {
  270. showBulletGreen();
  271. canFire = false;
  272. canFireTimer = 0;
  273. }
  274. }
  275. }
  276.  
  277. //Timer to know when to try to spawn an enemy
  278. enemy1SpawnTimer++;
  279. missileSpawnTimer++;
  280. if(enemy1SpawnTimer > enemy1SpawnInterval) {
  281. if(canSpawn) {
  282. checkSpawnEnemy1();
  283. enemy1SpawnTimer = 0;
  284. }
  285. }
  286. if(missileSpawnTimer > missileSpawnInterval) {
  287. if(canSpawn) {
  288. checkSpawnMissile();
  289. missileSpawnTimer = 0;
  290. }
  291. }
  292. if(!canSpawn) {
  293. var outs = 0;
  294. for(i=0;i<enemies1.length;i++) {
  295. if(!enemies1[i].inGame) outs++;
  296. }
  297. if(outs === enemies1.length){
  298. canSpawn = true;
  299. if(lives>=1) {
  300. player = new ship();
  301. } else {
  302. playerDead = true;
  303. }
  304. }
  305. }
  306. drawHUD();
  307. checkPlayerHit();
  308. player.draw();
  309. drawBulletsGreen();
  310. drawEnemies1();
  311. drawMissiles();
  312. checkBulletGreenHit();
  313. setTimeout(function () {
  314. if(isEscKey){
  315. window.requestAnimationFrame(pausedLoop);
  316. //Only layers above pause
  317. clearCtx(ctxMissiles);
  318. return;
  319. }
  320. if(playerDead) {
  321. window.requestAnimationFrame(endLoop);
  322. clearCtx(ctxHUD);
  323. clearCtx(ctxEnemy);
  324. clearCtx(ctxPlayer);
  325. clearCtx(ctxBullets);
  326. clearCtx(ctxMissile);
  327. return;
  328. } else if(inLoop1) window.requestAnimationFrame(loop1);
  329.  
  330. }, 10);
  331. }
  332.  
  333. function endLoop(time) {
  334. if(isEnterKey) playerDead = false;
  335. if (playerDead) {
  336. clearCtx(ctxEnd);
  337. ctxEnd.drawImage(deathScreen,0,0);
  338. ctxEnd.fillStyle = "black";
  339. ctxEnd.font = "bold 60px Arial";
  340. var textX = 200;
  341. if(points > 9 && points < 100) {
  342. textX -= 15;
  343. } else if (points > 99 && points < 1000) {
  344. textX -= 35;
  345. }
  346. ctxEnd.fillText("You Scored: " + points, textX, 320);
  347. blinkTimer++;
  348. if(blinkTimer > blinkInterval) {
  349. if(blink) blink = false;
  350. else if(!blink) blink = true;
  351. blinkTimer = 0;
  352. }
  353. if(blink){
  354. ctxEnd.drawImage(sprites, 110, 70, 90, 60, 150, 383, 70, 45);
  355. }
  356. setTimeout(function () {
  357. window.requestAnimationFrame(endLoop);
  358. }, 10);
  359. return;
  360. } else {
  361. clearCtx(ctxEnd);
  362. resetData();
  363. setTimeout(function () {
  364. window.requestAnimationFrame(loop1);
  365. }, 10);
  366. return;
  367. }
  368. setTimeout(function () {
  369. if(inLoop1) window.requestAnimationFrame(loop1);
  370. }, 10);
  371. }
  372.  
  373. function startingLoop(time) {
  374. ctxStart.drawImage(startScreen, 0,0);
  375. if(isEnterKey) {
  376. if(startSelect === 1) {
  377. clearCtx(ctxStart);
  378. inStart = false;
  379. inLoop1 = true;
  380. } else if (startSelect === 2) {
  381. window.location='jackscode.99k.org';
  382. }
  383. }
  384. if(isUpKey) {
  385. if(!hasSelected) {
  386. if(startSelect === 1) startSelect = 2;
  387. else if(startSelect === 2) startSelect = 1;
  388. hasSelected = true;
  389. }
  390. } else if (isDownKey) {
  391. if(!hasSelected) {
  392. if(startSelect === 1) startSelect = 2;
  393. else if(startSelect === 2) startSelect = 1;
  394. hasSelected = true;
  395. }
  396. }
  397. drawStartArrow();
  398. setTimeout(function () {
  399. if(inStart) window.requestAnimationFrame(startingLoop);
  400. if(inLoop1) {
  401. window.requestAnimationFrame(loop1);
  402. clearCtx(ctxStart);
  403. }
  404. }, 10);
  405. }
  406.  
  407. //Draw the HUD
  408. function drawHUD() {
  409. clearCtx(ctxHUD);
  410. ctxHUD.fillStyle = "white";
  411. ctxHUD.font = "bold 20px Arial";
  412. ctxHUD.fillText("Score: " + points , 680, 30);
  413. var liveX = 5;
  414. for(i=0;i<lives - 1;i++) {
  415. ctxHUD.drawImage(sprites, 0, 0, player.width, player.height, liveX, gameHeight - 50, 50, 40);
  416. liveX+=55;
  417. }
  418. }
  419. function checkSpawnEnemy1() {
  420. var randNum = Math.floor(Math.random()*2)
  421. if(randNum < 1) {
  422. showEnemy1(1);
  423. }
  424. }
  425.  
  426. function checkKeyDown(e) {
  427. var keyID = (e.keyCode) ? e.keyCode : e.which;
  428. if (keyID === 38 || keyID === 87) { //38 is up arrow and 87 is W.
  429. isUpKey = true;
  430. e.preventDefault();
  431. }
  432. if (keyID === 39 || keyID === 68) { //39 is right arrow and 68 is D.
  433. isRightKey = true;
  434. e.preventDefault();
  435.  
  436. }
  437. if (keyID === 40 || keyID === 83) { //40 is down arrow and 83 is S.
  438. isDownKey = true;
  439. e.preventDefault();
  440.  
  441. }
  442. if (keyID === 37 || keyID === 65) { //37 is left arrow and 65 is A.
  443. isLeftKey = true;
  444. e.preventDefault();
  445.  
  446. }
  447. if (keyID === 32) { //Space Bar
  448. isSpacebar = true;
  449. e.preventDefault();
  450. }
  451. if (keyID === 13) { //Enter key
  452. isEnterKey = true;
  453. e.preventDefault();
  454. }
  455. if (keyID === 27) {
  456. if(isEscKey) isEscKey = false;
  457. else if(!isEscKey) isEscKey = true;
  458. e.preventDefault();
  459. }
  460. }
  461.  
  462. function checkKeyUp(e) {
  463. var keyID = (e.keyCode) ? e.keyCode : e.which;
  464. if (keyID === 38 || keyID === 87) { //38 is up arrow and 87 is W.
  465. isUpKey = false;
  466. e.preventDefault();
  467.  
  468. }
  469. if (keyID === 39 || keyID === 68) { //39 is right arrow and 68 is D.
  470. isRightKey = false;
  471. e.preventDefault();
  472.  
  473. }
  474. if (keyID === 40 || keyID === 83) { //40 is down arrow and 83 is S.
  475. isDownKey = false;
  476. e.preventDefault();
  477.  
  478. }
  479. if (keyID === 37 || keyID === 65) { //37 is left arrow and 65 is A.
  480. isLeftKey = false;
  481. e.preventDefault();
  482.  
  483. }
  484. if (keyID === 32) { //Space Bar
  485. isSpacebar = false;
  486. e.preventDefault();
  487. }
  488. if (keyID === 13) { ///Enter Key
  489. isEnterKey = false;
  490. e.preventDefault();
  491. }
  492. hasSelected = false;
  493. }
  494.  
  495. function drawStartArrow() {
  496. blinkTimer++;
  497. if(blinkTimer > blinkInterval) {
  498. if(blink) blink = false;
  499. else if(!blink) blink = true;
  500. blinkTimer = 0;
  501. }
  502. if(blink) {
  503. if(startSelect === 1) {
  504. ctxStart.drawImage(sprites, 110, 70, 90, 60, 200, 200, 90, 60);
  505. } else if (startSelect === 2) {
  506. ctxStart.drawImage(sprites, 110, 70, 90, 60, 200, 340, 90, 60);
  507. }
  508. }
  509. }
  510. function bulletGreen() {
  511. inGame = false;
  512. this.width = 9;
  513. this.height = 34;
  514. this.srcX = 350;
  515. this.srcY= 0;
  516. this.drawX = player.drawX + player.width/2 - 5;
  517. this.drawY = player.drawY - this.height;
  518. }
  519.  
  520. function showBulletGreen() {
  521. for(i=0;i<bulletsGreen.length;i++) {
  522. if(!bulletsGreen[i].inGame) {
  523. bulletsGreen[i] = new bulletGreen();
  524. bulletsGreen[i].inGame = true;
  525. return;
  526. }
  527. }
  528. spawnBulletGreen();
  529. }
  530.  
  531. function spawnBulletGreen() {
  532. var i = bulletsGreen.length;
  533. bulletsGreen[i] = new bulletGreen;
  534. bulletsGreen[i].inGame = true;
  535.  
  536. }
  537.  
  538. function drawBulletsGreen() {
  539. clearCtx(ctxBullets);
  540. for(i=0;i<bulletsGreen.length;i++) {
  541. if(bulletsGreen[i].inGame) {
  542. bulletsGreen[i].draw();
  543. bulletsGreen[i].drawY -= 10;
  544. if(bulletsGreen[i].drawY + bulletsGreen[i].height < 0) {
  545. bulletsGreen[i].inGame = false;
  546. }
  547. }
  548. }
  549. }
  550.  
  551. function checkPlayerHit() {
  552. for(i=0;i<enemies1.length;i++) {
  553. if(enemies1[i].inGame && player.inGame){
  554. var x = enemies1[i];
  555. var y = player;
  556. if(rect_collision(x.drawX, x.drawY, x.width, x.height, y.drawX, y.drawY, y.height, y.width)) {
  557. enemies1[i].inGame = false;
  558. lives-=1;
  559. player.inGame = false;
  560. if(lives >= 0) canSpawn = false;
  561. }
  562. }
  563. }
  564. for(i=0;i<missiles.length;i++) {
  565. if(missiles[i].inGame && player.inGame){
  566. var x = missiles[i];
  567. var y = player;
  568. if(rect_collision(x.drawX, x.drawY, x.width, x.height, y.drawX, y.drawY, y.height, y.width)) {
  569. missiles[i].inGame = false;
  570. lives-=1;
  571. player.inGame = false;
  572. if(lives >= 0) canSpawn = false;
  573. }
  574. }
  575. }
  576. }
  577.  
  578. function checkBulletGreenHit() {
  579. for(i=0;i<bulletsGreen.length;i++) {
  580. if(bulletsGreen[i].inGame) {
  581. for(e=0;e<enemies1.length;e++){
  582. if(enemies1[e].inGame) {
  583. var x = bulletsGreen[i];
  584. var y = enemies1[e];
  585. //console.log(x);
  586. //console.log(y);
  587. if(rect_collision(x.drawX, x.drawY, x.width, x.height, y.drawX, y.drawY, y.height, y.width)) {
  588. enemies1[e].inGame = false;
  589. bulletsGreen[i].inGame = false;
  590. points += 10;
  591. }
  592. }
  593. }
  594. }
  595. }
  596. }
  597.  
  598. bulletGreen.prototype.draw = function() {
  599. ctxBullets.drawImage(sprites,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY, this.width, this.height);
  600. }
  601.  
  602. function missile() {
  603. this.height = 50;
  604. this.width = 20;
  605. this.inGame = true;
  606. this.drawX = (Math.random() * (gameWidth - this.width));
  607. this.drawY = this.height * -1;
  608. this.srcX = 150;
  609. this.srcY = 150;
  610. }
  611.  
  612. function checkSpawnMissile() {
  613. var randNum = Math.floor(Math.random()*2)
  614. if(randNum < 1) {
  615. showMissile();
  616. }
  617. }
  618.  
  619. function spawnMissile() {
  620. var i = missiles.length;
  621. missiles[i] = new missile;
  622. missiles[i].inGame = true;
  623.  
  624. }
  625.  
  626. function showMissile() {
  627. for(i=0;i<missiles.length;i++) {
  628. if(!missiles[i].inGame) {
  629. missiles[i] = new missile();
  630. missiles[i].inGame = true;
  631. return;
  632. }
  633. }
  634. spawnMissile();
  635. }
  636.  
  637. function drawMissiles() {
  638. clearCtx(ctxMissile);
  639. for(i=0;i<missiles.length;i++) {
  640. if(missiles[i].inGame) {
  641. missiles[i].draw();
  642. missiles[i].drawY += 10;
  643. if(missiles[i].drawY > gameHeight) {
  644. missiles[i].inGame = false;
  645. }
  646. }
  647. }
  648. }
  649.  
  650. missile.prototype.draw = function() {
  651. ctxMissile.drawImage(sprites,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY, this.width, this.height);
  652. }
  653.  
  654. function resetData() {
  655. points = 0;
  656. playerDead = false;
  657. lives = 3
  658. player = new ship();
  659. enemies1 = [];
  660. missiles=[];
  661. inLoop1 = true;
  662. bgY1 = 0;
  663. bgY2 = gameHeight * -1;
  664. }
  665.  
  666. function drawBg() {
  667. clearCtx(ctxBg);
  668.  
  669. ctxBg.drawImage(background1, 0, bgY1);
  670.  
  671. ctxBg.drawImage(background1, 0, bgY2);
  672. if(bgY1 >= gameHeight) bgY1 = gameHeight * -1;
  673. if(bgY2 >= gameHeight) bgY2 = gameHeight * -1;
  674. bgY1+= 2;
  675. bgY2+= 2;
  676.  
  677. }
  678.  
  679. function comet1() {
  680. this.height = 70;
  681. this.width = 75;
  682. this.srcX = 200;
  683. this.srcY = 100;
  684. this.inGame = true;
  685. this.drawX = (Math.random() * (gameWidth - this.width));
  686. this.drawY = this.height * -1;
  687. }
  688.  
  689. function comet2() {
  690. this.srcX = 300;
  691. this.srcY = 100;
  692. this.height = 42
  693. this.width = 44;
  694. this.inGame = true;
  695. this.drawX = (Math.random() * (gameWidth - this.width));
  696. this.drawY = this.height * -1;
  697. }
  698.  
  699. //50/50 chance of spawning either comet
  700. function checkSpawnComet() {
  701. var randNum = Math.floor(Math.random()*2);
  702. if(randNum < 1) {
  703. var randNum2 = Math.floor(Math.random()*2);
  704. if(randNum < 1) showComet1();
  705. else showComet2();
  706. }
  707. }
  708.  
  709. function checkSpawnComet1() {
  710. var randNum = Math.floor(Math.random()*2)
  711. if(randNum < 1) {
  712. showComet1();
  713. }
  714. }
  715.  
  716. function checkSpawnComet2() {
  717. var randNum = Math.floor(Math.random()*2)
  718. if(randNum < 1) {
  719. showComet2();
  720. }
  721. }
  722.  
  723. function spawnComet1() {
  724. var i = comets1.length;
  725. comets1[i] = new comet1;
  726. comet1[i].inGame = true;
  727.  
  728. }
  729.  
  730. function spawnComet2() {
  731. var i = comets2.length;
  732. comets2[i] = new missile;
  733. comets2[i].inGame = true;
  734.  
  735. }
  736.  
  737. function showComet1() {
  738. for(i=0;i<missiles.length;i++) {
  739. if(!missiles[i].inGame) {
  740. missiles[i] = new missile();
  741. missiles[i].inGame = true;
  742. return;
  743. }
  744. }
  745. spawnMissile();
  746. }
  747.  
  748. function showComet2() {
  749. for(i=0;i<missiles.length;i++) {
  750. if(!missiles[i].inGame) {
  751. missiles[i] = new missile();
  752. missiles[i].inGame = true;
  753. return;
  754. }
  755. }
  756. spawnMissile();
  757. }
  758.  
  759. function drawComets() {
  760. clearCtx(ctxMissile);
  761. for(i=0;i<missiles.length;i++) {
  762. if(missiles[i].inGame) {
  763. missiles[i].draw();
  764. missiles[i].drawY += 10;
  765. if(missiles[i].drawY > gameHeight) {
  766. missiles[i].inGame = false;
  767. }
  768. }
  769. }
  770. }
  771.  
  772. comet1.prototype.draw = function() {
  773. ctxMissile.drawImage(sprites,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY, this.width, this.height);
  774. }
  775.  
  776. comet2.prototype.draw = function() {
  777. ctxMissile.drawImage(sprites,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY, this.width, this.height);
  778. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement