We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Question about my code
Page Index Toggle Pages: 1
Question about my code (Read 436 times)
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?
Re: Question about my code
Reply #1 - Jun 30th, 2009, 1:05am
 
You need to extract the ellipse() from the framecounter==50 condition otherwise it will only get drawn when the condition is true - i.e. every 50 frames.  The following is untested and could probably be improved:

Code:
if (frameratecounter == 50){
  background (0);
  getal = int(random(25,75));
  println(getal);
  color fillColour = color(0,0,0);
  if (getal < 50) {
    fillColour = r;
    }  
  else {
    fillColour = g;
  }
 frameratecounter = 1;
} else{
 frameratecounter++;
}  

fill(fillColour);
ellipse(25,25,50,50);
Re: Question about my code
Reply #2 - Jun 30th, 2009, 1:07am
 
No wonder, you draw the circle only once every 50 frames...
You have to put the calculation of getal in the test, but do the fill()/ellipse() every time. Since you draw at the same place, you can put only the fill in the conditional and put the ellipse() call after it (ie. same call whatever the fill() call).
Re: Question about my code
Reply #3 - Jun 30th, 2009, 1:43am
 
blindfish wrote on Jun 30th, 2009, 1:05am:
You need to extract the ellipse() from the framecounter==50 condition otherwise it will only get drawn when the condition is true - i.e. every 50 frames.  The following is untested and could probably be improved:

Code:
if (frameratecounter == 50){
  background (0);
  getal = int(random(25,75));
  println(getal);
  color fillColour = color(0,0,0);
  if (getal < 50) {
    fillColour = r;
    }  
  else {
    fillColour = g;
  }
 frameratecounter = 1;
} else{
 frameratecounter++;
}  

fill(fillColour);
ellipse(25,25,50,50);


The above code doesn't work. I still don't get it. I understand what you are saying, but to actually do it and make it work is a whole different story. Thanks anyway!
Re: Question about my code
Reply #4 - Jun 30th, 2009, 2:32am
 
CMD2 wrote on Jun 30th, 2009, 1:43am:
The above code doesn't work.

Just declare fillColour before setup to make it global (it has limited scope in the given snippet), or follow my advice... Smiley

OK, to be explicit:
Code:
if (frameCount % 50 == 0){
background (0,0,0);
getal = int(random(25,75));
println(getal);
}
if (getal < 50) {
fill(r);
}
else {
fill(g);
}
ellipse(25,25,50,50);

Tested as much as blindfish's code... :-P
Re: Question about my code
Reply #5 - Jun 30th, 2009, 5:39am
 
PhiLho  wrote on Jun 30th, 2009, 2:32am:
CMD2 wrote on Jun 30th, 2009, 1:43am:
The above code doesn't work.

Just declare fillColour before setup to make it global (it has limited scope in the given snippet), or follow my advice... Smiley

OK, to be explicit:
Code:
if (frameCount % 50 == 0){
  background (0,0,0);
  getal = int(random(25,75));
  println(getal);
}
  if (getal < 50) {
    fill(r);
    }  
  else {
    fill(g);
  }
    ellipse(25,25,50,50);

Tested as much as blindfish's code... :-P


This one actually works! But I can't test it right now in the framedifferencing code, cause processing can't import my webcam here at home! I don't know why..maybe it has something to do with my settings. Thanks anyway!
Page Index Toggle Pages: 1