""

Tag Archive: 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){
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.



Slick2D engine is a high performance 2d game library based on a LWJGL(Lightweight Java Game Library). You can see the current features of Slick2D in their site. Download the library from here.
You can check the games made in Slick2D engine http://www.cokeandcode.com/ and http://slick.cokeandcode.com/gallery.php.

Tutorials : There are good and easy to go tutorials for beginners.

Now download the library. So here i’ll be telling how to setup the library with netbeans 6.5.1 so that u can write compile and run slick games easily and fast. I am using Ubuntu 8.04(Hardy heron), steps are similar for setting up in windows.

STEP 1: Download the library and save it in some appropriate place.
STEP 2: Open your Netbeans 6.5.1.
STEP 3: Go to tool->libraries. Library Manager window will open. Click New Library. Give it a name Slick. Then click OK.

screenshot-11

STEP 4: Now click classpath tab. Click Add Jar/Folder. Browse the folder where u have saved Your slick library. Go to lib folder and add all the jar files except slick.jar. Click OK

screenshot-2

STEP 5: Click JavaDoc tab. Click add/zip floder. Browse ur slick folder and select javadoc folder and add it. So done. Click OK.

screenshot-3

STEP 6: Ok so your slick library has been created. Now few steps remaining to complete the full set up. Create a new java project called SlickTest. Now Right Click SlickTest project, then click properties. Project properties window will open up. At left side, under categories select Libraries. Now at right side click Compile tab. Now Click Add Jar/Folder. Browse ur slick folder, go to lib folder, select slick.jar and add it.

screenshot-4

STEP 7: Now click Run tab. Click Add Library. Add Library window will open up. From there select Slick library that we previously created. Then add it.

screenshot-5

FINAL STEP 8 : Now at left side select Run. At Right side, go to VM Options, there u have to provide the parameter/argument that will link lwjgl libraries to your application. So at VM Options textbox, For Ubuntu users write: -Djava.library.path=lwjgl library path For Windows user write: -Djava.library.path=”lwjgl library path”. And Click Ok


Thats it Windows users have to put the path within double quotes. Here lwjgl library path is the path to the location where .so files for ubuntu linux(liblwjgl.so, liblwjgl64.so, libodejava.so,libopenal.so etc) and .dll for windows(liblwjgl.dll, liblwjgl64.dll, libodejava.dll,libopenal.dll etc) are located.

NOTE: Beware when you copy double quotes from this post

For windows user .dll files comes with slick library but linux users have to download the native .so lib files.

So now you are done and lets write a simple program for testing. This code will just create a window. Create a class called Test under the project that we had created.

package com.padam.testing;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

/**
*
* @author padam
*/
public class Test extends BasicGame{

public Test(String name){
super(name);
}

public static void main(String[] args){
try {
AppGameContainer container = new AppGameContainer(new Test(“Game”));
container.setDisplayMode(800,600,false);
container.start();
} catch (SlickException e) {
e.printStackTrace();
}
}

@Override
public void init(GameContainer gc) throws SlickException {

}

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

}

public void render(GameContainer gc, Graphics g) throws SlickException {

}

}

NOTE: Linux and Mac users must download the lwjgl library. Then browse the folder and go to native folder. There choose your native OS folder. That will be your lwjgl library path thatt will go to VM Options parameter as specified on last step or you can copy the .so/native files to your convenient location and provide that path to VM Options parameter.
Now after this just test the above code. You may get a runtime error saying:

Exception in thread “main” java.lang.LinkageError: Version mismatch: jar version is ’16’, native libary version is ’17’
at org.lwjgl.Sys.(Sys.java:105)
at org.lwjgl.opengl.Display.(Display.java:128)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.(AppGameContainer.java:36)
at com.padam.testing.Test.main(Test.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Linux/Mac/Solaris might face this problem because lwjgl jar files that comes with slick might be of older version which won’t match with the new lwjgl native .so/native files that u have downloaded. This means that the lwjgl jar files and lwjgl native/ .so files doesn’t match. So what u have to do is browse lwjgl folder that u have downloaded and go to jar folder and copy all the jar files and paste/replace the old jar files in the lib folder located in slick folder or location of slick library.

So Thats it. If u face any problem, comment it.

Gracias.