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 › using key for changing step in celullar automata
Page Index Toggle Pages: 1
using key for changing step in celullar automata (Read 988 times)
using key for changing step in celullar automata
May 5th, 2010, 10:59pm
 
hello, i been trying to make this cellular automata program, the idea is to change the step with the key, so each time i press the key "n" it goes to a step further.
The objective is when i run the program the cellullar automata is stopped and  everytime i press "n" it calculates a new generation or step.

The thing is that the program doesnt work perfect, i think this is because of the KeyPressed command, for example if i have this:
Code:

if(keyPressed) {
if (key == 'n' || key == 'N') {
sube = sube + 1;
sube = sube % 2;
print(" " + sube);
}
}


I would like that each time i press the "n" key it prints 0, and if i press "n" again it prints 1, and if i print "n" again it prints 0 and so on.
But it doenst work as i expected and if i press "n" i get  1 0  , sometime i get 1 0 1 and sometimes i get 1 or 0.


here is the code

Code:


int numero_de_cubos = 13;
int mouse_x;
int mouse_y;
int sube = 0;

cubo[][] matriz_de_cubos = new cubo[numero_de_cubos][numero_de_cubos];

void setup() {
size(390,390);
frameRate(20);
smooth();
background(255, 255, 255);
for (int i = 0 ; i< numero_de_cubos ; i++ ){
for (int j = 0 ; j< numero_de_cubos ; j++ ){
matriz_de_cubos[i][j] = new cubo(i * 30, j * 30 , random(300));
}
}
}
void draw() {
background(0, 255, 255);
if(keyPressed) {
if (key == 'b' || key == 'B') {
presiona() ;
}

if (key == 'n' || key == 'N') {
sube = sube + 1;
sube = sube % 2;

print(" " + sube);
}
}

for (int i = 0 ; i< numero_de_cubos ; i++ ){
for (int j = 0 ; j< numero_de_cubos ; j++ ){
matriz_de_cubos[i][j].dibuja();

if (sube == 0) {

if(i < numero_de_cubos - 1 && i > 1 && j < numero_de_cubos - 1 && j > 1 ){
if( (matriz_de_cubos[i + 1][j].ron + matriz_de_cubos[i - 1][j].ron + matriz_de_cubos[i][j + 1].ron + matriz_de_cubos[i][j - 1].ron ) == 1 ){
matriz_de_cubos[i ][j].ron = 0;
}
else if(( matriz_de_cubos[i + 1][j].ron + matriz_de_cubos[i - 1][j].ron + matriz_de_cubos[i][j + 1].ron + matriz_de_cubos[i][j - 1].ron) < 1) {
matriz_de_cubos[i ][j].ron = 1 ;
}
else if(( matriz_de_cubos[i + 1][j].ron + matriz_de_cubos[i - 1][j].ron + matriz_de_cubos[i][j + 1].ron + matriz_de_cubos[i][j - 1].ron) > 2 ) {
matriz_de_cubos[i ][j].ron = 1;
}
}
}
}
}
sube = 1;
}

void presiona() {
for (int i = 0 ; i< numero_de_cubos ; i++ ){
for (int j = 0 ; j< numero_de_cubos ; j++ ){
matriz_de_cubos[i][j].randomiza();
matriz_de_cubos[i][j].dibuja();
}
}
}

void mousePressed() {
mouse_x = mouseX * numero_de_cubos / width ;
mouse_y = mouseY * numero_de_cubos / height ;
}

class cubo {
int xx;
int yy;
float frecuencias;
float ron = random(2);
color coloro ;

cubo(int x, int y , float valores_frecuencias ) {
xx = x;
yy = y;
frecuencias = valores_frecuencias;
ron = int(ron);
}
void dibuja(){
if (ron == 1){
coloro = color(220, 10, 60);
}
else if (ron == 0){
coloro= color(55, 5 ,255);
}
else if (ron == 2){
coloro= color(5, 225 ,25);
}
fill(coloro);
rect (xx, yy, 30, 30 );
}
void vecinos(){

}

void randomiza(){
ron = random(2) ;
ron = int(ron);
}
}




Re: using key for changing step in celullar automata
Reply #1 - May 6th, 2010, 12:16am
 
boolean trigger;
void setup(){ trigger = false; }
void draw(){ if( trigger ){ println( "Some key was pressed quickly!" ); trigger = false; } }
void keyTyped(){ trigger = true; }
Re: using key for changing step in celullar automata
Reply #2 - May 6th, 2010, 6:24am
 
Yep - that's a common problem with keyPressed: it's not recording the instant that the key is pressed but the state of it being pressed, so it can trigger over multiple frames even with one seemingly short press.

You can take the approach TFGuy suggests or alternatively you should be able to use keyPressed() or keyReleased(), which are one-off events (though note the caveat about holding keys and key repeats, which is also an issue with TFGuy's approach).

If the issue with key repeats is a problem for you then you have to adopt a slightly different solution:

Code:
// adapted from keyPressed() example:
// http://processing.org/reference/keyPressed_.html

boolean isPressed;
int value = 0;

void draw() {
 fill(value);
 rect(25, 25, 50, 50);
}

void keyPressed() {
 // only record a key having been pressed if it is currently released
 if (!isPressed) {
   isPressed = true;
   
   if (value == 0) {
     value = 255;
   } else {
     value = 0;
   }
 }
}

void keyReleased() {
 // a key can only be released if it is first pressed
 if (isPressed) {
   isPressed = false;
 }
}


Though note that if working with several different inputs you'll need separate booleans for each...
Re: using key for changing step in celullar automata
Reply #3 - May 6th, 2010, 11:57am
 
cool the second one works perfect but what does it means
!isPressed?

what does it means the ! before the name of the variable?


thanks
Re: using key for changing step in celullar automata
Reply #4 - May 6th, 2010, 12:47pm
 
! (logical NOT) can be found quickly by searching ! in the reference index... Wink
Page Index Toggle Pages: 1