xxxxxxxxxx
40
const WIDTH = 400;
const HEIGHT = 400;
const numLanes = 3;
let laneW = WIDTH/(2+numLanes);
let car1;
function setup() {
createCanvas(WIDTH, HEIGHT);
car1 = new Car((1+floor(random(numLanes)))*laneW + laneW/6, 200, 4/6*laneW, 7/6*laneW);
}
function draw() {
background(0, 120, 0);
// road
fill(0,0,0);
rect(laneW, 0, laneW*numLanes, HEIGHT);
// side lines of road
stroke(255);
strokeWeight(3);
line(laneW, 0, laneW, HEIGHT);
line(WIDTH - laneW, 0, WIDTH - laneW, HEIGHT);
// road lanes
for (let i=1; i<numLanes; i++) {
stroke(255, 255, 0);
strokeWeight(3);
drawingContext.setLineDash([15, 30]);
line((i+1)*laneW, 0, (i+1)*laneW, HEIGHT);
drawingContext.setLineDash([]);
noStroke();
}
// draw car
car1.show();
}