Вот продолжение и завершение кода. Мы допишем отрисовку босса, фон-звёзды, HUD (очки/жизни), инициализацию звёзд и запуск игры.
html
<!-- продолжение <script> -->
<script>
// Массив звёзд для фона
const stars = [];
function init() {
// заполняем массив звёзд
for (let i = 0; i < 100; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
r: Math.random() * 1.5 + 0.5
});
}
highScoreDisplay.textContent = highScore;
startLevelDisplay.textContent = level;
startLivesDisplay.textContent = lives;
player = {
x: canvas.width / 2 - 25,
y: canvas.height - 50,
width: 50,
height: 30,
speed: 5
};
createAliens();
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
window.addEventListener('keydown', (e) => keys[e.key] = true);
window.addEventListener('keyup', (e) => keys[e.key] = false);
requestAnimationFrame(gameLoop);
}
// ... (весь предыдущий код функций)
// Отрисовка элементов
function draw() {
// 1) фон - тёмное небо со звёздами
drawStars();
// 2) пули
bullets.forEach(b => {
ctx.fillStyle = '#00eeff';
ctx.fillRect(b.x, b.y, b.width, b.height);
});
enemyBullets.forEach(b => {
ctx.fillStyle = '#ff5555';
ctx.fillRect(b.x, b.y, b.width, b.height);
});
// 3) частицы взрывов
particles.forEach(p => {
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
ctx.fill();
});
// 4) игрок
ctx.fillStyle = '#00a8ff';
ctx.fillRect(player.x, player.y, player.width, player.height);
// крылья
ctx.fillStyle = '#0077ff';
ctx.beginPath();
ctx.moveTo(player.x - 10, player.y + player.height/2);
ctx.lineTo(player.x, player.y);
ctx.lineTo(player.x, player.y + player.height);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.moveTo(player.x + player.width + 10, player.y + player.height/2);
ctx.lineTo(player.x + player.width, player.y);
ctx.lineTo(player.x + player.width, player.y + player.height);
ctx.closePath();
ctx.fill();
// 5) инопланетяне (см. ваш код)
// 6) босс (если есть)
if (bossAlien) {
// тело боса: цвет меняется при получении урона
const c = bossAlien.moveCounter;
ctx.fillStyle = c < 50 ? '#ff5555' : c < 75 ? '#ffaa00' : '#ffff00';
ctx.fillRect(bossAlien.x, bossAlien.y, bossAlien.width, bossAlien.height);
// щит - кольцо
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.ellipse(
bossAlien.x + bossAlien.width/2,
bossAlien.y + bossAlien.height/2,
bossAlien.width/1.2,
bossAlien.height/1.2,
0, 0, Math.PI*2
);
ctx.stroke();
}
// 7) HUD: очки и жизни
ctx.fillStyle = '#ffffff';
ctx.font = '20px Segoe UI';
ctx.fillText(`Очки: ${score}`, 20, 30);
ctx.fillText(`Жизни: ${lives}`, canvas.width - 120, 30);
}
// Функция рисует звёздное небо
function drawStars() {
ctx.fillStyle = '#0a1929';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#ffffff';
stars.forEach(s => {
ctx.globalAlpha = Math.random() * 0.5 + 0.5;
ctx.beginPath();
ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
ctx.fill();
});
ctx.globalAlpha = 1;
}
// Запускаем инициализацию
init();
</script>
</body>
</html>
Теперь HTML-файл завершён. Просто сохраните его как index.html и откройте в браузере — у вас будет полнофункциональный Space Invaders на Canvas! 🚀