Advertisement
Sniperninja564

Missile Collector Game

Nov 17th, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.33 KB | None | 0 0
  1. <!DOCYPE html>
  2. <html>
  3. <head>
  4.     <title> My Canvas </title>
  5.     <meta charset = "utf-8" />
  6.     <style>
  7.         canvas {
  8.             background-image: url("sprites/bg_background.png");
  9.         }
  10.     </style>
  11. </head>
  12. <body>
  13.     <h1> Missile Collector! </h1>
  14.     <h2 onclick = "showCoords(event)">
  15.     <p id="demo"></p>
  16.     <canvas id="myCanvas" width="540" height="960"></canvas>
  17.     <script>
  18. // Initialize
  19. var canvas = document.getElementById("myCanvas")
  20. var ctx = canvas.getContext("2d");
  21. var x = 270;
  22.  
  23. // Get mouse position
  24. function showCoords(event) {
  25.     x = event.clientX;
  26.     var coords = "X coords: " + x;
  27.     document.getElementById("demo").innerHTML = coords;
  28. }
  29.  
  30. // Score Veriables
  31. var score = 0;
  32. var health = 5;
  33.  
  34. // Player Veriables
  35. var player = new Image();
  36. var playerX = 270;
  37. var playerY = 840;
  38. const playerThick = 48;
  39. const spd = 4;
  40.  
  41. // Missle Veriables
  42. var missile = new Image();
  43. const missileXThick = 42;
  44. const missileYThick = 56;
  45. var missileX = Math.floor((Math.random() * canvas.width-missileXThick) + 1+missileXThick);
  46. var missileY = 0;
  47.  
  48. // Draw the score
  49. function drawScore() {
  50.     ctx.font = "24px Arial";
  51.     ctx.fillStyle = "#000000";
  52.     ctx.fillText("Score: " + score, 5, 25);
  53.     ctx.fillText("Health: " + health, 5, 50);
  54.    
  55.     // Game Over
  56.     if (health <= 0) {
  57.         alert("Game Over");
  58.     }
  59. }
  60.  
  61. // Reset the Missle
  62. function missileReset() {
  63.     missileX = Math.floor((Math.random() * canvas.width-missileXThick) + 1+missileXThick);
  64.     missileY = 0;
  65. }
  66.  
  67. // Draw the Missle
  68. function drawMissile() {
  69.     ctx.drawImage(missile, missileX, missileY)
  70.     missile.src = 'sprites/spr_missile.png';
  71.    
  72.     missileY += 5;
  73.     missileCollision();
  74. }
  75.  
  76. // Draw Player
  77. function drawPlayer() {
  78.     ctx.drawImage(player, playerX, playerY);
  79.    player.src = 'sprites/spr_bowl.png';
  80.    
  81.     playerX = x-playerThick/2;
  82. }
  83.  
  84. // Collisions
  85. function missileCollision() {
  86.     if (missileY = playerY-missileYThick && missileX > x && missileX+missileXThick < x+playerThick) {
  87.         score += 1;
  88.         missileReset();
  89.     }
  90.     if (missileY >= canvas.height-missileYThick) {
  91.         health -= 1;
  92.         missileReset();
  93.     }
  94. }
  95.  
  96. // Draw Everything
  97. function draw() {
  98.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  99.    
  100.     ctx.onclick = function() {showCoords(event)};
  101.    
  102.     if (health > 0) {
  103.         drawPlayer();
  104.         drawMissile();
  105.         drawScore();
  106.     }
  107.    
  108.     requestAnimationFrame(draw);
  109. }
  110.  
  111. draw();
  112.  
  113.     </script>
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement