I have problems putting two parts of the program together

edited January 2016 in Programming Questions

I'm new with Processing and I have written this little program

Citta citta;     // istanza della classe 'Citta' [vedi sketch relativo]

Albero albero;   // istanza della class 'Albero' [vedi sketch relativo]

Nuvola nuvola1;  // prima istanza della class 'Nuvola' [vedi sketch relativo]
Nuvola nuvola2;  // seconda istanza della class "Nuvola'
Nuvola nuvola3;  // terza istanza della class "Nuvola'



String fase;     // fase corrente dell'animazione ("introduzione", "apparizione città", "sparizione città", "apparizione albero", "sparizione albero")

void setup()
{
  size (960, 640);

  // CREA GLI OGGETTI
  citta = new Citta();
  albero = new Albero();
  nuvola1 = new Nuvola(-670, 80, 0.24);
  nuvola2 = new Nuvola(-380, 30, 0.26);
  nuvola3 = new Nuvola(-90, 110, 0.28);

  // IMPOSTA LA FASE INIZIALE
  fase = "introduzione";
}

void draw() 
{
  background(255);

  // CONTROLLO E AGGIORNAMENTO DATI
  if (fase == "introduzione") {
    PFont font = createFont("Verdana", 20);
    textFont(font);
    textAlign(CENTER, CENTER);
    fill(64);
    text( "barra spazio per avviare", width/2, height/2-30 ); 
    text( "freccia destra per avanzare", width/2, height/2 );
  } 
  else if (fase == "apparizione città") {
    if (citta.stato == "visibile") {
      fase = "città grigia";
      citta.stato = "grigio";
    }
  } 
 else if (fase == "città grigia") {
    if (citta.stato == "visibile") {
      fase = "sparizione città";
      citta.stato = "sparizione";
    }
  }  
  else if (fase == "sparizione città") {
    if (citta.stato == "invisibile") {
      fase = "apparizione albero";
      albero.stato = "apparizione";
    }
  } 
  else if (fase == "apparizione albero") {
    if (albero.stato == "visibile") {
      fase = "albero grigio";
      albero.stato = "grigio";
    }
  } 
  else if (fase == "albero grigio") {
    if (citta.stato == "visibile") {
      fase = "sparizione albero";
      citta.stato = "sparizione";
    }
  } 
  else if (fase == "sparizione albero") {
    if (albero.stato == "invisibile") {
      fase = "apparizione città";
      citta.stato = "apparizione";
    }
  }
  if (fase != "introduzione") {   // se la fase NON è "introduzione"  
    nuvola1.update();
    nuvola2.update();
    nuvola3.update();
  }

  // DISEGNO DELLA SCENA  
  albero.draw();
  citta.display();
  nuvola1.display();
  nuvola2.display();
  nuvola3.display();
}

void keyPressed () 
{
  if (key == ' ') {                             // se viene premuta la barra di spazio ...
    // esci dalla fase introduttiva 
    if (fase == "introduzione") {
      fase = "apparizione città";
      citta.stato = "apparizione";
    }
  } 
  else if (key == CODED && keyCode == RIGHT) {  // se invece viene premuto il tasto freccia a destra ...    
    // procedi con l'avanzamento dell'animazione
    if (fase == "apparizione città") {
      citta.update();
            } 
    else if (fase == "città grigia") {
      citta.update();
    } 
    else if (fase == "sparizione città") {
      citta.update();
    } 
    else if (fase == "apparizione albero") {
      albero.update();
      } 
    else if (fase == "albero grigio") {
      albero.update();
    } 
    else if (fase == "sparizione albero") {
      albero.update();
    }
  }
}  



class Albero 
{
  // PROPRIETA'
  PImage img;         // immagine contenente la bitmap dell'albero 
  String stato;       // stato dell'albero (apparizione, visibile, sparizione, invisibile)
  float transizione;  // percentuale (da 0.0 a 1.0) dello stato [solo per stati dinamici come "apparizione" e "sparizione"]   
  float a = HALF_PI/3; //angolo;
float lunghezza_tronco=100;
float spessore_tronco=50;
int colore_tronco=30;
final int   maxRIC=1;
int leaves = 0;  // here is a variable whole number called 'leaves'
int leafMax = 600; // here is a variable whole number for the maximum number of leaves. It starts at 200.


