html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Космический Flappy</title>
<style>
body {
background: #0d1a2b;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
border: 2px solid #1d3557;
box-shadow: 0 0 20px #00ccff;
}
#game-over {
color: #ff5e5e;
font-family: 'Arial';
font-size: 2em;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: none;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="game-over">Игра окончена<br>Кликните для перезапуска</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gameOver = document.getElementById('game-over');
canvas.width = 400;
canvas.height = 600;
const gravity = 0.3;
const pipeGap = 150;
const pipeFrequency = 1400; // milliseconds
const scoreSound = new Audio('http://soundbible.com/grab.php?id=2200&type=mp3');
class Player {
constructor() {
this.x = 100;
this.y = 150;
this.radius = 20;
this.color = '#fff';
this.angle = 0;
this.velocity = 0;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
// Рисуем хвост корабля
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(this.x + 15 * Math.sin(this.angle), this.y - 10 * Math.cos(this.angle));
ctx.strokeStyle = '#fff';
ctx.stroke();
}
update() {
this.velocity += gravity;
this.y += this.velocity;
this.angle = Math.min(Math.max(-45, this.angle + (gravity * 3)), 45);
// Проверка на выход за границы
if(this.y + this.radius > canvas.height || this.y - this.radius < 0) {
this.gameOver();
}
}
jump() {
this.velocity = -8;
this.angle = 45;
}
gameOver() {
clearInterval(pipeTimer);
gameOver.style.display = 'block';
document.removeEventListener('click', this.jump);
}
}
class Pipe {
constructor() {
this.top = {
x: canvas.width,
y: 0,
height: Math.random() * (canvas.height/2) + 50,
width: 50,
color: '#3498db'
};
this.bottom = {
x: canvas.width,
y: this.top.height + pipeGap,
height: canvas.height - (this.top.height + pipeGap),
width: 50,
color: '#3498db'
};
this.passed = false;
}
update() {
this.top.x -= 2;
this.bottom.x -= 2;
// Проверка столкновения
if(this.top.x + this.top.width > player.x - player.radius &&
this.top.x < player.x + player.radius &&
(player.y - player.radius < this.top.height ||
player.y + player.radius > this.bottom.y)) {
player.gameOver();
}
// Проверка на прохождение
if(this.top.x + this.top.width < player.x && !this.passed) {
score++;
scoreSound.play();
this.passed = true;
}
}
draw() {
ctx.fillStyle = this.top.color;
ctx.fillRect(this.top.x, this.top.y, this.top.width, this.top.height);
ctx.fillRect(this.bottom.x, this.bottom.y, this.bottom.width, this.bottom.height);
// Добавляем звезды на трубы
ctx.fillStyle = '#fff';
for(let i = 0; i < 10; i++) {
ctx.fillRect(this.top.x + Math.random()*this.top.width,
Math.random()*this.top.height,
1,
1);
}
}
}
let player = new Player();
let pipes = [];
let score = 0;
let pipeTimer;
function gameLoop() {
ctx.fillStyle = '#0d1a2b';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Звезды
ctx.fillStyle = '#fff';
for(let i = 0; i < 50; i++) {
ctx.fillRect(Math.random() * canvas.width, Math.random() * canvas.height, 1, 1);
}
player.draw();
player.update();
ctx.fillStyle = '#fff';
ctx.font = '20px Arial';
ctx.fillText(`Счет: ${score}`, 20, 30);
pipes.forEach((pipe, index) => {
pipe.update();
pipe.draw();
if(pipe.top.x + pipe.top.width < 0) {
pipes.splice(index, 1);
}
});
}
function spawnPipe() {
pipes.push(new Pipe());
}
document.addEventListener('click', () => {
if(gameOver.style.display === 'none') {
player.jump();
} else {
restartGame();
}
});
function restartGame() {
player = new Player();
pipes = [];
score = 0;
pipeTimer = setInterval(spawnPipe, pipeFrequency);
gameOver.style.display = 'none';
document.addEventListener('click', player.jump);
requestAnimationFrame(gameLoop);
}
restartGame();
pipeTimer = setInterval(spawnPipe, pipeFrequency);
requestAnimationFrame(gameLoop);
</script>
</body>
</html>
Этот код создает игру с космической тематикой, где вы управляете космическим кораблем:
Игра использует простую физику: корабль постоянно ускоряется из-за гравитации, а клик создает импульс прыжка. Трубы движутся слева направо, и нужно пролететь между их частями. Игра заканчивается, если корабль столкнется с трубой или выйдет за границы экрана.