AndrewWang-Project-10

sketch

//Andrew Wang

var cars = [];
var count = 0;

function setup(){
    createCanvas(600,400);
    background(0,255,0);
    createCars();
    rectMode(CENTER);
    noStroke(); 
}

function draw(){
    count+=1
    //calls create cars every 200 counts
    if (count%250==0){
        createCars();
    }
    //draws the road
    fill(223);
    rect(width/2,height/2,600,200);
    fill("yellow");
    //yellow stripes on road
    for(i=1;i<=8;i++){
        rect(i*600/9,height/2,50,15)
    }
    fill(0);
    //increases cars speed and then draws the car
    for (var i = 0;i<cars.length;i++){
        cars[i].draw();
        cars[i].x+=cars[i].speed;
    }
}

function createCars(){
    //randomly creates a car for either one or both lanes
    var carChance = floor(random(1,4));
    if (carChance == 1){
        cars.push(Car(1));
    }else if (carChance == 2){
        cars.push(Car(1));
        cars.push(Car(2));
    }else{
        cars.push(Car(2));
    }
}

function drawCar(){
    //draws the body of the car
    fill(this.R,this.G, this.B);
    rect(this.x,this.y,this.carWidth,this.carHeight,this.roundness);
    fill(255);
    //draws the hood of the car
    rect(this.x,this.y,20,30);
    fill(0);
    //draws the wheels of the car
    ellipse(this.x-this.carWidth/3, this.y+this.carHeight/2,20,5);
    ellipse(this.x+this.carWidth/3, this.y+this.carHeight/2,20,5);
    ellipse(this.x+this.carWidth/3, this.y-this.carHeight/2,20,5);
    ellipse(this.x-this.carWidth/3, this.y-this.carHeight/2,20,5);
}

function Car(lane){
    //depending on the lane sets the car to that lane and then changes the car direction and starting position
    if (lane == 1){
        heightY=150;
        direction = -1
        widthX=600;
    }else{
        heightY=250;
        direction = 1;
        widthX =0;
    }
    return {x: widthX,
            y: heightY,
            roundness: random(0,10),
            carHeight: random(30,70),
            carWidth: random(50,100),
            R: random(0,255),
            G: random(0,255),
            B: random(0,255),
            speed: direction * random(0.7,1),
            draw: drawCar
           }
}

14963482_1217758704913996_159518689_o

I thought it would be cool to do a birds eye view of a road simulation. I liked the fact that there was a lot you could do with the idea such as cars moving in different directions, and making sure that the cars don’t collide with each other.There was also a lot of variability that could be put into each car, such as its roundness, color, speed, and such. Overall I think it worked out decently, and although in the future perhaps it would be cool to add some more functionalities to it such as more color options or car models.

Leave a Reply