sketch
var cloud = [];
var water = [];
var offset = 0;
mountain = [];
var inc = 0.01;
var xoff = 0;
function newWater(px, py, pw) {
var p = {x: px, y: py, w: pw,
right: watRight};
return p;
}
function watRight() {
return this.x + this.w;
}
function setup() {
createCanvas(480, 300);
for (var i = 0; i < 10; i++){
var rx = random(width);
cloud[i] = makeCloud(rx);
}
for (var i=0; i<10; i++) {
var rx = random(width);
}
var pl = newWater(0, 210, 60); water.push(pl);
for (var i=0; i<width/5+1; i++) {
var n = noise(xoff)
var value = map(n, 0, 1, 0, height);
xoff+=inc;
mountain.push(value);
}
frameRate(10);
}
function draw() {
background(220)
noStroke()
displayHorizon();
updateAndDisplaycloud();
removecloudThatHaveSlippedOutOfView();
addNewcloudWithSomeRandomProbability();
beginShape()
fill("blue")
vertex(0, height); for (var i=0; i<width/5; i++) {
vertex(i*6, mountain[i]) }
vertex(width, height) endShape();
mountain.shift();
mountain.push(map(noise(xoff), 0, 1, 0, 300));
xoff+= inc;
fill(235, 226, 160)
rect(0, 250, width, 200);
fill(98, 147, 204)
rect(0, 200, width, 50);
fill("blue");
noStroke(0);
for (var i = 0; i < water.length; i++) {
var p = water[i];
rect(p.x - offset, p.y, p.w, 10, 10);
}
if (water.length > 0 & water[0].right() < offset) {
water.shift();
}
var lastPlat = water[water.length-1];
if (lastPlat.right() - offset < width) {
var p = newWater(10+lastPlat.right(), random(210, 235), 60); water.push(p); }
offset += 1;
}
function updateAndDisplaycloud(){
for (var i = 0; i < cloud.length; i++){
cloud[i].move();
cloud[i].display();
}
}
function removecloudThatHaveSlippedOutOfView(){
var cloudToKeep = [];
for (var i = 0; i < cloud.length; i++){
if (cloud[i].x + cloud[i].breadth > 0) {
cloudToKeep.push(cloud[i]);
}
}
cloud = cloudToKeep;}
function addNewcloudWithSomeRandomProbability() {
var newCloudLikelihood = 0.007;
if (random(0,1) < newCloudLikelihood) {
cloud.push(makeCloud(width));
}
}
function cloudMove() {
this.x += this.speed;
}
function cloudDisplay() {
var floorHeight = 5;
var bHeight = 30;
var cHeight = 40;
fill(255);
noStroke()
push();
translate(this.x, height - 40);
rect(0, -bHeight-100, this.breadth, bHeight, 20);
rect(20, -bHeight-100, this.breadth, bHeight+10, 20);
rect(20, -bHeight-200, this.breadth, cHeight, 20);
stroke(200);
pop();
}
function makeCloud(birthLocationX) {
var bldg = {x: birthLocationX,
breadth: 60,
speed: -1.0,
nFloors: round(random(2, 7)),
move: cloudMove,
display: cloudDisplay}
return bldg;
}
function displayHorizon(){
stroke(0);
line (0,height-50, width, height-50);
}