xxxxxxxxxx
179
let trees = [];
let gameState = "start";
let score = 0;
let bgIntro, bgGame, bgEnd, bgMusic, collectSound;
let startButtonImg, restartButtonImg;
let startButton, restartButton;
let treeSpawnRate = 120;
function preload() {
bgIntro = loadImage("BG.png"); // Intro background
bgGame = loadImage("BG.jpg"); // Gameplay background
bgEnd = loadImage("BG.2.png"); // End-game background
bgMusic = loadSound("BG.Music.mp3"); // Background music
collectSound = loadSound("Collected.mp3"); // Collect sound
startButtonImg = loadImage("start.png"); // Start button image
restartButtonImg = loadImage("restart.png"); // Restart button image
}
function setup() {
createCanvas(800, 800);
startButton = createImg("start.png");
startButton.position(width / 2 - 50, height / 2);
startButton.size(100, 50);
startButton.mousePressed(startGame);
restartButton = createImg("restart.png");
restartButton.position(width / 2 - 50, height - 100);
restartButton.size(100, 50);
restartButton.mousePressed(startGame);
restartButton.hide();
}
function draw() {
if (gameState === "start") {
background(bgIntro);
} else if (gameState === "play") {
background(bgGame);
drawGame();
} else if (gameState === "gameOver") {
background(bgEnd);
drawGameOverScreen();
}
}
function startGame() {
gameState = "play";
bgMusic.stop(); // Stop the music when restarting
bgMusic.loop(); // Start the music again from the beginning
startButton.hide();
restartButton.hide();
trees = [];
score = 0;
}
function drawGame() {
if (frameCount % treeSpawnRate === 0) {
spawnTree();
}
for (let i = trees.length - 1; i >= 0; i--) {
trees[i].update();
trees[i].display();
if (trees[i].y > height && trees[i].leaves.length > 0) {
gameState = "gameOver";
restartButton.show();
}
if (trees[i].y > height && trees[i].leaves.length === 0) {
trees.splice(i, 1);
}
}
fill(0);
textSize(18);
text("Score: " + score, 60, 30);
}
function drawGameOverScreen() {
fill(255, 105, 180); // Pink color
textSize(24);
textAlign(CENTER);
text("Score: " + score, width / 2, height / 2);
restartButton.position(width / 2 - 50, height - 100);
bgMusic.stop(); // Stop the music when the game is over
}
class Tree {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = random(1, 3);
this.trunkHeight = random(100, 200);
this.trunkWidth = random(20, 40);
this.branches = this.generateBranches();
this.leaves = this.generateLeaves();
}
generateBranches() {
let branches = [];
let numBranches = floor(random(3, 5));
let baseX = this.x;
let baseY = this.y - this.trunkHeight;
for (let i = 0; i < numBranches; i++) {
let angle = map(i, 0, numBranches - 1, -PI / 3.5, PI / 3.5);
angle += radians(90);
let length = random(60, 90);
let bx = baseX + length * cos(angle);
let by = baseY - length * sin(angle);
branches.push({ x1: baseX, y1: baseY, x2: bx, y2: by });
}
return branches;
}
generateLeaves() {
let leaves = [];
for (let branch of this.branches) {
let lx = branch.x2;
let ly = branch.y2;
let size = random(30, 50);
let colorVal = color(random(50, 200), random(100, 255), random(50, 200));
leaves.push({ x: lx, y: ly, size: size, color: colorVal });
}
return leaves;
}
update() {
this.y += this.speed;
for (let branch of this.branches) {
branch.y1 += this.speed;
branch.y2 += this.speed;
}
for (let leaf of this.leaves) {
leaf.y += this.speed;
}
}
display() {
stroke(100, 50, 0);
strokeWeight(this.trunkWidth);
line(this.x, this.y, this.x, this.y - this.trunkHeight);
stroke(80, 40, 0);
strokeWeight(this.trunkWidth / 3);
for (let branch of this.branches) {
line(branch.x1, branch.y1, branch.x2, branch.y2);
}
noStroke();
for (let leaf of this.leaves) {
fill(leaf.color);
ellipse(leaf.x, leaf.y, leaf.size, leaf.size);
}
}
}
function spawnTree() {
let x = random(100, width - 100);
let y = -100;
trees.push(new Tree(x, y));
}
function mousePressed() {
for (let tree of trees) {
for (let i = tree.leaves.length - 1; i >= 0; i--) {
let leaf = tree.leaves[i];
let d = dist(mouseX, mouseY, leaf.x, leaf.y);
if (d < leaf.size / 2) {
tree.leaves.splice(i, 1);
score++;
collectSound.play();
}
}
}
}