  // COSTRUTTORE (chiamato con 'new Albero()')
  Albero () 
  {
        stato = "invisibile";
    transizione = 0;
  }

  // METODO DI AGGIORNAMENTO DELLE PROPRIETA'
  void update ()
  {
    if (stato == "apparizione" || stato == "sparizione") {  // se lo stato è "apparizione" o "sparizione" ...
      transizione = transizione + 0.01;
      if (transizione > 1.0) {
         transizione = 0.0;
         if (stato == "apparizione") {
           stato = "visibile";
         } else { 
           stato = "invisibile";
         }  
      }
    }
  }

  // METODO DI VISUALIZZAZIONE
  void draw() 
  {if (stato != "invisibile") {   // se lo stato NON è "invisibile" ...

      colorMode (HSB, 255, 255, 255);

      float alpha = 0;
      float saturazione = 0;

      if (stato=="visibile") {
        alpha = 255;
        saturazione = 255;
      } else if (stato=="apparizione") {
        alpha = transizione * 256;
        saturazione = 255;
      } else if (stato=="sparizione") {
        alpha = (1-transizione) * 256;
        saturazione = alpha-80;
      }


//stroke(colore_tronco); //colore linee
strokeWeight(spessore_tronco); //spessore linee
//disegno
translate(width/2,height); //si muove in basso al centro
line(0,0,0,-lunghezza_tronco); //disegna la linea di partenza
translate(0,-lunghezza_tronco); //si muove sulla punta della linea
albero(lunghezza_tronco,spessore_tronco,colore_tronco);



  }
  }
  }



void albero (float l,float s,int c) {
l = l*0.9; //ogni ramo successivo è lungo 2/3 il precedente
s = s*2/3; //ogni ramo successivo è spesso 2/3 il precedente
//c += 20; //ogni ramo successivo è più chiaro di 20 punti del precedente

if(l>50){ //condizione di uscita se la lunghezza è minore di 2 pixel
float a = HALF_PI/3; //angolo;

//costruzione braccio destro
pushMatrix(); //salva lo stato attuale della matrice di trasformazione
rotate(a); //ruota di a in senso orario
stroke(c);
strokeWeight(s);
line(0,0,0,-l); //disegna una linea
translate(0,-l); //si muove sulla punta della linea
albero(l,s,c); //richiama la funzione ricorsivamente
popMatrix(); //recupera l’ultimo stato salvato della matrice di trasformazione



//costruzione braccio sinistro
pushMatrix(); //salva lo stato attuale della matrice di trasformazione
rotate(-a); //ruota di a in senso antiorario
stroke(c);
strokeWeight(s);
line(0,0,0,-l); //disegna una linea
translate(0,-l); //si muove sulla punta della linea
albero(l,s,c); //richiama la funzione ricorsivamente
popMatrix(); //recupera l’ultimo stato salvato della matrice di trasformazione
}
} 


class Citta 
{
  // PROPRIETA'
  String stato;       // stato della città (apparizione, visibile, sparizione, invisibile)
  float transizione;  // percentuale (da 0.0 a 1.0) dello stato [solo per stati dinamici come "apparizione" e "sparizione"]   
  int a = 80 ;

  // COSTRUTTORE (chiamato con 'new Citta()')
  Citta () 
  {
    stato = "invisibile";
    transizione = 0;

  }

  // METODO DI AGGIORNAMENTO DELLE PROPRIETA'
  void update ()
  { 

    if (stato == "apparizione" || stato == "sparizione" || stato == "grigio") {  // se lo stato è "apparizione" o "sparizione" ...
      transizione = transizione + 0.01;
      if (transizione > 1.0) {
         transizione = 0.0;
         if (stato == "apparizione" || stato == "grigio") {

           stato = "visibile";
         } else { 
           stato = "invisibile";
         }  
      }
    }
  }

