Project – 11

Cars
//Shaun Murray, shaunmur
//Section B

var cars = [];


function setup() {
    createCanvas(300, 200); 
    
    // create an initial collection of cars
    for (var i = 0; i < 15; i++){
        var rx = random(width);
        cars[i] = makeCars(rx);
    }
    frameRate(12);
}


function draw() {
    background(220);
	noStroke();
	fill('Orange');
    rect(0, 0, 300, 120);
    fill('Yellow');
    circle(40, 110, 40);
    fill(248, 240, 164);
    rect(0, 120, 300, 140);
	line(20, 200, 100, 65);
	line(280, 200, 200, 65);
    displayHorizon();
    push();
    strokeWeight(8);
	stroke(0, 150, 0);
	line(width-75, height-60, width-75, height-73);
	strokeWeight(4);
	line(width-82, height-67, width-75, height-67);
	line(width-82, height-67, width-82, height-70);
	pop();
    updateAndDisplayCars();
    removeCarsThatHaveSlippedOutOfView();
    addNewCarsWithSomeRandomProbability(); 
}


function updateAndDisplayCars(){
    // Update the car's positions, and display them.
    for (var i = 0; i < cars.length; i++){
        cars[i].move();
        cars[i].display();
    }
}


function removeCarsThatHaveSlippedOutOfView(){
    var carsToKeep = [];
    for (var i = 0; i < cars.length; i++){
        if (cars[i].x + cars[i].breadth > 0) {
            carsToKeep.push(cars[i]);
        }
    }
    cars = carsToKeep; // remember the surviving cars
}


function addNewCarsWithSomeRandomProbability() {
    // With a very tiny probability, add a new car to the end.
    var newCarLikelihood = 0.03; 
    if (random(0,1) < newCarLikelihood) {
        cars.push(makeCars(width));
    }
}


// method to update position of car every frame
function carsMove() {
    this.x += this.speed;
}
    

// draw the car and some wheels
function carDisplay() {
    var floorHeight = 20;
    var bHeight = this.nFloors * floorHeight; 
    fill(255); 
    stroke(0); 
    push();
    translate(this.x, height - floor(random(30, 40)));
    //rect(0, -bHeight, this.breadth, bHeight);
    fill(255);
	circle(6, 6, 4);//Wheels
	circle(-6, 6, 4);
	fill('Red');
	beginShape();//body
	vertex(-9, -5);
	vertex(-10, 5);
	vertex(10, 5);
	vertex(9, -5);
	endShape(CLOSE);
	fill('cyan');
	rect(-9, -4, 4, 4);//windshield
    stroke(200); 
    pop();
}


function makeCars(birthLocationX) {
    var crs = {x: birthLocationX,
                breadth: 50,
                speed: -1.0,
                nFloors: round(random(1,1)),
                move: carsMove,
                display: carDisplay}
    return crs;
}


function displayHorizon(){
    stroke(0);
    fill(115,69,19);
    rect(0,height-50, 300, 30)
    line (0,height-50, width, height-50); 
    line (0,height-20, width, height-20);
}

function car() {
	push();
	translate(x, y);
	fill(255);
	circle(6, 6, 4);//Wheels
	circle(-6, 6, 4);
	fill('Red');
	beginShape();//body
	vertex(-9, -5);
	vertex(-10, 5);
	vertex(10, 5);
	vertex(9, -5);
	endShape(CLOSE);
	fill('cyan');
	rect(-9, -4, 4, 4);//windshield
	pop();
}

Cars on a bumpy desert road.

Project 11: Landscape Story

sketch – Copy
//Rachel Kim
//15-104(Section C)
//rachelki@andrew.cmu.edu
//Project 11


//global variables
var fish = [];

//background variables 
var mountainShapeA= 0.035;
var mountainSpeedA = 0.0009;

var mountainShapeB = 0.0175;
var mountainSpeedB = 0.0005;

var waterShape = 0.003;
var waterSpeed = 0.00005;


function setup() {
    
    createCanvas(480, 480);
    frameRate(10);

    //number of fish
    for (var j = 0; j < 30; j++) {

        fishX = random(width);
        fishY = random(350, 450);
        fish[j] = makeFish(fishX, fishY);

    }

}

function draw(){

    background(255, 225, 225);
    sun();
    mountainB();
    mountainA();
    boatPeople();
    water();
    drawFishies();

}

//LANDSCAPE BKGD ---MOUNTAINS, SUN, WATER
function mountainA(){
    noStroke();
    fill(255, 241, 224);
    beginShape();
    
    for (var i = 0; i < width; i ++){
        var x = (i * mountainShapeA) + (millis() * mountainSpeedA);
        var y = map(noise(x), 0, 1.2, 150, 250);
        vertex(i, y);
    }
    
    vertex(width, height);
    vertex(0, height);
    endShape();
}

function mountainB(){
    noStroke();
    fill(255, 251, 240);
    beginShape();
    
    for (var i = 0; i < width; i ++){
        var x = (i * mountainShapeB) + (millis() * mountainSpeedB);
        var y = map(noise(x), 0, 1.5, 50, 300);
        vertex(i, y);
    }
    
    vertex(width, height);
    vertex(0, height);
    endShape();
}

