Guest User

Untitled

a guest
Mar 31st, 2015
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1.  
  2. var canvas;
  3. var ctx;
  4. var width = 600;
  5. var height = 600;
  6. var starfield;
  7. var starX = 0;
  8. var starY = 0;
  9. var starY2 = -600;
  10.  
  11.  
  12. var score = 0;
  13. var highscore = 10;
  14. var alive = true;
  15. var lives = 3;
  16. var gameStarted = false;
  17.  
  18. livesimage = new Image()
  19. livesimage.src = 'hart.png';
  20.  
  21. var levens3 = true;
  22. var levens2 = false;
  23. var levens1 = false;
  24.  
  25.  
  26. function livedraw()
  27. {
  28. if(lives == 3)
  29. {
  30. ctx.drawImage(livesimage,10,10,50,38);
  31. ctx.drawImage(livesimage,50,10,50,38);
  32. ctx.drawImage(livesimage,90,10,50,38);
  33. }
  34. if(lives == 2)
  35. {
  36. ctx.drawImage(livesimage,10,10,50,38);
  37. ctx.drawImage(livesimage,50,10,50,38);
  38. }
  39. if(lives == 1)
  40. {
  41. ctx.drawImage(livesimage,10,10,50,38);
  42. }
  43.  
  44. }
  45.  
  46.  
  47. function clearCanvas() {
  48. ctx.clearRect(0, 0, width, height);
  49. }
  50.  
  51. function init() {
  52. canvas = document.getElementById('canvas');
  53. ctx = canvas.getContext('2d');
  54. ship = new Image();
  55. ship.src = 'ship.png';
  56. starfield = new Image();
  57. starfield.src = 'starfield.jpg';
  58. document.addEventListener('keydown', keyDown, false);
  59. document.addEventListener('keyup', keyUp, false);
  60. canvas.addEventListener('click', gameStart, false);
  61. gameLoop();
  62. }
  63.  
  64.  
  65. function gameStart() {
  66. gameStarted = true;
  67. canvas.removeEventListener('click', gameStart, false);
  68. }
  69.  
  70.  
  71. function gameLoop() {
  72. clearCanvas();
  73. drawStarfield();
  74. if (alive && gameStarted && lives > 0) {
  75.  
  76.  
  77. hitTest();
  78. moveEnemies();
  79. moveLaser();
  80. drawEnemies();
  81. drawShip();
  82. drawLaser();
  83. shipCollision();
  84. livedraw();
  85. update();
  86. DeleteItems();
  87. console.log(highscore);
  88.  
  89. }
  90. scoreTotal();
  91.  
  92. setTimeout(gameLoop, 1000 / 30);
  93. }
  94.  
  95.  
  96. function scoreTotal() {
  97. ctx.font = 'bold 18px VT323';
  98. ctx.fillStyle = '#fff';
  99. ctx.fillText('Score: ', 10, 55);
  100. ctx.fillText(score, 70, 55);
  101. ctx.fillText('Lives: ', 10, 30);
  102. ctx.fillText(lives, 68, 30);
  103.  
  104. if (!alive) {
  105. ctx.fillText('Game Over!', 245, (height / 2));
  106.  
  107.  
  108. ctx.fillRect((width / 2) - 65, (height / 2) + 10, 100, 40);
  109. ctx.fillStyle = '#000';
  110. ctx.fillText('Opnieuw?', 252, (height / 2) + 35);
  111. canvas.addEventListener('click', continueButton, false);
  112. }
  113.  
  114.  
  115. if (!gameStarted) {
  116. ctx.font = 'bold 50px VT323';
  117. ctx.fillText('Sander Gouman top down', (width / 2) - 210, (height / 2));
  118. ctx.font = 'bold 20px VT323';
  119. ctx.fillText('Klik om te spelen', (width / 2) - 56, (height / 2) + 30);
  120. ctx.fillText('Beweeg met de pijltjes', (width / 2) - 100, (height / 2) + 60);
  121. ctx.fillText('Gebruik de spatiebalk om te schieten', (width / 2) - 100, (height / 2) + 90);
  122. }
  123. }
  124.  
  125.  
  126. function continueButton(e) {
  127. var cursorPos = getCursorPos(e);
  128. if (cursorPos.x > (width / 2) - 53 && cursorPos.x < (width / 2) + 47 && cursorPos.y > (height / 2) + 10 && cursorPos.y < (height / 2) + 50) {
  129. alive = true;
  130. lives = 3;
  131. reset();
  132. canvas.removeEventListener('click', continueButton, false);
  133. }
  134. }
  135.  
  136.  
  137. function getCursorPos(e) {
  138. var x;
  139. var y;
  140.  
  141. if (e.pageX || e.pageY) {
  142. x = e.pageX;
  143. y = e.pageY;
  144. } else {
  145. x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  146. y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  147. }
  148.  
  149. x -= canvas.offsetLeft;
  150. y -= canvas.offsetTop;
  151. var cursorPos = new cursorPosition(x, y);
  152.  
  153. return cursorPos;
  154. }
  155.  
  156. function cursorPosition(x, y) {
  157. this.x = x;
  158. this.y = y;
  159. }
  160.  
  161. function checkLives() {
  162. lives -= 1;
  163. if (lives > 0) {
  164. reset();
  165. } else if (lives == 0) {
  166.  
  167.  
  168. alive = false;
  169. if (score > parseInt(localStorage.getItem("highscore"))) {
  170. localStorage.setItem("highscore", score);
  171.  
  172. }
  173.  
  174.  
  175. }
  176. }
  177.  
  178.  
  179. function reset() {
  180. var enemy_reset_x = 50;
  181. ship_x = (width / 2) - 25;
  182. ship_y = height - 75;
  183. ship_w = 50;
  184. ship_h = 57;
  185.  
  186. for (var i = 0; i < enemies.length; i++) {
  187. enemies[i][0] = enemy_reset_x;
  188. enemies[i][1] = -45;
  189. enemy_reset_x = enemy_reset_x + enemy_w + 60;
  190. }
  191. }
  192.  
  193.  
  194. function drawStarfield() {
  195. ctx.drawImage(starfield, starX, starY);
  196. ctx.drawImage(starfield, starX, starY2);
  197.  
  198. if (starY > 600) {
  199. starY = -599;
  200. }
  201.  
  202. if (starY2 > 600) {
  203. starY2 = -599;
  204. }
  205.  
  206. starY += 1;
  207. starY2 += 1;
  208. }
  209.  
  210. window.onload = init;
Advertisement
Add Comment
Please, Sign In to add comment