xxxxxxxxxx
166
let gameState = "intro"; // "intro", "play", "gameover"
let startButton;
let player;
let playerImg;
let bgImage;
let fallingBottles = [];
let score = 0;
let timer = 60;
let bgMusic;
// Colors
let bottleColors = ["#A2D2FF", "#BDE0FE", "#FFAFCC", "#FFC8A2", "#D0F4DE"];
let labelColors = ["#FFFFFF", "#FAF3DD", "#FFD6A5"];
let waterColors = ["#74C7F1", "#60A3D9", "#4A90E2"];
function preload() {
playerImg = loadImage("boy.png");
bgImage = loadImage("background.jpg");
bgMusic = loadSound("happy.mp3");
}
function setup() {
createCanvas(800, 800);
startButton = { x: width / 2 - 75, y: height / 2, w: 150, h: 50 };
player = { x: width / 2 - 50, y: height - 180, w: 160, h: 220 }; // Karakter büyütüldü
setInterval(updateTimer, 1000);
setInterval(spawnFallingBottle, 1500);
}
function draw() {
if (gameState === "intro") {
drawIntro();
} else if (gameState === "play") {
playGame();
} else if (gameState === "gameover") {
drawGameOver();
}
}
function drawIntro() {
background(50, 150, 200);
textAlign(CENTER);
fill(255);
textSize(36);
text("Hydro Hero: Catch the Bottles!", width / 2, height / 3);
textSize(18);
text("The more bottles you collect, the more animals you can help!", width / 2, height / 3 + 40);
text("Use LEFT and RIGHT arrows to move", width / 2, height / 3 + 80);
text("Catch falling water bottles to earn points!", width / 2, height / 3 + 110);
fill(0, 200, 100);
rect(startButton.x, startButton.y, startButton.w, startButton.h, 10);
fill(255);
textSize(24);
text("Start Game", width / 2, startButton.y + startButton.h / 2 + 8);
}
function playGame() {
background(bgImage); // Arka plan görüntüsü eklendi
if (keyIsDown(LEFT_ARROW)) player.x -= 10;
if (keyIsDown(RIGHT_ARROW)) player.x += 10;
player.x = constrain(player.x, 0, width - player.w);
for (let i = fallingBottles.length - 1; i >= 0; i--) {
let bottle = fallingBottles[i];
bottle.update();
bottle.display();
if (collideRectRect(player.x, player.y, player.w, player.h, bottle.x, bottle.y, bottle.bottleW, bottle.bottleH)) {
score++;
fallingBottles.splice(i, 1);
}
}
image(playerImg, player.x, player.y, player.w, player.h);
// Score and Timer
textAlign(LEFT);
fill("#FF5733");
textSize(28);
textFont("Arial");
text(`Score: ${score}`, 50, 50);
text(`Time Left: ${timer}`, width - 200, 50);
if (timer <= 0) gameState = "gameover";
}
function drawGameOver() {
background(0, 0, 0, 180);
textAlign(CENTER);
fill(255, 0, 0);
textSize(48);
text("Game Over!", width / 2, height / 3);
fill(255);
textSize(24);
text(`You provided water for ${score} animals!`, width / 2, height / 3 + 50);
text("Thank you for helping the street animals!", width / 2, height / 3 + 100);
setTimeout(() => {
gameState = "intro";
score = 0;
timer = 60;
fallingBottles = [];
}, 5000);
}
function mousePressed() {
if (gameState === "intro" && mouseX >= startButton.x && mouseX <= startButton.x + startButton.w && mouseY >= startButton.y && mouseY <= startButton.y + startButton.h) {
gameState = "play";
score = 0;
timer = 60;
fallingBottles = [];
if (!bgMusic.isPlaying()) bgMusic.loop();
}
}
class WaterBottle {
constructor(x, y, bottleW, bottleH, speed, bottleColor, labelColor, waterColor) {
this.x = x;
this.y = y;
this.bottleW = bottleW;
this.bottleH = bottleH;
this.speed = speed;
this.bottleColor = bottleColor;
this.labelColor = labelColor;
this.waterColor = waterColor;
}
display() {
noStroke(); // Çizgi kaldırıldı
fill(this.bottleColor);
rect(this.x, this.y, this.bottleW, this.bottleH, 10);
fill(this.waterColor);
rect(this.x, this.y + this.bottleH * 0.4, this.bottleW, this.bottleH * 0.6);
fill(this.labelColor);
rect(this.x + this.bottleW * 0.25, this.y + this.bottleH * 0.6, this.bottleW * 0.5, this.bottleH * 0.15);
fill("#555");
rect(this.x + this.bottleW * 0.1, this.y - 10, this.bottleW * 0.8, 10); // Kapak düzeltildi
}
update() {
this.y += this.speed;
if (this.y > height) {
this.y = -random(100, 300); // Yeni şişeler düşmeye devam etsin
this.x = random(50, width - 100);
this.speed = random(6, 10);
this.bottleColor = random(bottleColors);
this.labelColor = random(labelColors);
this.waterColor = random(waterColors);
}
}
}
function spawnFallingBottle() {
let x = random(50, width - 100);
let bottle = new WaterBottle(
x, -100,
random(40, 60), random(100, 140),
random(6, 10),
random(bottleColors), random(labelColors), random(waterColors)
);
fallingBottles.push(bottle);
}
function updateTimer() {
if (gameState === "play" && timer > 0) timer--;
}
function collideRectRect(x, y, w, h, x2, y2, w2, h2) {
return (x < x2 + w2 && x + w > x2 && y < y2 + h2 && y + h > y2);
}