Cours:BallePOO : Différence entre versions
(Page créée avec « <source lang=c> class Balle { PVector position; PVector vitesse; PVector zoneBD; PVector zoneHG; float rayon; int couleur = 100; Balle(PVector p, PVector... ») |
|||
| Ligne 47 : | Ligne 47 : | ||
| + | </source> | ||
| + | |||
| + | |||
| + | <source lang=java> | ||
| + | ArrayList <Balle> objets; | ||
| + | |||
| + | PVector coinHG1, coinBD1; | ||
| + | PVector coinHG2, coinBD2; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | PVector espace = new PVector(600,400); | ||
| + | size((int)espace.x,(int)espace.y); | ||
| + | |||
| + | coinHG1 = new PVector(10,40); | ||
| + | coinBD1 = new PVector(200,300); | ||
| + | coinHG2 = new PVector(210,10); | ||
| + | coinBD2 = new PVector(550,350); | ||
| + | objets = new ArrayList<Balle>(); | ||
| + | |||
| + | objets.add(new Balle(new PVector(20,20),new PVector(1,1),coinBD1,coinHG1,4)); | ||
| + | objets.add(new Balle(new PVector(20,20),new PVector(5,-2),coinBD1,coinHG1,10)); | ||
| + | objets.add(new Balle(new PVector(20,20),new PVector(3.2,3),coinBD2,coinHG2,4)); | ||
| + | objets.add(new Balle(new PVector(20,20),new PVector(4,3),coinBD1,coinHG1,4)); | ||
| + | objets.add(new Balle(new PVector(20,20),new PVector(1,-1),coinBD2,coinHG2,4)); | ||
| + | } | ||
| + | |||
| + | void draw() | ||
| + | { | ||
| + | background(0); | ||
| + | fill(250); | ||
| + | rect(coinHG1.x,coinHG1.y,coinBD1.x-coinHG1.x,coinBD1.y-coinHG1.y); | ||
| + | fill(200); | ||
| + | rect(coinHG2.x,coinHG2.y,coinBD2.x-coinHG2.x,coinBD2.y-coinHG2.y); | ||
| + | for (int i=0;i<objets.size();i++) | ||
| + | objets.get(i).go(); | ||
| + | |||
| + | } | ||
</source> | </source> | ||
Version du 25 mars 2016 à 14:46
class Balle
{
PVector position;
PVector vitesse;
PVector zoneBD;
PVector zoneHG;
float rayon;
int couleur = 100;
Balle(PVector p, PVector v, PVector bBD,PVector bHG, float r)
{
p.add(bHG);
this.position = p;
this.vitesse = v;
this.rayon = r;
this.zoneBD = bBD;
this.zoneHG = bHG;
}
void deplacer()
{
this.position.add(this.vitesse);
}
void go()
{
this.deplacer();
this.rebondir();
this.afficher();
}
void rebondir()
{
if ((position.x-rayon)<zoneHG.x) {if (vitesse.x<0) vitesse.x=-vitesse.x;}
if ((position.y-rayon)<zoneHG.y) {if (vitesse.y<0) vitesse.y=-vitesse.y;}
if ((position.x+rayon)>zoneBD.x) {if (vitesse.x>0) vitesse.x=-vitesse.x;}
if ((position.y+rayon)>zoneBD.y) {if (vitesse.y>0) vitesse.y=-vitesse.y;}
}
void afficher()
{
fill(this.couleur);
ellipse(this.position.x,
this.position.y,
2*this.rayon,
2*this.rayon);
}
}
ArrayList <Balle> objets;
PVector coinHG1, coinBD1;
PVector coinHG2, coinBD2;
void setup()
{
PVector espace = new PVector(600,400);
size((int)espace.x,(int)espace.y);
coinHG1 = new PVector(10,40);
coinBD1 = new PVector(200,300);
coinHG2 = new PVector(210,10);
coinBD2 = new PVector(550,350);
objets = new ArrayList<Balle>();
objets.add(new Balle(new PVector(20,20),new PVector(1,1),coinBD1,coinHG1,4));
objets.add(new Balle(new PVector(20,20),new PVector(5,-2),coinBD1,coinHG1,10));
objets.add(new Balle(new PVector(20,20),new PVector(3.2,3),coinBD2,coinHG2,4));
objets.add(new Balle(new PVector(20,20),new PVector(4,3),coinBD1,coinHG1,4));
objets.add(new Balle(new PVector(20,20),new PVector(1,-1),coinBD2,coinHG2,4));
}
void draw()
{
background(0);
fill(250);
rect(coinHG1.x,coinHG1.y,coinBD1.x-coinHG1.x,coinBD1.y-coinHG1.y);
fill(200);
rect(coinHG2.x,coinHG2.y,coinBD2.x-coinHG2.x,coinBD2.y-coinHG2.y);
for (int i=0;i<objets.size();i++)
objets.get(i).go();
}