Frog-Leap-Game
109 строк · 3.2 Кб
1// Фон
2export function drawBackground(ctx, canvas) {3// Небо4ctx.fillStyle = "#87ceeb";5ctx.fillRect(0, 0, canvas.width, canvas.height * 0.7);6
7// Болото с градиентом8const gradient = ctx.createLinearGradient(90,10canvas.height * 0.7,110,12canvas.height13);14gradient.addColorStop(0, "darkgreen");15gradient.addColorStop(1, "black");16
17ctx.fillStyle = gradient;18ctx.fillRect(0, canvas.height * 0.7, canvas.width, canvas.height * 0.3);19
20// Камыши21for (let i = 0; i < canvas.width; i += 85) {22ctx.fillStyle = "#556B2F";23ctx.fillRect(i, canvas.height * 0.7 - 60, 6, 60); // Стебель24ctx.fillStyle = "#8B4513";25ctx.fillRect(i, canvas.height * 0.7 - 76, 6, 25); // Верхушка26}27
28// Солнце29ctx.fillStyle = "yellow";30ctx.beginPath();31ctx.arc(canvas.width - 100, 100, 50, 0, Math.PI * 2);32ctx.fill();33
34// Облако слева35ctx.fillStyle = "white";36ctx.beginPath();37ctx.arc(150, 100, 40, 0, Math.PI * 2);38ctx.arc(200, 100, 50, 0, Math.PI * 2);39ctx.arc(250, 100, 40, 0, Math.PI * 2);40ctx.fill();41
42// Облако справа43ctx.fillStyle = "white";44ctx.beginPath();45ctx.arc(450, 150, 40, 0, Math.PI * 2);46ctx.arc(500, 150, 50, 0, Math.PI * 2);47ctx.arc(550, 150, 40, 0, Math.PI * 2);48ctx.fill();49}
50
51// Кувшинки
52export const lilyPads = [53{ x: 150, y: 400, width: 100, height: 20 },54{ x: 350, y: 400, width: 100, height: 20 },55{ x: 550, y: 400, width: 100, height: 20 },56];57export function drawLilyPads(ctx) {58ctx.fillStyle = "#00ca33";59lilyPads.forEach((lilyPad) => {60ctx.fillRect(lilyPad.x, lilyPad.y, lilyPad.width, lilyPad.height);61});62}
63
64// Счёт - Score
65export function drawScore(ctx, score) {66ctx.fillStyle = "white";67ctx.font = "20px sans-serif";68// Заголовок Score, число 0, расположение на экране69ctx.fillText(`Score: ${score}`, 10, 30);70}
71
72// Проверка на столкновение лягушки и мошки
73export function detectCollision(frog, fly) {74return (75frog.x < fly.x + fly.width &&76frog.x + frog.width > fly.x &&77frog.y < fly.y + fly.height &&78frog.height + frog.y > fly.y79);80}
81
82// Обновление игры
83export function update(ctx, frog, flies, score, gameRunning) {84if (gameRunning) {85ctx.clearRect(0, 0, canvas.width, canvas.height);86drawBackground();87drawLilyPads();88frog.update();89frog.draw();90flies.forEach((fly) => {91fly.update();92fly.draw();93if (detectCollision(frog, fly)) {94score++;95fly.x = Math.random() * canvas.width;96fly.y = Math.random() * (canvas.height - 200);97}98});99drawScore();100
101// Обновляем и рисуем всплески102splashes = splashes.filter((splash) => splash.alpha > 0);103splashes.forEach((splash) => {104splash.update();105splash.draw();106});107}108requestAnimationFrame(update);109}
110