class extends

This commit is contained in:
bach 2024-12-20 14:51:00 +01:00
parent 0b8461da64
commit 2128b0467f

View File

@ -19,10 +19,9 @@ function degToRad(deg) {
// https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Classes_in_JavaScript
class Bidule {
// le constructeur est appelé une fois au moment de new Bidule
constructor(r, x, y) {
constructor(x, y) {
// on definie les valeur de départ propre a chauqe instance
// 'this' correpsond à l'instance, autant de 'this' que d'instance
this.r = r;
this.x = x;
this.y = y;
this.a = 360;
@ -37,6 +36,17 @@ class Bidule {
this.y += this.vitesse.y;
this.dessine();
}
dessine(){
}
}
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Classes#cr%C3%A9er_une_sous-classe_avec_extends
class BiduleRond extends Bidule{
constructor(r, x, y){
super(x, y); // appelle le constructeur parent avec le paramètre
this.r = r;
}
dessine(){
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, degToRad(this.a));
@ -44,9 +54,29 @@ class Bidule {
}
}
class BiduleCarre extends Bidule{
constructor(c, x, y){
super(x, y); // appelle le constructeur parent avec le paramètre
this.c = c;
}
dessine(){
ctx.beginPath();
ctx.rect(this.x-this.c/2, this.y-this.c/2, this.c, this.c);
ctx.fill();
}
}
let mesBidules = [];
for (let i = 0; i < 2000; i++) {
mesBidules.push(new Bidule(2 + Math.random()*4, Math.random()*canvas.width, Math.random()*canvas.height));
if (Math.random()>0.5) {
mesBidules.push(new BiduleRond(2 + Math.random()*4, Math.random()*canvas.width, Math.random()*canvas.height));
} else {
mesBidules.push(new BiduleCarre(4 + Math.random()*8, Math.random()*canvas.width, Math.random()*canvas.height));
}
}
let monAnime = function(){