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 › random() and open sketch file in a sketch file
Page Index Toggle Pages: 1
random() and open sketch file in a sketch file (Read 941 times)
random() and open sketch file in a sketch file
Nov 28th, 2009, 4:43pm
 
Hello, everyone! I am a newbie when it comes to processing, I can understand alittle bit but a lot of times I need explanations.  I really need some help on this question.  

Currently I found a script written by someone which i really like because it takes the time to explain how things work.  I am currently messing around with it and this is what i have so far


float a;

void setup()  
{
 size(800,600);
 stroke(#9d0000);
 frameRate(30);
}

void draw()
{
 
a -= 0.08;
 
background(0);  

 for (int x = -5; x < 25; x++) {
  for (int z = -5; z < 25; z++) {

   int y = int(24 * cos(-0.55 * distance(x,z,0,0) + a));


   float xm = x*17 -8.5;
   float xt = x*17 +8.5;
   float zm = z*17 -8.5;
   float zt = z*17 +8.5;
   
   int halfw = (int)width/2;
   int halfh = (int)height/2;
   
      int isox1 = int(xm - zm + halfw);
   int isoy1 = int((xm + zm) * 0.5 + halfh);
   int isox2 = int(xm - zt + halfw);
   int isoy2 = int((xm + zt) * 1 + halfh);
   int isox3 = int(xt - zt + halfw);
   int isoy3 = int((xt + zt) * 0.5 + halfh);
   int isox4 = int(xt - zm + halfw);
   int isoy4 = int((xt + zm) * 1 + halfh);
   
   fill (#9d0000);
   quad(isox2, isoy2-y, isox3, isoy3-y, isox3, isoy3+40, isox2, isoy2+40);
   fill (#9d0000);
   quad(isox3, isoy3-y, isox4, isoy4-y, isox4, isoy4+40, isox3, isoy3+40);
   
   fill(#9d0000 + y * 0.05);
   quad(isox1, isoy1-y, isox2, isoy2-x, isox3, isoy3-y, isox4, isoy4-x);
  }
 }
}

float distance(float x,float y,float cx,float cy) {
 return sqrt(sq(cx - x) + sq(cy - y));
}

void keyPressed() {
 if (key == CODED) {
   if (keyCode == UP) {
     a -= 0.08 + .1;
   } else if (keyCode == DOWN) {
     a -= 0.08 + .2;
   } else if (keyCode == LEFT) {
     a -= 0.08 + .3;
   } else if (keyCode == RIGHT){
     a -= 0.08 + .5;
 } else {
   a -= 0.08;  
 }
}

currently I have where you push the arrow keys it increases the radius and speed.  Thats good and what I want but i want it so instead of having to push the keys, it does it randomly by itself.
I know about the random function but i cant seem to get it to work.  Any help on how to make this work would be great.  Also if anyone knows how to make it simplier (if thats a word) that would be great as well.  Again the majority of this is not my coding the only thing i did was change some numbers here and there and typed the key functions.  im a newbie. thanks

Also does any one know if there is a code that allows you to open a sketch file within another sketch file?
Re: random() and open sketch file in a sketch file
Reply #1 - Nov 29th, 2009, 5:19am
 
Add this function and call it from draw().
It's not exactly what you had, but you get the drift ..

Code:

long nextMove = 0;
void pressKeys()
{
 if(millis() > nextMove)
 {
   int r = (int)random(4);
   a -= 0.08 + (r+1)*.1;

   //if you want the next key press to be randomly timed (max. 1 sec wait)
   nextMove = millis() + (long)random(1000);
 }
}
Re: random() and open sketch file in a sketch file
Reply #2 - Nov 29th, 2009, 1:39pm
 
thanks for the code but i must be doing something wrong cause im getting the same results.  no errors but no difference.  Could you elaborate more as to where to insert this code?  sorry for being a noob Tongue
Re: random() and open sketch file in a sketch file
Reply #3 - Nov 30th, 2009, 12:45am
 
Quote:
call it from draw()

Means you have to add the given code at the end of the sketch, and to add pressKeys(); somewhere in draw(), eg. at the start of the function.
Page Index Toggle Pages: 1