function sun(){

    noStroke();
    fill(255, 210, 210);
    ellipse(240, 180, 450, 450);
    fill(255, 195, 195);
    ellipse(240, 180, 400, 400);
    fill(255, 187.5, 187.5);
    ellipse(240, 180, 350, 350);
    fill(255, 183, 183);
    ellipse(240, 180, 300, 300);
    fill(255, 175, 175);
    ellipse(240, 180, 250, 250);
    fill(255, 157, 157);
    ellipse(240, 180, 200, 200);
    fill(255, 135, 135);
    ellipse(240, 180, 150, 150);
    fill(255, 123, 123);
    ellipse(240, 180, 100, 100);

}


function water(){

    noStroke();
    fill(255, 218, 203);
    beginShape();
    
    for (var i = 0; i < width; i ++){
        var x = (i * waterShape) + (millis() * waterSpeed);
        var y = map(noise(x), 0, 1.5, 320, 240);
        vertex(i, y);
    }

    vertex(width, height);
    vertex(0, height);
    endShape();

    noStroke();
    fill(255, 228, 213);
    beginShape();
    
    for (var i = 0; i < width; i ++){
        var x = (i * waterShape) + (millis() * waterSpeed);
        var y = map(noise(x), 0, 1.5, 320, 250);
        vertex(i, y);
    }

    vertex(width, height);
    vertex(0, height);
    endShape();
}

//TRAVELLERS
function boatPeople(){

	noStroke();
	fill(217, 154, 144);
    ellipse(330,280, 5, 20);
    ellipse(330, 270, 5, 5);
    ellipse(300,280, 5, 20);
    ellipse(300, 270, 5, 5);
    ellipse(270,280, 5, 20);
    ellipse(270, 270, 5, 5);
    ellipse(250,280, 5, 20);
    ellipse(250, 270, 5, 5);
    ellipse(220,280, 5, 20);
    ellipse(220, 270, 5, 5);
    ellipse(200,280, 5, 20);
    ellipse(200, 270, 5, 5);
    ellipse(175,280, 5, 20);
    ellipse(175, 270, 5, 5);

    noStroke();
    fill(239, 168, 158);
    triangle(258, 250, 140, 250, 258, 100);
    triangle(262, 250, 375, 250, 262, 100);

	strokeWeight(1.5);
	stroke(239, 168, 158);
	line(260, 100, 260, 300);//flag pole

    noStroke();
    fill(239, 168, 158);//color of boat
    arc(255,280, 200, 150, 0, PI); //boat
}

//FISH DETAILS
function drawFish() {
    var tailLength = 7;

    noStroke();
    fill(this.fishColor);
    ellipse(this.fx, this.fy, 12, 6);
    triangle(this.fx + (tailLength / 2.5), this.fy, this.fx + tailLength, this.fy - 2, this.fx + tailLength, this.fy + 2);
}

function makeFish(fishX, fishY) {//fish as object 
    
    var fish = {
    	fishColor: color(random(150,200), 100, 110),
    	fishSpeed: random(-1, -10),
    	fishAction: fishMotion,
        fx: fishX,
        fy: fishY,
        fishMaker: drawFish
    }

    return fish;
}

function fishMotion() { //movement of fish
    this.fx += this.fishSpeed;
    if (this.fx <= -20) {
        this.fx += width;
    }
}

function drawFishies() { //show onto canvas 
    for (i = 0; i < fish.length; i++) {
        fish[i].fishAction();
        fish[i].fishMaker();
    }

}



For this project, I wanted to focus on different aspects that are within a landscape. It was fun to see how many factors make up an environment. With the usage of different topics/functions we learn in labs, I was able to create mountains and water as well as a sea of fish. Lastly, a boat full of people were added to the landscape to show that it is a continuous journey.

Project 11: Landscape

sketchDownload
 /*
Nicholas Wong
Section A
nwong1@andrew.cmu.edu
*/

var rockForeground = []; //Rocks in foreground
var rockMidground = []; //Rocks in midground
var mountains = []; //Mountains in background

//Rock variables
var foregroundRockChance = 0.03; //Probability of rock spawn per frame
var midgroundRockChance = 0.03; //Probability of rock spawn per frame

//Mountain variables
var noiseParam = 1; //Noise param for mountain generation
var noiseStep = 0.01; //Noise step for mountain generation

//Sky variables
var offset = 23; //Wave offset

//Floor grid variables
var lineStart = []; //Line at horizon
var lineEnd = []; //Line at bottom of canvas

function setup() 
{
	frameRate(30);
    createCanvas(480, 300);

    //Initialize mountains
	for(let i = 0; i < width/4; i++)
	{
		var n = noise(noiseParam);
		mountains[i];
		var value = map(n, 0, 1, 0, height);
		mountains.push(value);
		noiseParam += noiseStep;
	}

	//Initialize Floor lines
	for(let i = 0; i<49; i++)
	{
		lineStart[i] = 10*i;
		lineEnd[i] = -1200 + (60*i);
	}

}

