Creating a simple shooting game for a webpage involves using HTML, CSS, and JavaScript. Below is a basic example of a shooting game where you control a player that can shoot projectiles at incoming enemies. This example will run on any modern web browser, including those on MacOS.
Step 1: Set Up the HTML Structure
Create an index.html file with the following content:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Shooting Game</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
background: #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script src="game.js"></script>
</body>
</html>
Step 2: Create the Game Logic with JavaScript
Create a game.js file with the following content:
javascript Copy code
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const player = {
x: canvas.width / 2,
y: canvas.height - 30,
width: 20,
height: 20,
color: 'white',
dx: 5
};
const bullets = [];
const enemies = [];
const bulletSpeed = 7;
const enemySpeed = 2;
let gameOver = false;
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawBullets() {
for (let i = 0; i < bullets.length; i++) {
ctx.fillStyle = 'yellow';
ctx.fillRect(bullets[i].x, bullets[i].y, 5, 10);
bullets[i].y -= bulletSpeed;
if (bullets[i].y < 0) {
bullets.splice(i, 1);
i--;
}
}
}
function drawEnemies() {
for (let i = 0; i < enemies.length; i++) {
ctx.fillStyle = 'red';
ctx.fillRect(enemies[i].x, enemies[i].y, 20, 20);
enemies[i].y += enemySpeed;
if (enemies[i].y > canvas.height) {
enemies.splice(i, 1);
i--;
}
}
}
function checkCollisions() {
for (let i = 0; i < bullets.length; i++) {
for (let j = 0; j < enemies.length; j++) {
if (
bullets[i].x < enemies[j].x + 20 &&
bullets[i].x + 5 > enemies[j].x &&
bullets[i].y < enemies[j].y + 20 &&
bullets[i].y + 10 > enemies[j].y
) {
bullets.splice(i, 1);
enemies.splice(j, 1);
i--;
break;
}
}
}
for (let i = 0; i < enemies.length; i++) {
if (
player.x < enemies[i].x + 20 &&
player.x + player.width > enemies[i].x &&
player.y < enemies[i].y + 20 &&
player.y + player.height > enemies[i].y
) {
gameOver = true;
}
}
}
function gameLoop() {
if (gameOver) {
ctx.fillStyle = 'white';
ctx.font = '48px serif';
ctx.fillText('Game Over', canvas.width / 6, canvas.height / 2);
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawBullets();
drawEnemies();
checkCollisions();
requestAnimationFrame(gameLoop);
}
function shootBullet() {
bullets.push({
x: player.x + player.width / 2 - 2.5,
y: player.y
});
}
function movePlayer(e) {
player.x = e.clientX - player.width / 2;
if (player.x < 0) {
player.x = 0;
} else if (player.x > canvas.width - player.width) {
player.x = canvas.width - player.width;
}
}
function createEnemy() {
enemies.push({
x: Math.random() * (canvas.width - 20),
y: 0
});
}
setInterval(createEnemy, 1000);
document.addEventListener('mousemove', movePlayer);
document.addEventListener('click', shootBullet);
gameLoop();
Step 3: Run the Game
Save both index.html and game.js in the same directory.
Open the Terminal on your MacOS.
Navigate to the directory where you saved the files using the cd command.
Start a simple HTTP server by running:
sh Copy code
python3 -m http.server
Open your web browser and go to http://localhost:8000.
You should now see your simple shooting game running. Use your mouse to move the player and click to shoot bullets at the incoming enemies.