generate the html, css and js all in a single document for a 2-d space shooter like game
- User
generate the html, css and js all in a single document for a 2-d space shooter like game
Assistant
I'll help create a simple 2D space shooter game using HTML5 Canvas. Here's a basic implementation where you control a spaceship that can shoot at incoming enemies:
<!DOCTYPE html> <html> <head> <title>Space Shooter</title> <style> canvas { border: 2px solid white; background-color: black; display: block; margin: 0 auto; } body { background-color: #222; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } #score { position: fixed; top: 20px; left: 20px; color: white; font-family: Arial, sans-serif; font-size: 24px; } </style> </head> <body> <div id="score">Score: 0</div> <canvas id="gameCanvas" width="800" height="600"></canvas> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const scoreElement = document.getElementById('score'); // Game objects const player = { x: canvas.width / 2, y: canvas.height - 50, width: 50, height: 30, speed: 5 }; let bullets = []; let enemies = []; let score = 0; let gameLoop; let keys = {}; // Event listeners document.addEventListener('keydown', (e) => { keys[e.key] = true; if (e.key === ' ') { shoot(); } }); document.addEventListener('keyup', (e) => { keys[e.key] = false; }); function shoot() { bullets.push({ x: player.x + player.width / 2, y: player.y, width: 4, height: 10, speed: 7 }); } function spawnEnemy() { enemies.push({ x: Math.random() * (canvas.width - 30), y: -30, width: 30, height: 30, speed: 2 }); } function updatePlayer() { if (keys['ArrowLeft'] && player.x > 0) { player.x -= player.speed; } if (keys['ArrowRight'] && player.x < canvas.width - player.width) { player.x += player.speed; } } function updateBullets() { for (let i = bullets.length - 1; i >= 0; i--) { bullets[i].y -= bullets[i].speed; if (bullets[i].y < 0) { bullets.splice(i, 1); } } } function updateEnemies() { if (Math.random() < 0.02) { spawnEnemy(); } for (let i = enemies.length - 1; i >= 0; i--) { enemies[i].y += enemies[i].speed; // Check collision with bullets for (let j = bullets.length - 1; j >= 0; j--) { if (checkCollision(enemies[i], bullets[j])) { enemies.splice(i, 1); bullets.splice(j, 1); score += 10; scoreElement.textContent = `Score: ${score}`; return; } } // Check collision with player if (checkCollision(enemies[i], player)) { gameOver(); return; } // Remove enemies that go off screen if (enemies[i] && enemies[i].y > canvas.height) { enemies.splice(i, 1); } } } function checkCollision(rect1, rect2) { return rect1.x < rect2.x + rect2.width && rect1.x + rect1.width > rect2.x && rect1.y < rect2.y + rect2.height && rect1.y + rect1.height > rect2.y; } function draw() { // Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw player ctx.fillStyle = 'lime'; ctx.fillRect(player.x, player.y, player.width, player.height); // Draw bullets ctx.fillStyle = 'yellow'; bullets.forEach(bullet => { ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height); }); // Draw enemies ctx.fillStyle = 'red'; enemies.forEach(enemy => { ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height); }); } function gameOver() { clearInterval(gameLoop); ctx.fillStyle = 'white'; ctx.font = '48px Arial'; ctx.textAlign = 'center'; ctx.fillText('Game Over!', canvas.width / 2, canvas.height / 2); ctx.font = '24px Arial'; ctx.fillText('Press F5 to restart', canvas.width / 2, canvas.height / 2 + 40); } function gameLoop() { updatePlayer(); updateBullets(); updateEnemies(); draw(); requestAnimationFrame(gameLoop); } // Start the game gameLoop(); </script> </body> </html>This creates a simple space shooter game where you can:
- Move the player (green rectangle) left and right using arrow keys
- Shoot bullets (yellow) by pressing spacebar
- Destroy enemies (red) by hitting them with bullets
- Score points for each enemy destroyed
- Game ends if an enemy hits the player
The game features:
- Score tracking
- Collision detection
- Continuous enemy spawning
- Simple controls
- Game over screen
To make the game more engaging, you could add:
- Different enemy types
- Power-ups
- Better graphics using sprites
- Sound effects
- Multiple lives
- Levels with increasing difficulty
Would you like me to explain any part of the code or suggest ways to enhance specific features?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.