xxxxxxxxxx
125
let pigeons = [];
let food;
let pigeonImg, foodImg, treeImg, leafImg;
let leaves = [];
let treePositions = [];
function preload() {
// Load an image of a pigeon, food, tree, and leaf
pigeonImg = loadImage('pigeon.png');
foodImg = loadImage('food.png');
treeImg = loadImage('tree.png');
leafImg = loadImage('leaf.png');
}
function setup() {
createCanvas(800, 600);
// Create multiple pigeons with random starting positions
for (let i = 0; i < 5; i++) {
pigeons.push({
position: createVector(random(width), random(height - 100)),
velocity: createVector(0, 0),
acceleration: createVector(0, 0),
state: 'walking', // Can be 'walking' or 'pecking'
peckTime: 0 // Timer to control how long they peck
});
}
// Generate the first piece of food at a random location
generateNewFood();
// Add positions for two trees on the left and right sides of the canvas
treePositions.push(createVector(100, height - 250));
treePositions.push(createVector(width - 200, height - 250));
}
function draw() {
background(150); // Gray canvas representing the ground
// Draw the food item and make it larger
imageMode(CENTER);
image(foodImg, food.position.x, food.position.y, 60, 60); // Larger food image
// Draw the trees on the left and right sides of the canvas
for (let i = 0; i < treePositions.length; i++) {
let treePos = treePositions[i];
image(treeImg, treePos.x, treePos.y, 150, 300); // Draw the tree image
}
// Update and draw falling leaves
for (let i = leaves.length - 1; i >= 0; i--) {
let leaf = leaves[i];
leaf.position.add(leaf.velocity); // Update leaf position
// Draw the leaf image
imageMode(CENTER);
image(leafImg, leaf.position.x, leaf.position.y, 30, 30); // Leaf size
// Remove the leaf if it touches the ground
if (leaf.position.y > height - 50) {
leaves.splice(i, 1); // Remove leaf from the array
}
}
// Update and draw pigeon movement
for (let i = 0; i < pigeons.length; i++) {
let pigeon = pigeons[i];
// If pigeon is walking, move toward the food
if (pigeon.state === 'walking') {
// Calculate direction to the food and apply it to the pigeon
let direction = p5.Vector.sub(food.position, pigeon.position);
direction.setMag(0.05); // Slow walking speed
pigeon.acceleration = direction;
pigeon.velocity.add(pigeon.acceleration);
pigeon.velocity.limit(1); // Limit speed for realistic walking
pigeon.position.add(pigeon.velocity); // Update pigeon position
// If the pigeon reaches the food, start pecking
if (p5.Vector.dist(pigeon.position, food.position) < 20) {
pigeon.state = 'pecking';
pigeon.peckTime = millis(); // Set the pecking timer
generateNewFood(); // Generate new food once the pigeon eats it
}
} else if (pigeon.state === 'pecking') {
// Stop the pigeon for pecking behavior
pigeon.velocity.mult(0); // Stop walking
// After 2 seconds of pecking, resume walking
if (millis() - pigeon.peckTime > 2000) {
pigeon.state = 'walking'; // Switch back to walking
}
}
// Draw the pigeon image
imageMode(CENTER);
image(pigeonImg, pigeon.position.x, pigeon.position.y, 60, 60); // Larger pigeon image
}
}
// Function to generate a new food position at random
function generateNewFood() {
food = {
position: createVector(random(width), random(height - 100)) // Random food location
};
}
// Function to handle mouse clicks and trigger falling leaves
function mousePressed() {
for (let i = 0; i < treePositions.length; i++) {
let treePos = treePositions[i];
// Check if the mouse click is within the tree area (using distance)
if (dist(mouseX, mouseY, treePos.x, treePos.y) < 75) {
// Generate 10 falling leaves from the clicked tree
for (let j = 0; j < 10; j++) {
leaves.push({
position: createVector(treePos.x + random(-20, 20), treePos.y),
velocity: createVector(random(-1, 1), random(1, 3)) // Random velocity for falling
});
}
}
}
}