Frog-Leap-Game
159 строк · 5.7 Кб
1import { lilyPads } from './gameDraw.js';2import { createSplash } from './Splash.js';3
4export class Frog {5constructor(canvas, ctx, splashes, setGameRunning, resetScore) {6this.canvas = canvas;7this.ctx = ctx;8this.width = 40;9this.height = 50;10this.vx = 0;11this.vy = 0;12this.jumpPower = -15;13this.gravity = 0.5;14this.speed = 5;15this.isJumping = false;16this.jumpSound = new Audio('./assets/8-bit-jump.mp3'); // звук прыжка17this.jumpSoundPlayed = false; // флаг звука прыжка18this.isDrowned = false;19this.splashes = splashes;20this.drownSound = new Audio('./assets/8-bit-water-drop.wav'); // звук утопления21this.drownSoundPlayed = false; // флаг звука утопления22this.setGameRunning = setGameRunning;23this.resetScore = resetScore;24this.resetPosition();25}26
27resetPosition() {28// Лягушка начинает с центральной кувшинки29this.x = this.canvas.width / 2 - this.width / 2;30this.y = this.canvas.height * 0.68 - this.height;31this.isDrowned = false;32this.vx = 0;33this.vy = 0;34}35
36// Логика рисования лягушки37draw() {38if (this.isDrowned) return; // Не рисовать лягушку, если она утонула39
40const { ctx } = this;41
42// Цвет, форма43ctx.fillStyle = "green";44ctx.fillRect(this.x, this.y, this.width, this.height);45
46// Глаза47ctx.fillStyle = "white";48ctx.fillRect(this.x + 5, this.y + 5, 10, 10);49ctx.fillRect(this.x + 25, this.y + 5, 10, 10);50
51// Рот52ctx.fillStyle = "red";53ctx.fillRect(this.x + 10, this.y + 25, 20, 5);54
55// Ноги56ctx.fillStyle = "darkgreen";57ctx.fillRect(this.x - 10, this.y + 30, 10, 20);58ctx.fillRect(this.x + 40, this.y + 30, 10, 20);59}60
61// Логика обновления позиции лягушки62update() {63this.vy += this.gravity;64this.x += this.vx;65this.y += this.vy;66
67// Проверка на столкновение с кувшинками68let onLilyPad = false;69
70for (const lilyPad of lilyPads) {71if (72this.y + this.height > lilyPad.y &&73this.x + this.width > lilyPad.x &&74this.x < lilyPad.x + lilyPad.width &&75this.y + this.height <= lilyPad.y + lilyPad.height76) {77// Если все условия выполнены, лягушка находится на кувшинке78onLilyPad = true;79this.y = lilyPad.y - this.height;80this.vy = 0;81this.isJumping = false; // Лягушка больше не прыгает82// Сбрасываем флаг, чтобы звук можно было проиграть снова83this.jumpSoundPlayed = false;84this.drownSoundPlayed = false;85}86}87
88// Если лягушка не на кувшинке и касается болота, то лягушка утонула89if (!onLilyPad && this.y + this.height > this.canvas.height * 0.7) {90this.drown();91}92
93// Лягушка не должна выходить за границы экрана94if (this.x < 0) this.x = 0;95if (this.x + this.width > this.canvas.width) {96this.x = this.canvas.width - this.width;97}98}99
100// Прыжок101jump() {102if (!this.isJumping) {103this.vy = this.jumpPower;104this.isJumping = true;105
106if (!this.jumpSoundPlayed) { // Проверяем, был ли уже проигран звук107this.jumpSound.currentTime = 0; // сбрасываем время воспроизведения звука108this.jumpSound.play();109this.jumpSoundPlayed = true; // звук проигран110}111}112}113
114// Движения влево, вправо, стоп115moveLeft() {116this.vx = -this.speed;117}118
119moveRight() {120this.vx = this.speed;121}122
123stop() {124this.vx = 0;125}126
127// Логика утопления128drown() {129// Создаем всплеск воды130createSplash(this.x + this.width / 2, this.y + this.height, this.splashes);131
132this.isDrowned = true; // Лягушка утонула133
134// Воспроизводим звук утопления135if (!this.drownSoundPlayed) { // проверяем, был ли уже проигран звук136this.drownSound.currentTime = 0; // сбрасываем время воспроизведения звука137this.drownSound.play();138this.drownSoundPlayed = true; // звук проигран139}140
141
142// Остановка игры с задержкой143setTimeout(() => {144
145this.setGameRunning(false);146
147// Сбрасываем счёт после утопления148this.resetScore();149
150
151// Показать текст "Game Over"152document.getElementById("gameOverText").style.display = "block";153
154// Обновить текст кнопки на "Play"155document.getElementById("playButton").innerText = "Play";156this.resetPosition();157}, 1000);158}159}
160