Перейти к содержимому
Главная
Почему я
Портфолио
Цены
Меню
Главная
Почему я
Портфолио
Цены
+7 (900) 173-35-93
Flappy Bird Clone
// Инициализация игрового поля var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // Загрузка изображений var birdImg = new Image(); birdImg.src = "bird.png"; var pipeTopImg = new Image(); pipeTopImg.src = "pipe_top.png"; var pipeBottomImg = new Image(); pipeBottomImg.src = "pipe_bottom.png"; // Задание начальных значений var birdX = 80; var birdY = 200; var birdDY = 0; var gravity = 0.25; var jumpStrength = -5; var score = 0; var pipes = []; // Добавление новых труб function addPipes() { var spaceHeight = 150; var minPipeHeight = 50; var maxPipeHeight = canvas.height - spaceHeight - minPipeHeight; var pipeHeight = Math.floor(Math.random() * (maxPipeHeight - minPipeHeight + 1)) + minPipeHeight; var pipe = { x: canvas.width, y: 0, height: pipeHeight, width: pipeTopImg.width, passed: false }; pipes.push(pipe); pipe = { x: canvas.width, y: pipeHeight + spaceHeight, height: canvas.height - pipeHeight - spaceHeight, width: pipeBottomImg.width, passed: false }; pipes.push(pipe); } // Обработка ввода пользователя document.addEventListener("keydown", function(event) { if (event.keyCode === 32 || event.keyCode === 38) { birdDY = jumpStrength; } }); // Основной игровой цикл function gameLoop() { // Очистка экрана ctx.clearRect(0, 0, canvas.width, canvas.height); // Обновление позиции птицы birdDY += gravity; birdY += birdDY; // Отрисовка труб for (var i = 0; i < pipes.length; i++) { var pipe = pipes[i]; pipe.x -= 2; if (!pipe.passed && pipe.x < birdX) { pipe.passed = true; score++; } if (pipe.x + pipe.width < 0) { pipes.splice(i, 1); i--; } else { if (pipe.y === 0) { ctx.drawImage(pipeTopImg, pipe.x, pipe.y, pipe.width, pipe.height); } else { ctx.drawImage(pipeBottomImg, pipe.x, pipe.y, pipe.width, pipe.height); } } } // Добавление новых труб if (pipes.length < 4) { addPipes(); } // Проверка столкновений for (var i = 0; i < pipes.length; i++) { var pipe = pipes[i]; if (birdX + birdImg.width > pipe.x && birdX < pipe.x + pipe.width) { if (pipe.y === 0 && birdY < pipe.height || pipe.y !== 0 && birdY + birdImg.height > pipe.y) { gameOver(); } } } // Отрисовка птицы ctx.drawImage(birdImg, birdX, birdY); // Отрисовка счета ctx.fillText("Score: " + score, 10, 25); // Запуск следующего кадра window.requestAnimationFrame(gameLoop); } // Игра начинается здесь function startGame() { addPipes(); gameLoop(); } // Обработка конца игры function gameOver() { alert("Game over. Score: " + score); document.location.reload(); } // Запуск игры startGame();
1
2
Узнайте стоимость и сроки реализации Вашего проекта
Есть ли у вас фирменный стиль?
Да
Нет
20%
Ваша скидка:
0
%
Скидка 10%
Домен в подарок
Далее
Далее
Узнайте стоимость и сроки реализации Вашего проекта
Какой сайт вам нужен?
Лендинг (одностраничный сайт)
Многостраничный сайт
Интернет-магазин
Другое
Ваша скидка:
0
%
Скидка 10%
Домен в подарок
Назад
Получить рассчет
Форма была успешно отправлена.
An error occured.