Cours:BallePOO : Différence entre versions

De troyesGEII
Aller à : navigation, rechercher
Ligne 63 : Ligne 63 :
  
  
 +
 +
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();
 +
}
 +
</source>
 +
 +
 +
={{Rouge|Première version}}=
 +
<source lang=java>
 +
abstract class Forme
 +
{
 +
  PVector position;
 +
  PVector vitesse;
 +
  color couleur;
 +
  Forme(PVector p,PVector v)
 +
  {
 +
    this(p,v,color(random(255),0,0));
 +
  }
 +
  Forme(PVector p,PVector v, color c)
 +
  {
 +
    position = p;
 +
    vitesse = v;
 +
    couleur=c;
 +
  }
 +
  abstract void rebondir();
 +
  void deplacer()
 +
  {
 +
    position.add(vitesse);
 +
  }
 +
  abstract void afficher();
 +
  void go()
 +
  {
 +
    deplacer();
 +
    rebondir();
 +
    afficher();
 +
  }
 +
}
 +
 +
class Balle extends Forme
 +
{
 +
  float rayon;
 +
 +
  Balle(float r, PVector p,PVector v)
 +
  {
 +
    this(r,p,v,color(random(255),0,0));
 +
  }
 +
 +
  Balle(float r, PVector p,PVector v, color c)
 +
  {
 +
    super(p,v,c);
 +
    rayon = r;
 +
  }
 +
 
 +
  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 afficher()
 +
  {
 +
    fill(couleur);
 +
    ellipse(position.x,position.y,2*rayon,2*rayon);
 +
  }
 +
}
  
 
Balle b1,b2;
 
Balle b1,b2;

Version du 13 septembre 2016 à 08:52

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


Première version

abstract class Forme
{
  PVector position;
  PVector vitesse;
  color couleur;
  Forme(PVector p,PVector v)
  {
    this(p,v,color(random(255),0,0));
  }
  Forme(PVector p,PVector v, color c)
  {
    position = p;
    vitesse = v;
    couleur=c;
  }
  abstract void rebondir();
  void deplacer()
  {
    position.add(vitesse);
  }
  abstract void afficher();
  void go()
  {
    deplacer();
    rebondir();
    afficher();
  }
}

class Balle extends Forme
{
  float rayon;
 
  Balle(float r, PVector p,PVector v)
  {
    this(r,p,v,color(random(255),0,0));
  }
 
  Balle(float r, PVector p,PVector v, color c)
  {
    super(p,v,c);
    rayon = r;
  }
  
  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 afficher()
  {
    fill(couleur);
    ellipse(position.x,position.y,2*rayon,2*rayon);
  }
}

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