Make a Game With LibGDX - Part 1
The first part of this tutorial introduces the LibGDX Java framework and demonstrates how to download it and configure it.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
Hi everyone!
In this series of articles, we are going to talk about game development with LibGDX.
This is a kind of introduction part where I will show you how to create a skeleton of our game and talk a little bit about the game’s plot and tools.
LibGDX is a Java Framework for cross-platform game development (2D or 3D) created by BadLogicGames. What does thatmean? So, with this framework you can write your code once and launch the game on different platforms such as Windows, Linux, Mac OS, Android, iOS, HTML5 and BlackBerry. Sounds awesome, doesn’t it?
This is also an open-source project and you can check some examples of game in the gallery.
You can find additional information like features, tools, documentation, news about releases and more on the site.
So let’s get started. First of all, you need to download gdx-setup app.
(Note: you can work without that tool but you need to write some boilerplate code manually, add libraries, set paths and so on).
Start gdx-setup. You will see the main screen of Libgdx Project Generator.
Here we can set up our project:
- Name – you can input the name of your game
- Package – your hierarchy of packages
- Game class – the main class of your game
- Destination – the place where you are going to save your project
- Android SDK – path to Android SDK (is needed if we want to create a game for Android)
- Sub projects – platforms for our game (for now we need to choose Desktop and Android)
- Extensions – select only Box2D (for now)
(Note: you can explore Third Party Extensions and Advanced by yourself, we'll skip them for now)
Click the Generate button and Libgdx Project Generator does all the routine work for us.
Once the project was generated you can open it in your favorite IDE (I am going to use Intellij IDEA. You can use Eclipse, IDEA, NetBeans, or Android Studio for instance). Let’s get through the structure of the project.
As you can see there are three main modules:
- android – contains resources for game (assets folder), AndroidLauncher class to start our game on Android devices
- desktop – contains DesktopLauncher class to start our game on Windows, Linux or Mac OS
- core – contains all logic of game (manage screens, control sprites, play engine, etc.)
One note – all resources (sprites, backgrounds, music, etc.) we are going to store in the assets folder.
The same folder you can find in core module if you chose only Desktop (in Subprojects, in Libgdx Project Generator). Let’s take a look at the code of the main class:
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 Ashooter extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
}
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();
}
public void dispose () {
batch.dispose();
img.dispose();
}
}
As you can see our class extends ApplicationAdapter
, we need to replace it with Game
class. Game class allows us use multiple screens in our game. Of course, we can go with ApplicationAdapter
and add usage Screen
class by ourselves but what is the point, if the wheel was invented before)
(note: ApplicationAdapter
and Game
classes are implementing the same interface ApplicationListener
and provide dispose()
, render()
, pause()
, resume()
, create()
, resize(int width, int height)
methods. We will discuss these ones in the next chapters)
Now just check that everything is OK and working runs the game (hit the run button). But before we need to edit configuration for that:
Select “add new configuration” and choose Application, name it desktop or whatever you want. Main class must point to DesktopLauncher
class, working directory must point to android\assets
folder.
Save the configuration and run the game. You will see something like that:
In the next chapters we will dig deeper into the game development and try to create a new game. This is about space shooter, which will be 2D game, with animation, music, parallax effect, multiple screens (there will be menu screen, play screen, setting screen, etc.), levels, tile sets, sprite sheets and so on.
The main idea of this game is destroying alien ships, asteroids, collecting some artifacts and bonuses that will help to extend our ship’s equipment. Also we can try to create a plot for game, levels and so on. In process we will discuss some tools that will help us in our game coding such as Audacity, Tiled, Spine and others.
So, I think that is it for this chapter and in the next one we will create menu screen. Something like this:
Thanks and see you in the next chapters. Bye!
Opinions expressed by DZone contributors are their own.
Trending
-
Measuring Service Performance: The Whys and Hows
-
How to Handle Secrets in Kubernetes
-
Integration Architecture Guiding Principles, A Reference
-
RBAC With API Gateway and Open Policy Agent (OPA)
Comments