xxxxxxxxxx
142
class Obstacle {
constructor(x, y, w, h, img) {
this.x = x;
this.y = y;
this.width = w;
this.height =h;
this.img = img;
}
display() {
push()
//scale(0.4, 0.4);
translate(0,0)
image(this.img, this.x, this.y, this.width, this.height);
translate(800,0)
image(this.img, this.x, this.y, this.width, this.height);
translate(1500,0)
image(this.img, this.x, this.y, this.width, this.height);
translate(450,-200)
image(this.img, this.x, this.y, this.width, this.height);
translate(1500,200)
image(this.img, this.x, this.y, this.width, this.height);
translate(450,-200)
image(this.img, this.x, this.y, this.width, this.height);
translate(450,200)
image(this.img, this.x, this.y, this.width, this.height);
translate(450,-200)
image(this.img, this.x, this.y, this.width, this.height);
translate(2500,200)
image(this.img, this.x, this.y, this.width, this.height);
translate(900,0)
image(this.img, this.x, this.y, this.width, this.height);
translate(1800,-200)
image(this.img, this.x, this.y, this.width, this.height);
translate(450,200)
image(this.img, this.x, this.y, this.width, this.height);
pop()
stroke(255,0,0)
// rect(this.x,this.y,this.width,this.height)
}
}
class Duck {
constructor(x, y, w, h, img) {
this.x = x;
this.y = y;
this.initialY = y;
this.img = img;
this.width = w ; // Adjusted width based on image scaling
this.height = h; // Adjusted height based on image scaling
this.velocity = 0;
this.gravity = 10;
this.jumpForce = -50;
this.maxJumps = 103;
this.jumpCount = 0;
this.isJumping = false;
}
display() {
push();
image(this.img,this.x,this.y,this.width,this.height);
pop();
}
gravityJump() {
if (this.isJumping) {
this.velocity += this.gravity;
this.y += this.velocity;
if (this.y >= this.initialY) {
this.y = this.initialY;
this.velocity = 0;
this.jumpCount = 0;
this.isJumping = false;
}
}
}
jump() {
if (!this.isJumping && this.jumpCount < this.maxJumps) {
this.velocity = this.jumpForce;
this.jumpCount++;
this.isJumping = true;
} else if (this.isJumping && this.jumpCount < this.maxJumps) {
this.velocity = this.jumpForce;
this.jumpCount++;
}
}
checkCollision(obstacle) {
// Define bounding box coordinates for the duck
let duckTop = this.y;
let duckLeft = this.x;
let duckBottom = this.y + this.height;
let duckRight = this.x + this.width;
// Define bounding box coordinates for the obstacle
let obstacleTop = obstacle.y;
let obstacleLeft = obstacle.x;
let obstacleBottom = obstacle.y + obstacle.height;
let obstacleRight = obstacle.x + obstacle.width;
// Check for collision
if (
duckRight >= obstacleLeft && // Duck's right edge intersects with obstacle's left edge
duckLeft <= obstacleRight && // Duck's left edge intersects with obstacle's right edge
duckBottom >= obstacleTop && // Duck's bottom edge intersects with obstacle's top edge
duckTop <= obstacleBottom // Duck's top edge intersects with obstacle's bottom edge
) {
// Collision detected
if (duckBottom >= obstacleTop && duckTop <= obstacleTop + 5) {
// Duck can jump on top of the obstacle
this.velocity = 0;
this.y = obstacleTop - this.height;
this.jumpCount = 0;
this.isJumping = false;
} else if (duckLeft <= obstacleRight && duckRight >= obstacleRight) {
// Duck collides with the side of the obstacle
gameOver();
}
}
}
}