Here's a simple Avatar: The Last Airbender bending arcade game implemented in HTML5 and JavaScript. The player can move around the screen using arrow keys and shoot projectiles by pressing the spacebar. The bending element can be changed by pressing keys 1-4 (air, water, earth, fire). Enemies spawn randomly and move towards the player. The player can defeat enemies by shooting projectiles at them, which increases the score.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Avatar Bending Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set canvas to full window size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Game state
const gameState = {
player: {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 20,
color: '#00F',
bendingElement: 'air' // Default bending element
},
elements: [],
projectiles: [],
enemies: [],
score: 0
};
// Handle keyboard input
const keys = {
ArrowUp: false,
ArrowDown: false,
ArrowLeft: false,
ArrowRight: false,
Space: false
};
window.addEventListener('keydown', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = true;
}
});
window.addEventListener('keyup', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = false;
}
});
// Change bending element
function changeBendingElement(element) {
gameState.player.bendingElement = element;
// Update player color based on bending element
switch (element) {
case 'air':
gameState.player.color = '#0FF';
break;
case 'water':
gameState.player.color = '#00F';
break;
case 'earth':
gameState.player.color = '#8B4513';
break;
case 'fire':
gameState.player.color = '#F00';
break;
default:
gameState.player.color = '#FFF';
}
}
// Game loop
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
function update() {
// Update player position based on key presses
if (keys.ArrowUp) gameState.player.y -= 5;
if (keys.ArrowDown) gameState.player.y += 5;
if (keys.ArrowLeft) gameState.player.x -= 5;
if (keys.ArrowRight) gameState.player.x += 5;
// Keep player within canvas bounds
gameState.player.x = Math.max(gameState.player.radius, Math.min(canvas.width - gameState.player.radius, gameState.player.x));
gameState.player.y = Math.max(gameState.player.radius, Math.min(canvas.height - gameState.player.radius, gameState.player.y));
// Update projectiles
for (let i = gameState.projectiles.length - 1; i >= 0; i--) {
const projectile = gameState.projectiles[i];
projectile.x += projectile.dx;
projectile.y += projectile.dy;
// Remove projectiles that are out of bounds
if (projectile.x < 0 || projectile.x > canvas.width || projectile.y < 0 || projectile.y > canvas.height) {
gameState.projectiles.splice(i, 1);
}
}
// Update enemies (for now, just spawn some and move them towards the player)
if (Math.random() < 0.02) {
const side = Math.floor(Math.random() * 4); // 0: top, 1: right, 2: bottom, 3: left
let x, y;
switch (side) {
case 0: // top
x = Math.random() * canvas.width;
y = 0;
break;
case 1: // right
x = canvas.width;
y = Math.random() * canvas.height;
break;
case 2: // bottom
x = Math.random() * canvas.width;
y = canvas.height;
break;
case 3: // left
x = 0;
y = Math.random() * canvas.height;
break;
}
gameState.enemies.push({
x: x,
y: y,
radius: 15,
color: '#F00',
speed: 2
});
}
// Move enemies towards the player
for (let i = gameState.enemies.length - 1; i >= 0; i--) {
const enemy = gameState.enemies[i];
const dx = gameState.player.x - enemy.x;
const dy = gameState.player.y - enemy.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
enemy.x += (dx / distance) * enemy.speed;
enemy.y += (dy / distance) * enemy.speed;
}
// Check for collision with player
const playerDistance = Math.sqrt((gameState.player.x - enemy.x) ** 2 + (gameState.player.y - enemy.y) ** 2);
if (playerDistance < gameState.player.radius + enemy.radius) {
// Game over or lose health logic can be added here
gameState.enemies.splice(i, 1);
}
}
// Check for collisions between projectiles and enemies
for (let i = gameState.projectiles.length - 1; i >= 0; i--) {
const projectile = gameState.projectiles[i];
for (let j = gameState.enemies.length - 1; j >= 0; j--) {
const enemy = gameState.enemies[j];
const distance = Math.sqrt((projectile.x - enemy.x) ** 2 + (projectile.y - enemy.y) ** 2);
if (distance < projectile.radius + enemy.radius) {
gameState.enemies.splice(j, 1);
gameState.projectiles.splice(i, 1);
gameState.score += 10;
break;
}
}
}
}
function render() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw player
ctx.beginPath();
ctx.arc(gameState.player.x, gameState.player.y, gameState.player.radius, 0, Math.PI * 2);
ctx.fillStyle = gameState.player.color;
ctx.fill();
ctx.closePath();
// Draw projectiles
gameState.projectiles.forEach(projectile => {
ctx.beginPath();
ctx.arc(projectile.x, projectile.y, projectile.radius, 0, Math.PI * 2);
ctx.fillStyle = projectile.color;
ctx.fill();
ctx.closePath();
});
// Draw enemies
gameState.enemies.forEach(enemy => {
ctx.beginPath();
ctx.arc(enemy.x, enemy.y, enemy.radius, 0, Math.PI * 2);
ctx.fillStyle = enemy.color;
ctx.fill();
ctx.closePath();
});
// Draw UI
ctx.fillStyle = '#FFF';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${gameState.score}`, 20, 30);
ctx.fillText(`Element: ${gameState.player.bendingElement}`, 20, 60);
ctx.fillText('Press 1-4 to change element', 20, 90);
ctx.fillText('Arrow keys to move, Space to shoot', 20, 120);
}
// Handle shooting projectiles and changing elements
window.addEventListener('keydown', (e) => {
if (e.key === ' ') {
// Create a projectile based on the current bending element
const projectile = {
x: gameState.player.x,
y: gameState.player.y,
radius: 10,
dx: 0,
dy: 0,
color: ''
};
// Determine direction based on last key pressed (simplified)
if (keys.ArrowUp) {
projectile.dy = -10;
} else if (keys.ArrowDown) {
projectile.dy = 10;
} else if (keys.ArrowLeft) {
projectile.dx = -10;
} else if (keys.ArrowRight) {
projectile.dx = 10;
} else {
// Default direction if no movement keys are pressed
projectile.dx = 10;
}
// Set color and behavior based on bending element
switch (gameState.player.bendingElement) {
case 'air':
projectile.color = '#0FF';
// Air projectiles move faster
projectile.dx *= 1.5;
projectile.dy *= 1.5;
break;
case 'water':
projectile.color = '#00F';
// Water projectiles are larger
projectile.radius = 15;
break;
case 'earth':
projectile.color = '#8B4513';
// Earth projectiles are slower but larger
projectile.radius = 20;
projectile.dx *= 0.7;
projectile.dy *= 0.7;
break;
case 'fire':
projectile.color = '#F00';
// Fire projectiles have a trail effect (not implemented yet)
break;
default:
projectile.color = '#FFF';
}
gameState.projectiles.push(projectile);
}
// Change bending element
if (e.key === '1') changeBendingElement('air');
if (e.key === '2') changeBendingElement('water');
if (e.key === '3') changeBendingElement('earth');
if (e.key === '4') changeBendingElement('fire');
});
// Start the game loop
gameLoop();
</script>
</body>
</html>
This code creates a simple arcade game where the player can control an Avatar character who can bend different elements (air, water, earth, fire). The player can move around the screen using arrow keys and shoot projectiles by pressing the spacebar. The bending element can be changed by pressing keys 1-4. Enemies spawn randomly and move towards the player. The player can defeat enemies by shooting projectiles at them, which increases the score.