"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