myoungsh-project-10-houses

sketch

var house = [];
var houseImg = [];
function preload() {
  houseImg[3] = loadImage("https://i.imgur.com/wsrsd27.png");
  houseImg[2] = loadImage("https://i.imgur.com/AG35goZ.png");
  houseImg[1] = loadImage("https://i.imgur.com/PkUvm1J.png");
}  
function setup() {
  createCanvas(480, 480);
  for (var i = 0; i < 1; i++){
    var x = random(0,width);
    house[i] = makeHouse(x);
  }
  frameRate(10);
}
function draw(){
  background(256);
  updateNdisplayHouses();
  newHouses();
}
function updateNdisplayHouses() {
  for (var i = 0; i < house.length; i ++){
    house[i].move();
    house[i].display();
  }
}
function newHouses() {
  var newHouseProb = .015;
  if (random(0,1) < newHouseProb) {
    house.push(makeHouse(width));
  }
}
function houseMove() {
  this.x += this.speed;
}
function houseDisplay() {
  push();
  translate(this.x, 0);
  // var imgNum = floor(random(1, 3.99));
  image(houseImg[1], 0, 0);
  image(houseImg[2], 220, 0);
  image(houseImg[3], 400, 0);
  noStroke();
  fill(0);
  rect(0, 400, 2000, 80);
  for (var i = 0; i < 2000; i += 48) {
    fill(256, 256, 0);
    rect(i, 435, 28, 10);
  }
  pop();
}
function makeHouse(startLocationX) {
  var house = {x: startLocationX,
              speed: -5,
              move: houseMove,
              display: houseDisplay}
  return house;
}

Leave a Reply