function draw() 
{

	background(255, 166, 200);

	//Draw sky wave
	drawSkyPattern();

	//Draw mountains
	drawMountains();

	//Draw floor
	drawFloor();

	//Draw floor lines
	drawFloorLines();


	//Color for rocks
	var foregroundColor = color(115, 31, 77);
	var midgroundColor = color(130, 57, 84);

	//Create rocks based on probability
	if(random(0,1) < foregroundRockChance)
	{
		rockForeground.push(createRock(width + 20,220 + random(0,20),20,random(20,30),foregroundColor)); //Rocks in foreground
	}
	else if (random(0,1) < midgroundRockChance)
	{
		rockMidground.push(createRock(width + 20, 190 + random(-20,20),10, 20, midgroundColor)); //Rocks in background
	}

	//Update positions
	updateMidgroundRocks();
	updateForegroundRocks();
}

//Draw wave
function drawSkyPattern()
{
	push();
	noFill();
	for(let w = 0; w <= height; w += 10) //Number of waves
    {       
        strokeWeight(1)
        stroke(255)

        //Create curvy lines
        beginShape();
		for(let x = 0; x < width; x++)
		{
		    let angle = offset + x *0.01;
		    let y = map(sin(angle), -1, 1, 0, 50);
		    vertex(x,y + w);
		}
		vertex(width,height);
		vertex(0,height);
		endShape();
    }
    pop();


}

//Draw mountains
function drawMountains()
{
	//Add and remove noise values to mountain list
	mountains.shift();
    var n = noise(noiseParam);
    var value = map(n, 0, 1, 0, height);
    noiseParam += noiseStep;
   	mountains.push(value);
	

	//Draw mountain shapes based on noise values
    beginShape();
    for(var i = 0; i <= width/4; i++)
    {
	    fill(224, 105, 149);
	    noStroke();
	    vertex((i * 6), mountains[i] - 50); 
	    vertex((i + 1) * 6, mountains[i + 1] - 50);   
	    vertex(300, 100000); 
    }
    endShape(CLOSE);

	//Draw mountain shapes based on noise values
    beginShape();
    for(var i = 0; i <= width/3; i++)
    {
	    fill(191, 86, 125);
	    noStroke();
	    vertex((i * 10), mountains[i]- 10); 
	    vertex((i + 1) * 10, mountains[i + 1] - 10);   
	    vertex(300, 100000); 
    }
    endShape(CLOSE);
}

//Update and cull rocks
function updateForegroundRocks()
{
	for(let i = 0; i < rockForeground.length; i++)
	{
		rockForeground[i].move();
		rockForeground[i].display();

		//Check to see if rocks are off canvas
		if(rockForeground[i].x + rockForeground[i].size < 0)
		{
			rockForeground.shift(); //Remove rock
		}
	}
}

//Update and cull rocks
function updateMidgroundRocks()
{
	//Check to 
	for(let i = 0; i < rockMidground.length; i++)
	{
		rockMidground[i].move();
		rockMidground[i].display();

		//Check to see if rocks are off canvas
		if(rockMidground[i].x + rockMidground[i].size < 0)
		{
			rockMidground.shift(); //Remove rock
		}
	}
}

//Draw floor lines
function drawFloorLines()
{
	push();
	for(let i = 0; i < 49; i++)
	{
		stroke(255, 148, 194);
		strokeWeight(1);

		//Draw line
		line(lineStart[i],170,lineEnd[i],height);

		//Move lines
		lineStart[i] -= 5;
		lineEnd[i] -= 30;

		//Reset lines if off canvas
		if(lineStart[i] == 0)
		{
			lineStart[i] = width;
		}
		if(lineEnd[i] == -1200)
		{
			lineEnd[i] = 1680;
		}
	}
	pop();
}

//Draw floor rectangle
function drawFloor()
{	
	push();
	noStroke();
	fill(158, 70, 102);
	rect(0,170,width,height);
	pop();
}



//Create rock
function createRock(xPos, yPos,rockSpeed, rockSize, rockColor, )
{
	var fRock = {x: xPos, 
				y: yPos,
				speed: rockSpeed * -1,
				sides: random(3,5),
				size: rockSize,
				color: rockColor,
				variation: random(0.5,1),
				move: rockMove,
				display: rockDisplay}
	return fRock;
}

function rockMove()
{
	this.x += this.speed; //Move the rock
}

function rockDisplay()
{
	//Change rock color
	fill(this.color);
	noStroke();

	//Create rock based on size, variation, and number of sides
	let angle = TWO_PI / this.sides;
	beginShape();
	for (let a = 0; a < TWO_PI; a += angle) 
	{
	let sx = this.x + cos(a * this.variation) * this.size;
	let sy = this.y + sin(a * this.variation) * this.size;
	vertex(sx, sy);
	}
	endShape(CLOSE);
}

A stylized desert. Rocks are objects that are randomly generated, and are automatically culled when off the canvas. Rocks further away from the bottom of the canvas move slower than rocks closer to the bottom of the canvas. Mountains vary in speed to give the illusion of depth.

Inspiration:

Mountains eps 10 background view pink Royalty Free Vector

Project 11- Landscape

sketchDownload




//Shruti Prasanth
//Section C
//Project 11


var move = 5; 
var cx = 100; //starting location of clouds
var cy = 100; 
var clouds = []; 
var terrainSpeed = 0.0005; 
var terrainDetail = 0.005; 

function setup() {

    createCanvas(480, 480); 
    frameRate(4); 

    //initial display of clouds  
    for(var i = 0; i < 3; i++){
        var cX = random(width); 
        var cY = random(75, 150); 
        clouds[i] = makeClouds(cX, cY); 

    }

}
   