  // METODO DI VISUALIZZAZIONE
  void display() 
  {
    if (stato != "invisibile") {   // se lo stato NON è "invisibile" ...

      colorMode (HSB, 255, 255, 255);

      float alpha = 0;
      float saturazione = 0;
      a = a + 1;

      if (stato=="visibile") {
        alpha = 255;
        saturazione = 255;
      } else if (stato=="apparizione") {
        alpha = transizione * 256;
        saturazione = 255;
      } else if (stato=="sparizione") {
        alpha = (1-transizione) * 256;
        saturazione = alpha-256;
      } else if (stato=="grigio") {
        alpha = 500;
        saturazione = alpha - a ;
      }

      fill (150, saturazione, 200, alpha);
      noStroke ();
      rect(0, 550, 50, 90);

      fill (80, saturazione, 400, alpha);   // alpha*0.5 se vuoi che il valore di opacità arrivi al massimo al 50%
      noStroke ();
      rect(35, 440, 150, 200);

      fill (220, saturazione, 150, alpha);  //saturazione*0.75 se vuoi che il valore di saturazione arrivi al massimo al 75%
      noStroke ();
      rect(50, 520, 50, 120);

      fill (50, saturazione, 230, alpha);
      noStroke ();
      rect(140, 570, 130, 120);

      fill (245, saturazione, 150, alpha);
      noStroke ();
      rect(150, 190, 80, 500);
      rect (230, 360, 350, 580);
      triangle (150, 190, 230, 190, 190, 30);

      fill (190, saturazione, 255, alpha);
      noStroke ();
      rect(210, 480, 50, 160);

      fill (30, saturazione, 244, alpha);
      noStroke ();
      rect(250, 590, 50, 100);

      fill (15, saturazione, 300, alpha);
      noStroke ();
      rect(290, 390, 380, 300);
      triangle (290, 390, 366, 390, 366, 320);
      triangle (366, 390, 442, 390, 442, 320);
      triangle (442, 390, 518, 390, 518, 320);
      triangle (518, 390, 594, 390, 594, 320);
      triangle (594, 390, 670, 390, 670, 320);

      fill (70, saturazione, 90, alpha);
      noStroke ();
      rect(320, 480, 110, 210);

      fill (140, saturazione, 255, alpha);
      noStroke ();
      rect(350, 550, 90, 110);

      fill (40, saturazione, 255, alpha);
      noStroke ();
      rect(410, 430, 70, 210);

      fill (115, saturazione, 127, alpha);
      noStroke ();
      rect(460, 510, 135, 130);

      fill (0, saturazione, 87, alpha);
      noStroke ();
      rect(500, 560, 40, 80);

      fill (225, saturazione, 255, alpha);
      noStroke ();
      rect(530, 460, 160, 180);

      fill (180, saturazione, 255, alpha);
      noStroke ();
      rect(560, 240, 90, 400);
      triangle(560, 240, 650, 240, 605, 70);

      fill (60, saturazione, 300, alpha) ;
      noStroke ();
      rect(620, 555, 150, 85);

      fill (200, saturazione, 190, alpha) ;
      noStroke ();
      rect(710, 420, 190, 220);

      fill (40, saturazione, 250, alpha) ;
      noStroke ();
      rect(735, 300, 90, 340);
      triangle (735, 300, 825, 300, 780, 180);

      fill (130, saturazione, 200, alpha) ;
      noStroke ();
      rect(790, 550, 340, 90);

      colorMode (RGB, 255, 255, 255);
    } 
  }
}



class Nuvola 
{
  // PROPRIETA'
  float x;     // posizione orizzontale (variabile)
  float y;     // posizione verticale (fissa)
  float vx;    // velocità orizzontale
  PImage img;  // immagine contenente la bitmap della nuvola 

  // COSTRUTTORE (chiamato con 'new Nuvola(...)')
  Nuvola (float xIniziale, float yIniziale, float velIniziale) 
  {
    x = xIniziale;
    y = yIniziale;
    vx = velIniziale;
    img = loadImage ("nuvola.png");
  }

  // METODO DI AGGIORNAMENTO DELLE PROPRIETA'
  void update ()
  {
    x = x + vx; 
    if (x > width) {
      x = -img.width;
    }
  }

  // METODO DI VISUALIZZAZIONE
  void display() 
  {
    image(img, x, y);
  }
}

