Starting some more code for fun

Haven’t done any games programming in a while, so I kicked off a new project using libgdx. It provides a bunch of utilities for making cross-platform games, and I especially like the JSON loading utilities and the Pixmap utilities for drawing.

Since I’m just doing this for fun, and I’m an awful artist, I started by playing around with some Link’s Awakening spritesheets to see if I can replicate a 13-year old game. Loading the image is trivial with libgdx:

Pixmap image = new Pixmap(Gdx.files.internal("filename.png"));

This gives you access to a Pixmap object that can be used to draw into other pixmap objects. Once you’re happy with your drawing you can write it to a Texture and draw it on the screen. I wrote some quick code to extract the individual tiles and drew some random art to the screen.

Random Tiles

Obviously not the most useful but fun to look at. Even the code calls out the uselessness of the method.

// Not a very useful method.
public Tile getRandom() {
Random random = new Random();
return get(random.nextInt(tileCols), random.nextInt(tileRows));

In order to do something useful with it, I needed to create maps from the tiles that I was using. I decided to add a layer of abstraction around the concept of a tile, so that it’s not just a graphic. So I broke it up into spritesheets – purely graphical objects – and tilesets – which contain gameplay semantics.

The JSON interfaces that libgdx provides allow files to be loaded with a small amount of code. I whipped up a quick tileset description and a loader for it to describe the various tiles that were available in the sprite sheet.

spritesheet: {
image: "overworld.png",
tiles: {
grass_ul: {
c: 0, r: 5,
passable: true,
// ...
house_roof_blue: {
c: 8, r: 2,
passable: false,
// ...

Once the sprite sheet was loaded, I created a map object that would be the actual method of interaction with the game, and created a loader for a test map that I made as well.

tileset: {
file: "overworld.tileset.json",
width: 10,
height: 8,
tiles: [
[tree_dr, grass_l, grass_h, grass_h, grass_h, grass_h, grass_h, grass_h, grass_h, grass],
// ...

After a bit of organization, and about an hour or so of work, I had something that looked a bit more pretty, if less chaotic.