• fullscreen
  • Avoidance.pde
  • Particle.pde
  • ParticleSystem.pde
  • /**
     * AVOIDANCE
     * by Jonathan Rioux
     * inspired from SimpleParticuleSystem exemple
     * 
     * Avoid the particules with your mouse.
     */
    
    final int level1 = 500;
    
    final int linePos = 30; // The position of the line from where the particule will be thrown
    final int timeBetween = 2; // Time between the trowing of two particules
    
    ParticleSystem ps;
    int timer = timeBetween;
    PFont alertFont, levelFont, scoreFont;
    int currentLevel;
    int toNextLevel;
    
    // Restart button coordonnates
    int x1, x2, y1, y2;
    
    void setup() {
      alertFont = loadFont("Ziggurat-HTF-Black-32.vlw");
      levelFont = loadFont("LucidaCalligraphy-Italic-48.vlw");
      scoreFont = loadFont("CourierNew36.vlw");
    
      currentLevel = 1;
      toNextLevel = level1;
    
      // Restart button coordonates
      x1 = 10;
      x2 = 80;
      y1 = height-30;
      y2 = height-50;
    
      size(640, 480);
      colorMode(RGB, 255, 255, 255, 100);
      ps = new ParticleSystem(1, new PVector(width/2,height/2,0));
      smooth();
    }
    
    void draw() {
      if(toNextLevel > 0){
        background(0);
        ps.run();
    
        showRestartButton();
        showScore();
    
        if(ps.godMode()){
          textFont(alertFont, 30);
          fill(color(255,80,20));
          textAlign(CENTER);
          text("YOU'V BEEN TOUCHED!", width/2, height / 2 - 50);
          textFont(alertFont, 20);
          text("+50 particules :(", width/2, height / 2 - 20);
        }
    
        if(timer <= 0){
          ps.addParticle(linePos);
          timer = timeBetween;
        }
        else{
          timer--;
        }
      }
      else{
        background(0);
        showRestartButton();
        showScore();
        textFont(levelFont, 40);
        fill(color(20,200,50));
        textAlign(CENTER);
        text("LEVEL COMPLETED!", width/2, height / 2 - 50);
        textFont(levelFont, 20);
        text("There is actually only one level... sorry :(", width/2, height / 2 - 20);
        ps.noGravity();
      }
    }
    
    void mousePressed()
    {
      if(overRestart()){
        setup();
      }
    }
    
    boolean overRestart() {
      if (mouseX >= 10 && mouseX <= 80 && 
        mouseY >= (height-30) && mouseY <= (height-10)) {
        return true;
      } 
      else
        return false;
    }
    
    void showRestartButton(){
      stroke(255);
      fill(50);
      rect(10, height-30, 70, 20);
      textFont(scoreFont, 13);
      fill(color(50,255,0));
      textAlign(LEFT);
      text("RESTART", 17, height - 15);
    }
    
    void showScore(){
      textFont(scoreFont, 15);
      fill(50,255,0);
      textAlign(RIGHT);
      text("Current level: "+currentLevel, width - 20, height - 60);
      text("Touched: "+ps.hitCount+" time"+ (ps.hitCount > 1 ? "s" : ""), width - 20, height - 40);
      text("Next level in: "+toNextLevel+" particules", width - 20, height - 20); 
    }
    
    
    // A simple Particle class
    
    class Particle {
      PVector loc;
      PVector vel, ivel;
      PVector acc;
      float r;
      float timeRemaining;
      final float lifeTime = 160;
    
      // Another constructor (the one we are using here)
      Particle(PVector l) {
        acc = new PVector(0,0.05,0);
        vel = new PVector(random(-1,1),random(-2,0),0);
        ivel = new PVector(0,random(-0.5,-0.2),0);
        loc = l.get();
        r = 20.0; // Size of the circles
        timeRemaining = lifeTime;
      }
    
      void run() {
        update();
        render();
      }
    
      // Method to update location
      void update() {
        vel.add(acc);
        loc.add(vel);
        timeRemaining -= 1.0;
      }
    
      // Method to display
      void render() {
        ellipseMode(CENTER);
        stroke(255,255);
        fill(color(150,150,200),100);
        ellipse(loc.x,loc.y,r,r);
      }
    
      // Is the particle still useful?
      boolean dead() {
        if (timeRemaining <= 0.0) {
          return true;
        } 
        else {
          return false;
        }
      }
    
      boolean isTouched(){
        if(loc.x <= mouseX+8 && loc.x >= mouseX-8){
          if(loc.y <= mouseY+8 && loc.y >= mouseY-8){
            timeRemaining = 0;
            return true;
          }
          else
            return false;
        }
        else
          return false;
      }
    
      void moveUp(){
        loc.add(ivel);
        ellipseMode(CENTER);
        stroke(255,255);
        fill(100,100);
        ellipse(loc.x,loc.y,r,r);
      }
    }
    
    // A class to describe a group of Particles
    // An ArrayList is used to manage the list of Particles 
    
    class ParticleSystem {
    
      ArrayList particles;    // An arraylist for all the particles
      PVector origin;        // An origin point for where particles are born
      int graceTime;          // Time between 2 touches
      int hitCount;
      int particuleCount;
    
      ParticleSystem(int num, PVector v) {
        particles = new ArrayList();              // Initialize the arraylist
        origin = v.get();                        // Store the origin point
      }
    
      void run() {
        // Cycle through the ArrayList backwards b/c we are deleting
        for (int i = particles.size()-1; i >= 0; i--) {
          Particle p = (Particle) particles.get(i);
          p.run();
          if (p.dead()) {
            particles.remove(i);
            toNextLevel--;
          }
          if(graceTime <= 0 && p.isTouched()){
            hitCount++;
            graceTime = 3000;
            toNextLevel += 50;
          }
          else{
            if(graceTime > 0)
              graceTime--;
          }
        }
      }
    
      void addParticle() {
        particles.add(new Particle(origin));
      }
    
      void addParticle(float y) {
        particles.add(new Particle(new PVector(random(0,width),y)));
      }
    
      void addParticle(Particle p) {
        particles.add(p);
      }
    
      // A method to test if the particle system still has particles
      boolean dead() {
        if (particles.isEmpty()) {
          return true;
        }
        else {
          return false;
        }
      }
    
      boolean godMode(){
        if(graceTime > 0)
          return true;
        else
          return false;
      }
    
      void noGravity(){
        for (int i = particles.size()-1; i >= 0; i--) {
          Particle p = (Particle) particles.get(i);
          p.moveUp();
        }
      }
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Jonathan Rioux

    Avoidance

    Add to Faves Me Likey@! 1
    You must login/register to add this sketch to your favorites.

    Avoid the particules with your cursor and reach the highest level.

    You need to login/register to comment.