• fullscreen
  • Ball.pde
  • new_pde_edited_one_note_working_memory_error.pde
  • class Ball {
      float x, y; // Position of the ball
      float speedX, speedY; // Speed of the ball
      float radius; // Radius of the ball
      color ball_color = color(0,random(0,250)); // Color of the ball
      int resizeControl = 0;
    
      // Constructor
      Ball(float x_, float y_, float speedX_, float speedY_, float radius_) {
        x = x_;
        y = y_ ;
        speedX = speedX_;
        speedY = speedY_;
        radius = radius_;
      }
    
    
      // Draws the ball
      void display() {
        fill(ball_color);
        stroke(ball_color);
        ellipse(x, y, radius * 2, radius * 2);
      }
    
      // Updates the ball's position, and reverses speed if it hits the wall
      void update() {
        x = x + speedX* mouseY/100;
        y = y + speedY* mouseY/100;     
        // Reverse speed if it hits the wall
    
        if (x + edge + radius > width || x - edge - radius < 0) {
          speedX = - speedX;
          note1 = minim.loadFile("note1.wav");
          note1.loop(0);
          minim.stop();
        }
        if (y + edge + radius > height || y -  edge - radius < 0) {
          speedY = - speedY;
          note1 = minim.loadFile("note1.wav");
          note1.loop(0);
          minim.stop();
        }
      }
    
      void mouse() {
        if (mousePressed) {
          resizeControl = 1;
          if(mouseButton == RIGHT) {
            if (resizeControl == 1) { 
              if(radius<50 && radicont==1) {
                radius=radius+1;
                if(radius==49) {
                  radicont=0;
                }
              }
              if(radius>10 && radicont==0) {
                radius=radius-1;
                if(radius==11) {
                  radicont=1;
                }
              }
            }
          }
          else if (mouseButton == LEFT) {    
            resizeControl = 0;
          }
        }
      }
      //  void sound() {    
      //  
      // play sound if it hits the wall
      //if (x + edge + radius > width || x - edge - radius < 0) {
      //}
      //if (y + edge + radius > height || y -  edge - radius < 0) {
      //note1.play();
      //minim.stop();
      //}
      //}
    }
    
    
    import ddf.minim.*;
    import ddf.minim.signals.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    
    Minim minim;
    AudioPlayer note1;
    AudioPlayer note2;
    AudioPlayer note3;
    AudioPlayer note4;
    AudioPlayer note5;
    AudioPlayer note6;
    
    int maxBalls = 11;
    Ball[] ballArray;
    int ballCount; // No of balls being created
    
    //Background colour variables
    float a = 0;
    float b = 0;
    float c = 0;
    float d = 102;
    float e = 204;
    float f = 255;
    int replace = 0;
    
    //controls
    int edge = 56;
    int radius = 0;
    int radicont = 1;
    
    void setup() {
      size(750, 750);
      smooth();
      // Create an array of balls
      ballArray = new Ball[maxBalls];
      //audio notes
      minim = new Minim(this);
    }
    
    void background1()
    {
      background(color(d+a,e+b,f+c));
      //Change the background colours
      a = (random(-1,1)) + a;
      b = (random(-1,1)) + b;
      c = (random(-1,1)) + c;
      //black border
      fill(0);
      rect(0,0,edge,height);
      rect(0,0,width,edge);
      rect(0,height-edge,width,height);
      rect(width-edge,0,width,height);
      noFill();
    }
    
    void draw() {
      background1();
    
    //    note2 = minim.loadFile("note2.wav");
      //  note3 = minim.loadFile("note3.wav");
        //note4 = minim.loadFile("note4.wav");
       // note5 = minim.loadFile("note5.wav");
        //note6 = minim.loadFile("note6.wav");
      // Display the balls and update the positions
      for (int i = 0; i < ballCount; i = i + 1) {
        ballArray[i].display();
        ballArray[i].update();
        ballArray[i].mouse();
    //    ballArray[i].sound();
      }
    }
    
    int as = 0;
    
    void mousePressed() {
      if (mouseButton == LEFT && replace == 0) {
        if (as < maxBalls) {
          ballArray[as] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0);
          ballCount++;
          as++;
          if(ballCount == maxBalls - 1) {
            replace = 1;
            as = 0;
          }
        }
      }
      if (mouseButton == LEFT && replace == 1) {
        if (ballCount < maxBalls) {
          ballArray[as] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0);
          as++;
          if(as == maxBalls - 1) {
            as=0;
          }
        }
      }
    }
    

    code

    tweaks (0)

    about this sketch

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

    license

    advertisement

    James Godfrey

    Interactive with sound

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

    This stage of my project i have added some sound.

    You need to login/register to comment.