xxxxxxxxxx
83
let dealership = new Array(15);
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB);
angleMode(DEGREES);
dealership[0] = new Car(50, 80 + 0 * 60, 0 * 10, random(1, 2));
print("see https://discourse.processing.org/t/error-when-embedding-one-class-inside-another/14762/3");
}
function draw() {
background(50);
dealership[0].displayCar();
dealership[0].moveCar();
}
class Wheel {
// --------- Wheel: Attributes ---------//
constructor(tempX1, tempY1, tempAngle1, tempColor1) {
this.x1 = tempX1;
this.y1 = tempY1;
this.angle1 = tempAngle1;
this.color1 = tempColor1;
}
//--------- Wheel: Display ---------//
// displayWheel() {
displayWheel(x , y ) {
fill(this.color1, 255, 255);
push();
// translate(this.x1 - 25, this.y1);
translate(x, y);
rotate(this.angle1);
ellipse(0, 0, 15, 20);
pop();
}
//-------- Wheel: Turn ---------//
turnWheel() {
this.angle1 += 3;//1; // The 1 rotates the ellipse by 1 degree
}
}
class Car {
// --------- CAR: Attributes ---------//
constructor(tempX, tempY, tempPaint, tempSpeed) {
this.x = tempX;
this.y = tempY;
this.paint = tempPaint;
this.speed = tempSpeed;
let backWheel; //-------------------------------------------------------------------- WHEEL
let frontWheel; //-------------------------------------------------------------------- WHEEL
this.backWheel = new Wheel(this.x - 25, this.y, 0, 52); //-------------------------------------------------------------------- WHEEL
this.frontWheel = new Wheel(this.x + 25, this.y, 0, 91); //-------------------------------------------------------------------- WHEEL
}
//--------- CAR: Display ---------//
displayCar() {
rectMode(CENTER);
fill(this.paint, 255, 255);
rect(this.x - 10, this.y - 30, 50, 20);
rect(this.x, this.y - 10, 75, 20);
fill(212, 242, 252);
rect(this.x + 5, this.y - 30, 15, 15);
// fill(0);
// ellipse(this.x - 25, this.y, 20, 20);
// ellipse(this.x + 25, this.y, 20, 20);
this.backWheel.displayWheel(this.x - 25, this.y); //-------------------------------------------------------------------- WHEEL
this.frontWheel.displayWheel(this.x + 25, this.y); //-------------------------------------------------------------------- WHEEL
}
//-------- CAR: Move ---------//
moveCar() {
this.x += this.speed;
this.backWheel.turnWheel(); //-------------------------------------------------------------------- WHEEL
this.frontWheel.turnWheel(); //-------------------------------------------------------------------- WHEEL
}
}