SmileyFace constructor()
The constructor runs once for each new SmileyFace object. By using random() here, we ensure that every spawned face is unique in position, speed, and size—key to making the game feel dynamic and unpredictable.
constructor() {
this.x = random(width); // Random starting X position
this.y = -50; // Start above the canvas
this.speed = random(2, 5); // Random falling speed
this.size = random(30, 60); // Random size for the smiley face
this.caught = false; // Flag to check if the face has been caught
}
Line-by-line explanation (5 lines)
🔧 Subcomponents:
this.x = random(width);
Ensures each smiley face appears at a different horizontal position across the canvas
this.speed = random(2, 5);
Varies how fast each face falls, making the game less predictable
this.size = random(30, 60);
Makes some faces bigger targets than others, adding visual variety
this.x = random(width);- Generates a random number between 0 and the canvas width, placing this smiley face somewhere across the top
this.y = -50;- Sets the face to spawn 50 pixels ABOVE the top of the canvas so it falls into view
this.speed = random(2, 5);- Each face falls at a different speed (2-5 pixels per frame), so some are faster than others
this.size = random(30, 60);- Smiley faces have different diameters (30-60 pixels), making some easier and some harder to catch
this.caught = false;- Initializes a flag that tracks whether this face has been caught; used to prevent double-collisions