inheritance
This commit is contained in:
11
w9_cars/css/style.css
Normal file
11
w9_cars/css/style.css
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
html,
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
/* padding: 5px; */
|
||||||
|
align-items: center;
|
||||||
|
text-align: center;
|
||||||
|
background-color: beige;
|
||||||
|
}
|
||||||
|
canvas {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
BIN
w9_cars/data/car.jpg
Normal file
BIN
w9_cars/data/car.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
BIN
w9_cars/data/convertible.jpg
Normal file
BIN
w9_cars/data/convertible.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
BIN
w9_cars/data/font.vlw
Normal file
BIN
w9_cars/data/font.vlw
Normal file
Binary file not shown.
BIN
w9_cars/data/sport.jpg
Normal file
BIN
w9_cars/data/sport.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 59 KiB |
BIN
w9_cars/data/truck.jpg
Normal file
BIN
w9_cars/data/truck.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
18
w9_cars/index.html
Normal file
18
w9_cars/index.html
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<script src='https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.3/p5.js'
|
||||||
|
integrity='sha512-BLd2MDTrBCo01Vkhjbjn3ITBDbx3o/Lt7D5hj5oLwW8vlDHYXMenxglTcUURCgBhdLNAjZ7KD8x4ZA7bQY3OhA=='
|
||||||
|
crossorigin='anonymous'></script>
|
||||||
|
|
||||||
|
|
||||||
|
<link rel="stylesheet" type="text/css" href="./css/style.css" />
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<script src="sketch.js"></script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
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() {}
|
||||||
|
}
|
||||||
38
w9_cars/test.wsd
Normal file
38
w9_cars/test.wsd
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
@startuml
|
||||||
|
class Vehicle {
|
||||||
|
-img: PImage
|
||||||
|
+ {abstract} getDescription(): String
|
||||||
|
+ draw (x: Number, y: Number, size: Number): void
|
||||||
|
}
|
||||||
|
|
||||||
|
class Car {
|
||||||
|
- passengers: Number
|
||||||
|
+ constructor (passengers: Number)
|
||||||
|
+ getDescription (): String
|
||||||
|
}
|
||||||
|
|
||||||
|
class Truck {
|
||||||
|
- load: Number
|
||||||
|
+ constructor (load: Number)
|
||||||
|
+ getDescription (): String
|
||||||
|
}
|
||||||
|
|
||||||
|
class SportCar {
|
||||||
|
- fullOp: Bolean
|
||||||
|
+ constructor (fullOptions: Boolean)
|
||||||
|
+ getDescription (): String
|
||||||
|
+ getTurbo (): String
|
||||||
|
+ getOptions (): String
|
||||||
|
}
|
||||||
|
|
||||||
|
class Convertible {
|
||||||
|
+ constructor()
|
||||||
|
+ getDescription(): String
|
||||||
|
+ getRooftop(): String
|
||||||
|
}
|
||||||
|
Vehicle <|.. Car
|
||||||
|
Vehicle <|.. Truck
|
||||||
|
Car <|-- SportCar
|
||||||
|
SportCar <|-- Convertible
|
||||||
|
|
||||||
|
@enduml
|
||||||
Reference in New Issue
Block a user