Letter l = new Letter(150, 15, 1.5);
void setup(){
size(300, 300);
}
void draw(){
background(0);
l.display();
}
class Letter{
PVector pos;
float speed;
String letter;
float change_threshold = 0.1;
color cor = color (3, 160, 98);
String character = "";
// maximum letters in a string
int maxLetters = 35;
// which character to swap
int charIndex = 0;
Letter (float xpos, float ypos, float vel) {
for (int i = 0; i < maxLetters; i++) {
character +=getRandomLetter() +"
";
}
this.pickLetter();
pos = new PVector (xpos, ypos);
speed = vel;
}
void display() {
fill(this.cor);
text(this.letter, this.pos.x, this.pos.y);
float p = random(1);
if (p < this.change_threshold && this.cor != color(255)) {
this.pickLetter();
}
}
void pickLetter() {
//String character = str (floor (random(10)));
//String character = new String ("a");
//character += char (int(random(65, 65+24)));
char randomChar = getRandomLetter();
character = setCharAt(character, randomChar, charIndex);
charIndex = (charIndex + 2)%character.length();
this.letter = character;
}
void fall() {
this.pos.y += this.speed;
}
// returns a random a-z char
char getRandomLetter() {
return char (int(random(65, 65+24)));
}
// return a new string with a char swapped at the given index
String setCharAt(String myString, char myNewChar, int myCharIndex) {
return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1);
}
}