xxxxxxxxxx
50
class Car {
constructor(x, y, width, height, color, velocity) {
this.posX = x;
this.posY = y;
this.carWidth = width;
this.carHeight = height;
this.carColor = color;
this.wheelWidth = this.carWidth * 0.3;
this.wheelHeight = this.wheelWidth / 2.5;
this.velocity = velocity;
}
display() {
fill(this.carColor);
noStroke();
rect(this.posX, this.posY, this.carWidth, this.carHeight);
fill(0);
rect(this.posX - this.carWidth / 2 + this.wheelWidth / 2, this.posY - this.carHeight / 2 - this.wheelHeight / 2, this.wheelWidth, this.wheelHeight);
rect(this.posX + this.carWidth / 2 - this.wheelWidth / 2, this.posY - this.carHeight / 2 - this.wheelHeight / 2, this.wheelWidth, this.wheelHeight);
rect(this.posX - this.carWidth / 2 + this.wheelWidth / 2, this.posY + this.carHeight / 2 + this.wheelHeight / 2, this.wheelWidth, this.wheelHeight);
rect(this.posX + this.carWidth / 2 - this.wheelWidth / 2, this.posY + this.carHeight / 2 + this.wheelHeight / 2, this.wheelWidth, this.wheelHeight);
}
drive() {
this.posX += this.velocity;
if (this.posX > width + this.carWidth / 2) {
this.posX = -this.carWidth / 2;
}
}
}
let cars = [];
function setup() {
createCanvas(600, 600);
rectMode(CENTER);
for (let i = 0; i < 5; i++) {
let car = new Car(random(width), random(height), 100, 40, color(random(255), random(255), random(255)), random(5, 15));
cars.push(car);
}
}
function draw() {
background(155);
for (let car of cars) {
car.display();
car.drive();
}
}