I'm trying to get this code I found for a duck hunt game to work for me. It is setup so that a webcam looks for a green laser pointer (or just green in general) and if the green falls within the "hit area" of the ducks, the duck disappears. Ideally would be used with a projector. The code works good on its own, however I want to replace the "duck.jpg" with multiple images, so that I can have not only a duck flying around, but someone's face, and a hotdog, and a computer monitor, etc. I'm having some trouble figuring this out. I don't want to have to create multiple moving sprite classes, if that would even solve my problem. I've tried multiple variations of using an image array, however my experience with processing is limited and my deadline is looming. here is the code as it works:
import processing.video.*;
// Variable for capture device
Capture video;
// A variable for the color we are searching for.
color trackColor;
class MovingSprite
{
float x, y ;
float xVel, yVel ;
PImage theImage;
float msWidth, msHeight ;
boolean isHidden ;
MovingSprite(float inX, float inY)
{
xVel = random(15) ;// horizontal speed of ducks, random
yVel = random(15) ;//vertical speed of ducks, random
theImage = loadImage("duck.jpeg");
x = inX ;
y = inY ;
isHidden = false ;
}
void draw()
{
if (video.available()) {
video.read();
}
// Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
float worldRecord = 500;
// XY coordinate of closest color
int closestX = 0;
int closestY = 0;
// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
// What is current color
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
// Using euclidean distance to compare colors
float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
// If current color is more similar to tracked color than
// closest color, save current location and current difference
if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;
}
}
}
// We only consider the color found if its color distance is less than 10.
// This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
if (worldRecord < 10) {
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(4.0);
stroke(0);
ellipse(closestX,closestY,16,16);
}