Hola Everyone.
I am working on a level for 2d game where i needed to add a rain effects. I am using Slick 2D library for making my 2d game. Before i started working on making 2D rain effect , i discussed this topic on slick 2d forum for how to get started ? Here is the forum thread. You can check it out.
So i wrote a simple code to create a rain effect:
1) Make a Rain Drop, i wrote a class for Rain Drop that extends Polygon class. Yes i m creating rain drops with polygons.
2) Create number of drops at random coordinates and simulate it across the screen i.e make them fall. You can use wind factor also for providing bit more interesting.


So this is the way i started.

Here is the Rain Drop class:


import org.newdawn.slick.geom.Polygon;

public class RainDrop extends Polygon{
public RainDrop(int size){
super();

/** coordinates of vertices of polygon(rain drop) */
this.addPoint(0, 0);
this.addPoint(0, size);
}
}

This is the main class where we are using rain drops and simulating it to create a simple rain effect, nothing fancy.


import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Rainy extends BasicGame{

private RainDrop[] drops;
private Image background;

public Rainy(){
super("");
}

public static void main(String[] args){
try {
AppGameContainer app = new AppGameContainer(new Rainy());
app.setDisplayMode(1024, 768, true);
app.setTargetFrameRate(100);
app.setShowFPS(false);
app.start();
} catch (SlickException ex) {
Logger.getLogger(Rainy.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
public void init(GameContainer gc) throws SlickException {
background=new Image("data/jungle.jpg");

/** creating 700 rain drops and placing it on random location across the screen */
initializeRain(900);
}

@Override
public void update(GameContainer gc, int delta) throws SlickException {

for(int i=0;i768){

drops[i].setX((float) ((Math.random() * 2000)+30));

drops[i].setY((float) ((Math.random() * 5)));

}

}

}

public void render(GameContainer gc, Graphics g) throws SlickException {
g.drawImage(background, 0, 0);
g.setColor(new Color(188.0f,227.0f,229.0f,0.3f));
for(int i=0;i

So what i am doing :
1) initializeRain(int num_of_drops) takes an int parameter which denotes the number of rain drops. This function creates a number of rain drops(instance of raindrop class) and assigns the random x and y position to each rain drops.
2) Then we simply iterate the array of rain drops and translate them across the screen. When drops reaches the bottom of screen then we simply restore back to random initial starting postion. This keeps going on and gives the impression of rainy effect.

This is very simple 2d effect that i started with. You can write your own optimized version and can add more feature to give fancy impression like adding alpha values for each rain drop etc.

Gracias.