xxxxxxxxxx
29
class Apple {
constructor(type) {
// Sets initial X position of apple randomly on screen width
this.x = random(width);
// Sets initial Y position of apple at top of screen
this.y = 0;
// Default size for apples. Value is used for red apples.
this.size = 30;
this.type = type; // Determines the type of apple,
this.speed = 2; // Sets the falling speed of the apple.
if (this.type === "dirty") {
this.size = 60; // Increase the size for the rotten apple, making it easier to differentiate from clean ones.
}
}
display() {
// Decides which image to render based on the type of apple.
if (this.type === "clean") {
image(imgRedApple, this.x, this.y, this.size, this.size); // Displays a clean apple image.
} else {
image(imgRottenApple, this.x, this.y, this.size, this.size); // Displays a rotten (dirty) apple image.
}
}
move() {
this.y += this.speed; // Moves the apple down the screen at a constant speed.
}
}