HTML : <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Rowdy…
- User
HTML :
#game-container { position: relative; width: 800px; margin: 0 auto; }
#gameCanvas { background-color: #444; }
#ui-overlay { position: absolute; top: 0; left: 0; width: 800px; height: 600px; pointer-events: none; }
#health-bars { display: flex; justify-content: space-between; padding: 10px; }
.health-bar-container { width: 200px; height: 20px; background-color: #555; border: 2px solid #fff; position: relative; }
.health-bar { height: 100%; background-color: green; width: 100%; }
#score { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); }
#game-over-overlay, #start-screen { position: absolute; top: 0; left: 0; width: 800px; height: 600px; background-color: rgba(0, 0, 0, 0.8); display: none; flex-direction: column; justify-content: center; align-items: center; }
#game-over-overlay.show, #start-screen.show { display: flex; } JAVASCRIPT : // Get canvas and context const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d');
// Game variables let player, opponents, keys, score, gameOver, gameStarted;
// Initialize game function init() { // Player properties player = { x: 100, y: 500, width: 40, height: 60, speed: 5, dx: 0, dy: 0, health: 100, maxHealth: 100, attacking: false, attackCooldown: false, color: 'blue', };
// Opponents array opponents = []; spawnOpponent();
// Game state keys = {}; score = 0; gameOver = false; gameStarted = false;
// Event listeners document.addEventListener('keydown', keyDownHandler); document.addEventListener('keyup', keyUpHandler);
// Start game loop update(); }
// Key handlers function keyDownHandler(e) { keys[e.key] = true; if (!gameStarted && e.key === 'Enter') { gameStarted = true; } }
function keyUpHandler(e) { delete keys[e.key]; }
// Spawn a new opponent function spawnOpponent() { const opponent = { x: canvas.width - 140, y: Math.random() * (canvas.height - 100) + 50, width: 40, height: 60, speed: 3 + Math.random() * 2, dx: 0, dy: 0, health: 100, maxHealth: 100, attacking: false, attackCooldown: false, color: 'red', }; opponents.push(opponent); }
// Update health bars function updateHealthBars() { const playerHealthBar = document.getElementById('player-health'); const opponentHealthBar = document.getElementById('opponent-health');
playerHealthBar.style.width = (player.health / player.maxHealth) * 100 + '%';
if (opponents[0]) { opponentHealthBar.style.width = (opponents[0].health / opponents[0].maxHealth) * 100 + '%'; } else { opponentHealthBar.style.width = '0%'; } }
// Move player function movePlayer() { if (keys['ArrowLeft'] && player.x > 0) { player.dx = -player.speed; } else if (keys['ArrowRight'] && player.x + player.width < canvas.width) { player.dx = player.speed; } else { player.dx = 0; }
- Assistant