After failing to complete the 7 Day Roguelike challenge I was left with some working code for a simple pixel art game that had the basics that might help new game developers.

I plan on developing this game further, demonstrating some basic techniques and hopefully motivate people to make their own games.

* source code for this post is available on Github

To start you will need to download LibGDX setup application and generate a new project, there is information on getting setup on the LibGDX website. I will be using Eclipse Luna as my editor.

Here is my project setup, to keep this less complicated I am only launching this application on the desktop, I used a random name generator which came up with EvoScape.

Once you have created and imported your project into Eclipse (Gradle Porject) or your preferred editor run the application as a Java App, you should see this:

If you have worked with LibGDX previously I am sure you have seen this screen many times! There are only three files changed / created for this first part of the tutorial:

DesktopLauncher.java

Add these two lines to the launcher so the game height and width are set:

config.width = 800; config.height = 600;

gameclass.java

Apart from the standard Texture and Spritebatch variables that LibGDX add as default these class variables are defined:

OrthographicCamera camera; Control control; // Display Size private int displayW; private int displayH; // Temp x and y co-ords int x, y; // For Movement int direction_x, direction_y; int speed = 3;

Control is our own class used to handle keyboard input, the Orthographic-camera will handle the viewport, other variables are used to hold the screen size and some temporary values to track the position of the camera.

// CAMERA displayW = Gdx.graphics.getWidth(); displayH = Gdx.graphics.getHeight(); // For 800x600 we will get 266*200 int h = (int) (displayH/Math.floor(displayH/160)); int w = (int) (displayW/(displayH/ (displayH/Math.floor(displayH/160)))); camera = new OrthographicCamera(w,h); camera.zoom = .4f; // Used to capture Keyboard Input control = new Control(displayW, displayH, camera); Gdx.input.setInputProcessor(control);

Gdx.graphics is used to get the width and height of the screen, we have set the screen size to 800 by 600 but we can allow the user to resize the screen or go full screen, these values can later be updated when the screen size changes.

There are many ways to set up the OrthographicCamera, depending on the type of game you may want different sized viewports, check the LibGDX wiki for additional information. It is also worth looking at the viewport options but for the moment this simple setup is fine. The camera also has the ability to zoom in/out.

Before looking at the render loop of the main game class lets take a look at Control.java:

public class Control extends InputAdapter implements InputProcessor {

It is important the class that handles keyboard input extends and implements the above classes, the editor will ask us to add the unimplemented methods after we change our class to extent and implement the LibGDX classes, keyDown and keyUp are what we will use at this point. Using a simple switch statement the class booleans can be set or functions called when certain buttons are pressed/released:

switch (keycode) { case Keys.DOWN: down = false; break; case Keys.UP: up = false; break; ....

WASD and the arrow keys set the up,down,left, right values to true on press and false on release, these boolean values can be used to determine if movement should occur. Backspace is used to toggle debug, this will be useful for displaying collision hit-boxes and displaying debug info later on and esc key is set to exit the application. The other functions already added will be explained in another post.

Looking back to the gameclass.java file and the render funciton:

// GAME LOGIC // Reset the direction values direction_x=0; direction_y=0; if(control.down) direction_y = -1 ; if(control.up) direction_y = 1 ; if(control.left) direction_x = -1; if(control.right) direction_x = 1; camera.position.x += direction_x * speed; camera.position.y += direction_y * speed; camera.update(); // GAME DRAW batch.setProjectionMatrix(camera.combined); batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); batch.begin(); batch.draw(img, 0, 0); batch.end();

The render function is called 60 times every second, LibGDX defaults to 60 FPS. Here we reset the current direction in x (horizontal) and y (vertical) to 0, then given up, down, left, right are pressed (checked via the Control Class) the direction variables are set accordingly, the camera x and y positions are added to using the value and a speed multiplier. After changing the camera in anyway we have to call .update() for the changes to be applied to the camera.

The SpriteBatch projection is then set to that of the camera before drawing the default LibGDX logo, running the application you should be able to move the camera arond and exit using esc:

In part two we will look at creating a small island, using a simple chunk system.