I'll create a Battle City game for you that's playable in HTML. This will be a simplified version of the classic tank game with basic mechanics.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Battle City</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: Arial, sans-serif;
}
#gameContainer {
text-align: center;
}
canvas {
border: 2px solid #666;
background-color: #000;
}
#info {
color: white;
margin-top: 10px;
font-size: 18px;
}
#controls {
color: white;
margin-top: 10px;
font-size: 14px;
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas" width="624" height="624"></canvas>
<div id="info">
<span>Score: <span id="score">0</span></span> |
<span>Lives: <span id="lives">3</span></span> |
<span>Level: <span id="level">1</span></span>
</div>
<div id="controls">
Use ARROW KEYS to move, SPACE to shoot
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const TILE_SIZE = 24;
const GRID_WIDTH = 26;
const GRID_HEIGHT = 26;
let score = 0;
let lives = 3;
let level = 1;
let gameRunning = true;
// Game objects
let player = {
x: 9 * TILE_SIZE,
y: 24 * TILE_SIZE,
width: TILE_SIZE,
height: TILE_SIZE,
direction: 'up',
speed: 2,
canShoot: true,
shootCooldown: 0
};
let bullets = [];
let enemyBullets = [];
let enemies = [];
let walls = [];
let base = {
x: 12 * TILE_SIZE,
y: 24 * TILE_SIZE,
width: TILE_SIZE * 2,
height: TILE_SIZE,
destroyed: false
};
// Input handling
const keys = {};
document.addEventListener('keydown', (e) => {
keys[e.key] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.key] = false;
});
// Initialize level
function initLevel() {
// Clear existing enemies and bullets
enemies = [];
bullets = [];
enemyBullets = [];
// Create walls (simplified layout)
walls = [];
// Border walls
for (let i = 0; i < GRID_WIDTH; i++) {
walls.push({ x: i * TILE_SIZE, y: 0, width: TILE_SIZE, height: TILE_SIZE });
walls.push({ x: i * TILE_SIZE, y: (GRID_HEIGHT - 1) * TILE_SIZE, width: TILE_SIZE, height: TILE_SIZE });
}
for (let i = 1; i < GRID_HEIGHT - 1; i++) {
walls.push({ x: 0, y: i * TILE_SIZE, width: TILE_SIZE, height: TILE_SIZE });
walls.push({ x: (GRID_WIDTH - 1) * TILE_SIZE, y: i * TILE_SIZE, width: TILE_SIZE, height: TILE_SIZE });
}
// Add some internal walls
for (let i = 0; i < 10; i++) {
let x = Math.floor(Math.random() * (GRID_WIDTH - 4) + 2) * TILE_SIZE;
let y = Math.floor(Math.random() * (GRID_HEIGHT - 10) + 2) * TILE_SIZE;
walls.push({ x: x, y: y, width: TILE_SIZE * 2, height: TILE_SIZE });
}
// Create enemies
let enemyCount = 3 + level * 2;
for (let i = 0; i < enemyCount; i++) {
enemies.push({
x: (2 + i * 4) * TILE_SIZE,
y: TILE_SIZE,
width: TILE_SIZE,
height: TILE_SIZE,
direction: 'down',
speed: 1,
moveTimer: 0,
shootTimer: 0
});
}
}
// Collision detection
function checkCollision(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;
}
function canMove(obj, dx, dy) {
let newX = obj.x + dx;
let newY = obj.y + dy;
let testRect = { x: newX, y: newY, width: obj.width, height: obj.height };
// Check canvas boundaries
if (newX < 0 || newX + obj.width > canvas.width ||
newY < 0 || newY + obj.height > canvas.height) {
return false;
}
// Check wall collisions
for (let wall of walls) {
if (checkCollision(testRect, wall)) {
return false;
}
}
return true;
}
// Update game state
function update() {
if (!gameRunning) return;
// Update player
if (keys['ArrowUp'] && canMove(player, 0, -player.speed)) {
player.y -= player.speed;
player.direction = 'up';
}
if (keys['ArrowDown'] && canMove(player, 0, player.speed)) {
player.y += player.speed;
player.direction = 'down';
}
if (keys['ArrowLeft'] && canMove(player, -player.speed, 0)) {
player.x -= player.speed;
player.direction = 'left';
}
if (keys['ArrowRight'] && canMove(player, player.speed, 0)) {
player.x += player.speed;
player.direction = 'right';
}
// Player shooting
if (keys[' '] && player.shootCooldown <= 0) {
let bulletX = player.x + player.width / 2 - 2;
let bulletY = player.y + player.height / 2 - 2;
bullets.push({
x: bulletX,
y: bulletY,
width: 4,
height: 4,
direction: player.direction,
speed: 5
});
player.shootCooldown = 20;
}
if (player.shootCooldown > 0) player.shootCooldown--;
// Update bullets
for (let i = bullets.length - 1; i >= 0; i--) {
let bullet = bullets[i];
switch (bullet.direction) {
case 'up': bullet.y -= bullet.speed; break;
case 'down': bullet.y += bullet.speed; break;
case 'left': bullet.x -= bullet.speed; break;
case 'right': bullet.x += bullet.speed; break;
}
// Remove bullets that hit walls or go off screen
let hit = false;
if (bullet.x < 0 || bullet.x > canvas.width ||
bullet.y < 0 || bullet.y > canvas.height) {
hit = true;
}
for (let wall of walls) {
if (checkCollision(bullet, wall)) {
hit = true;
break;
}
}
// Check enemy hits
for (let j = enemies.length - 1; j >= 0; j--) {
if (checkCollision(bullet, enemies[j])) {
enemies.splice(j, 1);
score += 100;
hit = true;
break;
}
}
if (hit) {
bullets.splice(i, 1);
}
}
// Update enemies
for (let enemy of enemies) {
enemy.moveTimer++;
enemy.shootTimer++;
// Enemy movement
if (enemy.moveTimer > 30) {
let directions = ['up', 'down', 'left', 'right'];
let moved = false;
// Try current direction first
let dx = 0, dy = 0;
switch (enemy.direction) {
case 'up': dy = -enemy.speed; break;
case 'down': dy = enemy.speed; break;
case 'left': dx = -enemy.speed; break;
case 'right': dx = enemy.speed; break;
}
if (canMove(enemy, dx, dy)) {
enemy.x += dx;
enemy.y += dy;
moved = true;
}
// Change direction randomly or if can't move
if (!moved || Math.random() < 0.1) {
enemy.direction = directions[Math.floor(Math.random() * directions.length)];
}
enemy.moveTimer = 0;
}
// Enemy shooting
if (enemy.shootTimer > 100 && Math.random() < 0.3) {
let bulletX = enemy.x + enemy.width / 2 - 2;
let bulletY = enemy.y + enemy.height / 2 - 2;
enemyBullets.push({
x: bulletX,
y: bulletY,
width: 4,
height: 4,
direction: enemy.direction,
speed: 3
});
enemy.shootTimer = 0;
}
}
// Update enemy bullets
for (let i = enemyBullets.length - 1; i >= 0; i--) {
let bullet = enemyBullets[i];
switch (bullet.direction) {
case 'up': bullet.y -= bullet.speed; break;
case 'down': bullet.y += bullet.speed; break;
case 'left': bullet.x -= bullet.speed; break;
case 'right': bullet.x += bullet.speed; break;
}
let hit = false;
if (bullet.x < 0 || bullet.x > canvas.width ||
bullet.y < 0 || bullet.y > canvas.height) {
hit = true;
}
for (let wall of walls) {
if (checkCollision(bullet, wall)) {
hit = true;
break;
}
}
// Check player hit
if (checkCollision(bullet, player)) {
lives--;
player.x = 9 * TILE_SIZE;
player.y = 24 * TILE_SIZE;
hit = true;
if (lives <= 0) {
gameRunning = false;
alert('Game Over! Score: ' + score);
}
}
// Check base hit
if (!base.destroyed && checkCollision(bullet, base)) {
base.destroyed = true;
gameRunning = false;
alert('Base Destroyed! Game Over! Score: ' + score);
hit = true;
}
if (hit) {
enemyBullets.splice(i, 1);
}
}
// Check level completion
if (enemies.length === 0) {
level++;
initLevel();
}
// Update UI
document.getElementById('score').textContent = score;
document.getElementById('lives').textContent = lives;
document.getElementById('level').textContent = level;
}
// Render game
function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw walls
ctx.fillStyle = '#8B4513';
for (let wall of walls) {
ctx.fillRect(wall.x, wall.y, wall.width, wall.height);
}
// Draw base
if (!base.destroyed) {
ctx.fillStyle = '#FFD700';
ctx.fillRect(base.x, base.y, base.width, base.height);
}
// Draw player
ctx.fillStyle = '#00FF00';
ctx.fillRect(player.x, player.y, player.width, player.height);
// Draw player direction indicator
ctx.fillStyle = '#00AA00';
switch (player.direction) {
case 'up':
ctx.fillRect(player.x + 10, player.y, 4, 8);
break;
case 'down':
ctx.fillRect(player.x + 10, player.y + 16, 4, 8);
break;
case 'left':
ctx.fillRect(player.x, player.y + 10, 8, 4);
break;
case 'right':
ctx.fillRect(player.x + 16, player.y + 10, 8, 4);
break;
}
// Draw enemies
ctx.fillStyle = '#FF0000';
for (let enemy of enemies) {
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
}
// Draw bullets
ctx.fillStyle = '#FFFF00';
for (let bullet of bullets) {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
ctx.fillStyle = '#FF00FF';
for (let bullet of enemyBullets) {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
}
// Game loop
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
// Start game
initLevel();
gameLoop();
</script>
</body>
</html>
This Battle City game includes:
Player tank (green) that you control with arrow keys
Enemy tanks (red) that move and shoot automatically
Base (gold) at the bottom that you need to protect
Walls (brown) that block movement and bullets
Shooting with the spacebar
Score system - 100 points per enemy destroyed
Lives system - you start with 3 lives
Level progression - more enemies appear as you advance
The game features basic tank combat mechanics where you need to destroy all enemy tanks while protecting your base. If your base is destroyed or you lose all