CMD2
YaBB Newbies
Offline
Posts: 7
Question about my code
Jun 30th , 2009, 12:11am
I wrote this code, but it's nog working properly. Every 2 seconds, you see a green or red ellipse in the left corner. This ellipse is supposes to stay there and then change color. Why is is disappearing again? This might be a noob question, but im not very good at this. import processing.video.*; int numPixels; int[] previousFrame; Capture video; int r; int g; int getal = 0; int frameratecounter = 50; int startTime; int time; int restTime; float score = 0; PFont font; void setup() { size(640, 500); r = color(255, 0, 0); g = color(0, 255, 0); frameRate(25); video = new Capture(this, width, height, 25); numPixels = video.width * video.height; previousFrame = new int[numPixels]; loadPixels(); rect(0,0,640,20); startTime = millis(); font = loadFont("Ziggurat-HTF-Black-32.vlw"); } void draw() { if (frameratecounter == 50){ background (0,0,0); getal = int(random(25,75)); println(getal); if (getal < 50) { fill(r); ellipse(25,25,50,50); } else { fill(g); ellipse(25,25,50,50); } frameratecounter = 1; } else{ frameratecounter++; } if (video.available()) { video.read(); video.loadPixels(); int movementSum = 0; float points = 0; time = millis() - startTime; restTime = ((10000 - time) / 1000); if (time < 10000) { for (int i = 0; i < numPixels; i++) { color currColor = video.pixels[i]; color prevColor = previousFrame[i]; int currR = (currColor >> 16) & 0xFF; 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(currColor); previousFrame[i] = currColor; } points = abs((movementSum - 3000000)/4000000); score += points; // To prevent flicker from frames that are all black (no movement), // only update the screen if the image has changed. if (movementSum > 0) { updatePixels(); image(video, 0, 20); fill(255); rect(0, 0, 640, 20); fill(0, 255, 0); rect(0, 0, score, 20); fill(0); textFont(font, 16); text(restTime, 600, 18); } } else { fill(255); rect(0, 20, 640, 480); textFont(font, 32); textAlign(CENTER); fill(0); text("Je score is:", 320, 220); text(score, 320, 260); textFont(font, 16); text("Druk spatiebalk om opnieuw te starten.", 320, 290); } } } void keyPressed() { if (key == ' ') { //als de spatie balk gedruikt word,dan: startTime = millis(); score = 0; } } Can someone help me?