function draw() {
    background(154,73,147);

    //sun
    noStroke(); 
    fill(236,210,108);
    ellipse(width / 3, height / 3, 125, 125);  

    var mountain1 = mountains("#purple", height * 0.20, height * 0.65, 4); //backmost mountain
    var mountain2 = mountains("darkgrey", height * 0.37, height * 0.65, 2); // middle ground mountains 
    var mountain3 = mountains("grey", height * 0.50, height * 0.65, 3); //foreground mountains 


    //ocean 
    noStroke(); 
    fill(137,172,315); 
    rect(0, height * 0.65, width, height * 0.35); 

    //sun reflection 
    noStroke(); 
    fill(250, 103, 71, 100); 
    arc(width / 3, height * 0.65, 125, 125, 0, PI, OPEN);


    update(); 
    removeClouds(); 
    addCloud();   

}

function update(){ //update all the clouds 
    for(var i = 0; i < clouds.length; i++){
        clouds[i].move(); 
        clouds[i].display(); 
    }
}

function removeClouds(){ //checks to see where clouds are and changes positions accordingly
    var cloudKeep = []; 
    for(var i = 0; i < clouds.length; i++){
        if(clouds[i].x + 25 > 0){ // cloud is 50 wide, so 25 is point from center
            cloudKeep.push(clouds[i]); 
        }
    }
    clouds = cloudKeep; //keep only clouds still on screen
}

function addCloud(){ //randomly adds clouds at intervals 
    var newCloud = 0.03; 
    if(random(0,1) < newCloud){
        var cloudY = random(75, 150); 
        clouds.push(makeClouds(480, cloudY)); 
    }
}

function cloudMove(){ // move the clouds 
    this.x += this.speed; 
}

function cloudDisplay(){ //draw the clouds 
    fill(255); //white
    noStroke(); 
    ellipse(this.x, this.y, 90, 15); 
    ellipse(this.x - 25, this.y - 10, 35, 30);
    ellipse(this.x, this.y - 20, 40, 40);
    ellipse(this.x + 20, this.y - 15, 35, 30);  

}

function makeClouds(cloudLocX, cloudLocY){ //cloud object definition 
    var cloud = { x: cloudLocX,
                y: cloudLocY,
                speed: -5, 
                move: cloudMove, 
                display: cloudDisplay}

    return cloud; 
} 
function mountains(color, min, max, noiseS) {

    noStroke(); 
    fill(color); 
    noiseSeed(noiseS); 

    beginShape(); 
    for(var x = 0; x < width; x++){
        var t = (x * terrainDetail + (millis() * terrainSpeed)); 
        var y = map(noise(t), 0, 1, min, max); 
        vertex(x, y); 
    }
    vertex(width, height); 
    vertex(0, height); 
    endShape(); 




}

For this project, I wanted to play with complimentary colors like purple and yellow and create a different version of a sunset landscape. The clouds are moving and the snow peaked mountains fluctuate in peaks.

Project 11

sketchDownload
var hills = [];
var noiseParam = 0;
var noiseStep = 0.03;
var xTree = [];
var yTree = [];
var car;

function preload() {
    car = loadImage("https://i.imgur.com/dk31naF.png");
}

function setup() {
    createCanvas(400, 250);
    for (var i = 0; i <= width/5; i ++) {
        var n = noise(noiseParam);
        var value = map(n, 0, 1, 0 ,height);
        print(n);
        hills.push(value);
        noiseParam += noiseStep;
    }
    frameRate(10);
    imageMode(CENTER);
}

function draw() {
    background(0, 59, 107); //dark sky
    hills.shift();
    var n = noise(noiseParam);
    var value = map(n, 0, 1, 0 ,height);
    hills.push(value);
    noiseParam += noiseStep;
    for (var i = 0; i < width/5; i ++) {
        var x = i*5;
        noStroke();
        fill(22, 105, 120);
        beginShape(); //drawing the hills
        vertex(x, height);
        vertex(x, hills[i]); 
        vertex(x+5, hills[i+1]);
        vertex(x+5, height);
        endShape();
        //draw tree if hillValue[i] is smaller than the points before & after
        if (hills[i] < hills[i+1] && hills[i] < hills[i-1]) {
            tree(x, hills[i]);
        }
    }
    //insert car image
    image(car, width/2, height/2, 450, 300);

}
function tree(x, hills) {
    fill(1, 120, 33)
    triangle(random(x-2, x-5), hills*1.2, random(x, x+3), 
    hills*0.8, x+5, hills*1.2);
    stroke(46, 19, 19);
    strokeWeight(2);
    line(x, hills*1.2, x, hills*1.5); 
}

This animation shows a baby sitting inside a car in the middle of a long drive at night, staring out into the mountains.

Project 11: Space Rocket

I had a rough week and was unable to complete this code 🙁 It was supposed to be a rocket traveling through space with stars generating in the background and asteroids randomly generating to travel parallel to the ship to give the impression of speed. The two static objects were the rocket and the earth. I drew them myself, trying to get a laugh. But since the code doesn’t work, it just seems overwhelmingly pathetic.

sketchDownload
//jubbies

var stars = [];
var asteroids = [];

var rocket;

var counter = 0;


function preload() { 
    rocket = loadImage("https://i.imgur.com/YMS5MRv.png");
    earth = loadImage("https://i.imgur.com/7N4avKI.png");
} 

function setup() {
    createCanvas(400, 250);
    frameRate(12);

    //setting up the stars - priming the array
    for (var i = 0; i < 140; i++) {
        var sx = random(width);
        var sy = random(height);
        var sWidth = random(20);
        stars[i] = makeStars(rx, ry, 2, sWidth);
    }
    //setting up the asteroids - priming the array
    for (var i = 0; i < 4; i++) {
        var rx = random(width);
        var ry = random(150);
        asteroids[i] = makeAsteroids(rx, ry);
    }
    
}

function draw() {
    background('black');

    //EARTH (BEHIND ALL)
    image(earth, 270, 40, 50, 50);
  
    //STARS
    updateAndDisplayStars();
    removeStarsOutOfView();
    addNewStars();

    //ASTEROIDS
    updateAndDisplayAsteroid();
    removeAsteroidsOutOfView();
    addNewAsteroids();
    
    //ROCKET
    image(rocket, 30, 140, 150, 80);
    
    counter++;
}

//STARS- ALL FUNCTIONS
function updateAndDisplayStars(){
    for (var i = 0; i < stars.length; i++){
        stars[i].stepFunction();
        stars[i].drawFunction();
    }
}

function removeStarsOutOfView(){
    //dropping off old stars
    var starsToKeep = [];
    for (var i = 0; i < stars.length; i++){
        if (stars[i].x + stars[i].width > 0) {
            starsToKeep.push(stars[i]);
        }
    }
    stars = starsToKeep;
}