Where you see the tree, you should see it as it is with this code below and it should appear and disappear as the city does but when I try to put the two codes together they just don't work properly. I'm doing something wrong somewhere but I can't understand where. Also the clouds shouldn't stay down but up in the sky. Thanks for helping!

float a=HALF_PI/3; //angolo;
float lunghezza_tronco=100;
float spessore_tronco=50;
int colore_tronco=30;
final int   maxRIC=1;
int leaves = 0;  // here is a variable whole number called 'leaves'
int leafMax = 600; // here is a variable whole number for the maximum number of leaves. It starts at 200.

void setup(){
size(960,640); //dimensione della finestra
background(255); //sfondo bianco
smooth(); //antialiasing
stroke(200); // make the lines pale grey
  strokeWeight(0.5); // make the lines thin
frameRate (300);
}

void draw () {

//stroke(colore_tronco); //colore linee
strokeWeight(spessore_tronco); //spessore linee
//disegno
translate(width/2,height); //si muove in basso al centro
line(0,0,0,-lunghezza_tronco); //disegna la linea di partenza
translate(0,-lunghezza_tronco); //si muove sulla punta della linea
albero(lunghezza_tronco,spessore_tronco,colore_tronco);

 fill(random(255),random(255),random(255),200); // make the fill mostly red and green, and a bit translucent

  if (leaves < leafMax) { // if there are fewer than the maximum number of leaves, run the following code

    float cx =  random(-300,300); // here's a variable that is within a range from 150 to 450
    float cy = -330 + random(0,230); // here's a variable that is within a range from 150 to 450

    translate(cx,cy); // move the position of the drawing as far as cx across and cy down
    rotate(random(TWO_PI)); // rotate the drawing in a random direction
    scale(random(0.9,1.8)); // scale the drawing to an extent between 90% and 180%
    drawLeaf(); // draw a leaf!
    leaves++; // add one to the leaf count
  }

}



void drawLeaf(){ // draw a leaf as follows

 float pointShift = random(-20,20); // here is a variable between -20 and 20
  beginShape(); // start to draw a shape
  vertex(20, 45); // begin at this point x, y
  // bezierVertex(30,30,60,40,70 + random(-20,20),50); // moving only the pointy point meant that sometimes the leaf shape would turn into a heart shape, because the control points were not also moving. So I created a variable called pointShift
    bezierVertex(30,30, 60 + pointShift,40 + pointShift/2, 70 + pointShift,50); // make the pointy end of the leaf vary on the x axis (so the leaf gets longer or shorter) AND vary the y axis of the control points by the same amount. It should be possible to have 'normal' leaves, very short fat ones and very long thin ones.
    bezierVertex(60 + pointShift,55, 30,65, 20,45); // draw the other half of the shape
  endShape();


}



void albero (float l,float s,int c) {
l = l*0.9; //ogni ramo successivo è lungo 2/3 il precedente
s = s*2/3; //ogni ramo successivo è spesso 2/3 il precedente
//c += 20; //ogni ramo successivo è più chiaro di 20 punti del precedente

if(l>50){ //condizione di uscita se la lunghezza è minore di 2 pixel

//costruzione braccio destro
pushMatrix(); //salva lo stato attuale della matrice di trasformazione
rotate(a); //ruota di a in senso orario
stroke(c);
strokeWeight(s);
line(0,0,0,-l); //disegna una linea
translate(0,-l); //si muove sulla punta della linea
albero(l,s,c); //richiama la funzione ricorsivamente
popMatrix(); //recupera l’ultimo stato salvato della matrice di trasformazione



//costruzione braccio sinistro
pushMatrix(); //salva lo stato attuale della matrice di trasformazione
rotate(-a); //ruota di a in senso antiorario
stroke(c);
strokeWeight(s);
line(0,0,0,-l); //disegna una linea
translate(0,-l); //si muove sulla punta della linea
albero(l,s,c); //richiama la funzione ricorsivamente
popMatrix(); //recupera l’ultimo stato salvato della matrice di trasformazione
}
}
Tagged:

