Cours:PooProcessing2Corrige

De troyesGEII
Aller à : navigation, rechercher

Sans PVector

Balle b1;
Balle b2;
void settings()
{
  size(500,500);
}
void setup()
{
  b1=new Balle();
  b2 = new Balle(); 
}
void draw()
{
  background(0,255,0);
  b1.deplacer();
  b1.rebondir();
  b1.afficher();
  b2.deplacer();
  b2.rebondir();
  b2.afficher();
}
class Balle
{
  int rayon;
  int x;
  int y;
  int dx;
  int dy;
  color couleur;
  Balle()
  {
    rayon=20;
    couleur=color(0, 0, 255);
    x=width/2;
    y=height/2;
    dx=3;
    dy=4;
  }
  Balle (int _dx, int _dy) {
    rayon=20;
    couleur=color(0, 0, 255);
    x=width/2;
    y=height/2;
    dx=_dx;
    dy= _dy;
  }
  void afficher()
  { 
    fill(couleur);
    ellipse(x, y, rayon*2, rayon*2);
  }
  void deplacer()
  {
    x=x+dx;
    y=y+dy;
  }
  void rebondir()
  {
    if (x>= width-rayon)
    {
      dx=-dx;
    }
    if (x<=rayon)
    {
      dx=-dx;
    }
    if (y>= height-rayon)
    {
      dy=-dy;
    }
    if (y<=rayon)
    {
      dy=-dy;
    }
  }
}

Avec PVector

class Carre
{
  PVector position;
  PVector vitesse;
  float cote;
  Carre(PVector _position, PVector _vitesse, float _cote)
  {
    position=_position;
    vitesse =_vitesse;
    cote=_cote;
  }
  void deplacer()
  {
    position.add(vitesse);
  }
  void rebondir()
  {
    if (position.x<=0) vitesse.x=-vitesse.x;
    if (position.y<=0) vitesse.y=-vitesse.y;
    if (position.x>=width -cote) vitesse.x=-vitesse.x;
    if (position.y>=height-cote) vitesse.y=-vitesse.y;
  }
  void afficher()
  {
    fill(125, 125, 0);
    
    rect(position.x, position.y, cote, cote);
  }
  void go()
  {
    deplacer();
    rebondir();
    afficher();
  }
}


class Balle
{
  PVector position;
  PVector vitesse;
  float rayon;
  Balle(PVector _position, PVector _vitesse, float _rayon)
  {
    position=_position;
    vitesse =_vitesse;
    rayon=_rayon;
  }
  void deplacer()
  {
    position.add(vitesse);
  }
  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(125, 125, 0);
    ellipse(position.x, position.y, 2*rayon, 2*rayon);
  }
  void go()
  {
    deplacer();
    rebondir();
    afficher();
  }
}


Balle b1,b2;

void settings()
{
  size(800,600);
}
void setup()
{
  PVector p1=new PVector( 400 , 300);
  PVector v1=new PVector( 5 , 2 );
  b1 = new Balle(p1,v1,10);
  PVector p2=new PVector( 40 , 300);
  PVector v2=new PVector( 4 , 2 );
  b2 = new Balle(p2,v2,10);
}
void draw()
{
  background(0);
  b1.go();
  b2.go();
}