DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

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)
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Make a Game With LibGDX - Part 1

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.

Iurii Golubnichenko user avatar by
Iurii Golubnichenko
·
Jun. 07, 20 · Tutorial
Like (5)
Save
Tweet
Share
9.05K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

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.

libGDX screen


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.

Project structure


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.

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:

Java
 




x
32


 
1
import com.badlogic.gdx.ApplicationAdapter;
2
import com.badlogic.gdx.Gdx;
3
import com.badlogic.gdx.graphics.GL20;
4
import com.badlogic.gdx.graphics.Texture;
5
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
6

          
7
public class Ashooter extends ApplicationAdapter {
8

          
9
   SpriteBatch batch;
10
   Texture img;
11
   
12
   @Override
13
   public void create () {
14
      batch = new SpriteBatch();
15
      img = new Texture("badlogic.jpg");
16
   }
17

          
18
   @Override
19
   public void render () {
20
      Gdx.gl.glClearColor(1, 0, 0, 1);
21
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
22
      batch.begin();
23
      batch.draw(img, 0, 0);
24
      batch.end();
25
   }
26
   
27
   @Override
28
   public void dispose () {
29
      batch.dispose();
30
      img.dispose();
31
   }
32
}



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:


Configuration


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:

Saving the game


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:

Menu screen


Thanks and see you in the next chapters. Bye!


Android (robot)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: