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.

Leave a Reply