//from the lovely miss Jia Zhang import processing.video.*; static public void main(String args[]) { PApplet.main(new String[] { "--present", "prototype_motiondetect_1" } ); } float z = 0.0; float k = 0.0; float q = 0; float angle; int numFrames = 18; PImage[] images = new PImage[numFrames]; int numPixels; int[] previousFrame; Capture video; int cellSize = 50; void setup(){ size(screen.width, screen.height); video = new Capture(this, width, height, 24); numPixels = video.width * video.height; previousFrame = new int[numPixels]; for (int i = 0; i < images.length; i++){ String imageName = "tetris" + nf(i,0) + ".png"; // println(imageName); images[i] = loadImage(imageName); } smooth(); } void draw() { noCursor(); frameRate(30); background(0); if (video.available()) { video.read(); // Read the new frame from the camera video.loadPixels(); // Make its pixels[] array available int movementSum = 0; // Amount of movement in the frame loadPixels(); for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... color currColor = video.pixels[i]; color prevColor = previousFrame[i]; int currR = (currColor >> 16) & 0xFF; // Like red(), but faster (see p. 673) int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; int prevR = (prevColor >> 16) & 0xFF; int prevG = (prevColor >> 8) & 0xFF; int prevB = prevColor & 0xFF; int diffR = abs(currR - prevR); int diffG = abs(currG - prevG); int diffB = abs(currB - prevB); movementSum += diffR + diffG + diffB; pixels[i] = color(diffR, diffG, diffB); previousFrame[i] = currColor; } println(movementSum); background(0); if(movementSum < 8000000){ bigTetris(true); println("moving slowly "); } else if ((movementSum < 10000000) && (movementSum >=8000000)){ shakeTetris(true); println("moving medium"); } else if(movementSum > 12000000){ twistTetris(); //drawTetris(); //largeTetris(true); println("moving fast"); } } } void bigTetris(boolean big){ if (big){ int i =0; for (k = 0; k <= width; k+=width/17 + 30){ image(images[i], k, 0,images[i].width/3, images[i].height/3); image(images[i], k, 150,images[i].width/3, images[i].height/3); image(images[i], k, 300,images[i].width/3, images[i].height/3); image(images[i], k, 450,images[i].width/3, images[i].height/3); image(images[i], k, 600,images[i].width/3, images[i].height/3); image(images[i], k, 750,images[i].width/3, images[i].height/3); i++; } } } void shakeTetris(boolean big){ if (big){ int i =0; for (z = 10; z <= width; z+=width/17 + 30){ image(images[i], z+(random(-7,7)), 0+(random(-7,7)),images[i].width/3, images[i].height/3); image(images[i], z+(random(-4,4)), 150+(random(-7,7)),images[i].width/3, images[i].height/3); image(images[i], z+(random(-2,4)), 300+(random(-7,7)),images[i].width/3, images[i].height/3); image(images[i], z+(random(-4,2)), 450+(random(-7,7)),images[i].width/3, images[i].height/3); image(images[i], z+(random(-3,3)), 600+(random(-7,7)),images[i].width/3, images[i].height/3); image(images[i], z+(random(-6,6)), 750+(random(-7,7)),images[i].width/3, images[i].height/3); i++; } } } void largeTetris(boolean big){ if (big){ int i =0; for (z = 10; z <= width; z+=width/17 + 30){ image(images[i], z+(random(-7,7)), 0+(random(-7,7)),images[i].width, images[i].height); image(images[i], z+(random(-4,4)), 150+(random(-7,7)),images[i].width, images[i].height); image(images[i], z+(random(-2,4)), 300+(random(-7,7)),images[i].width, images[i].height); image(images[i], z+(random(-4,2)), 450+(random(-7,7)),images[i].width, images[i].height); image(images[i], z+(random(-3,3)), 600+(random(-7,7)),images[i].width, images[i].height); image(images[i], z+(random(-6,6)), 750+(random(-7,7)),images[i].width, images[i].height); i++; } } } void twistTetris(){ int i =0; for (z = 10; z <= width; z+=width/17){ angle = angle + 0.2; translate(500,370); rotate(angle); image(images[i], q, z,images[i].width/2, images[i].height/2); image(images[i], q+300, z,images[i].width/3, images[i].height/3); image(images[i], q+700, z, images[i].width/4, images[i].height/4); i++; q+= 2; if (q > 780){ q = -15; } } } void drawTetris(){ int i =0; for (z = 10; z <= width; z+=123){ image(images[i], z, q+(random(1, 500))); image(images[i], z, (q-600)+(random(1,500))); i++; q+= 2; if (q > 740){ q = -15; } } }