I'll create a complete HTML fighting game for you. This will be a 2D side-scrolling fighter where you control a character fighting waves of enemies.
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>Street Fighter Clone</title>
<style>
body {
margin: 0;
padding: 0;
background: linear-gradient(180deg, #87CEEB 0%, #98FB98 100%);
font-family: Arial, sans-serif;
overflow: hidden;
}
#gameContainer {
width: 100vw;
height: 100vh;
position: relative;
overflow: hidden;
}
#gameArea {
width: 100%;
height: 80%;
position: relative;
border-bottom: 5px solid #8B4513;
}
.character {
position: absolute;
width: 60px;
height: 80px;
transition: all 0.1s ease;
}
.player {
background: linear-gradient(45deg, #4169E1, #1E90FF);
border: 2px solid #000080;
border-radius: 10px;
z-index: 10;
}
.enemy {
background: linear-gradient(45deg, #DC143C, #FF6347);
border: 2px solid #8B0000;
border-radius: 10px;
z-index: 5;
}
.attack-effect {
position: absolute;
width: 30px;
height: 30px;
background: radial-gradient(circle, #FFD700, #FFA500);
border-radius: 50%;
animation: attackBurst 0.3s ease-out forwards;
}
@keyframes attackBurst {
0% { transform: scale(0); opacity: 1; }
100% { transform: scale(2); opacity: 0; }
}
.damage-text {
position: absolute;
color: #FF0000;
font-weight: bold;
font-size: 20px;
animation: damageFloat 1s ease-out forwards;
pointer-events: none;
}
@keyframes damageFloat {
0% { transform: translateY(0); opacity: 1; }
100% { transform: translateY(-50px); opacity: 0; }
}
#ui {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-size: 18px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
#controls {
position: absolute;
bottom: 10px;
left: 10px;
color: white;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
.health-bar {
width: 200px;
height: 20px;
background: #333;
border: 2px solid #000;
margin: 5px 0;
overflow: hidden;
}
.health-fill {
height: 100%;
background: linear-gradient(90deg, #FF0000, #FFFF00, #00FF00);
transition: width 0.3s ease;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0,0,0,0.8);
color: white;
padding: 30px;
border-radius: 10px;
text-align: center;
display: none;
}
button {
padding: 10px 20px;
font-size: 16px;
background: #4169E1;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 10px;
}
button:hover {
background: #1E90FF;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="ui">
<div>Health: <div class="health-bar"><div id="playerHealth" class="health-fill" style="width: 100%"></div></div></div>
<div>Score: <span id="score">0</span></div>
<div>Wave: <span id="wave">1</span></div>
<div>Enemies Left: <span id="enemiesLeft">3</span></div>
</div>
<div id="gameArea">
<div id="player" class="character player"></div>
</div>
<div id="controls">
<strong>Controls:</strong><br>
WASD - Move | SPACE - Attack | R - Restart
</div>
<div id="gameOver">
<h2 id="gameOverText">Game Over!</h2>
<p id="finalScore">Final Score: 0</p>
<button onclick="restartGame()">Play Again</button>
</div>
</div>
<script>
class Game {
constructor() {
this.player = {
x: 100,
y: 400,
width: 60,
height: 80,
health: 100,
maxHealth: 100,
speed: 5,
attackCooldown: 0,
invulnerable: 0
};
this.enemies = [];
this.gameArea = document.getElementById('gameArea');
this.playerElement = document.getElementById('player');
this.score = 0;
this.wave = 1;
this.enemiesPerWave = 3;
this.gameRunning = true;
this.keys = {};
this.init();
}
init() {
this.updatePlayerPosition();
this.spawnWave();
this.bindEvents();
this.gameLoop();
}
bindEvents() {
document.addEventListener('keydown', (e) => {
this.keys[e.key.toLowerCase()] = true;
});
document.addEventListener('keyup', (e) => {
this.keys[e.key.toLowerCase()] = false;
});
}
spawnWave() {
this.enemies = [];
for (let i = 0; i < this.enemiesPerWave; i++) {
setTimeout(() => {
this.spawnEnemy();
}, i * 1000);
}
document.getElementById('enemiesLeft').textContent = this.enemiesPerWave;
}
spawnEnemy() {
const enemy = {
x: Math.random() * (window.innerWidth - 60),
y: Math.random() * (window.innerHeight * 0.6) + 200,
width: 60,
height: 80,
health: 30 + (this.wave * 10),
maxHealth: 30 + (this.wave * 10),
speed: 1 + (this.wave * 0.5),
attackCooldown: 0,
element: document.createElement('div')
};
enemy.element.className = 'character enemy';
enemy.element.style.left = enemy.x + 'px';
enemy.element.style.top = enemy.y + 'px';
this.gameArea.appendChild(enemy.element);
this.enemies.push(enemy);
}
updatePlayerPosition() {
this.playerElement.style.left = this.player.x + 'px';
this.playerElement.style.top = this.player.y + 'px';
}
handleInput() {
if (!this.gameRunning) return;
const prevX = this.player.x;
const prevY = this.player.y;
if (this.keys['a'] && this.player.x > 0) {
this.player.x -= this.player.speed;
}
if (this.keys['d'] && this.player.x < window.innerWidth - this.player.width) {
this.player.x += this.player.speed;
}
if (this.keys['w'] && this.player.y > 0) {
this.player.y -= this.player.speed;
}
if (this.keys['s'] && this.player.y < window.innerHeight * 0.8 - this.player.height) {
this.player.y += this.player.speed;
}
if (this.keys[' '] && this.player.attackCooldown <= 0) {
this.playerAttack();
this.player.attackCooldown = 30;
}
if (this.keys['r']) {
this.restartGame();
}
if (prevX !== this.player.x || prevY !== this.player.y) {
this.updatePlayerPosition();
}
}
playerAttack() {
const attackRange = 100;
const attackDamage = 25;
this.createAttackEffect(this.player.x + this.player.width / 2, this.player.y + this.player.height / 2);
this.enemies.forEach((enemy, index) => {
const distance = Math.sqrt(
Math.pow(enemy.x - this.player.x, 2) +
Math.pow(enemy.y - this.player.y, 2)
);
if (distance < attackRange) {
enemy.health -= attackDamage;
this.createDamageText(enemy.x, enemy.y, attackDamage);
if (enemy.health <= 0) {
this.killEnemy(index);
}
}
});
}
createAttackEffect(x, y) {
const effect = document.createElement('div');
effect.className = 'attack-effect';
effect.style.left = (x - 15) + 'px';
effect.style.top = (y - 15) + 'px';
this.gameArea.appendChild(effect);
setTimeout(() => {
if (effect.parentNode) {
effect.parentNode.removeChild(effect);
}
}, 300);
}
createDamageText(x, y, damage) {
const text = document.createElement('div');
text.className = 'damage-text';
text.textContent = '-' + damage;
text.style.left = x + 'px';
text.style.top = y + 'px';
this.gameArea.appendChild(text);
setTimeout(() => {
if (text.parentNode) {
text.parentNode.removeChild(text);
}
}, 1000);
}
killEnemy(index) {
const enemy = this.enemies[index];
if (enemy.element.parentNode) {
enemy.element.parentNode.removeChild(enemy.element);
}
this.enemies.splice(index, 1);
this.score += 100;
this.updateUI();
if (this.enemies.length === 0) {
this.nextWave();
}
}
nextWave() {
this.wave++;
this.enemiesPerWave = Math.min(3 + Math.floor(this.wave / 2), 8);
setTimeout(() => {
this.spawnWave();
}, 2000);
}
updateEnemies() {
this.enemies.forEach((enemy, index) => {
// AI: Move towards player
const dx = this.player.x - enemy.x;
const dy = this.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;
}
// Attack player if close enough
if (distance < 70 && enemy.attackCooldown <= 0) {
this.enemyAttack(enemy);
enemy.attackCooldown = 60;
}
if (enemy.attackCooldown > 0) {
enemy.attackCooldown--;
}
// Update position
enemy.element.style.left = enemy.x + 'px';
enemy.element.style.top = enemy.y + 'px';
});
}
enemyAttack(enemy) {
if (this.player.invulnerable <= 0) {
this.player.health -= 15;
this.player.invulnerable = 60;
this.createDamageText(this.player.x, this.player.y, 15);
if (this.player.health <= 0) {
this.gameOver();
}
}
}
updateUI() {
document.getElementById('score').textContent = this.score;
document.getElementById('wave').textContent = this.wave;
document.getElementById('enemiesLeft').textContent = this.enemies.length;
const healthPercent = (this.player.health / this.player.maxHealth) * 100;
document.getElementById('playerHealth').style.width = healthPercent + '%';
}
gameOver() {
this.gameRunning = false;
document.getElementById('finalScore').textContent = 'Final Score: ' + this.score;
document.getElementById('gameOver').style.display = 'block';
}
gameLoop() {
if (this.gameRunning) {
this.handleInput();
this.updateEnemies();
if (this.player.attackCooldown > 0) {
this.player.attackCooldown--;
}
if (this.player.invulnerable > 0) {
this.player.invulnerable--;
// Flash effect when invulnerable
this.playerElement.style.opacity = Math.sin(Date.now() * 0.05) > 0 ? '1' : '0.5';
} else {
this.playerElement.style.opacity = '1';
}
this.updateUI();
}
requestAnimationFrame(() => this.gameLoop());
}
}
function restartGame() {
// Clear existing enemies
const existingEnemies = document.querySelectorAll('.enemy');
existingEnemies.forEach(enemy => enemy.remove());
// Clear effects
const effects = document.querySelectorAll('.attack-effect, .damage-text');
effects.forEach(effect => effect.remove());
// Hide game over screen
document.getElementById('gameOver').style.display = 'none';
// Start new game
new Game();
}
// Start the game
let game = new Game();
</script>
</body>
</html>
The game is fully functional and playable directly in any modern web browser. You can expand it by adding sound effects, better graphics, special moves, power-ups, or more enemy types.