CODE 10
//Main Code
Drop[] drops = new Drop[100] ;
void setup(){
size(640, 360); //size of the screen
for (int i= 0; i < drops.length; i++) { // creating a loop to add 100 drops into the array
drops[i] = new Drop();
}
}
void draw() {
background(random(255)); //color of the screen
for (int i = 0; i < drops.length; i++) { //creating a loop to draw 100 drops
drops[i].fall();
drops[i].show();
}
}
//Sub Code
class Drop {
float x = random(width) ; //random location to star on the screen
float y = random(-500,-100); // object starting randomly off the screen
float yspeed = random(4, 10); //random speed of the drop
void fall() { //function to make it fall
y = y + yspeed; // a variable to reset the y axis - to make it look as it is moving
yspeed = yspeed + 0.05; //increase speed while falling
if (y> height) {
y = random (-200, -100); // this will make the drop reset back to the top to star again
yspeed = random(4, 10); //reset speed
}
}
void show() { // function to show or rendered on the screen
stroke(148, 100, 226); // color of the object or text
textSize(random(12,60));
fill(random(255));
text("Yann",x,y);
}
}
CODE 10
Function Analyse
As clearly seen above, the code makes the letter falling like the rain. This code can be splitted into main code and sub code. The main code creates an array of many lines moving from the top of the screen down as if it was rain. The number inside the Drop{} decides the number of the 'rains' to drop. Then the main code part also declared the background colour and the size of the screen. The line, for (int i = 0; i < drops.length; i++) create a loop to add 100 drops into the array.
Then, the sub code is the part where it is able to change the speed and location of the rain. In this part of the code, the rain can also changed into words and phrase. This part of code is also the part where the 'rains' fall.
float x = random(width) ; //random location to star on the screen
float y = random(-500,-100); // object starting randomly off the screen
float yspeed = random(4, 10); //random speed of the drop
This part of the code decide the starting location of the dropping letters and it also controls the speed of the drop. The another part of the code will reset the speed and the location which makes the whole thing move continuously :
if (y> height) {
y = random (-200, -100); // this will make the drop reset back to the top to star again
yspeed = random(4, 10); //reset speed
}
}
The last part of the code change the rain into any phrase, in this case, it was changed into the word "Yann"
text("Yann",x,y);
Original Code
Drop[] drops = new Drop[100] ; //this is an array for making 100 drops to fall. To make more increase the number. It is calling the function Drop is that we created.
void setup(){
size(640, 360); //size of the screen
for (int i= 0; i < drops.length; i++) { // creating a loop to add 100 drops into the array
drops[i] = new Drop();
}
}
void draw() {
background(230,230,250); //color of the screen
for (int i = 0; i < drops.length; i++) { //creating a loop to draw 100 drops
drops[i].fall();
drops[i].show();
}
}
class Drop {
float x = random(width) ; //random location to star on the screen
float y = random(-500,-100); // object starting randomly off the screen
float yspeed = random(4, 10); //random speed of the drop
void fall() { //function to make it fall
y= y + yspeed; // a variable to reset the y axis - to make it look as it is moving
yspeed = yspeed + 0.05; //increase speed while falling
if (y> height) {
y = random (-200, -100); // this will make the drop reset back to the top to star again
yspeed = random(4, 10); //reset speed
}
}
void show() { // function to show or rendered on the screen
stroke(148, 43, 226); // color of the object or text
line(x, y, x, y+10); // here we are creating a line to represent a drop
}
}
Original Code
Modification
-
The lines rain was changed into the phrase Yann
-
The speed of the rain has been changed
-
The colour of both the background and the phrase has been changed