xxxxxxxxxx
63
let button;
function setup() {
createCanvas(600, 400);
// Create the "Generate New Landscape" button
button = createButton('Generate New Landscape');
button.position(10, 85);
button.mousePressed(drawLandscape); // Assign the callback function to generate a new landscape
drawLandscape(); // Initial landscape generation
}
function mouseClicked() {
save('landscape.jpg');
}
function drawLandscape() {
// Sky background
background("lightblue");
// Grass at the bottom
drawGrass(0, 350, width, 50);
// Generate random mountains
for (let i = 0; i < 5; i++) {
let mountainX = random(0, width);
let mountainHeight = random(100, 200);
drawMountain(mountainX, 400 - mountainHeight, mountainHeight);
}
// Generate random clouds
for (let i = 0; i < 10; i++) {
let cloudX = random(0, width);
let cloudY = random(50, 200);
drawCloud(cloudX, cloudY, random(50, 100));
}
}
function drawGrass(x, y, w, h) {
let grassColor = color(34, 139, 34); // A shade of green
fill(grassColor);
noStroke();
for (let i = x; i <= x + w; i += 10) {
let grassBaseHeight = y + h;
let grassTipHeight = grassBaseHeight - random(15, 30);
triangle(i - 10, grassBaseHeight, i + 10, grassBaseHeight, i, grassTipHeight);
}
}
function drawMountain(x, y, height) {
fill(100, 100, 100);
triangle(x - 50, y + height, x, y, x + 50, y + height);
}
function drawCloud(x, y, size) {
fill(255);
noStroke();
ellipse(x, y, size, size / 2);
ellipse(x - size / 4, y + size / 4, size / 1.5, size / 2);
ellipse(x + size / 4, y + size / 4, size / 1.5, size / 2);
}