html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shooting Game</title>
<style>
body {
background: #222;
margin: 0;
overflow: hidden;
}
canvas {
display: block;
margin: 0 auto;
background: #111;
border: 2px solid #fff;
}
#score {
color: #fff;
font-family: Arial, sans-serif;
font-size: 24px;
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="gameCanvas" width="600" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDiv = document.getElementById('score');
// Player
const player = {
x: canvas.width / 2,
y: canvas.height - 40,
width: 40,
height: 20,
color: '#0f0',
speed: 5
};
// Bullets
const bullets = [];
// Enemies
const enemies = [];
let enemyTimer = 0;
let score = 0;
// Controls
let leftPressed = false;
let rightPressed = false;
let spacePressed = false;
document.addEventListener('keydown', (e) => {
if (e.code === 'ArrowLeft') leftPressed = true;
if (e.code === 'ArrowRight') rightPressed = true;
if (e.code === 'Space') spacePressed = true;
});
document.addEventListener('keyup', (e) => {
if (e.code === 'ArrowLeft') leftPressed = false;
if (e.code === 'ArrowRight') rightPressed = false;
if (e.code === 'Space') spacePressed = false;
});
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawBullets() {
ctx.fillStyle = '#ff0';
bullets.forEach(bullet => {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
});
}
function drawEnemies() {
ctx.fillStyle = '#f00';
enemies.forEach(enemy => {
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
});
}
function updatePlayer() {
if (leftPressed && player.x > 0) player.x -= player.speed;
if (rightPressed && 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() {
for (let i = enemies.length - 1; i >= 0; i--) {
enemies[i].y += enemies[i].speed;
if (enemies[i].y > canvas.height) enemies.splice(i, 1);
}
}
function shootBullet() {
if (spacePressed && bullets.length < 5) { // Limit bullets on screen
bullets.push({
x: player.x + player.width / 2 - 2,
y: player.y,
width: 4,
height: 10,
speed: 7
});
}
}
function spawnEnemy() {
if (enemyTimer <= 0) {
const enemyWidth = 40;
enemies.push({
x: Math.random() * (canvas.width - enemyWidth),
y: -20,
width: enemyWidth,
height: 20,
speed: 2 + Math.random() * 2
});
enemyTimer = 60 + Math.random() * 60; // Spawn every 1-2 seconds
} else {
enemyTimer--;
}
}
function checkCollisions() {
for (let i = enemies.length - 1; i >= 0; i--) {
for (let j = bullets.length - 1; j >= 0; j--) {
const e = enemies[i];
const b = bullets[j];
if (
b.x < e.x + e.width &&
b.x + b.width > e.x &&
b.y < e.y + e.height &&
b.y + b.height > e.y
) {
enemies.splice(i, 1);
bullets.splice(j, 1);
score++;
scoreDiv.textContent = 'Score: ' + score;
break;
}
}
}
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
updatePlayer();
updateBullets();
updateEnemies();
shootBullet();
spawnEnemy();
checkCollisions();
drawPlayer();
drawBullets();
drawEnemies();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>
This is a basic shooting game. You can expand it with more features, sounds, or graphics as you wish! If you want to package it as a standalone Mac app, you can use tools like Electron , but for most users, running it in the browser is easiest.