<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Shooter Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
}
#gameCanvas {
display: block;
background-color: #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Get the canvas element and set its dimensions
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Player object
const player = {
x: canvas.width / 2 - 15,
y: canvas.height - 60,
width: 30,
height: 30,
speed: 7,
color: 'lime',
dx: 0
};
// Bullet array
const bullets = [];
// Enemy array
const enemies = [];
let enemyTimer = 0;
const enemyInterval = 100; // Adjust this value to change enemy spawn rate
// Listen for keyboard events
let keys = {};
document.addEventListener('keydown', function(e) {
keys[e.code] = true;
});
document.addEventListener('keyup', function(e) {
keys[e.code] = false;
});
// Game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Update game objects
function update() {
// Move player
if (keys['ArrowLeft']) {
player.dx = -player.speed;
} else if (keys['ArrowRight']) {
player.dx = player.speed;
} else {
player.dx = 0;
}
player.x += player.dx;
// Prevent player from going off-screen
if (player.x < 0) player.x = 0;
if (player.x + player.width > canvas.width) player.x = canvas.width - player.width;
// Shoot bullet
if (keys['Space']) {
shootBullet();
}
// Update bullets
for (let i = bullets.length - 1; i >= 0; i--) {
bullets[i].y -= bullets[i].dy;
// Remove bullets that go off-screen
if (bullets[i].y < 0) {
bullets.splice(i, 1);
}
}
// Spawn enemies
enemyTimer++;
if (enemyTimer > enemyInterval) {
spawnEnemy();
enemyTimer = 0;
}
// Update enemies
for (let i = enemies.length - 1; i >= 0; i--) {
enemies[i].y += enemies[i].dy;
// Remove enemies that go off-screen
if (enemies[i].y > canvas.height) {
enemies.splice(i, 1);
// You can add code here to reduce player's life or end the game
}
}
// Check for collisions between bullets and enemies
for (let i = enemies.length - 1; i >= 0; i--) {
for (let j = bullets.length - 1; j >= 0; j--) {
if (isColliding(bullets[j], enemies[i])) {
enemies.splice(i, 1);
bullets.splice(j, 1);
break;
}
}
}
}
// Draw game objects
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw player
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
// Draw bullets
ctx.fillStyle = 'red';
bullets.forEach(function(bullet) {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
});
// Draw enemies
ctx.fillStyle = 'white';
enemies.forEach(function(enemy) {
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
});
}
// Shoot a bullet
let canShoot = true;
function shootBullet() {
if (canShoot) {
const bullet = {
x: player.x + player.width / 2 - 2.5,
y: player.y,
width: 5,
height: 10,
dy: 10
};
bullets.push(bullet);
canShoot = false;
// Prevent immediate subsequent shooting
setTimeout(() => { canShoot = true; }, 300);
}
}
// Spawn an enemy
function spawnEnemy() {
const enemySize = Math.random() * 30 + 20;
const enemy = {
x: Math.random() * (canvas.width - enemySize),
y: -enemySize,
width: enemySize,
height: enemySize,
dy: Math.random() * 2 + 1
};
enemies.push(enemy);
}
// Check for collision between two rectangles
function isColliding(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
);
}
// Start the game
gameLoop();
</script>
</body>
</html>