https://d.pr/i/Z3yBZu/hufUkBcdg8
//declare variables
var groundPos_y;
var sunPos_y;
//variables for the tree
var treePos_x;
var treeTrunkHeight;
var treeTrunkWidth;
var treeRadius;
function setup()
{
createCanvas(512,512);
//initialise variables
treePos_x = 256;
treeTrunkHeight = 100;
treeRadius = 120;
treeTrunkWidth = 40;
groundPos_y = 400;
sunPos_y = 200;
}
function draw()
{
background(150,150,255);
if(sunPos_y >210){
background(150, 150, 255);
}
if(sunPos_y >220){
background(122, 124, 226);
}
if(sunPos_y >250){
background(93, 99, 198);
}
if(sunPos_y >260){
background(29, 53, 143);
}
if(sunPos_y >300){
background(0, 13, 92);
}
if(sunPos_y >450){
background(0, 3, 44);
fill(255);
ellipse(430, 100 ,100,100);
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
ellipse(random(5, 512), random(10, 400), random(1, 4));
}
//sun
noStroke();
fill(255,150,0);
ellipse(430, sunPos_y ,100,100);
sunPos_y = sunPos_y + 0.5;
//tree
stroke(0);
//trunk
fill(180,80,0);
ellipse(
treePos_x,
groundPos_y - treeTrunkHeight/2 + 10,
treeTrunkWidth,
treeTrunkHeight);
//leaves
fill(0,150,0);
ellipse(
treePos_x,
groundPos_y - treeTrunkHeight,
treeRadius,
treeRadius);
//cloud
if(sunPos_y > 450) {
noStroke();
fill(48, 50, 52);
ellipse(100,50,50,50);
ellipse(130,50,30,30);
ellipse(150,50,20,20);
}
else {
noStroke();
fill(255);
ellipse(100,50,50,50);
ellipse(130,50,30,30);
ellipse(150,50,20,20);
}
//ground
fill(200,130,0);
rect(0,groundPos_y,width,112);
//sun
if(sunPos_y > 700){
sunPos_y = 200;
}
}
For Reading
1. Extra events
p5 commands to study:
- keyReleased()
- mouseReleased()
- mouseMoved()
- mouseDragged()
- key
- keyCode
Appears in case 303
2. Operator shorthand
See if you can work out how to use these:
- +=
- -=
- /=
- *=
3. Math functions
p5 commands to study:
- random()
- min() and max()
- map()
- constrain()
Appears in case 303 – later stages
Leave a Reply