noobie question. triggering an event only the first time something has changed.... for some funny self portraits
in
Core Library Questions
•
2 years ago
inspired to experiment this weekend after learning from the great Golan at the #eyeo festival this weekend. So this code is hacked from his. Recommendations to clean it up welcome.
In context, I have a simple camera game where the whitest smile (within a boundary) is rewarded with a shiny star...and points. I only want to print an image when the star enters the rectangle either by being stolen from the other box (player) or anywhere else. Not when the star was already in that rectangle.
Help!
--
- /**
- * Brightness Tracking
- * by Golan Levin.
- *
- * Tracks the brightest pixel in a live video signal.
- */
- import processing.video.*;
- Capture video;
- float speed = 2.5;
- float starSize = 100;
- int player1 = 0;
- int player2 = 0;
- PShape star;
- PFont fontA;
- int hPadding = 60;
- int vPadding = 60;
- int hWidth = 200;
- int hHeight = 300;
- void setup() {
- size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
- // Uses the default video input, see the reference if this causes an error
- video = new Capture(this, width, height, 30);
- star = loadShape("star.svg");
- stroke(255);
- fill(255, 25);
- smooth();
- fontA = loadFont("BingoStar-48.vlw");
- textAlign(LEFT);
- textFont(fontA, 50);
- }
- void draw() {
- if (video.available()) {
- video.read();
- image(video, 0, 0, width, height); // Draw the webcam video onto the screen
- strokeWeight(10);
- line(width/2, 0, width/2, height);
- strokeWeight(0);
- fill(255, 25);
- rect(hPadding, vPadding, hWidth, hHeight);
- pushMatrix();
- translate(width/2, 0);
- rect(hPadding, vPadding, hWidth, hHeight);
- popMatrix();
- //text("Player 1:", hPadding, vPadding);
- //text("Player 2:", (width/2) + hPadding, vPadding);
- fill(255);
- text(player1, hPadding, vPadding-10);
- text(player2, (width/2) + hPadding, vPadding-10);
- int brightestX = 0; // X-coordinate of the brightest video pixel
- int brightestY = 0; // Y-coordinate of the brightest video pixel
- float brightestValue = 0; // Brightness of the brightest video pixel
- // Search for the brightest pixel: For each row of pixels in the video image and
- // for each pixel in the yth row, compute each pixel's index in the video
- video.loadPixels();
- int index = 0;
- for (int y = 0; y < video.height; y++) {
- for (int x = 0; x < video.width; x++) {
- // Get the color stored in the pixel
- int pixelValue = video.pixels[index];
- // Determine the brightness of the pixel
- float pixelBrightness = brightness(pixelValue);
- // If that value is brighter than any previous, then store the
- // brightness of that pixel, as well as its (x,y) location
- if (pixelBrightness > brightestValue) {
- brightestValue = pixelBrightness;
- brightestY = y;
- brightestX = x;
- }
- index++;
- }
- }
- // Draw a large, yellow circle at the brightest pixel
- brightestX += random(-speed, speed);
- brightestY += random(-speed, speed);
- shape(star, brightestX, brightestY);
- if ((brightestX > (width/2 +hPadding)) && (brightestX < width-hPadding) && (brightestY > vPadding) && (brightestY < vPadding+hHeight)) {
- star.setVisible(true);
- player2++;
- } else if((brightestX > hPadding) && (brightestX < hWidth+hPadding) && (brightestY > vPadding) && (brightestY < vPadding+hHeight)) {
- star.setVisible(true);
- player1++;
- } else {
- star.setVisible(false);
- }
- }
- }
1