"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Thursday 25 April 2013

Fun programming: Java and Slick2D


Many may not believe me but programming can be fun. If you really love programming even watching a XML file going to, or coming, from a remote server can be fun but, of course, if your program produces something funny then programming is also more fun. So, while looking for something to relax after watching too much XML files, I decided to experiment with some 2D game engine. 2D gaming has been for many years confined to the Adobe Flash or J2MEmini game” context. More recently , with the ever wider smart-phones diffusion, 2D games have known a real revival.
Among the many 2D games engines available I first restricted my choice to the Java-based ones. After a short examining of available features and documentation I decided to try first the Slick2D engine.

Slick2D

Slick2D is a Java game engine mostly based on the LWJGL (Light-Weight Java Game Library) library. The aspect of Slick2D the more appealed to me, and triggered my interest, is the ability to begin coding a simple game by just extending a class and writing three methods.
Slick2D offers in fact an abstract class BasicGame, once this class is extended just three methods have to be implemented:
  • init() : is called once when the game is started
  • render() : is called every time the screen is refreshed
  • update() : is called when controls (keyboard, mouse, joystick, ...) are read
The programmer can mostly ignore the other aspects of the game and concentrate on these three events.
Of course writing a complete game will still be a complex task but, Slick2D makes very simple the initial approach to game programming easing a lot the learning curve at the beginning.

First project

To prepare my first project I first downloaded Slick2D jar file and LWJGL zip archive, I then extracted it in a convenient folder in my home directory. In Eclipse I first made a standard Java application project

I added Slick2D and LWJGL libraries to the project class-path
at last I did set the native libraries location for LWJGL libraries (I selected the Linux path of course)

Hello world (what else?)

To test the project set-up was successful I created a class “DemoHello” then, like Slick2D wiki itself suggests, I implemented the render() method with a simple text printing instruction.


after writing a main() method with the minimal code to start the the game


I got my hello-world displayed.

Make it movin'

One step after hello-world was to make the text moving. In order to have something moving you update it's coordinates in the render() method while you control movement changes in the update() method.
I so added some position and direction variables to the DemoHello class.


then changed the render() method in order to make it write at x,y coordinates and implemented a very simple coordinates update and bounce algorithm in the update() method


here is the result.

A bouncing sprite …

As my last (for now) test I decided to replace the moving text with an image. I added a resource folder to my project and copied in it a png image of a ball (I got it from the LWJGL resources folder).
like for the bouncing text demo I added field variables for position, direction plus one for the image


the image is loaded as the program start in the init() method


the render method just display the image at the x,y position


while the update() method handles image movement and bouncing.


using image width and height to obtain a more correct bounce effect.
Here is the final result

Conclusions

Programming with Slick2D is fun. This library hides most of the “boring” part of writing a game leaving to the programmer only what is directly related with the game logic. Of course writing a complex game isn't going to be an easy task anyway but Slick2D makes entering in the game-writing world much more easier.

Links

No comments :

Post a Comment