___ _ _ _ |_ | | | ( ) | | | | __ _ _ __ ___| | _|/ ___ __ _____| |__ | |/ _` | '__/ _ \ |/ / / __| \ \ /\ / / _ \ '_ \ /\__/ / (_| | | | __/ < \__ \ \ V V / __/ |_) | \____/ \__,_|_| \___|_|\_\ |___/ \_/\_/ \___|_.__/ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
THUNDERSTORM.JAVA
--
--
This is the raw java file generated from Processing. I took inspiration from a youtube channel called The Coding Train.
I've added some additonal features like splashes, lightnings and clouds. The lightning looks like trash, but I've decided to keep it as it is, because there is a nice for statement there which works absolutely flawlessly. The two arrays store the lightnings coordinates.
To turn on realistic cloud textures uncomment marked parts of the code. I have not bothered with explaining the code so you will have to explore it on your own.
Overall I am pretty happy with the end results and I do plan on expanding the code a bit more in the future.
/*************************************************** Thunderstorm - original idea by: The Coding Train - https://www.youtube.com/user/shiffman (c) Dorint 2018 ***************************************************/ import processing.core.*; import processing.data.*; import processing.event.*; import processing.opengl.*; import java.util.HashMap; import java.util.ArrayList; import java.io.File; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; public class thunderstorm extends PApplet { float bg = 51; float prevBG; float col1; float colPrev; float num; float[] time = {0,0,0}; float[] del = {0,0,0}; PImage texture1; PImage texture2; PImage texture3; rain[] drop = new rain[500]; lightning[]lightning = new lightning[3]; cloud[] cloud = new cloud[20]; public void setup(){ background(70); texture1 = loadImage("cloud.png"); texture2 = loadImage("cloud2.png"); texture3 = loadImage("cloud3.png"); for(int i =0; i< cloud.length; i++){ cloud[i] = new cloud(); } for(int i = 0; i < lightning.length; i++){ lightning[i] = new lightning(); } for(int x = 0; x< drop.length; x++){ drop[x] = new rain(); } } public void draw(){ background(70); rain(); strike(); cloud(); } public void rain(){ for(int i = 0; i< drop.length; i++){ drop[i].move(); drop[i].display(); } } public void strike(){ del[0] = random(7000,15000); if(millis() >= time[0] + del[0]){ for(int i = 0; i < lightning.length; i++){ time[0] = millis(); lightning[i].get_pos(); lightning[i].display(); } } } public void cloud(){ del[1] = 2; if(millis() >= time[1] + del[1]){ for(int i = 0; i < cloud.length; i++){ time[1] = millis(); cloud[i].move(); cloud[i].display(); } } } public class cloud{ float xSpeed = random(-0.5f,0.5f); float y = random(0, 100); float x = random(width); float[] del = {0, width}; float fill = random(50,255); float xSize = random(250, 500); float ySize = random(80, 200); int randomTexture = round(random(0,2)); public cloud(){ } public void move(){ x = x + xSpeed; if( x > width || x < 0){ if(xSpeed <= 0){ x = del[1]; } else if(xSpeed >= 0){ x = del[0]; } } } public void display(){ stroke(fill); fill(fill); //------uncomment for realistic cloud textures------// /*switch(randomTexture){ case 0: image(texture1, x, y, 300, 120); break; case 1: image(texture2, x, y, 300, 120); break; case 2: image(texture3, x, y, 300, 120); break; } */ //------comment for realistic cloud textures------// ellipse(x, y, 80, 80); ellipse(x+40, y, 80,80); ellipse(x+80, y, 80,80); ellipse(x+20, y-40, 80,80); ellipse(x+60, y-40, 80,80); //-----------------------------------------// } } public class lightning{ float x; float y; float Xline[] ={50,55,85,90,0,120}; float Yline[] = {40,0,40,80,80,200}; float xAdd; float yAdd; int r = 255, g = 253, b = 188; public lightning(){ } public void get_pos(){ x = random(100,500); y = random(100,500); } public void display(){ for(int i = 0; i < 5; i++){ if(i < 2){ stroke(r,g,b); fill(r,g,b); triangle(x - Xline[1], y - Yline[1], x - Xline[0], y - Yline[0], x - Xline[2], y - Yline[2]); } else if(i == 2){ stroke(r,g,b); strokeWeight(4); line(x - Xline[1], y - Yline[1], x - Xline[3], y - Yline[3]); } else{ stroke(r,g,b); strokeWeight(4); line(x - Xline[i], y - Yline[i], x - Xline[i+1], y - Yline[i+1]); } } } } public class rain{ float x = random(width); float y = random(-200, -100); float yspeed = random(5, 15); float weight = map(yspeed, 5, 15, 1, 4); float len = map(yspeed, 5, 15, 10, 20); float defAngle = 18; float angle = -18; float xNext; float yNext; float hypotenuse = map(yspeed, 5, 15, 10, 20); public rain(){ } public void move(){ y = y + yspeed; if(y>height){ splash(); y = random(-200, -100); } } public void display(){ stroke(186, 195, 224); strokeWeight(weight); line(x,y, x, y+len); } public void splash(){ stroke(186, 195, 224); for(int i = 0; i<11; i++){ angle = angle + defAngle; xNext = x + cos(angle) * hypotenuse; yNext = y + sin(angle) * hypotenuse; line(x,y, xNext, yNext); } } } public void settings() { size(600,600); } static public void main(String[] passedArgs) { String[] appletArgs = new String[] { "thunderstorm" }; if (passedArgs != null) { PApplet.main(concat(appletArgs, passedArgs)); } else { PApplet.main(appletArgs); } } }
--
BY DORINT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------