/
novakovichid
/
test
Обзор
Документация
Войти
/
novakovichid
/
test
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
script.js
243 строки
8 KB
novakovichidSHP
init
25 янв 2026, 12:16
25 янв 2026, 12:16
de68e55
Код
Авторство
О чём код?
class Game { constructor() { this.gameArea = document.getElementById('gameArea'); this.playerTank = document.getElementById('playerTank'); this.gameOverScreen = document.getElementById('gameOverScreen'); // Размеры игровой области this.gameWidth = this.gameArea.offsetWidth; this.gameHeight = this.gameArea.offsetHeight; // Позиция игрока this.playerX = this.gameWidth / 2 - 15; this.playerY = this.gameHeight - 50; this.playerWidth = 30; this.playerHeight = 30; this.playerSpeed = 4; // Управление this.keys = {}; this.playerAngle = 0; // Массивы для объектов this.bullets = []; this.enemies = []; // Параметры игры this.lives = 3; this.score = 0; this.bulletSpeed = 5; this.enemySpeed = 1; this.gameRunning = true; this.enemySpawnTimer = 0; this.enemySpawnInterval = 120; // Слушатели событий window.addEventListener('keydown', (e) => this.handleKeyDown(e)); window.addEventListener('keyup', (e) => this.handleKeyUp(e)); // Запуск игры this.gameLoop(); this.spawnEnemyTimer(); } handleKeyDown(e) { this.keys[e.key] = true; if (e.key === ' ') { e.preventDefault(); this.shoot(); } if (e.key === 'r' || e.key === 'R') { location.reload(); } } handleKeyUp(e) { this.keys[e.key] = false; } updatePlayer() { // Движение влево if (this.keys['ArrowLeft'] && this.playerX > 0) { this.playerX -= this.playerSpeed; this.playerAngle = 90; } // Движение вправо if (this.keys['ArrowRight'] && this.playerX < this.gameWidth - this.playerWidth) { this.playerX += this.playerSpeed; this.playerAngle = -90; } // Движение вверх if (this.keys['ArrowUp'] && this.playerY > 0) { this.playerY -= this.playerSpeed; this.playerAngle = 0; } // Движение вниз if (this.keys['ArrowDown'] && this.playerY < this.gameHeight - this.playerHeight) { this.playerY += this.playerSpeed; this.playerAngle = 180; } // Обновляем позицию игрока на экране this.playerTank.style.left = this.playerX + 'px'; this.playerTank.style.top = this.playerY + 'px'; this.playerTank.style.transform = `translateX(-50%) rotate(${this.playerAngle}deg)`; } shoot() { const bulletX = this.playerX; const bulletY = this.playerY; const radians = (this.playerAngle * Math.PI) / 180; const vx = Math.sin(radians) * this.bulletSpeed; const vy = -Math.cos(radians) * this.bulletSpeed; this.bullets.push({ x: bulletX, y: bulletY, vx: vx, vy: vy, width: 4, height: 10 }); } updateBullets() { for (let i = this.bullets.length - 1; i >= 0; i--) { const bullet = this.bullets[i]; bullet.x += bullet.vx; bullet.y += bullet.vy; // Удаляем пули, вышедшие за границы if (bullet.x < 0 || bullet.x > this.gameWidth || bullet.y < 0 || bullet.y > this.gameHeight) { this.bullets.splice(i, 1); continue; } // Отрисовка пули const bulletElement = document.createElement('div'); bulletElement.className = 'bullet'; bulletElement.style.left = bullet.x + 'px'; bulletElement.style.top = bullet.y + 'px'; bulletElement.style.transform = `rotate(${this.playerAngle}deg)`; } } spawnEnemy() { const x = Math.random() * (this.gameWidth - 30); const y = -30; this.enemies.push({ x: x, y: y, width: 30, height: 30, element: this.createEnemyElement(x, y) }); document.getElementById('enemies').textContent = this.enemies.length; } createEnemyElement(x, y) { const enemy = document.createElement('div'); enemy.className = 'enemy-tank'; enemy.style.left = x + 'px'; enemy.style.top = y + 'px'; this.gameArea.appendChild(enemy); return enemy; } updateEnemies() { for (let i = this.enemies.length - 1; i >= 0; i--) { const enemy = this.enemies[i]; enemy.y += this.enemySpeed; // Обновляем позицию enemy.element.style.top = enemy.y + 'px'; // Враг достиг нижней границы if (enemy.y > this.gameHeight) { this.lives--; document.getElementById('lives').textContent = this.lives; enemy.element.remove(); this.enemies.splice(i, 1); document.getElementById('enemies').textContent = this.enemies.length; if (this.lives <= 0) { this.gameOver(); } continue; } // Проверяем столкновение с пулями for (let j = this.bullets.length - 1; j >= 0; j--) { const bullet = this.bullets[j]; if (this.checkCollision(bullet, enemy)) { // Уничтожаем врага и пулю this.createExplosion(enemy.x, enemy.y); enemy.element.remove(); this.enemies.splice(i, 1); this.bullets.splice(j, 1); document.getElementById('enemies').textContent = this.enemies.length; // Увеличиваем счёт this.score += 10; document.getElementById('score').textContent = this.score; break; } } } } checkCollision(obj1, obj2) { return obj1.x < obj2.x + obj2.width && obj1.x + obj1.width > obj2.x && obj1.y < obj2.y + obj2.height && obj1.y + obj1.height > obj2.y; } createExplosion(x, y) { const explosion = document.createElement('div'); explosion.className = 'explosion'; explosion.style.left = x + 'px'; explosion.style.top = y + 'px'; this.gameArea.appendChild(explosion); setTimeout(() => explosion.remove(), 500); } spawnEnemyTimer() { setInterval(() => { if (this.gameRunning && this.enemies.length < 8) { this.spawnEnemy(); } }, this.enemySpawnInterval); } gameOver() { this.gameRunning = false; document.getElementById('finalScore').textContent = this.score; this.gameOverScreen.classList.remove('hidden'); } gameLoop() { if (!this.gameRunning) return; // Очищаем пули с экрана document.querySelectorAll('.bullet').forEach(el => el.remove()); this.updatePlayer(); this.updateBullets(); this.updateEnemies(); requestAnimationFrame(() => this.gameLoop()); } } // Запуск игры при загрузке страницы window.addEventListener('load', () => { new Game(); });