Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Airplane Shooter Game</title>
- <style>
- canvas { background: #000; display: block; margin: 0 auto; }
- body { text-align: center; color: white; font-family: sans-serif; }
- </style>
- </head>
- <body>
- <canvas id="gameCanvas" width="800" height="600"></canvas>
- <script>
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- function getUsername() {
- // Attempt to detect the OS and return the username
- const platform = window.navigator.platform;
- let username = '';
- if (platform.startsWith('Win')) {
- // Windows OS - Use environment variable to get the username
- username = process.env.USERNAME || process.env.USER || 'defaultUser';
- } else if (platform.startsWith('Mac') || platform.startsWith('Linux')) {
- // MacOS or Linux
- username = process.env.USER || process.env.LOGNAME || 'defaultUser';
- }
- return username;
- }
- const username = getUsername();
- const planeImage = new Image();
- // Construct the path dynamically
- planeImage.src = `/Users/${username}/Desktop/plane2.png`; // macOS/Linux path example
- // For Windows, it could be something like:
- // planeImage.src = `C:/Users/${username}/Desktop/plane2.png`;
- planeImage.onload = function() {
- console.log("Plane image loaded successfully");
- ctx.drawImage(planeImage, 100, 100, 100, 100); // Draw it at a specific location
- };
- planeImage.onerror = function() {
- console.error("Failed to load the plane image.");
- };
- let planeX = canvas.width / 2 - 25;
- let planeY = canvas.height - 80;
- const planeWidth = 50; // Adjust based on image dimensions
- const planeHeight = 50; // Adjust based on image dimensions
- function drawPlane() {
- console.log("Drawing the plane at", planeX, planeY);
- ctx.drawImage(planeImage, planeX, planeY, planeWidth, planeHeight);
- }
- function draw() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- drawPlane();
- requestAnimationFrame(draw);
- }
- document.addEventListener('keydown', (e) => {
- if (e.key === 'ArrowLeft' && planeX > 0) planeX -= 10;
- if (e.key === 'ArrowRight' && planeX < canvas.width - planeWidth) planeX += 10;
- if (e.key === ' ') console.log("Space pressed - bullet would be shot");
- });
- // Start the game loop
- planeImage.onload = function() {
- draw(); // Start the game loop once the image is loaded
- };
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement