exercises/w9_cars/sketch.js
2025-04-22 12:26:03 +09:00

45 lines
729 B
JavaScript

const vehicles = [];
const IMG_SIZE = 400;
let message = '';
const images = {};
function setup() {
// createCanvas(400, 300);
createCanvas(window.innerWidth, window.innerHeight);
vehicles.push(new Car(4));
}
function draw() {
background(255);
for (let i = 0; i < vehicles.length; i += 1) {
vehicles[i].draw(i * IMG_SIZE, 0, IMG_SIZE);
}
fill(0);
textFont('Arial', 20);
textAlign(CENTER);
text(message, width / 2, IMG_SIZE);
}
function mousePressed() {
const index = floor(mouseX / IMG_SIZE);
message = vehicles[index]?.getDescription() ?? 'None'; // polymorphism!
}
// CLASSES
class Car {
constructor(passengers) {
// ...
}
getDescription() {
// PLACEHOLDER
}
draw() {}
}