Advertisement
TheiPhoneFan

Spaceship Game

Jan 4th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.37 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Space Invaders Game</title>
  8.     <style>
  9.         body {
  10.             margin: 0;
  11.             overflow: hidden;
  12.             font-family: Arial, sans-serif;
  13.         }
  14.  
  15.         canvas {
  16.             display: block;
  17.         }
  18.     </style>
  19. </head>
  20.  
  21. <body>
  22.     <canvas id="gameCanvas" width="800" height="600"></canvas>
  23.  
  24.     <script>
  25.         const canvas = document.getElementById("gameCanvas");
  26.         const ctx = canvas.getContext("2d");
  27.  
  28.         // Player spaceship
  29.         const player = {
  30.             x: canvas.width / 2,
  31.             y: canvas.height - 50,
  32.             width: 50,
  33.             height: 50,
  34.             color: "#00F"
  35.         };
  36.  
  37.         // Falling objects
  38.         const objects = [];
  39.  
  40.         function drawPlayer() {
  41.             ctx.fillStyle = player.color;
  42.             ctx.fillRect(player.x - player.width / 2, player.y - player.height / 2, player.width, player.height);
  43.         }
  44.  
  45.         function drawObjects() {
  46.             for (const object of objects) {
  47.                 ctx.fillStyle = object.color;
  48.                 ctx.fillRect(object.x - object.width / 2, object.y - object.height / 2, object.width, object.height);
  49.             }
  50.         }
  51.  
  52.         function update() {
  53.             // Move player with arrow keys
  54.             window.addEventListener('keydown', function(e) {
  55.                 if (e.key === 'ArrowLeft' && player.x > player.width / 2) {
  56.                    player.x -= 10;
  57.                 } else if (e.key === 'ArrowRight' && player.x < canvas.width - player.width / 2) {
  58.                    player.x += 10;
  59.                 }
  60.             });
  61.  
  62.             // Move falling objects
  63.             for (const object of objects) {
  64.                 object.y += 5;
  65.  
  66.                 // Check collision with player
  67.                 if (
  68.                     object.x + object.width / 2 > player.x - player.width / 2 &&
  69.                    object.x - object.width / 2 < player.x + player.width / 2 &&
  70.                    object.y + object.height / 2 > player.y - player.height / 2 &&
  71.                    object.y - object.height / 2 < player.y + player.height / 2
  72.                ) {
  73.                    alert("Game Over! You got hit!");
  74.                     document.location.reload();
  75.                 }
  76.             }
  77.  
  78.             // Remove objects that are out of the canvas
  79.             objects.forEach((object, index) => {
  80.                 if (object.y > canvas.height) {
  81.                     objects.splice(index, 1);
  82.                 }
  83.             });
  84.  
  85.             // Add a new falling object randomly
  86.             if (Math.random() < 0.02) {
  87.                const newObject = {
  88.                    x: Math.random() * canvas.width,
  89.                    y: 0,
  90.                    width: 30,
  91.                    height: 30,
  92.                    color: "#F00"
  93.                };
  94.                objects.push(newObject);
  95.            }
  96.        }
  97.  
  98.        function draw() {
  99.            ctx.clearRect(0, 0, canvas.width, canvas.height);
  100.            drawPlayer();
  101.            drawObjects();
  102.        }
  103.  
  104.        function gameLoop() {
  105.            update();
  106.            draw();
  107.            requestAnimationFrame(gameLoop);
  108.        }
  109.  
  110.        gameLoop();
  111.    </script>
  112. </body>
  113.  
  114. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement