xxxxxxxxxx
157
var numItems = 2;
var birdArray = [];
var margin = 100;
var trambolin;
var dikenImg;
var dikenScale = 5;
var score = 0;
let startButton;
let restartButton;
let timerView;
var endImg;
var bgMusic;
var bgSound;
let currentScene = "intro";
function preload() {
startbtnImg = loadImage("start.btn.png");
introImg = loadImage("intro.png");
bgImg = loadImage("bg.png");
dikenImg = loadImage("diken.png");
endImg = loadImage("end.png");
bgMusic = loadSound("bgMusic.mp3");
boingSound = loadSound("boing.mp3");
screamSound = loadSound("scream.wav");
ComicSans = loadFont("comic.ttf")
timeIcon = loadImage("time.png")
restart = loadImage("restart.png")
}
function setup() {
createCanvas(800, 800);
bgMusic.loop();
bgMusic.setVolume(0.5);
// Initialize button
startButton = new Button(width / 2, height / 2);
startButton.setImage("start.btn.png");
startButton.y -= 168;
startButton.x -= 13;
startButton.addListener(startGame);
// restart button
restartButton = new Button(width / 2 + 80, 700);
restartButton.setImage("restart.png");
restartButton.y -= 168;
restartButton.x -= 13;
restartButton.addListener(restartGame);
// Scene and game objects
sceneIntro = new intro();
sceneIntro.setImage("intro.png");
sceneGame = "game";
trambolin = new Trambolin();
for (var i = 0; i < numItems; i++) {
birdArray[i] = new Bird();
birdArray[i].x = random(margin, width - margin);
birdArray[i].y = random(-100, -300);
}
timer = new Timer();
timer.setCountDown(60); // Set countdown to 60 seconds
}
function startGame() {
timer.start();
currentScene = "game"; // Change scene to "game" after starting
}
function restartGame() {
timer.restart();
timer.setCountDown(60);
timer.start();
currentScene = "game";
score = 0;
}
function draw() {
background(250);
// Intro scene
if (currentScene == "intro") {
image(introImg, 0, 0, width, height); // Show intro image
startButton.draw(); // Draw the start button
}
// Game scene
else if (currentScene == "game") {
image(bgImg, -70, 0, width * 1.25, height);
var dikenH = 12 * dikenScale;
image(dikenImg, 0, height - dikenH, width, dikenH);
trambolin.move();
trambolin.display();
for (var i = 0; i < numItems; i++) {
birdArray[i].y = birdArray[i].y + birdArray[i].speed;
if (
birdArray[i].y + 20 >= trambolin.y &&
birdArray[i].x >= trambolin.x &&
birdArray[i].x <= trambolin.x + trambolin.w
) {
birdArray[i].y = trambolin.y - 20;
birdArray[i].speed *= -5;
score++;
boingSound.play();
}
if (birdArray[i].y < -200) {
birdArray[i].speed = random(1, 10);
birdArray[i].x = random(0, width);
birdArray[i].y = random(-150, -280);
}
if (birdArray[i].y >= height - dikenH) {
birdArray[i].y = random(-150, -280);
//score--;
screamSound.play();
}
birdArray[i].display();
}
textSize(24);
textFont(ComicSans);
fill("#006f98");
text("Saved Birds: " + score, 50, 100);
timer.draw(width / 2 + 30, 50);
image(timeIcon, width / 2 - 20, 40);
if (timer.isFinished) {
currentScene = "end";
}
}
// End scene (Optional, can add content here if needed)
else if (currentScene == "end") {
image(endImg, 0, 0, width, height);
textSize(28);
textFont(ComicSans);
fill("#006f98");
text(+score, width / 2 + 13, height / 2 - 76);
restartButton.draw();
}
}