Answers

  • I think you will have to try to format your post again, it is difficult to read the code

  • edited January 2014

    hello, pls try to post the code in a better form...

    past the code

    leave 2 blank lines before

    mark the code

    press C in the tool bar

    float a=HALF_PI/3; //angolo; 
    float lunghezza_tronco=100; 
    float spessore_tronco=50;
    int colore_tronco=30;
    final int maxRIC=1;
    int leaves = 0; 
    // here is a variable whole number called 'leaves'
    int leafMax = 600; // here is a variable whole number for the maximum number of leaves. It starts at 200.
    
    void setup() { 
      size(960, 640); //dimensione della finestra 
      background(255); //sfondo bianco 
      smooth(); //antialiasing 
      stroke(200); // make the lines pale grey 
      strokeWeight(0.5); // make the lines thin 
      frameRate (300);
    }
    
    void draw () {
    
      //stroke(colore_tronco); //colore linee 
      strokeWeight(spessore_tronco); //spessore linee //disegno 
      translate(width/2, height); //si muove in basso al centro 
      line(0, 0, 0, -lunghezza_tronco); //disegna la linea di partenza
      translate(0, -lunghezza_tronco); //si muove sulla punta della linea 
      albero(lunghezza_tronco, spessore_tronco, colore_tronco);
    
      fill(random(255), random(255), random(255), 200); // make the fill mostly red and green, and a bit translucent
    
      if (leaves < leafMax) { // if there are fewer than the maximum number of leaves, run the following code
    
        float cx =  random(-300, 300); // here's a variable that is within a range from 150 to 450
        float cy = -330 + random(0, 230); // here's a variable that is within a range from 150 to 450
    
          translate(cx, cy); // move the position of the drawing as far as cx across and cy down
        rotate(random(TWO_PI)); // rotate the drawing in a random direction
        scale(random(0.9, 1.8)); // scale the drawing to an extent between 90% and 180%
        drawLeaf(); // draw a leaf!
        leaves++; // add one to the leaf count
      }
    }
    
    void drawLeaf() { // draw a leaf as follows
    
      float pointShift = random(-20, 20); // here is a variable between -20 and 20 
      beginShape(); // start to draw a shape
      vertex(20, 45); // begin at this point x, y 
      // bezierVertex(30,30,60,40,70 + random(-20,20),50); 
      // moving only the pointy point meant that sometimes the leaf shape would turn into a heart shape, because the control points were not also moving. 
      // So I created a variable called pointShift 
      bezierVertex(30, 30, 60 + pointShift, 40 + pointShift/2, 70 + pointShift, 50); 
      // make the pointy end of the leaf vary on the x axis (so the leaf gets longer or shorter) AND vary the y axis of the control points by the same amount. 
      // It should be possible to have 'normal' leaves, very short fat ones and very long thin ones. 
      bezierVertex(60 + pointShift, 55, 30, 65, 20, 45); // draw the other half of the shape 
      endShape();
    }
    
    void albero (float l, float s, int c) { 
      //l = l0.9; //ogni ramo successivo è lungo 2/3 il precedente
      //s = s2/3; //ogni ramo successivo è spesso 2/3 il precedente 
      //c += 20; //ogni ramo successivo è più chiaro di 20 punti del precedente
    
      if (l>50) { //condizione di uscita se la lunghezza è minore di 2 pixel
    
        //costruzione braccio destro 
        pushMatrix(); //salva lo stato attuale della matrice di trasformazione 
        rotate(a); //ruota di a in senso orario
        stroke(c); 
        strokeWeight(s); 
        line(0, 0, 0, -l); //disegna una linea 
        translate(0, -l); //si muove sulla punta della linea 
        albero(l, s, c); //richiama la funzione ricorsivamente 
        popMatrix(); //recupera l’ultimo stato salvato della matrice di trasformazione
    
        //costruzione braccio sinistro 
        pushMatrix(); //salva lo stato attuale della matrice di trasformazione
        rotate(-a); //ruota di a in senso antiorario 
        stroke(c); 
        strokeWeight(s); 
        line(0, 0, 0, -l); //disegna una linea 
        translate(0, -l); //si muove sulla punta della linea 
        albero(l, s, c); //richiama la funzione ricorsivamente 
        popMatrix(); //recupera l’ultimo stato salvato della matrice di trasformazione
      }
    }
    //
    
  • Done, I'm sorry I'm new here. Thanks for helping

Sign In or Register to comment.