xxxxxxxxxx
199
let fruits = [];
let bombs = [];
let score = 0;
let slices = [];
function setup() {
createCanvas(400, 600);
// Generate fruits
for (let i = 0; i < 5; i++) {
fruits.push(new Fruit(random(50, 350), height, random(3, 6), random(-2, 2)));
}
// Generate bombs
bombs.push(new Bomb(random(50, 350), height, random(3, 6), random(-2, 2)));
}
function draw() {
background(220);
// Draw and update fruits
for (let i = fruits.length - 1; i >= 0; i--) {
fruits[i].update();
fruits[i].display();
// Check if fruit is sliced
if (fruits[i].isSliced(mouseX, mouseY)) {
slices.push(new Slice(fruits[i].x, fruits[i].y, fruits[i].radius));
fruits[i].reset();
score += 1;
}
}
// Draw and update bombs
for (let i = bombs.length - 1; i >= 0; i--) {
bombs[i].update();
bombs[i].display();
// Check if bomb is sliced
if (bombs[i].isSliced(mouseX, mouseY)) {
// End game or penalize player
noLoop();
textSize(32);
fill(255, 0, 0);
text("Game Over!", width / 2 - 80, height / 2);
}
}
// Display the slicing effect
for (let i = slices.length - 1; i >= 0; i--) {
slices[i].update();
slices[i].display();
// Remove the slice effect after it completes
if (slices[i].finished) {
slices.splice(i, 1);
}
}
// Display score
textSize(24);
fill(0);
text("Score: " + score, 10, 30);
}
// Fruit class
class Fruit {
constructor(x, y, speed, xSpeed) {
this.x = x;
this.y = y;
this.radius = 30;
this.speed = speed; // Vertical speed
this.xSpeed = xSpeed; // Horizontal drift
this.sliced = false;
}
update() {
this.y -= this.speed;
this.x += this.xSpeed;
// If the fruit leaves the screen, reset it
if (this.y < -this.radius) {
this.reset();
}
}
display() {
fill(0, 255, 0);
ellipse(this.x, this.y, this.radius * 2);
}
isSliced(mx, my) {
// Check if the fruit is within slicing distance of the mouse
if (dist(mx, my, this.x, this.y) < this.radius) {
return true;
}
return false;
}
reset() {
// Reset the fruit at the bottom with a random speed and position
this.x = random(50, 350);
this.y = height + this.radius;
this.speed = random(3, 6);
this.xSpeed = random(-2, 2);
}
}
// Bomb class
class Bomb {
constructor(x, y, speed, xSpeed) {
this.x = x;
this.y = y;
this.radius = 30;
this.speed = speed; // Vertical speed
this.xSpeed = xSpeed; // Horizontal drift
}
update() {
this.y -= this.speed;
this.x += this.xSpeed;
// If the bomb leaves the screen, reset it
if (this.y < -this.radius) {
this.reset();
}
}
display() {
fill(255, 0, 0);
ellipse(this.x, this.y, this.radius * 2);
}
isSliced(mx, my) {
// Check if the bomb is within slicing distance of the mouse
if (dist(mx, my, this.x, this.y) < this.radius) {
return true;
}
return false;
}
reset() {
// Reset the bomb at the bottom with a random speed and position
this.x = random(50, 350);
this.y = height + this.radius;
this.speed = random(3, 6);
this.xSpeed = random(-2, 2);
}
}
// Slice class to simulate the sliced fruit effect
class Slice {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
this.leftX = x - r / 2;
this.rightX = x + r / 2;
this.ySpeed = random(1, 3);
this.angle = 0;
this.finished = false;
}
update() {
this.y += this.ySpeed;
this.angle += 0.1; // Add some rotation to each half
// When slices fall off the screen, mark them as finished
if (this.y > height + this.r) {
this.finished = true;
}
}
display() {
fill(255, 100, 100);
// Left half of the fruit
push();
translate(this.leftX, this.y);
rotate(-this.angle);
arc(0, 0, this.r, this.r, PI, 0, CHORD);
pop();
// Right half of the fruit
push();
translate(this.rightX, this.y);
rotate(this.angle);
arc(0, 0, this.r, this.r, 0, PI, CHORD);
pop();
}
}
function mouseDragged() {
// Track the slicing motion of the mouse
stroke(255, 0, 0);
strokeWeight(5);
line(pmouseX, pmouseY, mouseX, mouseY);
}