function addNewStars(){
    var newstars = 0.455
    if (random(0, 1) < newstars) {
        stars.push(makeStars(255, 255, 3, random(1, 10));
    }
}

function starsStep() {
    this.x -= this.dx;
}

function starsDraw() {
    stroke(this.c);
    strokeWeight(this.size);
    point(this.x, this.y);
}

function makeStars(sx, sy, sdx, sWidth) {
    var s = {x: sx, y: sy, dx: sdx, 
        width: sWidth,
        c: fill(255),
        drawFunction: starsDraw, stepFunction: starsStep
    }
    return s
}

Project-11

sketch
//Robert Rice
//rdrice
//section c

var sun = {filename:'https://i.imgur.com/74H6zli.png', //original URL https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/7bcda634-15a0-43a8-9de8-2800412220c0/datjxhn-b3b3c9fa-946d-43e2-b2b4-28dc84b56ca0.png/v1/fit/w_150,h_150,strp/retrowave_sun_with_alpha_background_by_robilo_datjxhn-150.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9MTAyNCIsInBhdGgiOiJcL2ZcLzdiY2RhNjM0LTE1YTAtNDNhOC05ZGU4LTI4MDA0MTIyMjBjMFwvZGF0anhobi1iM2IzYzlmYS05NDZkLTQzZTItYjJiNC0yOGRjODRiNTZjYTAucG5nIiwid2lkdGgiOiI8PTEwMjQifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6aW1hZ2Uub3BlcmF0aW9ucyJdfQ.sdt5sozezYuJLi-b3ecAoqszmafFkXh8VGg3G1-YSqE
            x:300,
            y:175,
            drawFunc: drawImg}

var mustang = {filename:'https://i.imgur.com/ZQ6wSq5.png', //I DREW THIS (i traced it but still)
            x:100,
            y:279,
            drawFunc: drawImg}

var wheels = {filename:'https://i.imgur.com/5edjrVN.png', //I DREW THIS (okay i traced but still its technically original)
            x:103,
            y:289,
            drawFunc: drawImg}


var dx = 2; //the speed at which the furthest background objects pass by

var marketvalue = [];   //Reusing my code from assignment 07-a to make an undulating middleground
var noiseParam = 0;
var noiseStep = 0.005;
var n;  //end reused code, appears as DUNES below

var hills=[];

var sign = 1    //the value that the car is pushed up/down by
var pushCount = 0   //keeps track of how far the car has bumped up/down


function preload() {
    // call loadImage() and loadSound() for all media files here
    sun.image = loadImage(sun.filename);
    mustang.image = loadImage(mustang.filename);
    wheels.image = loadImage(wheels.filename);
}

function setup() {
    createCanvas(500, 300);
    background(0);
    imageMode(CENTER);

    strokeWeight(5);
    stroke(119, 179, 0);

    n = (width / 5) + 1 //sets up initial points for the dunes
    for(i = 0; i <= n; i++) {
        noiseParam = noiseStep*i
        marketvalue[i] = (noise(noiseParam) * 150)+150;
    }
}

function draw() {
    //BEGIN BACKGROUND

    push();
    background(38, 0, 77);  //gradient background
    for(i=0; i<300; i++) {
        strokeWeight(1);
        stroke(255-(0.723*i), 77-(0.256*i), 196-(0.396*i))
        line(0, 299-i, 500, 299-i);
    }
    sun.drawFunc(150, 150); //background Sun
    pop();

    //END BACKGROUND
    //BEGIN HILLS. makes some hills/mesas that lazily move along in the background
    
    push();
    if(random(1) > 0.99) {
        makeHill(random(50, 200), random(50, 125), color(random(169, 189), random(49, 69), random(0, 10)));
    }

    var numH = hills.length;
    if (numH > 0) {
        for (i=0; i<numH; i++) {
            hills[i].drawFunc();
            hills[i].upFunc();
        }

        if (hills[0].x+hills[0].r < 0) {
            hills.shift();
        }
    }
    pop();

    //END HILLS
    //BEGIN DUNES (the 07-a reused code i mentioned)

    push();
    marketvalue.shift();    //gets rid of the first value in the list in order to make room for a new one

    strokeWeight(5);
    stroke(179, 119, 0);
    fill(204, 170, 0);
    beginShape();
    vertex(0, height);
    for(i=0; i<n; i++) {
        vertex(5*i, marketvalue[i])
    }
    vertex(width, height);
    endShape(CLOSE);
    
    noiseParam = noiseParam + noiseStep;    //increases the noise paramter, generates a new rolling value
    marketvalue.push(noise(noiseParam)*150+150); //appends the new value
    pop();

    //END DUNES
    //BEGIN ROAD
    push();
    strokeWeight(20);
    stroke(150);
    line(0, 295, 499, 295)
    stroke(75);
    line(0, 299, 499, 299);
    pop();
    //END ROAD
    //BEGIN CAR
    push();
        mustang.y += sign/4
        wheels.drawFunc(77, 15);
        mustang.drawFunc(100, 35);

    pushCount++
    if (pushCount > 8) {
        sign = -sign
        pushCount = 0
    }
    pop();
    //END CAR
}

//END CODE
//BEGIN HELPERFUNCTIONS

function drawImg(w, h) {
    image(this.image, this.x, this.y, w, h);
}

function makeHill(r, h, c) {
    var newHill = {r: r,
        h: h,
        x: 499,
        y: 299,
        drawFunc: drawHill,
        upFunc: updateHill,
        color: c,
        speed: dx}

    hills.push(newHill);
}

function drawHill() {
    noStroke();
    fill(this.color);
    rect(this.x, this.y, this.r, -this.h);
    ellipse(this.x+this.r/2, this.y-this.h, this.r, this.r);
}

function updateHill() {
    this.x -= this.speed;
}

Project-11

sketch

I created this landscape story about skiing. There was mountain scrolling in the background and decoration banners saying that this is a skiing resort. The trees are scrolling to the left to create the effect that the skier is moving to the right. There are four types of trees and they all appear randomly. The size of the tree is also randomly decided. There are balloons floating in the sky and yellow stars shining in the background to make this scene more interesting. The balloons appear at random height from mid-air to the highest sky in the program.

//Heying Wang 
//Section B
var skier;
var birdImg;
var treeTypes=[]
var trees=[];
var birds=[];

//use noise to draw terrain
var noiseParam=0;
var noiseStep=0.05;
var values=[];


function preload(){
    //three types of Christmas tree
    var tree2 = loadImage("https://i.imgur.com/NJBdtua.png");
    treeTypes.push(tree2);
    var tree3=  loadImage("https://i.imgur.com/CU8yOAQ.png");
    treeTypes.push(tree3);
    var tree4=  loadImage("https://i.imgur.com/5Nyq5gE.png");
    treeTypes.push(tree4);
    var tree5=  loadImage("https://i.imgur.com/gCqcv9f.png");
    treeTypes.push(tree5);
    skier=loadImage("https://i.imgur.com/zbJ0fyD.png");
    birdImg=loadImage("https://i.imgur.com/H0cfnnj.png");
    

  }
function setup() {
    createCanvas(480, 300);
    for (var i = 0; i < 10; i++){
        var rx = random(width*3);
        trees[i] = makeTree(rx);
    }

    for(i=0;i<=width/12;i++){
        var n=noise(noiseParam);
        var value=map(n,0,1,0,height);
        values.push(value);
        noiseParam+=noiseStep;
    }
    for (var i = 0; i < 3; i++){
        var rx = random(width);
        birds[i] = makeBird(rx);
    }
    frameRate(13);
 
    
}

function draw() {
    background(220,220,250);
    imageMode(CENTER);

    
    fill(90,20,60);
    rect(0,0, width, height-150); 
    values.shift();
    var newRandom=map(noise(noiseParam),0,1,0,height);
    values.push(newRandom);
    noiseParam += noiseStep;
    drawTerrain();
    //draw the banner and pillers
    fill('grey');
    rect(0,50,30,height-50);
    rect(width-30,50,30,height-50);
    fill(140,40,40);
    rect(30,50,width-60,20);
    fill(255);
    text('2020 ski season',width/2-20,62);
    fill(20,30,70);
    rect(0,height-50, width, height-150); 


    updateAndDisplaytrees();
    removeTreesThatHaveSlippedOutOfView();
    addNewTreesWithSomeRandomProbability(); 

    updateAndDisplaybirds();
    removeBirdsThatHaveSlippedOutOfView();
    addNewBirdsWithSomeRandomProbability(); 
    //draw skier
    var skierX=240+random(-0.5,0.5);
    image(skier,skierX,240,100,100);

    noStroke();
    fill('yellow');
    for(var i=0;i<10;i++){
        circle(random(width),random(height-100),2,2);
    }

}

function updateAndDisplaytrees(){
    // Update the trees' positions, and display them.
    for (var i = 0; i < trees.length; i++){
        trees[i].move();
        trees[i].display();
    }
}  
function updateAndDisplaybirds(){
    // Update the birds' positions, and display them.
    for (var i = 0; i < birds.length; i++){
        birds[i].move();
        birds[i].display();
    }
}      

function removeTreesThatHaveSlippedOutOfView(){
    var treesToKeep = [];
    for (var i = 0; i < trees.length; i++){
        if (trees[i].x + trees[i].breadth > 0) {
            treesToKeep.push(trees[i]);
        }
    }
    trees = treesToKeep; 
}
function removeBirdsThatHaveSlippedOutOfView(){
    var birdsToKeep = [];
    for (var i = 0; i < birds.length; i++){
        if (birds[i].x + birds[i].size > 0) {
            birdsToKeep.push(birds[i]);
        }
    }
    birds = birdsToKeep; 
}


function addNewTreesWithSomeRandomProbability() {
    var newTreeLikelihood = 0.007; 
    if (random(0,1) < newTreeLikelihood) {
        trees.push(makeTree(width));
    }
}

function addNewBirdsWithSomeRandomProbability() {
    var newBirdLikelihood = 0.02; 
    if (random(0,1) < newBirdLikelihood) {
        birds.push(makeBird(width));
    }
}

function makeTree(birthLocationX) {
    var tre = {x: birthLocationX,
                breadth: random(60,70),
                speed: -2,
                treeType: random(treeTypes),
                move: treeMove,
                display: treeDisplay}
    return tre;
};
function makeBird(birthLocationX) {
    var bir = {x: birthLocationX,
                birdImage:birdImg,
                y:random(30,200),
                speed: -2,
                size:random(15,60),
                move: birdMove,
                display: birdDisplay}
    return bir;
};

function treeMove() {
    this.x += this.speed;
}
function treeDisplay() {
   image(this.treeType,this.x,215,this.breadth,this.breadth*1.4);

}
function birdMove() {
    this.x += this.speed;
}
function birdDisplay() {
    image(this.birdImage,this.x,this.y,this.size,this.size*1.2);

}



function drawTerrain(){
    //draw the terrain using begin shape and end shape
    noStroke();
    fill('white');
    beginShape();
    vertex(0,height);
    vertex(0,height);
    for(i=0;i<=width;i+=12){
        curveVertex(i,values[i/12]);

    }
    vertex(width,height);
    vertex(width,height);
    endShape(CLOSE);}

Project 11 – Generative Landscape

sketch
/*
 * Eric Zhao
 * ezhao2@andrew.cmu.edu
 *
 * Generative ladnscape 
 *
 */

var hillHeights = [];
var mountainHeights = [];
//arrays for foreground and background
var noiseParam = 0;
var noiseParam2 = 0;
var noiseStep = 0.01;
var numpoints = 80;
//bottom 4 used as placemarkers and attempts
//to make the hobbit holes not disappear on screen
var xInc;
var circleRad; 
var breadth = 25;
var marginRatio = (numpoints+breadth)/numpoints;

function drawMountain(hill, numpoints){
    //draws a perlin noise mountain
    beginShape();
    vertex(width - width*marginRatio, height);
    vertex(width - width*marginRatio, height);
    for(let i = -breadth; i <= numpoints+breadth; i++){
        vertex(i * xInc, hill[i]);
    }
    vertex(width*marginRatio, height);
    vertex(width*marginRatio, height);   

    endShape();
}

function stepMountain(param, step, hill, numpoints){
    //generates array of perlin values used for mountain and trees
    hill.shift();
    for(let i = -breadth; i <= numpoints+breadth; i++){
        var n = noise(param);
        var value = map(n, 0, 1, 0, height);
        hill.push(value);
        param += step;
    }
    noiseParam = param;
}

function drawHole(circleRad){
    //hobbit hole draw function
    fill(22, 98, 71);
    ellipse(0, 0, circleRad, circleRad);
    fill(random(0, 360), 94, 25);
    ellipse(0, 0, circleRad*0.8, circleRad*0.8);
    stroke(0);
    noFill();
    for(var i = 0; i <= 8; i ++){
        arc(0, 0, circleRad*0.8, circleRad*0.8, i*(PI/8), -i*(PI/8), CHORD);
    }
    fill(50, 80, 90);
    ellipse(0, 0, max(10, circleRad*0.1), max(10, circleRad*0.1));
}

function setup() {
    createCanvas(600, 400);
    colorMode(HSB);
    frameRate(30);
    noStroke();
    strokeWeight(2);
    stepMountain(noiseParam, noiseStep, hillHeights);
}

function draw() {
    xInc = width/numpoints;
    background(183, 33, 95);
    //draws background/dark green mountain
    fill(120, 100, 20);
    stepMountain(noiseParam2, 0.4, mountainHeights, 320)
    drawMountain(mountainHeights, 320);
    //draws foreground scenery
    fill(110, 64, 52);
    stepMountain(noiseParam, noiseStep, hillHeights, numpoints);
    drawMountain(hillHeights, numpoints);
    push();
    translate(0, 20);
    fill(42, 30, 90);
    drawMountain(hillHeights, numpoints);
    //at peaks of hill, add a hobbit hole
    for(let i = -breadth; i <= numpoints+breadth; i++){
            if(i != -breadth & i !=numpoints+breadth){
                if (hillHeights[i] < hillHeights[i-1] &&
                hillHeights[i] < hillHeights[i+1]){
                    push();
                    fill(0);
                    translate(i * xInc, (height + hillHeights[i])/2);
                    drawHole((height-hillHeights[i])/1.5);
                    pop();              
                }

            }
        }
    pop();

}

My idea was to generate a rolling hill and place hobbit holes at random locations or peaks under the hill, with their basic shape looking something like this:

Unfortunately, I realized too late that the program I wrote didn’t utilize objects and made changing variables about the holes (ex. colors and varying designs) very difficult without restructuring the program significantly.

Project_11_landscape

Y-sketch-landscapeDownload
//Huijun Shen
//huijuns@andrew.cmu.edu 
//section D

// the general idea of this project is presenting an array of buildings 
//which are on the hill and are coverd by trees.

var buildings = [];
var trees = [];
var noiseParam = 0;
var noiseStep = 0.02;
var hillHeight = [];
// var clouds = [];   
// var land = [];
// var cX = random(100,200);
// var cY = random(100,200);


function setup(){
    createCanvas(480,480);

    //hill
    for (var i = 0; i <= width/3; i+=1){
       
        var n = noise(noiseParam);

        var value = map(n,0,1,0,width);    

        //hillHeight.shift();

        hillHeight.push(value);

        noiseParam += noiseStep;

        
    }

    //buildings
    for(var i = 0; i < 10; i ++){
        var rx = random(width);
        buildings[i]=makeBuilding(rx);
    }

    frameRate(10);
    
}

function draw(){
    background(144,232,232);

    //sun
    fill(223,242,136);
    circle(400,50,50);

    //hill
    noStroke();

    var n = noise(noiseParam);

    var value = map(n,0,1,0,width);    

    hillHeight.shift(); //update value in array

    hillHeight.push(value);

    noiseParam += noiseStep;
    
    for (var i = 0; i < width/5 ; i += 1){

        fill(50,120,20); //square shape
        beginShape();
        vertex(5*i,hillHeight[i]);
        vertex(5*i+5, hillHeight[i+1]);
        vertex(5*i+5,height);
        vertex(5*i,height);   
        endShape(); 
    }



    //displayHorizon();
    
    //buildings
    updateBuildings();
    removeBuilding();
    addNewBuilding();

    //tree
    
    updateTrees();
    removeTrees();
    addNewTrees();
    
    fill(181,170,156);
    rect(0,460,480,30);
  

}

//horizon

function displayHorizon(){
    stroke(0);
    line(0,height-50,width,height-50);
}



//buildings

function updateBuildings(){
    for (var i = 0; i < buildings.length; i ++){
        buildings[i].move();
        buildings[i].display();
    }
}

function removeBuilding(){
    var buildingsToKeep = [];
    for (var i = 0; i < buildings.length; i ++){
        if (buildings[i].x + buildings[i].breadth > 0){
            buildingsToKeep.push(buildings[i]);
        }
    }
    buildings = buildingsToKeep;

}

function addNewBuilding(){
    if(random(0,1)<0.03){
        buildings.push(makeBuilding(width-2));
    }

}

function buildingMove(){
    this.x += this.speed;
}

function makeBuilding(birthLocationX){
    var bldg = {
        x:birthLocationX,
        y:random(400,450),
        breadth:random(30,50),
        r:random(255),
        b:random(255),
        g:random(255),
        speed : -2.0,
        nFloors : round(random(2,6)),
        move:buildingMove,
        display:buildingDisplay
    }
    return bldg;
}



function buildingDisplay(){
    var floorHeight = 25;
    var bHeight = this.nFloors*floorHeight;
    fill(this.r,this.g,this.b);
    stroke(0);
    push();
    translate(this.x,this.y);
    rect (0,-bHeight-10,this.breadth,bHeight);
    stroke(200);
    for(var i = 0; i < this.nFloors; i ++){
        fill(this.r+10,this.g-20,this.b+50);
        //rect(5,-15-(i*floorHeight),this.breadth-10, 10);
        rect(5,-20-(i*floorHeight),this.breadth-10, 10);
    }
    pop();
}

//tree

function updateTrees(){
    for (var i = 0; i < trees.length; i ++){
        trees[i].move();
        trees[i].display();
    }
}

function removeTrees(){
    var treesToKeep = [];
    for (var i = 0; i < trees.length; i ++){
        if (trees[i].x + trees[i].breadth > 0){
            treesToKeep.push(trees[i]);
        }
    }
    trees = treesToKeep;

}

function addNewTrees(){
    if(random(0,1)<0.1){
        trees.push(makeTrees(width+30));
    }

}

function treeMove(){
    this.x += this.speed;
}

function makeTrees(birthLocationX){
    var tree = {
        x:birthLocationX,
        y:random(400,450),
        treeHeight:random(30,150),
        breadth:random(10,20),
        width:random(50,80),
        r:random(100,120),
        b:random(120,130),
        g:random(150,255),
        speed : -5.0,
        move:treeMove,
        display:treeDraw,
    }
    return tree;
}



function treeDraw(){
    var treeHeight;
    
    push();
    translate(this.x,this.y);
    fill(128,101,64);
    rect(0,0,10,50);
    fill(this.r,this.g,this.b);
    ellipse (0,0,this.width,treeHeight);

    pop();
}


I would like to present building on hills which are cover by trees.