Category: Slick 2D

2D Rain Effect

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){

/** 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(){

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

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 */

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.


Hola EveryOne.

There are various methods/ways to detect a collision between two objects. Each having its own advantage and disadvantage and based on our requirement we implement one of the collsion detection algorithm.

Here i am not going to talk about collision algorithm. I am going to talk about determining the nearest object with which collision will take place. Suppose there are many objects across the screen and u have to detect the collision between your game character and those objects. And you run the algorithm to detect collision with each of every object in every frame. So it decreases the efficiency of your program. Its better to detremine the nearest object to your game character and detect collision with that determined nearest object.

Here i m using bounding rectangle concept for collision detection.

Code Example:

public static Sprite getNearestGroundSprite(ArrayList sprites,Sprite hero){
Sprite sprite=null;
float minDistance=9999;
for(int i=0;iscreenWidth || (sprites.get(i).getSX()+sprites.get(i).getWidth()<0)){ continue; } else /* ignore all those objects which are located above the game character */ if(hero.getSY()>sprites.get(i).getSY()){
/* ignore all those objects that are located at the left side of game character */
/* ignore all those objects that are located at right side of game character */

Sprite is just a class representing characters of game. getSX() and getSY() returns the x and y coordinate location of that sprite. getWidth() simply returns the width of the sprite image.

In above code i am ignoring all those sprites that are located at left, right and top of my game character because i need bottom sprite to check collision with i.e when character falls down. This way the number of sprites decreases for checking collisions. You can write function for finding nearest left and right sprites also. You need to call this function at right place and when needed.

This is very simple and basic way i started and it gets the job done. You can write your own optimized code. This is how i started. I have written my optimized code for finding nearest sprite but right now i won't be posting it but i'l share it soon in near future.