xxxxxxxxxx
74
var trees = [];
function setup() {
createCanvas(500, 500);
// add trees to the arraylist.
trees.push(new tree(150,150,120));
trees.push(new tree(300,150,120));
trees.push(new tree(150,200,120));
trees.push(new tree(100,200,120));
trees.push(new tree(350,250,120));
}
function draw() {
//sizeTreeHead = sizeTreeHead+0.3;
if (mouseY < 300) {
background(194, 237, 255);
} else {
background(18, 0, 89);
}
noStroke();
drawBackDrop();
for (var i = 0; i < trees.length; i = i + 1) {
trees[i].draw();
}
}
function drawBackDrop()
{
// sun
fill(251, 255, 0);
ellipse(350, mouseY, 30, 30);
// grass
fill(18, 117, 0);
rect(0, 300, width, 200);
}
class tree {
constructor(x,y,size)
{
this.x = x;
this.y = y;
this.sizeTreeHead = size;
}
draw() {
//stem
fill(97, 71, 0);
rect(this.x, this.y, 30, 200);
// head of the tree
fill(34, 196, 6);
ellipse(this.x + 15, this.y, this.sizeTreeHead, this.sizeTreeHead);
//apples
fill(255, 0, 0);
for (var i = 0; i < 10; i = i + 1) {
ellipse(this.x - 40 + i * 12, this.y, 10, 10);
}
}
}