Cours:BallePOO : Différence entre versions

De troyesGEII
Aller à : navigation, rechercher
m
Ligne 2 : Ligne 2 :
 
={{Rouge|Première version}}=
 
={{Rouge|Première version}}=
 
<source lang=java>
 
<source lang=java>
 
 
class Balle
 
class Balle
 
{
 
{
 +
  float rayon;
 
   PVector position;
 
   PVector position;
   PVector vitesse;
+
   color couleur;
  PVector zoneBD;
 
  PVector zoneHG;
 
  float rayon;
 
  int couleur = 100;
 
 
    
 
    
   Balle(PVector p, PVector v, PVector bBD,PVector bHG, float r)
+
   Balle(float r, PVector p)
 
   {
 
   {
     p.add(bHG);
+
     rayon = r;
    this.position = p;
+
     position = p;
    this.vitesse = v;
+
     couleur = color(100,0,0);
    this.rayon = r;
 
     this.zoneBD = bBD;
 
     this.zoneHG = bHG;
 
 
   }
 
   }
   void deplacer()
+
 
 +
   Balle(float r, PVector p, color c)
 
   {
 
   {
     this.position.add(this.vitesse);
+
     this(r,p);
  }
+
     couleur = c;
  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()
 
   void afficher()
 
   {
 
   {
     fill(this.couleur);
+
     fill(couleur);
     ellipse(this.position.x,
+
     ellipse(position.x,position.y,2*rayon,2*rayon);
            this.position.y,
 
            2*this.rayon,
 
            2*this.rayon);
 
 
   }
 
   }
 
}
 
}
  
  
</source>
+
Balle b1,b2;
 
 
  
<source lang=java>
 
ArrayList <Balle> objets;
 
 
PVector coinHG1, coinBD1;
 
PVector coinHG2, coinBD2;
 
  
 
void setup()
 
void setup()
 
{
 
{
   PVector espace = new PVector(600,400);
+
   float r = 10;
  size((int)espace.x,(int)espace.y);
+
   PVector p;
+
   p = new PVector(15,20);
  coinHG1 = new PVector(10,40);
+
   b1 = new Balle(r,p);
   coinBD1 = new PVector(200,300);
+
   color c;
   coinHG2 = new PVector(210,10);
+
   c = color(0,100,0);
  coinBD2 = new PVector(550,350);
+
   p = new PVector(40,40);
   objets = new ArrayList<Balle>();
+
   b2 = new Balle(r,p,c);
 
 
  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()
 
void draw()
 
{
 
{
  background(0);
+
b1.afficher();
  fill(250);
+
b2.afficher();
  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>
 
 
={{Rouge|avec héritage}}=
 
  
<source lang=java>
 
class Balle extends Objet
 
{
 
  float rayon;
 
  Balle(PVector p, PVector v, PVector bBD,PVector bHG, float r)
 
  {
 
    super(p,v,bBD,bHG);
 
    this.rayon = r;
 
  }
 
  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()
 
  {
 
    ellipse(this.position.x,
 
            this.position.y,
 
            2*this.rayon,
 
            2*this.rayon);
 
  }
 
}
 
</source>
 
<source lang=java>
 
abstract class Objet
 
{
 
  PVector position;
 
  PVector vitesse;
 
  PVector zoneBD;
 
  PVector zoneHG;
 
  Objet(PVector p, PVector v, PVector bBD,PVector bHG) {
 
    this.position = p;
 
    this.vitesse = v;
 
    this.zoneBD = bBD;
 
    this.zoneHG = bHG;
 
  }
 
  void deplacer(){
 
    this.position.add(this.vitesse);
 
  }
 
  void go() {
 
    this.deplacer();
 
    this.rebondir();
 
    this.afficher();
 
  }
 
  abstract void rebondir();
 
  abstract void afficher();
 
}
 
 
 
  /*{
 
    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;}
 
  }*/
 
 
</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 6 septembre 2016 à 08:47

Première version

class Balle
{
  float rayon;
  PVector position;
  color couleur;
  
  Balle(float r, PVector p)
  {
    rayon = r;
    position = p;
    couleur = color(100,0,0);
  }

  Balle(float r, PVector p, color c)
  {
    this(r,p);
    couleur = c;
  }
  
  void afficher()
  {
    fill(couleur);
    ellipse(position.x,position.y,2*rayon,2*rayon);
  }
}


Balle b1,b2;


void setup()
{
  float r = 10;
  PVector p;
  p = new PVector(15,20);
  b1 = new Balle(r,p);
  color c;
  c = color(0,100,0);
  p = new PVector(40,40);
  b2 = new Balle(r,p,c);
}


void draw()
{
 b1.afficher();
 b2.afficher();
}