For our video manipulation we borrowed Echo’s code from her bubble game (thank you!!) and modified it to use color tracking to turn the player’s hand into the “catcher”. We also added an “end game” function and tried to add a timer in the beginning to allow time for the color/pixel selection at the beginning of the game, but weren’t able to get the timer to work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import processing.video.*; Catcher catcher; // a catcher object Timer timer; // a timer Bubble[] bubbles; // an array of bubbles Capture video; // Variable for capture device PImage vidMirror; int totalBubbles = 0; color trackColor; // A variable for the color we are searching for. float thresh = 20; int avgX, avgY; //this is what we are trying to find void setup(){ size(640, 480); smooth(); // tracker information video = new Capture(this, width, height); video.start(); vidMirror = new PImage(video.width, video.height); // Start off tracking for red trackColor = color(255, 0, 0); catcher = new Catcher(80); // catcher’s width 80; // bubble information timer = new Timer(1000, 5000); timer.start(); timer.waitToStart(); bubbles = new Bubble[1000]; } void draw(){ // Capture and display the video if (video.available() == true) { video.read(); } video.loadPixels(); //Mirroring the image for(int x = 0; x < video.width; x++){ for(int y = 0; y < video.height; y++){ vidMirror.pixels[x+y*video.width] = video.pixels[(video.width-(x+1))+y*video.width]; } } vidMirror.updatePixels(); image(vidMirror,0,0); float maxCloseness = 500; //XY coord for closest color int closestX = 0; int closestY = 0; //Begin loop to walk through every pixel for(int x = 0; x < vidMirror.width; x++){ for(int y = 0; y < vidMirror.height; y++){ int loc = x + y*vidMirror.width; //get current color color currentColor = vidMirror.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); //euclidean distance to compute colors float d = dist(r1,g1,b1,r2,g2,b2); //if the current color is closer than the last max closeness, get its location if(d < maxCloseness){ maxCloseness = d; closestX = x; closestY = y; } } } //Make a threshold; if the color distance is less than 10, draw a circle if(maxCloseness < 20){ //Draw a circle at the tracked pixel fill(trackColor); stroke(0); ellipse(closestX, closestY, 16, 16); } // CATCHER INFO catcher.position(closestX, closestY); // catcher position needs to target tracker catcher.display(); if (timer.isFinished()){ bubbles[totalBubbles] = new Bubble(); totalBubbles++; if(totalBubbles >= bubbles.length){ totalBubbles = 0; } timer.start(); } for (int i = 0; i < totalBubbles; i++){ bubbles[i].move(); bubbles[i].display(); if (catcher.intersect(bubbles[i])){ bubbles[i].caught(); } } for (int i = 0; i < totalBubbles; i++){ bubbles[i].gameOver(); } } void mousePressed() { // Save color where the mouse is clicked in trackColor variable int loc = mouseX + mouseY*video.width; trackColor = vidMirror.pixels[loc]; } void keyPressed() { //for adjusting things on the fly if (key == '-') { thresh--; println("Threshold " + thresh); } else if (key == '=') { thresh++; println("Threshold " + thresh); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
class Bubble{ float x,y; float speed; float r; color c; Bubble(){ r= 16; x= random(width); y= -r*2; speed=random(1,5); c = color(random(255),random(255),random(255)); } void move() { y += speed; } boolean reachedBottom() { if (y > height + r*2){ return true; }else{ return false; } } void display() { fill(c); noStroke(); for (int i =2; i < r; i++){ ellipse (x, y, i*2, i*2); } } void caught(){ speed = 0; y = -700; // set the location off the screen } void gameOver(){ if (y > height || y > height-r/2){ background(0); textSize(20); text("Game Over", 260, 240); fill (255); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Catcher{ float h; float x,y; color c; Catcher(float tempH){ h=tempH; x=0; y=0; c=color(#30B5FA); } void position(float tempX, float tempY) { x = tempX; y = tempY; } void display(){ noStroke(); fill(c); //rect(x, y, h, h); } boolean intersect (Bubble d){ float distance = dist (x, y, d.x, d.y); if (distance < h + d.r){ return true; } else { return false; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Timer{ int savedTime; int preTime; int totalTime; Timer(int tempTotalTime, int tempPreTime) { totalTime = tempTotalTime; preTime = tempPreTime; } void start(){ savedTime = millis(); preTime = millis(); } boolean waitToStart(){ // our waitToStart function isn't working! int timePassed = millis()-preTime; if(totalTime > preTime) { return true; } else { return false; } } boolean isFinished() { int passedTime = millis()-savedTime; if (passedTime > totalTime) { return true; } else { return false; } } } |
One response to “Sam and Anna’s Human Bubble Game”