Loading...
Logo
Processing Forum
Hey, my idea is to make a kind of game where different coloured bubbles appear, grow and are burst once a bright area (e.g. a phone torch) is shined over them.

I have my bubbles appearing and growing. Although I do want them to appear at random times not all at once, and I want a maximum of say 10 on the screen at once and a new one appears once one is 'popped'.

Also I'm having trouble linking up the co-ordinates of the bubbles and the bright area. I tried using super in my bubble class to pass the coordinates through but processing wasn't having any of it.

Here's my main code:

Copy code
  1. import processing.video.*;

  2. Capture myCaptureDevice;

  3. int imageWidth = 640;
  4. int imageHeight = 480;

  5. Bubble bubble1;

  6. void setup()
  7. {
  8.   size(imageWidth, imageHeight, P2D);
  9.   myCaptureDevice = new Capture(this, imageWidth, imageHeight);
  10.   background(0);
  11.   smooth();

  12.   bubble1 = new Bubble();
  13. }

  14. void draw()
  15. {
  16.   myCaptureDevice.read();
  17.   myCaptureDevice.loadPixels();
  18.   float brightestValue = 0;
  19.   int brightestX = 0;
  20.   int brightestY = 0;
  21.   int[] pixels = myCaptureDevice.pixels;

  22.   int index = 0;
  23.   for (int y = 0; y < myCaptureDevice.height; y++) {
  24.     for (int x = 0; x < myCaptureDevice.width; x++) {
  25.       int pixelValue = myCaptureDevice.pixels[index];
  26.       float pixelBrightness = brightness(pixelValue);
  27.       if (pixelBrightness > brightestValue) {
  28.         brightestValue = pixelBrightness;
  29.         brightestY = y;
  30.         brightestX = x;
  31.       }
  32.       index++;
  33.     }
  34.   }
  35.   for (int i = 0; i < 100; i++) {
  36.     if (brightestX == xArray[i] && brightnessY == yArray[i]) {
  37.       println("Dick");
  38.     }
  39.   }

  40.   /*for (int i=0; i<pixels.length ;i++) {
  41.    int pixelValue = myCaptureDevice.pixels[i];
  42.    float pixelBrightness = brightness(pixelValue);
  43.    if (pixelBrightness > brightestVal) {
  44.    brightestVal = pixelBrightness;
  45.    //brightestY = y;
  46.    brightestX = i;
  47.    if (brightness(pixels[i]) > 220) {
  48.    
  49.    }
  50.    }*/

  51.   image(myCaptureDevice, 0, 0, imageWidth, imageHeight);

  52.   bubble1.drawBubble();
  53. }

And my bubble class under a new tab:

Copy code
  1. public class Bubble
  2. {
  3.   float bubbleIncrement[] = new float[100];
  4.   float bubbleSize[] = new float[100];
  5.   float r[] = new float[100];
  6.   float g[] = new float[100];
  7.   float b[] = new float[100];
  8.   int currentBubbles = 0;

  9.   float[] xArray = new float[(imageWidth*imageHeight)];
  10.   float[] yArray = new float[(imageWidth*imageHeight)];

  11.   public Bubble()
  12.   {
  13.   }

  14.   void drawBubble()
  15.   {
  16.     if (currentBubbles < 100)
  17.     {

  18.       for (int i=0; i<100; i++)
  19.       {    
  20.         xArray[i] = (random(imageWidth));
  21.         yArray[i] = (random(imageHeight));
  22.         bubbleIncrement[i] = random(50);

  23.         r[i] = random(255);
  24.         g[i] = random(255);
  25.         b[i] = random(255);

  26.         ellipse(xArray[i], yArray[i], bubbleIncrement[i]*bubbleSize[i], bubbleIncrement[i]*bubbleSize[i]);

  27.         currentBubbles++;
  28.       }
  29.     }

  30.     for (int j=0; j<10; j++) {
  31.       if (bubbleSize[j] < 100.0) {
  32.         bubbleSize[j] = bubbleSize[j] + 0.005;
  33.       }

  34.       noStroke();        
  35.       fill(r[j], g[j], b[j], 150);

  36.       ellipse(xArray[j], yArray[j], bubbleIncrement[j]*bubbleSize[j], bubbleIncrement[j]*bubbleSize[j]);
  37.     }
  38.   }
  39. }

Any pointers would be greatly appreciated!

Thanks

Replies(3)

Check out this tutorial on a similar concept.
Right, I've had a look at the tutorial. My game is a little different i that I'm not using images of bubbles and I'm using ellipses instead among other things. I'm having a little trouble with my function for finding the bubbles with the brightest pixel.

I'm unsure about how to pass variables correctly between my main sketch and my bubble class.

Here's my main sketch:

