gondola moment

sketch
// jaden luscher
// jluscher@andrew.cmu.edu
// section a
// project 11

// this program draws a gondola ascending a mountain
// populated by random cabins on a snowy slope

var hillheight = [];
var noiseParam = 0;
var noiseStep = 0.01;
var gondola;
var house = [];   // array of houses
var newhouseProbability = 0.01; // likelihood of a new house popping up per frame

function preload() {
    gondola = loadImage("https://i.imgur.com/H4WC448.png");
    // gondola drawn in illustrator
}

function setup() {
    createCanvas(400, 400);
    frameRate(20);
    noStroke();

    // set up hill data
    for (i = 0; i <= width; i++) {
        var n = noise(noiseParam);
        var value = map(n, 0, 1, 50, 250);
        hillheight.push(value);
        noiseParam += noiseStep;
    }

    // first house
    var onehouse = makehouse();
    house.push(onehouse);
}


function draw() {
    background("orange");
    drawMoon();

    movehill();
    drawhill();
    drawslope();

    for (var i = 0; i < house.length; i++) {
        movehouse(house[i]);
        showhouse(house[i]);
    }
    if (house.length > 0 & house[0].y - house[0].rh > height) {
        // delete off-screen houses
        house.shift();
    }
    if (newhouseProbability > random(1.0)) {  // make new house randomly
        var newhouse = makehouse();
        house.push(newhouse);
    }

    push();
    stroke(100);
    strokeWeight(2);
    line(0, 260, width, 40);
    image(gondola, 170, 130, 100, 150);
    pop();
}


function showhouse(h) {  // house x, y, width, height, roof height
    push();
    fill(200, 150, 100);
    rect(h.x, h.y, h.w, h.h);
    beginShape();
    vertex(h.x - 2, h.y);
    vertex(h.x + (h.w/2), h.y - h.rh); // roof peak
    vertex(h.x + h.w + 2, h.y);
    endShape();
    fill(230, 180, 120);
    rect(h.x + h.w / 2 - 4, h.y + h.h, 8, -15); // door
    pop();
}

function movehouse(h) {
    h.x += h.dx;
    h.y += h.dy;
}


function makehouse() {
    var house = {x : width + 1,
                y : random(120, 300),
                w : random(15, 50),
                h : random(15, 50),
                rh : random(5, 20),
                dx : -1,
                dy : 0.55,
                show : showhouse}
    return house;
}


function movehill() {
    hillheight.shift();   // creates scrolling effect
    var n = noise(noiseParam);
    var value = map(n, 0, 1, 50, 250);
    hillheight.push(value);
    noiseParam += noiseStep;
}

// background hills
function drawhill() {
    push();
    fill("lightblue")
    beginShape();
    for (var i = 0; i < width +1; i++) {
        var x = i;
        vertex(x, hillheight[i]);
    }
    vertex(width, height);
    vertex(0, height);
    endShape(CLOSE);
    pop();
}

// foreground white slope
function drawslope() {
    push();
    fill(250);
    beginShape();
    vertex(width, height);
    vertex(0, height);
    vertex(0, 370);
    vertex(width, 150);
    endShape();
    pop();
}

function drawMoon() {
    fill(250);
    ellipse(100, 80, 50, 50);
    fill("orange");
    ellipse(105, 80, 45, 45);
}

Leave a Reply