Cours:BallePOO : Différence entre versions

De troyesGEII
Aller à : navigation, rechercher
m
m
 
(7 révisions intermédiaires par le même utilisateur non affichées)
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;
 
   PVector vitesse;
   PVector zoneBD;
+
   color couleur;
  PVector zoneHG;
+
  float rayon;
+
   Balle(float r, PVector p,PVector v)
  int couleur = 100;
 
 
 
   Balle(PVector p, PVector v, PVector bBD,PVector bHG, float r)
 
 
   {
 
   {
     p.add(bHG);
+
     rayon = r;
     this.position = p;
+
     position = p;
     this.vitesse = v;
+
     vitesse = v;
     this.rayon = r;
+
     couleur = color(100,0,0);
    this.zoneBD = bBD;
 
    this.zoneHG = bHG;
 
 
   }
 
   }
   void deplacer()
+
 +
   Balle(float r, PVector p,PVector v, color c)
 
   {
 
   {
     this.position.add(this.vitesse);
+
     this(r,p,v);
 +
    couleur = c;
 
   }
 
   }
   void go()
+
 
 +
   void rebondir()
 
   {
 
   {
     this.deplacer();
+
     if (position.x<=rayon)
     this.rebondir();
+
    {
     this.afficher();
+
      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 rebondir()
+
 
 +
   void deplacer()
 
   {
 
   {
     if ((position.x-rayon)<zoneHG.x) {if (vitesse.x<0) vitesse.x=-vitesse.x;}
+
     position.add(vitesse);
    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);
+
  void go()
 +
  {
 +
    deplacer();
 +
    rebondir();
 +
    afficher();
 
   }
 
   }
 
}
 
}
  
 
</source>
 
  
  
<source lang=java>
+
Balle b1,b2;
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;
 +
   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);
 +
}
 +
 
   
 
   
  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()
 
void draw()
 
{
 
{
   background(0);
+
   background(255);
   fill(250);
+
   b1.go();
  rect(coinHG1.x,coinHG1.y,coinBD1.x-coinHG1.x,coinBD1.y-coinHG1.y);
+
   b2.go();
  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>
 +
  
 
={{Rouge|avec héritage}}=
 
={{Rouge|avec héritage}}=
 
 
<source lang=java>
 
<source lang=java>
class Balle extends Objet
+
abstract class Forme
 
{
 
{
   float rayon;
+
   PVector position;
   Balle(PVector p, PVector v, PVector bBD,PVector bHG, float r)
+
  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()
 
   {
 
   {
     super(p,v,bBD,bHG);
+
     position.add(vitesse);
     this.rayon = r;
+
     coinBD.add(vitesse);
 +
    coinHG.add(vitesse);
 
   }
 
   }
 
   void rebondir()
 
   void rebondir()
 
   {
 
   {
     if ((position.x-rayon)<zoneHG.x) {if (vitesse.x<0) vitesse.x=-vitesse.x;}
+
     if (coinHG.x<=0)  
     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;}
+
      vitesse.x=-vitesse.x;
     if ((position.y+rayon)>zoneBD.y) {if (vitesse.y>0) vitesse.y=-vitesse.y;}
+
    }
 +
     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()
 
   void afficher()
 
   {
 
   {
     ellipse(this.position.x,
+
     fill(couleur);
            this.position.y,
+
    rect(position.x-cote/2,position.y-cote/2,cote,cote);
            2*this.rayon,
 
            2*this.rayon);
 
 
   }
 
   }
 +
 
}
 
}
</source>
+
<source lang=java>
+
 
abstract class Objet
+
class Balle extends Forme
 
{
 
{
   PVector position;
+
   float rayon;
   PVector vitesse;
+
   Balle(PVector p,PVector v, color c,float r)
  PVector zoneBD;
+
   {
   PVector zoneHG;
+
    super(p,v,c);
  Objet(PVector p, PVector v, PVector bBD,PVector bHG) {
+
    rayon=r;
     this.position = p;
+
     coinHG.x=position.x-rayon;
     this.vitesse = v;
+
     coinHG.y=position.y-rayon;
     this.zoneBD = bBD;
+
     coinBD.x=position.x+rayon;
     this.zoneHG = bHG;
+
     coinBD.y=position.y+rayon;
 
   }
 
   }
   void deplacer(){
+
   void afficher()
     this.position.add(this.vitesse);
+
  {
 +
     fill(couleur);
 +
    ellipse(position.x,position.y,2*rayon,2*rayon);
 
   }
 
   }
  void go() {
 
    this.deplacer();
 
    this.rebondir();
 
    this.afficher();
 
  }
 
  abstract void rebondir();
 
  abstract void afficher();
 
 
}
 
}
 +
  
 
+
Forme f1,f2;
  /*{
+
    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()
 
void setup()
 
{
 
{
   PVector espace = new PVector(600,400);
+
  float r;
   size((int)espace.x,(int)espace.y);
+
  float a;
+
   PVector p;
   coinHG1 = new PVector(10,40);
+
  PVector v;
   coinBD1 = new PVector(200,300);
+
   color c;
   coinHG2 = new PVector(210,10);
+
 
   coinBD2 = new PVector(550,350);
+
   c = color(100,100,0);
   objets = new ArrayList<Balle>();
+
   p = new PVector(15,20);
 +
   v = new PVector(1,1);
 +
   r = 10;
 +
   f1 = new Balle(p,v,c,r);
 
    
 
    
   objets.add(new Balle(new PVector(20,20),new PVector(1,1),coinBD1,coinHG1,4));
+
   c = color(0,100,0);
   objets.add(new Balle(new PVector(20,20),new PVector(5,-2),coinBD1,coinHG1,10));
+
   p = new PVector(18,20);
   objets.add(new Balle(new PVector(20,20),new PVector(3.2,3),coinBD2,coinHG2,4));
+
   v = new PVector(0.5,1);
   objets.add(new Balle(new PVector(20,20),new PVector(4,3),coinBD1,coinHG1,4));
+
   a = 20;
   objets.add(new Balle(new PVector(20,20),new PVector(1,-1),coinBD2,coinHG2,4));
+
   f2 = new Carre(p,v,c,a);
 
}
 
}
 
+
 +
 
void draw()
 
void draw()
 
{
 
{
   background(0);
+
   background(255);
   fill(250);
+
   f1.go();
  rect(coinHG1.x,coinHG1.y,coinBD1.x-coinHG1.x,coinBD1.y-coinHG1.y);
+
   f2.go();
  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 actuelle datée du 19 septembre 2017 à 11:32

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();
}