<!DOCTYPE html>
<html>
<head>
<title>Missile Game</title>
<style>
canvas {
background-color: #000; /* black background */
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
// Game Code
var canvas = document.getElementById('gameCanvas');
var ctx = canvas.getContext('2d');
// Game variables
var missile = {
x: canvas.width / 2,
y: canvas.height / 2,
speed: 4,
dx: 0,
dy: -1,
size: 10
};
var obstacles = [];
var particles = [];
var gameState = 'playing'; // 'playing', 'won', 'lost'
// Keys pressed
var keys = {};
document.addEventListener('keydown', function(e) {
keys[e.code] = true;
switch(e.code) {
case 'KeyW':
missile.dx = 0;
missile.dy = -1;
break;
case 'KeyS':
missile.dx = 0;
missile.dy = 1;
break;
case 'KeyA':
missile.dx = -1;
missile.dy = 0;
break;
case 'KeyD':
missile.dx = 1;
missile.dy = 0;
break;
}
});
document.addEventListener('keyup', function(e) {
delete keys[e.code];
});
function createObstacles() {
for (var i = 0; i < 10; i++) {
var obstacle = {
x: Math.random() * (canvas.width - 50),
y: Math.random() * (canvas.height - 50),
width: 50,
height: 50
};
obstacles.push(obstacle);
}
}
createObstacles();
var door = {
x: Math.random() * (canvas.width - 50),
y: Math.random() * (canvas.height - 80),
width: 50,
height: 80,
destroyed: false
};
function isColliding(rect1, rect2) {
return !(rect2.x > rect1.x + rect1.size ||
rect2.x + rect2.width < rect1.x ||
rect2.y > rect1.y + rect1.size ||
rect2.y + rect2.height < rect1.y);
}
function createExplosion(x, y) {
for (var i = 0; i < 100; i++) {
var particle = {
x: x,
y: y,
dx: (Math.random() - 0.5) * 5,
dy: (Math.random() - 0.5) * 5,
life: Math.random() * 30 + 10,
alpha: 1,
color: 'rgb(' + (255) + ',' + Math.floor(Math.random() * 255) + ',0)'
};
particles.push(particle);
}
}
function update() {
// Move the missile
missile.x += missile.dx * missile.speed;
missile.y += missile.dy * missile.speed;
// Keep missile within canvas bounds
if (missile.x < 0) missile.x = 0;
if (missile.x > canvas.width) missile.x = canvas.width;
if (missile.y < 0) missile.y = 0;
if (missile.y > canvas.height) missile.y = canvas.height;
// Check collision with obstacles
for (var i = 0; i < obstacles.length; i++) {
if (isColliding(missile, obstacles[i])) {
// Missile collided with an obstacle - game over
gameState = 'lost';
break;
}
}
// Check collision with door
if (!door.destroyed && isColliding(missile, door)) {
door.destroyed = true;
createExplosion(missile.x, missile.y);
}
// Update particles
for (var i = particles.length - 1; i >= 0; i--) {
var p = particles[i];
p.x += p.dx;
p.y += p.dy;
p.life--;
p.alpha -= 0.02;
if (p.life <= 0 || p.alpha <= 0) {
particles.splice(i, 1); // Remove particle
}
}
if (door.destroyed && particles.length === 0 && gameState === 'playing') {
gameState = 'won';
}
}
function draw() {
// Clear canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw obstacles
ctx.fillStyle = 'grey';
for (var i = 0; i < obstacles.length; i++) {
var obs = obstacles[i];
ctx.fillRect(obs.x, obs.y, obs.width, obs.height);
}
// Draw door
if (!door.destroyed) {
ctx.fillStyle = 'brown';
ctx.fillRect(door.x, door.y, door.width, door.height);
}
// Draw missile
ctx.fillStyle = 'red';
ctx.fillRect(missile.x - missile.size / 2, missile.y - missile.size / 2, missile.size, missile.size);
// Draw particles
for (var i = 0; i < particles.length; i++) {
var p = particles[i];
ctx.fillStyle = p.color;
ctx.globalAlpha = p.alpha;
ctx.fillRect(p.x, p.y, 3, 3);
}
ctx.globalAlpha = 1.0; // reset alpha
// Draw messages
if (gameState === 'won') {
ctx.fillStyle = 'white';
ctx.font = '48px sans-serif';
ctx.fillText('Mission Accomplished!', canvas.width / 2 - 200, canvas.height / 2);
} else if (gameState === 'lost') {
ctx.fillStyle = 'white';
ctx.font = '48px sans-serif';
ctx.fillText('Mission Failed!', canvas.width / 2 - 150, canvas.height / 2);
}
}
function gameLoop() {
if (gameState === 'playing') {
update();
}
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
document.addEventListener('keydown', function(e) {
if (gameState !== 'playing' && e.code === 'Enter') {
window.location.reload();
}
});
</script>
</body>
</html>
This code creates a simple HTML canvas game where you control a missile using the WASD keys. The missile moves automatically like in the classic snake game. Your goal is to avoid the obstacles and find the door to destroy it. When you collide with the door, the missile explodes using a particle effect. The entire game is contained within a single HTML file, and no external resources are needed.
Instructions:
- Use W to move up, A to move left, S to move down, and D to move right.
- Avoid the grey obstacles.
- Find the brown door and collide with it to destroy it.
- If you win or lose, press Enter to restart the game.