inheritance
This commit is contained in:
43
w9_cars/sketch.js
Normal file
43
w9_cars/sketch.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const vehicles = [];
|
||||
const IMG_SIZE = 400;
|
||||
let message = '';
|
||||
|
||||
const images = {};
|
||||
|
||||
function setup() {
|
||||
createCanvas(4 * IMG_SIZE, 300);
|
||||
|
||||
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() {}
|
||||
}
|
||||
Reference in New Issue
Block a user