xxxxxxxxxx
const basket = {
x: 0,
y: 0,
w: 50,
h: 50,
speed: 5,
};
let fruitHue = 0;
let score = 0;
let fruitSpeed = 3;
let colorIncrement = 1;
function preload() {
fruits = [
{ x: 0, y: 0, w: 50, h: 50, img: loadImage('apple.png') },
{ x: 0, y: 0, w: 50, h: 50, img: loadImage('pear.png') },
{ x: 0, y: 0, w: 50, h: 50, img: loadImage('orange.png') },
];
}
function setup() {
createCanvas(windowWidth, windowHeight);
}
function draw() {
background(color(fruitHue, 204, 100));
fruitHue = (fruitHue + colorIncrement) % 360;
fruits.forEach((fruit, index) => {
image(fruit.img, fruit.x, fruit.y, fruit.w, fruit.h);
if (fruit.y > height) {
fruit.x = random(width);
fruit.y = -fruit.h;
}
if (
fruit.x < basket.x + basket.w &&
fruit.x + fruit.w > basket.x &&
fruit.y + fruit.h > basket.y &&
fruit.y < basket.y
) {
score++;
fruit.x = random(width);
fruit.y = -fruit.h;
}
fruit.y += fruitSpeed;
});
image(loadImage('basket.png'), basket.x, basket.y, basket.w, basket.h);
if (keyIsDown(LEFT_ARROW)) basket.x -= basket.speed;
if (keyIsDown(RIGHT_ARROW)) basket.x += basket.speed;
textSize(24);
text(`Score: ${score}`, 20, 40);
if (score % 10 === 0) {
fruitSpeed += 0.5;
colorIncrement += 0.1;
}
if (basket.x < 0) basket.x = 0;
if (basket.x + basket.w > width) basket.x = width - basket.w;
}