Создам для вас браузерную версию игры "Поле боя" в стиле Heroes of Might and Magic 3 на чистом JavaScript:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Поле боя - Heroes 3</title>
<style>
body {
margin: 0;
padding: 20px;
background: linear-gradient(45deg, #2c1810, #4a2c1a);
font-family: 'Courier New', monospace;
color: #fff;
}
.battlefield {
display: grid;
grid-template-columns: repeat(15, 40px);
grid-template-rows: repeat(11, 40px);
gap: 1px;
background: #1a1a1a;
border: 3px solid #8b4513;
margin: 20px auto;
width: fit-content;
}
.cell {
width: 40px;
height: 40px;
background: #654321;
border: 1px solid #8b4513;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
position: relative;
font-size: 12px;
font-weight: bold;
}
.cell:hover {
background: #7a5230;
}
.cell.selected {
background: #ffd700;
box-shadow: 0 0 10px #ffd700;
}
.cell.possible-move {
background: #90ee90;
}
.cell.enemy-target {
background: #ff6b6b;
}
.unit {
width: 35px;
height: 35px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 10px;
font-weight: bold;
color: white;
text-shadow: 1px 1px 1px black;
}
.player1 {
background: linear-gradient(45deg, #4169e1, #1e90ff);
border: 2px solid #0000cd;
}
.player2 {
background: linear-gradient(45deg, #dc143c, #ff1493);
border: 2px solid #8b0000;
}
.ui {
max-width: 600px;
margin: 0 auto;
text-align: center;
}
.turn-info {
background: #2c1810;
padding: 10px;
border: 2px solid #8b4513;
margin-bottom: 20px;
border-radius: 5px;
}
.unit-info {
background: #2c1810;
padding: 15px;
border: 2px solid #8b4513;
margin-top: 20px;
border-radius: 5px;
}
.controls {
margin: 20px 0;
}
button {
background: #8b4513;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 5px;
cursor: pointer;
font-family: inherit;
}
button:hover {
background: #a0522d;
}
button:disabled {
background: #555;
cursor: not-allowed;
}
.health-bar {
width: 100%;
height: 4px;
background: #333;
margin-top: 2px;
border-radius: 2px;
overflow: hidden;
}
.health-fill {
height: 100%;
background: linear-gradient(90deg, #ff0000, #ffff00, #00ff00);
transition: width 0.3s ease;
}
</style>
</head>
<body>
<div class="ui">
<h1>⚔️ Поле боя Heroes 3 ⚔️</h1>
<div class="turn-info">
<h3 id="currentPlayer">Ход игрока 1</h3>
<p id="gameStatus">Выберите юнита для хода</p>
</div>
<div class="controls">
<button onclick="game.endTurn()">Закончить ход</button>
<button onclick="game.resetGame()">Новая игра</button>
</div>
<div class="battlefield" id="battlefield"></div>
<div class="unit-info" id="unitInfo">
<h4>Информация о юните</h4>
<p>Выберите юнита для просмотра информации</p>
</div>
</div>
<script>
class Unit {
constructor(type, player, x, y) {
this.type = type;
this.player = player;
this.x = x;
this.y = y;
this.maxHp = this.getMaxHp();
this.hp = this.maxHp;
this.damage = this.getDamage();
this.speed = this.getSpeed();
this.range = this.getRange();
this.moved = false;
this.attacked = false;
}
getMaxHp() {
const hpMap = {
'Мечник': 100,
'Лучник': 80,
'Маг': 60,
'Рыцарь': 150,
'Дракон': 200
};
return hpMap[this.type] || 100;
}
getDamage() {
const damageMap = {
'Мечник': 25,
'Лучник': 20,
'Маг': 35,
'Рыцарь': 40,
'Дракон': 60
};
return damageMap[this.type] || 25;
}
getSpeed() {
const speedMap = {
'Мечник': 3,
'Лучник': 2,
'Маг': 2,
'Рыцарь': 4,
'Дракон': 5
};
return speedMap[this.type] || 3;
}
getRange() {
const rangeMap = {
'Мечник': 1,
'Лучник': 6,
'Маг': 4,
'Рыцарь': 1,
'Дракон': 2
};
return rangeMap[this.type] || 1;
}
getSymbol() {
const symbolMap = {
'Мечник': '⚔️',
'Лучник': '🏹',
'Маг': '🔮',
'Рыцарь': '🛡️',
'Дракон': '🐉'
};
return symbolMap[this.type] || '⚔️';
}
takeDamage(damage) {
this.hp = Math.max(0, this.hp - damage);
return this.hp <= 0;
}
getHealthPercent() {
return (this.hp / this.maxHp) * 100;
}
canMove() {
return !this.moved && this.hp > 0;
}
canAttack() {
return !this.attacked && this.hp > 0;
}
resetTurn() {
this.moved = false;
this.attacked = false;
}
}
class Game {
constructor() {
this.battlefield = Array(11).fill().map(() => Array(15).fill(null));
this.currentPlayer = 1;
this.selectedUnit = null;
this.gamePhase = 'select'; // select, move, attack
this.units = [];
this.initializeUnits();
this.render();
}
initializeUnits() {
// Игрок 1 (слева)
const player1Units = [
{ type: 'Мечник', x: 1, y: 2 },
{ type: 'Лучник', x: 0, y: 4 },
{ type: 'Маг', x: 1, y: 6 },
{ type: 'Рыцарь', x: 0, y: 8 },
{ type: 'Дракон', x: 1, y: 5 }
];
// Игрок 2 (справа)
const player2Units = [
{ type: 'Мечник', x: 13, y: 2 },
{ type: 'Лучник', x: 14, y: 4 },
{ type: 'Маг', x: 13, y: 6 },
{ type: 'Рыцарь', x: 14, y: 8 },
{ type: 'Дракон', x: 13, y: 5 }
];
// Создаем юнитов
player1Units.forEach(unitData => {
const unit = new Unit(unitData.type, 1, unitData.x, unitData.y);
this.units.push(unit);
this.battlefield[unitData.y][unitData.x] = unit;
});
player2Units.forEach(unitData => {
const unit = new Unit(unitData.type, 2, unitData.x, unitData.y);
this.units.push(unit);
this.battlefield[unitData.y][unitData.x] = unit;
});
}
render() {
const battlefieldElement = document.getElementById('battlefield');
battlefieldElement.innerHTML = '';
for (let y = 0; y < 11; y++) {
for (let x = 0; x < 15; x++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.x = x;
cell.dataset.y = y;
cell.onclick = () => this.handleCellClick(x, y);
const unit = this.battlefield[y][x];
if (unit) {
const unitElement = document.createElement('div');
unitElement.className = `unit player${unit.player}`;
unitElement.innerHTML = unit.getSymbol();
const healthBar = document.createElement('div');
healthBar.className = 'health-bar';
const healthFill = document.createElement('div');
healthFill.className = 'health-fill';
healthFill.style.width = unit.getHealthPercent() + '%';
healthBar.appendChild(healthFill);
cell.appendChild(unitElement);
cell.appendChild(healthBar);
if (this.selectedUnit === unit) {
cell.classList.add('selected');
}
}
battlefieldElement.appendChild(cell);
}
}
this.updateUI();
this.highlightPossibleMoves();
}
handleCellClick(x, y) {
const unit = this.battlefield[y][x];
if (this.gamePhase === 'select') {
if (unit && unit.player === this.currentPlayer && unit.hp > 0) {
this.selectedUnit = unit;
this.gamePhase = 'move';
this.render();
}
} else if (this.gamePhase === 'move') {
if (unit && unit.player === this.currentPlayer) {
this.selectedUnit = unit;
this.render();
} else if (this.canMoveTo(x, y)) {
this.moveUnit(x, y);
} else if (this.canAttack(x, y)) {
this.attackUnit(x, y);
}
}
}
canMoveTo(x, y) {
if (!this.selectedUnit || !this.selectedUnit.canMove()) return false;
if (this.battlefield[y][x]) return false;
const distance = Math.abs(x - this.selectedUnit.x) + Math.abs(y - this.selectedUnit.y);
return distance <= this.selectedUnit.speed;
}
canAttack(x, y) {
if (!this.selectedUnit || !this.selectedUnit.canAttack()) return false;
const target = this.battlefield[y][x];
if (!target || target.player === this.selectedUnit.player) return false;
const distance = Math.max(
Math.abs(x - this.selectedUnit.x),
Math.abs(y - this.selectedUnit.y)
);
return distance <= this.selectedUnit.range;
}
moveUnit(x, y) {
if (!this.canMoveTo(x, y)) return;
this.battlefield[this.selectedUnit.y][this.selectedUnit.x] = null;
this.selectedUnit.x = x;
this.selectedUnit.y = y;
this.selectedUnit.moved = true;
this.battlefield[y][x] = this.selectedUnit;
this.render();
}
attackUnit(x, y) {
if (!this.canAttack(x, y)) return;
const target = this.battlefield[y][x];
const damage = this.selectedUnit.damage + Math.floor(Math.random() * 10) - 5;
const actualDamage = Math.max(1, damage);
const isDead = target.takeDamage(actualDamage);
this.selectedUnit.attacked = true;
if (isDead) {
this.battlefield[y][x] = null;
this.units = this.units.filter(u => u !== target);
}
this.selectedUnit = null;
this.gamePhase = 'select';
this.render();
if (this.checkGameEnd()) {
this.endGame();
}
}
highlightPossibleMoves() {
if (!this.selectedUnit || this.gamePhase !== 'move') return;
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
const x = parseInt(cell.dataset.x);
const y = parseInt(cell.dataset.y);
if (this.canMoveTo(x, y)) {
cell.classList.add('possible-move');
} else if (this.canAttack(x, y)) {
cell.classList.add('enemy-target');
}
});
}
endTurn() {
// Сброс состояния юнитов текущего игрока
this.units.filter(u => u.player === this.currentPlayer).forEach(u => u.resetTurn());
this.currentPlayer = this.currentPlayer === 1 ? 2 : 1;
this.selectedUnit = null;
this.gamePhase = 'select';
this.render();
}
checkGameEnd() {
const player1Units = this.units.filter(u => u.player === 1 && u.hp > 0);
const player2Units = this.units