Cours:BallePOO : Différence entre versions
(→{{Rouge|avec héritage}}) |
(→{{Rouge|avec héritage}}) |
||
Ligne 98 : | Ligne 98 : | ||
PVector vitesse; | PVector vitesse; | ||
color couleur; | color couleur; | ||
+ | PVector coinHG; | ||
+ | PVector coinBD; | ||
Forme(PVector p,PVector v,color c) | Forme(PVector p,PVector v,color c) | ||
Ligne 104 : | Ligne 106 : | ||
position = p; | position = p; | ||
vitesse = v; | vitesse = v; | ||
+ | coinBD = new PVector(); | ||
+ | coinHG = new PVector(); | ||
} | } | ||
Ligne 109 : | Ligne 113 : | ||
{ | { | ||
position.add(vitesse); | position.add(vitesse); | ||
+ | coinBD.add(vitesse); | ||
+ | coinHG.add(vitesse); | ||
} | } | ||
− | + | void rebondir() | |
+ | { | ||
+ | if (coinHG.x<=0) | ||
+ | { | ||
+ | vitesse.x=-vitesse.x; | ||
+ | } | ||
+ | if (coinHG.y<=0) | ||
+ | { | ||
+ | vitesse.y=-vitesse.y; | ||
+ | } | ||
+ | if (coinBD.x>=width) | ||
+ | { | ||
+ | vitesse.x=-vitesse.x; | ||
+ | } | ||
+ | if (coinBD.y>=height) | ||
+ | { | ||
+ | vitesse.y=-vitesse.y; | ||
+ | } | ||
+ | } | ||
void go() | void go() | ||
{ | { | ||
Ligne 119 : | Ligne 143 : | ||
abstract void afficher(); | abstract void afficher(); | ||
− | + | } | |
− | |||
+ | class Carre extends Forme | ||
+ | { | ||
+ | float cote; | ||
+ | |||
+ | Carre(PVector p,PVector v, color c,float a) | ||
+ | { | ||
+ | super(p,v,c); | ||
+ | cote=a; | ||
+ | coinHG.x=position.x-cote/2; | ||
+ | coinHG.y=position.y-cote/2; | ||
+ | coinBD.x=position.x+cote/2; | ||
+ | coinBD.y=position.y+cote/2; | ||
+ | } | ||
+ | void afficher() | ||
+ | { | ||
+ | fill(couleur); | ||
+ | rect(position.x-cote/2,position.y-cote/2,cote,cote); | ||
+ | } | ||
+ | |||
} | } | ||
+ | |||
class Balle extends Forme | class Balle extends Forme | ||
{ | { | ||
float rayon; | float rayon; | ||
− | |||
Balle(PVector p,PVector v, color c,float r) | Balle(PVector p,PVector v, color c,float r) | ||
{ | { | ||
super(p,v,c); | super(p,v,c); | ||
rayon=r; | rayon=r; | ||
+ | coinHG.x=position.x-rayon; | ||
+ | coinHG.y=position.y-rayon; | ||
+ | coinBD.x=position.x+rayon; | ||
+ | coinBD.y=position.y+rayon; | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
void afficher() | void afficher() | ||
{ | { | ||
Ligne 162 : | Ligne 185 : | ||
ellipse(position.x,position.y,2*rayon,2*rayon); | ellipse(position.x,position.y,2*rayon,2*rayon); | ||
} | } | ||
− | |||
} | } | ||
− | + | Forme f1,f2; | |
void setup() | void setup() | ||
{ | { | ||
− | float r | + | float r; |
+ | float a; | ||
PVector p; | PVector p; | ||
PVector v; | PVector v; | ||
color c; | color c; | ||
+ | |||
c = color(100,100,0); | c = color(100,100,0); | ||
p = new PVector(15,20); | p = new PVector(15,20); | ||
v = new PVector(1,1); | v = new PVector(1,1); | ||
− | + | r = 10; | |
+ | f1 = new Balle(p,v,c,r); | ||
+ | |||
+ | c = color(0,100,0); | ||
+ | p = new PVector(18,20); | ||
+ | v = new PVector(0.5,1); | ||
+ | a = 20; | ||
+ | f2 = new Carre(p,v,c,a); | ||
} | } | ||
Ligne 184 : | Ligne 215 : | ||
{ | { | ||
background(255); | background(255); | ||
− | + | f1.go(); | |
+ | f2.go(); | ||
} | } | ||
</source> | </source> |
Version du 19 septembre 2016 à 11:17
Première version
class Balle
{
float rayon;
PVector position;
PVector vitesse;
color couleur;
Balle(float r, PVector p,PVector v)
{
rayon = r;
position = p;
vitesse = v;
couleur = color(100,0,0);
}
Balle(float r, PVector p,PVector v, color c)
{
this(r,p,v);
couleur = c;
}
void rebondir()
{
if (position.x<=rayon)
{
vitesse.x=-vitesse.x;
}
if (position.y<=rayon)
{
vitesse.y=-vitesse.y;
}
if (position.x>=(width-rayon))
{
vitesse.x=-vitesse.x;
}
if (position.y>=(height-rayon))
{
vitesse.y=-vitesse.y;
}
}
void deplacer()
{
position.add(vitesse);
}
void afficher()
{
fill(couleur);
ellipse(position.x,position.y,2*rayon,2*rayon);
}
void go()
{
deplacer();
rebondir();
afficher();
}
}
Balle b1,b2;
void setup()
{
float r = 10;
PVector p;
PVector v;
p = new PVector(15,20);
v = new PVector(1,1);
b1 = new Balle(r,p,v);
color c;
c = color(0,100,0);
p = new PVector(40,40);
v = new PVector(0.5,1);
b2 = new Balle(r,p,v,c);
}
void draw()
{
background(255);
b1.go();
b2.go();
}
avec héritage
abstract class Forme
{
PVector position;
PVector vitesse;
color couleur;
PVector coinHG;
PVector coinBD;
Forme(PVector p,PVector v,color c)
{
couleur = c;
position = p;
vitesse = v;
coinBD = new PVector();
coinHG = new PVector();
}
void bouger()
{
position.add(vitesse);
coinBD.add(vitesse);
coinHG.add(vitesse);
}
void rebondir()
{
if (coinHG.x<=0)
{
vitesse.x=-vitesse.x;
}
if (coinHG.y<=0)
{
vitesse.y=-vitesse.y;
}
if (coinBD.x>=width)
{
vitesse.x=-vitesse.x;
}
if (coinBD.y>=height)
{
vitesse.y=-vitesse.y;
}
}
void go()
{
bouger();
rebondir();
afficher();
}
abstract void afficher();
}
class Carre extends Forme
{
float cote;
Carre(PVector p,PVector v, color c,float a)
{
super(p,v,c);
cote=a;
coinHG.x=position.x-cote/2;
coinHG.y=position.y-cote/2;
coinBD.x=position.x+cote/2;
coinBD.y=position.y+cote/2;
}
void afficher()
{
fill(couleur);
rect(position.x-cote/2,position.y-cote/2,cote,cote);
}
}
class Balle extends Forme
{
float rayon;
Balle(PVector p,PVector v, color c,float r)
{
super(p,v,c);
rayon=r;
coinHG.x=position.x-rayon;
coinHG.y=position.y-rayon;
coinBD.x=position.x+rayon;
coinBD.y=position.y+rayon;
}
void afficher()
{
fill(couleur);
ellipse(position.x,position.y,2*rayon,2*rayon);
}
}
Forme f1,f2;
void setup()
{
float r;
float a;
PVector p;
PVector v;
color c;
c = color(100,100,0);
p = new PVector(15,20);
v = new PVector(1,1);
r = 10;
f1 = new Balle(p,v,c,r);
c = color(0,100,0);
p = new PVector(18,20);
v = new PVector(0.5,1);
a = 20;
f2 = new Carre(p,v,c,a);
}
void draw()
{
background(255);
f1.go();
f2.go();
}