xxxxxxxxxx
94
let fishX = [];
let fishDirection = [];
let cloudX = [];
let mountainX = [];
function setup() {
createCanvas(700, 500);
// Initialize fish
for (let i = 0; i < 5; i++) {
fishX.push(random(50, width - 50));
fishDirection.push(random([1, -1]));
}
// Initialize clouds and mountains
for (let i = 0; i < 3; i++) {
cloudX.push(random(i * 250, (i + 1) * 200)); // Clouds spaced by 200px
}
for (let i = 0; i < 2; i++) {
mountainX.push(random(width / 6, width / 2)); // First mountain (left side)
}
for (let i = 0; i < 2; i++) {
mountainX.push(random(width / 2 + 100, width)); // Second mountain (right side)
}
// Create button
var patternButton = createButton('Press');
patternButton.mousePressed(picture); // Call picture() when pressed
patternButton.size(150, 50);
patternButton.position(0, 500);
}
function water() {
background("lightblue");
fill("#3966a2");
noStroke();
rect(0, 300, width, 200);
}
function fish(x, y) {
fill(255, 204, 0);
noStroke();
ellipse(x, y + 100, 60, 30); // Fish body
fill(255, 165, 0);
triangle(x + 30, y + 100, x + 60, y + 85, x + 60, y + 115); // Fish tail
}
function mountain(x) {
fill("grey");
triangle(x, 50, x - 150, 300, x + 150, 300);
}
function drawCloud(x, y) {
fill(255);
noStroke();
ellipse(x, y, 100, 60); // Main cloud
ellipse(x - 40, y, 70, 50); // Left part
ellipse(x + 40, y, 70, 50); // Right part
}
function picture() {
water();
// Move and draw mountains
for (let i = 0; i < mountainX.length; i++) {
mountain(mountainX[i]);
mountainX[i] += random(-1, 50);
if (mountainX[i] > width + 50) {
mountainX[i] = -50;
}
}
// Fish movement
for (let i = 0; i < fishX.length; i++) {
fish(fishX[i], 300); // Keep fish swimming at y = 300
fishX[i] += random(10, 100) * fishDirection[i];
if (fishX[i] > width - 50 || fishX[i] < 50) {
fishDirection[i] *= -1; // Reverse direction when reaching edges
}
}
// Move and draw clouds
for (let i = 0; i < cloudX.length; i++) {
drawCloud(cloudX[i], random(100, 100));
cloudX[i] += random(-2, 50); // Movement for clouds
if (cloudX[i] > width + 50) {
cloudX[i] = -50; // Reset to the left side
}
}
}