xxxxxxxxxx
185
let fruits = [];
let basketY;
let lives = 3;
let score = 0;
let highScore = 0;
let fruitSpeed = 2;
let basketColor = "";
let bgImage;
let basketImage;
let easySong;
let mediumSong;
let hardSong;
let fft, peakDetect;
function setup() {
createCanvas(800, 600);
basketY = height - 50; // Basket at bottom
// for the frequency analysis
fft = new p5.FFT();
// detect peaks across frequencies
peakDetect = new p5.PeakDetect(20,20000,0.1,20);
}
function preload(){
// load the background image
bgImage = loadImage('images/background.png');
basketImage = loadImage('images/basket.png');
easySong = loadSound('audio/InMyRoom_Easy.mp3');
// mediumSong = loadSound('audio/');
// hardSong = loadSound('audio/');
}
function draw() {
background(200);
image(bgImage,0,0,width,height);
// Draw basket
image(basketImage, width/2 - 50, basketY, 100,50);
// Analyze the audio
fft.analyze();
peakDetect.update(fft);
// Spawn a fruit if a beat is detected
if (peakDetect.isDetected){
spawnFruit();
}
// Draw fuits and update their position
for (let i = fruits.length - 1; i >= 0; i--){
let fruit = fruits[i];
fruit.update();
fruit.display();
// check if the first fruit falls into the basket
if (i === 0 && fruit.y > basketY){
// loose a life for missing the fruit first
lives--;
// remove the missed fruit
fruits.splice(i,1);
}
}
// get bass energy (low frequency)
let amplitude = fft.getEnergy("bass");
// map amplitude to speed range
fruitSpeed = map(amplitude,0,255,2,10);
// Display score and lives
fill(0);
textSize(20);
text(`Score: ${score}`, 10, 30);
if (score > highScore){
highScore = score;
}
text(`High Score: ${highScore}`, 10, 60);
text(`Lives: ${lives}`, 10, 90);
// End game
if (lives <= 0) {
textSize(40);
text("Game Over", width / 2 - 100, height / 2);
textSize(20);
text("Press 'R' to Restart", width / 2 - 80, height / 2 + 50);
noLoop(); // Stop the game
}
}
// Fruit class
class Fruit {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color;
this.velocity = fruitSpeed;
}
display() {
let colorMap = {
blue: color(0,0,255),
white: color(255),
green: color(0,255,0),
yellow: color(255,255,0),
};
fill(colorMap[this.color]);
ellipse(this.x, this.y, 20, 20);
}
update() {
// makes the velocity dynamic
this.y += this.velocity * fruitSpeed;
}
isOutOfBounds() {
return this.y > height;
}
}
function spawnFruit() {
let colors = ["blue", "white", "green", "yellow"];
let randomColor = random(colors);
let x = random(50, width - 50);
fruits.push(new Fruit(x, 0, randomColor));
}
function keyPressed() {
if (key === "b") basketColor = "blue";
if (key === "w") basketColor = "white";
if (key === "g") basketColor = "green";
if (key === "y") basketColor = "yellow";
// map keys to colors
let keyToColor = {
b: "blue",
w: "white",
g: "green",
y: "yellow",
};
let matchedColor = keyToColor[key.toLowerCase()];
// check only if the first ball in the array
if (fruits.length > 0){
// The first ball to conisder
let firstFruit = fruits[0];
if (firstFruit.color === matchedColor && firstFruit.y < basketY){
// correct key pressed for the first fruit
score++;
// remove the first fruit
fruits.splice(0,1);
} else {
// incorrect key pressed
lives--;
// remove the first fruit even if incorrect
fruits.splice(0,1);
}
}
// restart the game loop
if (key === "r"){
score = 0;
lives = 3;
fruits = [];
loop();
// restart the song
// if (!song.isPlaying()){
// song.play();
// }
}
}
// Spawn a new fruit every 1.5 seconds
setInterval(spawnFruit, 1500);