A classic request! Battle City, also known as Tank Force, is a iconic game from the 80s. I'll guide you through creating a simplified version of the game using HTML, CSS, and JavaScript. We'll use the HTML5 Canvas for rendering the game. Buckle up!
javascript Copy code
// Get the canvas element
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions
canvas.width = 416;
canvas.height = 416;
// Game constants
const TANK_SIZE = 16;
const BULLET_SIZE = 4;
const WALL_SIZE = 16;
const ENEMY_SPAWN_RATE = 2000; // milliseconds
const PLAYER_SPEED = 2;
const BULLET_SPEED = 4;
// Colors
const TANK_COLOR = '#00ff00';
const BULLET_COLOR = '#ffffff';
const WALL_COLOR = '#666666';
const ENEMY_COLOR = '#ff0000';
// Player tank object
const player = {
x: canvas.width / 2,
y: canvas.height - TANK_SIZE * 2,
speed: PLAYER_SPEED,
dir: 'up',
shooting: false,
bullets: []
};
// Enemy tanks array
const enemies = [];
// Walls array ( predefined obstacles )
const walls = [
{ x: 0, y: 0, w: canvas.width, h: WALL_SIZE }, // top wall
{ x: 0, y: canvas.height - WALL_SIZE, w: canvas.width, h: WALL_SIZE }, // bottom wall
{ x: 0, y: 0, w: WALL_SIZE, h: canvas.height }, // left wall
{ x: canvas.width - WALL_SIZE, y: 0, w: WALL_SIZE, h: canvas.height }, // right wall
{ x: 100, y: 100, w: WALL_SIZE * 3, h: WALL_SIZE }, // middle obstacle
{ x: 250, y: 250, w: WALL_SIZE, h: WALL_SIZE * 3 } // another obstacle
];
// Score and lives
let score = 0;
let lives = 3;
// Draw everything
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw walls
walls.forEach(wall => {
ctx.fillStyle = WALL_COLOR;
ctx.fillRect(wall.x, wall.y, wall.w, wall.h);
});
// Draw player tank
ctx.fillStyle = TANK_COLOR;
ctx.fillRect(player.x, player.y, TANK_SIZE, TANK_SIZE);
// Draw player bullets
player.bullets.forEach(bullet => {
ctx.fillStyle = BULLET_COLOR;
ctx.fillRect(bullet.x, bullet.y, BULLET_SIZE, BULLET_SIZE);
});
// Draw enemy tanks
enemies.forEach(enemy => {
ctx.fillStyle = ENEMY_COLOR;
ctx.fillRect(enemy.x, enemy.y, TANK_SIZE, TANK_SIZE);
// Draw enemy bullets
enemy.bullets.forEach(bullet => {
ctx.fillStyle = BULLET_COLOR;
ctx.fillRect(bullet.x, bullet.y, BULLET_SIZE, BULLET_SIZE);
});
});
// Draw score and lives
ctx.font = '16px Arial';
ctx.fillStyle = '#ffffff';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.fillText(`Score: ${score}`, 10, 10);
ctx.fillText(`Lives: ${lives}`, 10, 30);
}
// Update game state
function update() {
// Move player tank
if (player.dir === 'up') {
player.y -= player.speed;
} else if (player.dir === 'down') {
player.y += player.speed;
} else if (player.dir === 'left') {
player.x -= player.speed;
} else if (player.dir === 'right') {
player.x += player.speed;
}
// Collision detection with walls
walls.forEach(wall => {
if (checkCollision(player, wall)) {
// Move player back
if (player.dir === 'up') {
player.y += player.speed;
} else if (player.dir === 'down') {
player.y -= player.speed;
} else if (player.dir === 'left') {
player.x += player.speed;
} else if (player.dir === 'right') {
player.x -= player.speed;
}
}
});
// Update player bullets
player.bullets.forEach((bullet, index) => {
bullet.y -= BULLET_SPEED;
if (bullet.y < 0) {
player.bullets.splice(index, 1);
}
// Collision with walls
walls.forEach(wall => {
if (checkCollision(bullet, wall)) {
player.bullets.splice(index, 1);
}
});
// Collision with enemies
enemies.forEach((enemy, enemyIndex) => {
if (checkCollision(bullet, enemy)) {
player.bullets.splice(index, 1);
enemies.splice(enemyIndex, 1);
score++;
}
});
});
// Update enemies
enemies.forEach((enemy, index) => {
enemy.y += 1; // move down
if (enemy.y > canvas.height) {
enemies.splice(index, 1);
lives--;
}
// Shoot occasionally
if (Math.random() < 0.01) {
enemy.bullets.push({ x: enemy.x + TANK_SIZE / 2, y: enemy.y + TANK_SIZE });
}
// Update enemy bullets
enemy.bullets.forEach((bullet, bulletIndex) => {
bullet.y += BULLET_SPEED;
if (bullet.y > canvas.height) {
enemy.bullets.splice(bulletIndex, 1);
}
// Collision with player
if (checkCollision(bullet, player)) {
enemy.bullets.splice(bulletIndex, 1);
lives--;
}
});
});
// Spawn new enemies
if (Math.random() < 0.05) {
enemies.push({
x: Math.random() * (canvas.width - TANK_SIZE),
y: 0,
bullets: []
});
}
}
// Check collision between two objects
function checkCollision(obj1, obj2) {
return (
obj1.x + (obj1.w || TANK_SIZE) > obj2.x &&
obj1.x < obj2.x + (obj2.w || TANK_SIZE) &&
obj1.y + (obj1.h || TANK_SIZE) > obj2.y &&
obj1.y < obj2.y + (obj2.h || TANK_SIZE)
);
}
// Handle key presses
document.addEventListener('keydown', e => {
switch (e.key) {
case 'ArrowUp':
player.dir = 'up';
break;
case 'ArrowDown':
player.dir = 'down';
break;
case 'ArrowLeft':
player.dir = 'left';
break;
case 'ArrowRight':
player.dir = 'right';
break;
case ' ':
if (!player.shooting) {
player.bullets.push({ x: player.x + TANK_SIZE / 2, y: player.y });
player.shooting = true;
setTimeout(() => {
player.shooting = false;
}, 500); // cooldown
}
break;
}
});
// Main game loop
setInterval(() => {
update();
draw();
// Game over condition
if (lives <= 0) {
alert(`Game Over! Your score: ${score}`);
// Reset game state
player.x = canvas.width / 2;
player.y = canvas.height - TANK_SIZE * 2;
player.bullets = [];
enemies = [];
score = 0;
lives = 3;
}
}, 1000 / 60); // 60 FPS
That's it! You've now got a basic Battle City game running in your browser. Feel free to experiment, modify, and enhance the code to make it your own. Happy coding!