LibGDX introduction Tutorial

Certainly while learning programming, you not once thought of creation a video game. For many, this is often the reason why they become interested in programming. There are now many framworków and libraries to help us create a fully functional games. Personally, some time ago I became interested in Libgdx.

 
LibGDX to narzędzie do tworzenia gier wykorzystujący język Java. Za jego pomocą możemy stworzyć gry na androida ale również na iOS, komputery czy przeglądarkę. Samo narzędzie jest rozbudowane i daję wiele możliwości. Za jego pomocą stworzono takie tytuły jak wydany przez Google “Ingress”, Moy 4 (obydwie ponad 10mln pobrań) czy Five Nights at Freddy’s 3.
Libgdx is a tool for creating games using Java. With its help, we can create games for android but also for iOS, PC or browser. The tool itself is quite complex and gives a lot of possibilities. With it there were created such titles as released by Google “Ingress”, Moy 4 (both over 10 million downloads) and Five Nights at Freddy’s 3.
 
Starting using Libgdx is relatively easy we will need:
 
LibGDX – https://libgdx.badlogicgames.com/download.html
Gradle – http://gradle.org/gradle-download/
Our favorite IDE, in my case Eclipse.
 
Download Gradle it is a tool to automate the process of building projects. We unpack it on our hard drive and add the path to the environment variable. If all goes well we should be able to check our versions of Gradle at the command prompt (cmd) using the following command:
 

gradle -v

 
Then we download file gdx-setup.jar from Libgdx website and run it (we need the java sdk installed). We should get a panel with options for project preparation.
 
Name: The name of our project
Package: The package for our project
Game Class: Main Class of project
Destination: Where to save our project
Android: The location of our Android SDK
 
In Sub Projects you choose what types of devices you want to work with. The names are self-explainable. The next section Extensions are additional libraries for our application. If you create a simple 2D applications probably you will need library that includes Box2D physics for 2-dimensional objects. In case of need, we can also download other libraries as the framework for artificial intelligence or lighting objects 2d. When you click Show third part extensions there can be added libraries that are not supported by Libgdx but they can help us with the project. If you want project to be created for the Eclipse enter the advanced tab and select Eclipse. My project is as follows. click Generate
 
setup
 
Turn eclipse on. If you do not have installed Gradle Tools install them. We enter the Help -> Eclipse Marketplace, enter into search engine “Gradle” and choose Gradle (STS) Integration For Eclipse, and after the installation restart the IDE.
 
gradle eclipse
 
It remains for us to import the project. Select File -> Import seek out new folder Gradle (STS) and choose Gradle (STS) Project there give the path where you exported Libgdx project. Then click Build model. Select your project, and click Finish
 

import

import
 

There will be created as many projects as we indicated platforms, plus an additional two. Most of the code we write in project core which by default looks like this:
 

package com.binaryalchemist.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MainGame extends ApplicationAdapter {
	SpriteBatch batch;
	Texture img;
	
	@Override
	public void create () {
		batch = new SpriteBatch();
		img = new Texture("badlogic.jpg");
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(1, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();
		batch.draw(img, 0, 0);
		batch.end();
	}
	
	@Override
	public void dispose () {
		batch.dispose();
		img.dispose();
	}
}

 
Here we see 3 functions create, render and dispose. The life cycle for Libgdx is as follows.
 

life
 
As you can see here we override only three methods. Create is always invoked at the beginning of the application. In this case, we are creating object SpriteBatch that is peculiar scene and Texture. Then we have a method render. This is a method where most of the operations are carried out. This method is called all the time until we dont pause or stop application. This is where we draw the objects or update most of the data in the game.
Gdx.gl.glClearColor(1, 0, 0, 1); allows us to set the background color in this case it is red. Gdx.gl.glClear (GL20.GL_COLOR_BUFFER_BIT); cleans for us screen. To put Texture on the screen it must be placed on the SpriteBatch. All operations performed on SpriteBatch are made between methods Batch.begin () and batch.end(); batch.draw() allows us to draw the texture specified in the selected location, in this case in the upper corner of the screen. Dispose method to use to avoid a memory leak. Here we get rid of all objects not needed in this case spritebatch and textura.
 
Running this code shows us a picture such as bellow it is not a lot but its a good start.
 
image

 
Currently I’am working on a more complex top-down shooter using this framework. I hope that in the future I will be able to write more guides for this platform and I can finish my project. Here a small preview for Libgdx capabilities:
 

time collapsetime collapsetime collapse
time collapsetime collapsetime collapse

 
 
On Libgdx website you can find more than a few thousands of games created using this libary https://libgdx.badlogicgames.com/gallery.html
 
There are also many useful tools to facilitate the creation of our applications. Few I recommend:
 
2D particle generator https://github.com/libgdx/libgdx/wiki/2D-Particle-Editor Software allows us to generate interesting effects using particles.
Tiled http://www.mapeditor.org/download.html Software helps us to generate all the maps and backgrounds.
Physics body editor http://www.aurelienribon.com/blog/projects/physics-body-editor/ Software which easily allows us to create boundaries for objects.
Texture Packer https://code.google.com/archive/p/libgdx-texturepacker-gui/ Pleasant software for packing textures.
 

Interesting references:
https://github.com/libgdx/libgdx/wiki
https://www.youtube.com/channel/UC3ZN1N_OD5XZpKf9vlTHddg
https://www.youtube.com/user/ForeignGuyMike/playlists
http://www.gamefromscratch.com/page/LibGDX-Tutorial-series.aspx

2 thoughts on “LibGDX introduction Tutorial

  1. Helo are using WordPress for yur site platform? I’m new to the blog world but I’m trying to get started and set up my own.Do yoou need any coding knowledge to make your own blog? Any help would be really appreciated!

    • It depends on what features you need on your blog and how much such blog should be unique. However, in most cases, you do not need programming knowledge to set up a professional blog on WordPress. All you need to do is familiarize yourself with several instructions on how to set up wordpress blog, which are plenty on the internet, and choose the right template. When you need some additional functions there is a lot of free plugins on the network. WordPress itself is relatively simple and intuitive to use.

Leave a Reply to rps mata kuliah Cancel reply

WordPress Video Lightbox Plugin