xxxxxxxxxx
48
var cars = [];
function setup() {
createCanvas(400, 400);
rectMode(CENTER);
for (var i = 0; i < 10; i++) {
cars[i] = new Car(random(50, 100), random(20, 50),
random(0, width), random(0, height),
random(0, 3));
}
}
function draw() {
background(220);
for (var i = 0; i < cars.length; i++) {
cars[i].display();
cars[i].move();
cars[i].reset();
}
}
class Car {
constructor(height, width, x, y, speed) {
this.height = height;
this.width = width;
this.x = x;
this.y = y;
this.speed = speed;
}
display() {
rect(this.x, this.y,
this.height, this.width);
}
move() {
this.x = this.x + this.speed;
}
reset() {
if (this.x > width) {
this.x = 0;
}
}
}