Copy code
  1. import processing.video.*;

  2. Capture myCaptureDevice;

  3. int imageWidth = 640;
  4. int imageHeight = 480;

  5. Bubble bubble1;

  6. void setup()
  7. {
  8.   size(imageWidth, imageHeight, P2D);
  9.   myCaptureDevice = new Capture(this, imageWidth, imageHeight);
  10.   background(0);
  11.   smooth();

  12.   bubble1 = new Bubble();
  13.   bubble1.addBubble();
  14.   bubble1.addBubble();
  15.   bubble1.addBubble();
  16.   bubble1.addBubble();
  17.   bubble1.addBubble();
  18.   bubble1.addBubble();
  19. }

  20. void draw()
  21. {
  22.   myCaptureDevice.read();
  23.   myCaptureDevice.loadPixels();
  24.   float brightestValue = 0;
  25.   int brightestX = 0;
  26.   int brightestY = 0;
  27.   int[] pixels = myCaptureDevice.pixels;

  28.   int index = 0;
  29.   for (int y = 0; y < myCaptureDevice.height; y++) {
  30.     for (int x = 0; x < myCaptureDevice.width; x++) {
  31.       int pixelValue = myCaptureDevice.pixels[index];
  32.       float pixelBrightness = brightness(pixelValue);
  33.       if (pixelBrightness > brightestValue) {
  34.         brightestValue = pixelBrightness;
  35.         brightestY = y;
  36.         brightestX = x;
  37.       }
  38.       index++;
  39.     }
  40.   }
  41.   int found = bubble1.find(brightestX, brightestY);
  42.   if (found != -1) {
  43.     bubble1.remove(found);
  44.   }

  45.   /*for (int i = 0; i < 100; i++) {
  46.    if (brightestX == xArray[i] && brightnessY == yArray[i]) {
  47.    println("");
  48.    }
  49.    }*/

  50.   /*for (int i=0; i<pixels.length ;i++) {
  51.    int pixelValue = myCaptureDevice.pixels[i];
  52.    float pixelBrightness = brightness(pixelValue);
  53.    if (pixelBrightness > brightestVal) {
  54.    brightestVal = pixelBrightness;
  55.    //brightestY = y;
  56.    brightestX = i;
  57.    if (brightness(pixels[i]) > 220) {
  58.    
  59.    }
  60.    }*/

  61.   image(myCaptureDevice, 0, 0, imageWidth, imageHeight);

  62.   bubble1.drawBubbles();
  63. }

and my bubble class:

Copy code
  1. public class Bubble
  2. {
  3.   float bubbleIncrement[] = new float[100];
  4.   float bubbleSize[] = new float[100];
  5.   float r[] = new float[100];
  6.   float g[] = new float[100];
  7.   float b[] = new float[100];
  8.   int currentBubbles = 0;

  9.   int[] xArray = new int[(imageWidth*imageHeight)];
  10.   int[] yArray = new int[(imageWidth*imageHeight)];

  11.   public Bubble()
  12.   {
  13.   }

  14.   void addBubble()
  15.   {
  16.     if (currentBubbles < 100)
  17.     {
  18.       xArray[currentBubbles] = int(random(imageWidth));
  19.       yArray[currentBubbles] = int(random(imageHeight));
  20.       bubbleIncrement[currentBubbles] = random(50);

  21.       r[currentBubbles] = random(255);
  22.       g[currentBubbles] = random(255);
  23.       b[currentBubbles] = random(255);

  24.       ellipse(xArray[currentBubbles], yArray[currentBubbles], bubbleIncrement[currentBubbles]*bubbleSize[currentBubbles], bubbleIncrement[currentBubbles]*bubbleSize[currentBubbles]);

  25.       currentBubbles++;
  26.     }
  27.   }

  28.   int find(int x, int y)
  29.   {
  30.     for (int i = 0; i<100; i++) {
  31.       if (xArray[i] == x && yArray[i] == y) {
  32.         return 1;
  33.       }
  34.       else {
  35.         return -1;
  36.       }
  37.     }
  38.   } 

  39.   void remove(int number)
  40.   {
  41.   }

  42.   void drawBubbles()
  43.   {
  44.     for (int j=0; j<10; j++) {
  45.       if (bubbleSize[j] < 100.0) {
  46.         bubbleSize[j] = bubbleSize[j] + 0.005;
  47.       }

  48.       strokeWeight(5);
  49.       stroke(r[j], g[j], b[j], 100);     
  50.       fill(r[j], g[j], b[j], 50);

  51.       ellipse(xArray[j], yArray[j], bubbleIncrement[j]*bubbleSize[j], bubbleIncrement[j]*bubbleSize[j]);
  52.     }
  53.   }
  54. }

The parts I'm having trouble with are the find and found functions. It says I need to return a value of type int which as far as I can tell I am doing. Any pointers?
bump... I'm really struggling with this