how can I combine multiple sketches into one?

edited November 2017 in Questions about Code

I want these two sketches to be in one sketch. How can I do this?

The firtst sketch is:

    ArrayList explosions;

    void setup() {

      size(300,300);
      smooth();


      explosions = new ArrayList();

      frameRate(20);
    }


    void draw() {

      background(0);


      for ( int i = explosions.size() -1 ; i >= 0 ; i-- ) {
        Explosion b = (Explosion) explosions.get(i);
        b.draw();
        if ( b.isAlive() == false ) {
          explosions.remove(i);
        }
      }


    }

    void mousePressed() {

      int quantite = floor(random(4,30));
      for ( int i = 0; i < quantite; i++) {
        Explosion b = new Explosion(mouseX,mouseY);
        explosions.add(b);
      }
    }

    class Explosion {


      float x;
      float y;
      color c;
      float taille;
      int duree;
      private int debut;
      float direction;
      float vitesse;
      float rotation;
      float vitesseRotation;

      Explosion( float x, float y) {

        this.x = x;
        this.y = y;
        c = color(random(200,256),random(0,256),random(0,30));
        taille = random(5,50);
        duree = floor(random(200,1500));
        debut = millis();
        direction = random(TWO_PI);
        vitesse = random(2,6);
        rotation = random(TWO_PI);
        vitesseRotation = random(-0.5,0.5);
      }


      void draw() {



          x = x + cos(direction) * vitesse;
          y = y + sin(direction) * vitesse;

          noStroke();
          fill(c);
          pushMatrix();
          translate(x,y);
          rotation = rotation + vitesseRotation;
          rotate(rotation);
          rectMode(CENTER);


          int debutEcoule = millis() - debut;
          float tailleCourante =  map( debutEcoule , 0 , duree , taille , 0);

          rect( 0, 0, tailleCourante, tailleCourante );
          popMatrix();

      }

      boolean isAlive() {
        if ( millis() - debut > duree ) {
          return false;
        } 

       return true; 
      }
    }

The other sketch is as follows:

import java.util.ArrayList;
ArrayList<Explosion> exps = new ArrayList<Explosion>();

float x;
float y;
float spd;
float sign;

void setup() {
  size(400, 400);
  background(0);
  smooth();

  if (random(1) >= 0.5) {
    sign = 1;
    x = 0;
  } else {
    sign = -1;
    x = width;
  }
  y = random(height);

  spd = 5;
}

void draw() {
  background(0);

  x += sign * spd;

  for (int i = 0; i < exps.size();i++) {
    Explosion exp = exps.get(i);

    exp.plus();
    exp.display();
    if (exp.finished()) {
      exps.remove(i);
    }
  }

  exps.add(new Explosion(x, y+random(-5, 5)));

  if (x > width + 30 || x < -30) {
    if (random(1) >= 0.5) {
      sign = 1;
      x = 0;
    } else {
      sign = -1;
      x = width;
    }
    y = random(height);
  }
}

class Explosion {
  float x;
  float y;
  float r;
  float r_goal = random(10, 100);
  float alp = 150;
  int life = 200;
  float g = random(255);

  Explosion(float tmpX, float tmpY) {
    x = tmpX;
    y = tmpY;
  }

  void plus() {
    r += 1;
    if (r > r_goal) {
      r = r_goal;
      alp -= 5;
    }
  }

  void display() {
    noStroke();
    fill(255, g, 0, alp);
    ellipse(y, x, r, r);
  }

  boolean finished() {
    life--;
    if (life < 0) {
      return true;
    } else {
      return false;
    }
  }
}

Thank you

Tagged:

Answers

Sign In or Register to comment.