Here's a fun, replayable space shooter game with power-ups, enemy waves, scoring, and increasing difficulty. The game features smooth controls, visual effects, and sound to enhance the experience.
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Galactic Defender</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
overflow: hidden;
font-family: 'Arial', sans-serif;
color: white;
}
#gameCanvas {
display: block;
margin: 0 auto;
background-color: #111;
}
#gameUI {
position: absolute;
top: 10px;
left: 10px;
font-size: 18px;
}
#startScreen, #gameOverScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10;
}
#gameOverScreen {
display: none;
}
h1 {
font-size: 48px;
color: #4af;
text-shadow: 0 0 10px #4af;
margin-bottom: 20px;
}
button {
padding: 15px 30px;
font-size: 20px;
background-color: #4af;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 10px;
transition: all 0.3s;
}
button:hover {
background-color: #6cf;
transform: scale(1.05);
}
.powerup-indicator {
position: absolute;
top: 10px;
right: 10px;
font-size: 16px;
color: #ff0;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="gameUI">
<div>Score: <span id="score">0</span></div>
<div>Lives: <span id="lives">3</span></div>
<div>Wave: <span id="wave">1</span></div>
</div>
<div class="powerup-indicator" id="powerupIndicator"></div>
<div id="startScreen">
<h1>GALACTIC DEFENDER</h1>
<p>Use arrow keys to move, space to shoot</p>
<p>Collect power-ups for special abilities</p>
<button id="startButton">START GAME</button>
</div>
<div id="gameOverScreen">
<h1>GAME OVER</h1>
<p>Final Score: <span id="finalScore">0</span></p>
<button id="restartButton">PLAY AGAIN</button>
</div>
<audio id="shootSound" src="data:audio/wav;base64,UklGRl9vT19XQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YU..."></audio>
<audio id="explosionSound" src="data:audio/wav;base64,UklGRl9vT19XQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YU..."></audio>
<audio id="powerupSound" src="data:audio/wav;base64,UklGRl9vT19XQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YU..."></audio>
<script>
// Game variables
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const waveElement = document.getElementById('wave');
const powerupIndicator = document.getElementById('powerupIndicator');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const startButton = document.getElementById('startButton');
const restartButton = document.getElementById('restartButton');
// Sound effects (simplified base64 strings - in a real game, use proper sound files)
const shootSound = document.getElementById('shootSound');
const explosionSound = document.getElementById('explosionSound');
const powerupSound = document.getElementById('powerupSound');
// Game state
let gameRunning = false;
let score = 0;
let lives = 3;
let wave = 1;
let gameTime = 0;
let enemiesDefeated = 0;
let powerupActive = false;
let powerupType = '';
let powerupTime = 0;
// Player
const player = {
x: canvas.width / 2,
y: canvas.height - 50,
width: 40,
height: 40,
speed: 5,
color: '#4af',
bullets: [],
lastShot: 0,
shootDelay: 300,
isAlive: true
};
// Enemies
let enemies = [];
const enemyTypes = [
{ color: '#f44', health: 1, speed: 2, score: 100, size: 30 },
{ color: '#f84', health: 2, speed: 1.5, score: 200, size: 35 },
{ color: '#8f4', health: 3, speed: 1, score: 300, size: 40 }
];
// Power-ups
let powerups = [];
const powerupTypes = [
{ color: '#ff0', type: 'rapid', duration: 5000 },
{ color: '#f8f', type: 'shield', duration: 7000 },
{ color: '#8ff', type: 'spread', duration: 6000 }
];
// Explosions
let explosions = [];
// Controls
const keys = {
ArrowLeft: false,
ArrowRight: false,
ArrowUp: false,
ArrowDown: false,
' ': false
};
// Event listeners
document.addEventListener('keydown', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = true;
}
});
document.addEventListener('keyup', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = false;
}
});
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
// Game functions
function startGame() {
gameRunning = true;
score = 0;
lives = 3;
wave = 1;
gameTime = 0;
enemiesDefeated = 0;
powerupActive = false;
player.x = canvas.width / 2;
player.y = canvas.height - 50;
player.bullets = [];
player.isAlive = true;
enemies = [];
powerups = [];
explosions = [];
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
updateUI();
spawnEnemyWave();
requestAnimationFrame(gameLoop);
}
function gameLoop(timestamp) {
if (!gameRunning) return;
// Clear canvas
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw stars (background)
drawStars();
// Update and draw player
if (player.isAlive) {
updatePlayer();
drawPlayer();
}
// Update and draw bullets
updateBullets();
drawBullets();
// Update and draw enemies
updateEnemies();
drawEnemies();
// Update and draw power-ups
updatePowerups();
drawPowerups();
// Update and draw explosions
updateExplosions();
drawExplosions();
// Check for next wave
if (enemies.length === 0) {
wave++;
spawnEnemyWave();
updateUI();
}
// Update power-up timer
if (powerupActive) {
powerupTime--;
if (powerupTime <= 0) {
powerupActive = false;
powerupType = '';
powerupIndicator.textContent = '';
player.shootDelay = 300; // Reset to normal fire rate
}
}
gameTime++;
requestAnimationFrame(gameLoop);
}
function drawStars() {
ctx.fillStyle = 'white';
for (let i = 0; i < 100; i++) {
const x = Math.sin(i * 100) * canvas.width / 2 + canvas.width / 2;
const