Skip to content

Getting Started

Sven Arends edited this page Jun 26, 2016 · 6 revisions

Introduction

LibZ is an 2D game library written in pure Java.
With LibZ making games becomes easy.
And it's FREE. That means no monthly costs and no royalty fee.


Setting up your project

Download the latest build of LibZ at: http://winspeednl.co.nf/libz/
Once you have downloaded it, open eclipse (Or any other IDE | We describe it for eclipse here)
Create an new 'Java Project' And give it a name.
Once you have that Right-Click and go to 'Build Path > Configure Build Path' After that select 'Libraries' and Click 'Add External Jar' and select your downloaded LibZ.jar.

Now you have loaded The Library and you are ready to make your project, Easy isn't it?
Right-Click the folder named 'src' and Click 'New > Package' and give it an name.
After that Right-Click your package and Click 'New > Class' And give it a name (IE. Game or Main)

Let's create a directory for the textures/sounds.
Right-Click on your game directory and Click 'New > Folder' and call it 'res'. Now Right-Click 'res' and Click 'Build Path > Use As Source Folder'.
place all your textures and sound files in that folder.

Once you have that go ahead go to the next section because you're done with the setup :D


The Game class

We use in this section 'Game.java' As the main class.
After you created your Game class you will have the default class, we need to change that before we continue.
Begin by extending 'LibZ'
public class Game extends libZ {
After that you need three functions: init, update and render.
public void init(GameCore gc) { ... }
public void update(GameCore gc, float tc) { ... }
public void render(GameCore gc, Render r) { ... }
To be able to run your game , you need the function 'main'
public static void main(String[] args) { ... }

once you have that it's time to create the game yay!
Add the following to your 'main' function to setup the game window
GameCore gc = new GameCore(new Game()); // new Game() is this class
gc.setname("game name");
gc.setWidth(640); // Game width
gc.setHeight(360); // Game height
gc.setScale(1f); // Game scale
gc.setSpriteBGColor(0xFF000000); // Sprite alpha color (Start with 0x then AARRGGBB | 0=black F=white | [0-9 A-F])
gc.start(); // Run the game (it first call 'init' then calls update and render)
``


Player input handling

Create a new class called 'Player.java'
In your 'Game' class create a new field Player player;
Then in your 'init' put player = new Player(gc);

Go back to your 'Player' class and create a constructor with 'GameCore' as variable
public Player(GameCore gc) { ... }
And write Input input at the beginning.
Inside the constructor write input = gc.getInput(); This will give the keyboard functionality to the player.

To be able to do stuff with the player the 'Player' class needs to extends 'Entity'
public class Player extends Entity {
After that create two function called: update, render.
public void update(GameCore gc, float tc) { ... }
public void render(GameCore gc, Render r) { ... }

Go to your 'Game' class again and put in the 'update' function player.update(gc, tc);
and in the 'render' function player.render(gc, r);

Now for loading and rendering the player, go to your 'Player' class.
Under Input input; create a new field int[] pixels;'.
Inside your constructor you nee to define some variables called: x, y, w, h
Set the 'w' and 'h' to the size of your sprite.
set the 'x' and 'y' to the position you want to start.
to start in thee center use:
x = gc.getWidth() / 2 - w / 2;
y = gc.getHeight() / 2 - h / 2;
At the end of the constructor place pixels = new Sprite("/Player.png", 0, 0, w, h).pixels; // /Player.png reveres to 'res/Player.png'!

Now for rendering the player, go to the 'render' function and place:
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++)
r.setPixel(this.x + x, this.y + y, pixels[x + y * w]);
This will loop over the pixels and render it on the screen.

Moving your player

To move your player, create a new field int speed = 1; // Player walk speed and go to the 'update' function.
In this example we use the W,A,S,D keys.
if (input.isKeyPressed(KeyEvent.VK_W)) if (y > 0) y -= speed;
if (input.isKeyPressed(KeyEvent.VK_S)) if (y < gc.getHeight() - h) y += speed;
if (input.isKeyPressed(KeyEvent.VK_A)) if (x > 0) x -= speed;
if (input.isKeyPressed(KeyEvent.VK_D)) if (x < gc.getWidth() - w) x += speed;
Once you have that, you're done!.


Creating a map

W.I.P.


Enemies

W.I.P.


Sound

What is a game without sound.
To create a new sound, go to yout 'Game' class and create a new field
public Sound jump = new Sound("/Jump.wav"); // /Jump.wav reveres to 'res/Jump.wav'
Now you have an sound, you could play it back using jump.play();. Easy as pie, Right?

Clone this wiki locally