xxxxxxxxxx
71
let treeColors;
let grassColor;
let gridColor;
let pixelSize = 5; // define pixel size
function setup() {
createCanvas(1000, 1000);
noSmooth(); // keep the pixelation effect
// Define a color palette
treeColors = [
color(30, 150, 50), // dark green
color(50, 200, 70), // medium green
color(70, 255, 90) // light green
];
grassColor = color(200, 255, 200); // light green for the background
gridColor = color(170, 220, 170); // slightly darker green for grid lines
drawGrid();
// draw trees pixel by pixel
for (let i = 0; i < 3000; i++) {
let x = floor(random(width / pixelSize)); // random x position based on pixel size
let y = floor(random(10, height / pixelSize)); // random y position based on pixel size
if (random(1) > 0.5) {
drawDeciduousTree(x, y);
} else {
drawConiferousTree(x, y);
}
}
noLoop(); // stop the draw loop to keep the forest static
}
function drawGrid() {
background(grassColor);
stroke(gridColor);
strokeWeight(1); // thin lines for the grid
for (let i = 0; i < width; i += pixelSize) {
line(i, 0, i, height); // vertical lines
line(0, i, width, i); // horizontal lines
}
}
function drawDeciduousTree(x, y) {
// draw a deciduous tree pixel by pixel
// Trunk (2x3 brown pixels)
fill(100, 50, 20); // Brown color for trunk
rect(x * pixelSize + 1 * pixelSize, y * pixelSize + 2 * pixelSize, 2 * pixelSize, 3 * pixelSize); // Small rectangle for trunk
// Leaves (a blocky shape of green pixels)
fill(random(treeColors));
rect(x * pixelSize, y * pixelSize, 4 * pixelSize, 2 * pixelSize); // Top part of the leaves
rect(x * pixelSize - 1 * pixelSize, y * pixelSize + 1 * pixelSize, 6 * pixelSize, 1 * pixelSize); // Bottom part of the leaves
}
function drawConiferousTree(x, y) {
// Draw a coniferous tree pixel by pixel
// Trunk (2x3 brown pixels)
fill(100, 50, 20); // Brown color for trunk
rect(x * pixelSize + 2 * pixelSize, y * pixelSize + 3 * pixelSize, 1 * pixelSize, 3 * pixelSize); // Small rectangle for trunk
// Leaves (triangle-like shape of green pixels)
fill(random(treeColors));
rect(x * pixelSize + 1 * pixelSize, y * pixelSize, 3 * pixelSize, 1 * pixelSize); // Top part (1 pixel wide)
rect(x * pixelSize, y * pixelSize + 1 * pixelSize, 5 * pixelSize, 1 * pixelSize); // Middle part (3 pixels wide)
rect(x * pixelSize - 1 * pixelSize, y * pixelSize + 2 * pixelSize, 7 * pixelSize, 1 * pixelSize); // Bottom part (5 pixels wide)
}