Loading...
Logo
Processing Forum
Hey guys I need help with some short code. I need to know how to make text fall from top to bottom in a slow manner. It needs to loops so everytime it reaches the bottom it gets repeated. This also has to be produced randomly on the screen so it is not repetitive. Also if possible I need some help making an array for this so it loops and the word or text is falling down again in random spots with more than one word on the screen. If possible I do not want them falling too fast.

Replies(11)

I started with this but I can only do it with circles now how do i do it with text and not as constant as this array. So a little slower.

class Rain {
  
float r = random(600);
float y = 0;

void fall() {

 y = y + 7;
 fill(0,10,200,180);
 ellipse(r, y, 10, 10);

}

}

int numDrops = 0;
Rain[] drops = new Rain[400]; 

void setup() {
 
  size(600, 600);
 background(255);
 smooth();
 noStroke();
 drops[0] = new Rain(); 
}

void draw() {
 
 fill(255, 80);
 rect(0, 0, 600, 600);
 fill(0);
 rect(mouseX,550,30,10);
 if (random(10) < 1) {
   drops[++numDrops] = new Rain(); 
 }
 //Loop through array to use objects.
 for (int i = 0; i < numDrops; i++) {
   drops[i].fall();
 }
}
A very simple example... I would make a little FallingWord class to replicate this for a larger number. The speed can be controlled by the increment used in y axis (frameCount here).
Copy code
  1. String word = "estou";

  2. PFont font;
  3. float xpos;
  4. float ypos;
  5. float speed = 4; 

  6. void setup(){
  7.   size(400,400);
  8.   font = createFont("arial",32);
  9.   smooth();
  10.   fill(0);
  11.   background(255);
  12.   xpos = random (width);
  13. }

  14. void draw(){
  15.   background(255);
  16.   ypos +=speed;
  17.   text(word, xpos, ypos);
  18.   if (ypos > height ){
  19.   xpos = random (width);
  20.   speed = random(0.5, 3);
  21.   ypos = 0;
  22. println("new xpos = " + xpos + " new speed = " + speed);
  23. }
  24.   
  25. }
Unfortunately I am really new to this :$ so where exactly would I increase the speed ? and how would I make it loop so it is repeated. Thank you for your help
Unfortunately I am really new to this :$ so where exactly would I increase the speed ? and how would I make it loop so it is repeated. Thank you for your help
It is repeating, at least here it is. The modulo  '%' operator does it.  I edited the code above (in bold) so it is easier to understand. frameCount is an internal variable holding the numbers of frames (one run trough draw()) since the sketch started. Ask more if you need.
okay awesome i get it
Perfect now how would I produce these words without follow the mouse position so it would generate randomly on the screen. What I am trying to do it make a little game to catch words:

void draw(){
  background(255);
 String word = "estou";

PFont font;
float xpos;
float ypos;
float speed = 3; 

void setup(){
  size(400,400);
  font = createFont("arial",32);
  smooth();
  fill(0);
  background(255);
  xpos = random (width);
}

void draw(){
  background(255);
  ypos +=speed;
  text(word, xpos, ypos%height);
  if ((ypos%height) == 0)
  xpos = random (width);
  
}
  rect(mouseX,350,30,10);
}
Hi, I changed the code again removing the modulo. It was failing to test every time for out of screen. Now i reset ypos. Also it gets a random speed each new cycle. The code you pasted is not compiling. There is a lot of examples like this catch game in the forum. You will need to have some collision detection
okay this awesome !!!! Thank you!! now I got to figure out how to increase the speed at a constant rate until it hits a peak and stays at that speed. So between 1.8 being the lowest speed and as soon as it hits 5 for example it stays like that and records your score until you lose. This is basically for a simple game I have to make for class.
is there anyway to do this?
well not really what you asked for (I just saw it), but I made this that you might like to have a look (you need a txt file in the data folder of your sketch, you can use "add file" from sketch menu) . I got to go now, later If you have not figured out I can try to help you. Do some search in the forum meanwhile ...


Copy code
  1. String [] s;
  2. ArrayList <FallingWord>  words = new ArrayList<FallingWord>();
  3. PFont font = createFont("arial", 20);


  4. void setup() {
  5.   size(1000, 600);

  6.   // load text
  7.   s = loadStrings("leminsk.txt");

  8.   // split in words
  9.   for (int i = 0; i < s.length; i++) {

  10.     String [] lineWords = splitTokens(s[i]);

  11.     for (int j = 0; j < lineWords.length; j++) {
  12.       words.add(new FallingWord(lineWords[j]));
  13.     }
  14.   }
  15.   noStroke();
  16.   smooth();
  17. }

  18. void draw() {

  19.   background(255);
  20.   for (FallingWord fw: words) {
  21.     fw.update();
  22.     fw.draw();
  23.   }
  24. }


  25. class FallingWord {

  26.   String word;
  27.   float xpos, ypos, speed, acc;
  28.   color filler;
  29.   float fSize;



  30.   FallingWord (String _word) {
  31.     word = _word;
  32.     initWord();
  33.   }

  34.   void initWord() {
  35.     xpos   = random (width);
  36.     ypos   = random(-height/2, -20);
  37.     speed  = random(0.4, 1);
  38.     acc    = random(0.01, 0.08);
  39.     filler = color(0, random(180, 250));
  40.     fSize  = random(14, 22);
  41.   }




  42.   void update() {
  43.     speed+=acc;
  44.     ypos +=speed;
  45.     if (ypos > height ) {
  46.       initWord();
  47.     }
  48.     if (mousePressed)
  49.       initWord();
  50.   }

  51.   void draw() {
  52.     textFont(font);
  53.     fill(filler);
  54.     textSize(fSize);
  55.     text(word, xpos, ypos);
  56.   }
  57. }//eof FallingWord