Chicken()
Chicken() is a constructor function used like a class (JavaScript's older 'this' pattern instead of ES6 class syntax). It bundles the chicken's position, physics state, and its own show/update/jump/duck methods into a single reusable object created with `new Chicken()`.
🔬 This function only lets the chicken jump if isJumping is false. What happens if you remove the `if (!this.isJumping)` check entirely - can you now double-jump mid-air?
this.jump = function() {
if (!this.isJumping) {
this.vy = -15; // Higher jump
this.isJumping = true;
this.isDucking = false; // Can't duck while jumping
}
};
function Chicken() {
this.x = 50;
this.y = groundY - 40; // Top-left of the bounding box
this.w = 30; // Effective width for standing bounding box
this.h = 40; // Effective height for standing bounding box
this.vy = 0; // Vertical velocity
this.gravity = 1;
this.isJumping = false;
this.isDucking = false;
this.show = function() {
fill(255, 200, 0); // Yellow for chicken body
noStroke();
if (this.isDucking) {
// Ducking chicken shape
// Body (squashed oval)
ellipse(this.x + this.w * 0.75, this.y + this.h * 0.75, this.w * 1.5, this.h * 0.5);
// Head (slightly lower)
ellipse(this.x + this.w * 1.2, this.y + this.h * 0.7, this.w / 2, this.h / 2);
// Beak
fill(255, 100, 0);
triangle(this.x + this.w * 1.2 + this.w / 4, this.y + this.h * 0.7,
this.x + this.w * 1.2 + this.w / 4, this.y + this.h * 0.7 + this.h / 8,
this.x + this.w * 1.2 + this.w / 2, this.y + this.h * 0.7 + this.h / 16);
// Legs (shorter)
stroke(150, 75, 0); // Brown for legs
strokeWeight(2);
line(this.x + this.w * 0.75 - 5, groundY, this.x + this.w * 0.75 - 5, groundY + 5);
line(this.x + this.w * 0.75 + 5, groundY, this.x + this.w * 0.75 + 5, groundY + 5);
} else {
// Standing chicken shape
// Body (oval)
ellipse(this.x + this.w / 2, this.y + this.h / 2, this.w, this.h);
// Head (smaller circle)
ellipse(this.x + this.w, this.y + this.h / 4, this.w / 2, this.h / 2);
// Beak (triangle)
fill(255, 100, 0); // Orange for beak
triangle(this.x + this.w + this.w / 4, this.y + this.h / 4,
this.x + this.w + this.w / 4, this.y + this.h / 4 + this.h / 8,
this.x + this.w + this.w / 2, this.y + this.h / 4 + this.h / 16);
// Comb (small triangles)
fill(200, 0, 0); // Red for comb
triangle(this.x + this.w, this.y, this.x + this.w + 5, this.y - 5, this.x + this.w + 10, this.y);
// Legs (lines)
stroke(150, 75, 0); // Brown for legs
strokeWeight(2);
line(this.x + this.w / 2 - 5, groundY, this.x + this.w / 2 - 5, groundY + 10);
line(this.x + this.w / 2 + 5, groundY, this.x + this.w / 2 + 5, groundY + 10);
}
};
this.jump = function() {
if (!this.isJumping) {
this.vy = -15; // Higher jump
this.isJumping = true;
this.isDucking = false; // Can't duck while jumping
}
};
this.duck = function() {
if (!this.isJumping) {
this.isDucking = true;
}
};
this.stand = function() {
this.isDucking = false;
};
this.update = function() {
// Apply gravity
this.y += this.vy;
this.vy += this.gravity;
// Prevent going below ground
this.y = constrain(this.y, 0, groundY - this.h);
// If on ground and not ducking, reset jumping state
if (this.y >= groundY - this.h && !this.isDucking) {
this.vy = 0;
this.isJumping = false;
}
// If on ground and ducking, reset jumping state
else if (this.y >= groundY - this.h && this.isDucking) {
this.vy = 0;
this.isJumping = false;
this.y = groundY - this.h / 2; // Chicken is half height when ducking
}
};
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
if (this.isDucking) { ... } else { ... }
Draws a squashed, wider shape when ducking and a taller upright shape when standing
if (!this.isJumping) {
Only launches the chicken upward if it isn't already mid-air
if (this.y >= groundY - this.h && !this.isDucking) {
Resets velocity and jumping flag once the chicken touches the ground, handling ducking and standing separately
this.x = 50;- The chicken's horizontal position never changes - only obstacles move, creating the illusion the chicken is running.
this.y = groundY - 40;- Places the chicken standing on the ground, since y is measured from the top of its bounding box.
this.gravity = 1;- How strongly the chicken accelerates downward each frame after jumping.
this.isJumping = false;- Tracks whether the chicken is currently airborne so it can't jump again mid-air.
this.vy = -15; // Higher jump- A negative vertical velocity launches the chicken upward; gravity will pull it back down over subsequent frames.
this.y += this.vy;- Moves the chicken by its current vertical velocity - this is the actual motion each frame.
this.vy += this.gravity;- Gravity constantly increases vy, so upward motion slows, stops, then turns into falling - classic projectile motion.
this.y = constrain(this.y, 0, groundY - this.h);- Clamps the chicken's position so it never flies above the canvas top or sinks through the ground.
this.y = groundY - this.h / 2; // Chicken is half height when ducking- When ducking on the ground, the chicken's y is adjusted upward slightly since its drawn height shrinks.