Advertisement
EdmundC

skydef2

Aug 8th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Airplane Shooter Game</title>
  7.     <style>
  8.         canvas { background: #000; display: block; margin: 0 auto; }
  9.         body { text-align: center; color: white; font-family: sans-serif; }
  10.     </style>
  11. </head>
  12. <body>
  13.     <canvas id="gameCanvas" width="800" height="600"></canvas>
  14.     <script>
  15.         const canvas = document.getElementById('gameCanvas');
  16.         const ctx = canvas.getContext('2d');
  17.  
  18.         function getUsername() {
  19.             // Attempt to detect the OS and return the username
  20.             const platform = window.navigator.platform;
  21.             let username = '';
  22.  
  23.             if (platform.startsWith('Win')) {
  24.                 // Windows OS - Use environment variable to get the username
  25.                 username = process.env.USERNAME || process.env.USER || 'defaultUser';
  26.             } else if (platform.startsWith('Mac') || platform.startsWith('Linux')) {
  27.                 // MacOS or Linux
  28.                 username = process.env.USER || process.env.LOGNAME || 'defaultUser';
  29.             }
  30.  
  31.             return username;
  32.         }
  33.  
  34.         const username = getUsername();
  35.         const planeImage = new Image();
  36.  
  37.         // Construct the path dynamically
  38.         planeImage.src = `/Users/${username}/Desktop/plane2.png`;  // macOS/Linux path example
  39.         // For Windows, it could be something like:
  40.         // planeImage.src = `C:/Users/${username}/Desktop/plane2.png`;
  41.  
  42.         planeImage.onload = function() {
  43.             console.log("Plane image loaded successfully");
  44.             ctx.drawImage(planeImage, 100, 100, 100, 100);  // Draw it at a specific location
  45.         };
  46.  
  47.         planeImage.onerror = function() {
  48.             console.error("Failed to load the plane image.");
  49.         };
  50.  
  51.         let planeX = canvas.width / 2 - 25;
  52.         let planeY = canvas.height - 80;
  53.         const planeWidth = 50;  // Adjust based on image dimensions
  54.         const planeHeight = 50; // Adjust based on image dimensions
  55.  
  56.         function drawPlane() {
  57.             console.log("Drawing the plane at", planeX, planeY);
  58.             ctx.drawImage(planeImage, planeX, planeY, planeWidth, planeHeight);
  59.         }
  60.  
  61.         function draw() {
  62.             ctx.clearRect(0, 0, canvas.width, canvas.height);
  63.             drawPlane();
  64.             requestAnimationFrame(draw);
  65.         }
  66.  
  67.         document.addEventListener('keydown', (e) => {
  68.             if (e.key === 'ArrowLeft' && planeX > 0) planeX -= 10;
  69.             if (e.key === 'ArrowRight' && planeX < canvas.width - planeWidth) planeX += 10;
  70.             if (e.key === ' ') console.log("Space pressed - bullet would be shot");
  71.         });
  72.  
  73.         // Start the game loop
  74.         planeImage.onload = function() {
  75.             draw();  // Start the game loop once the image is loaded
  76.         };
  77.     </script>
  78. </body>
  79. </html>
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement