Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LibGDX Game Development By Example

You're reading from   LibGDX Game Development By Example Learn how to create your very own game using the libGDX cross-platform framework

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher
ISBN-13 9781785281440
Length 280 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
James Cook James Cook
Author Profile Icon James Cook
James Cook
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting to Know LibGDX FREE CHAPTER 2. Let's Get These Snakes Out of This Book! 3. Making That Snake Slick 4. What the Flap Is the Hype About? 5. Making Your Bird More Flightworthy 6. Onto the Next Platform...Game 7. Extending the Platform 8. Why Are All the Birds Angry? 9. Even Angrier Birds! 10. Exporting Our Games to the Platforms 11. Third-party Services Index

Creating a project

Follow the given steps to create your own project:

  1. As mentioned earlier, LibGDX comes with a really useful project setup tool. Download the application from here:

    http://libgdx.badlogicgames.com/download.html

    At the time of writing, it is the big red "Download Setup App" button in the middle of your screen.

  2. Once downloaded, open the command line and navigate to the location of the application. You will notice that it is a JAR file type. This means we need to use Java to run it.
    Creating a project
  3. Running this will open the setup UI:
    Creating a project

Before we hit the Generate button, let's just take a look at what we are creating here:

  • Name: This is the name of our game.
  • Package: This is the Java package our game code will be developed in.
  • Game class: This parameter sets the name of our game class, where the magic happens!
  • Destination: This is the project's directory. You can change this to any location of your choice.
  • Android SDK: This is the location of the SDK. If this isn't set correctly, we can change it here. Going forward, it might be worth setting the ANDROID_HOME environment variable.

Next is the version of LibGDX we want to use. At time of writing, the version is 1.5.4.

Now, let's move on to the subprojects. As we are only interested in desktops at the moment, let's deselect the others.

Finally, we come to extensions. Feel free to uncheck any that are checked. We won't be needing any of them at this point in time. For more information on available extensions, check out the LibGDX wiki (https://github.com/libgdx/libgdx/wiki).

Once all is set, let's hit the Generate button!

There is a little window at the bottom of the UI that will now spring to life. Here, it will show you the setup progress as it downloads the necessary setup files.

Once complete, open that command line, navigate to the directory, and run your preferred tree command (in Windows, it is just "tree").

Creating a project

Hopefully, you will have the same directory layout as the previous image shows.

The astute among you will now ask, "What is this Gradle?" and quite rightly so. I haven't mentioned it yet, although it appears twice in our projects directory.

What is Gradle?

Well, Gradle is a very excellent build tool and LibGDX leverages its abilities to look after the dependencies, build process, and IDE integration. This is especially useful if you are going to be working in a team with a shared code base. Even if you are not, the dependency management aspect is worth it alone.

Anyone who isn't familiar with dependency management may well be used to downloading Java JARs manually and placing them in a libs folder, but they might run into problems later when the JAR they just downloaded needs another JAR, and so on. The dependency management will take care of this for you and even better is that the LibGDX setup application takes care of this for you by already describing the dependencies that you need to run!

Within LibGDX, there is something called the Gradle Wrapper. This is essentially the Gradle application embedded into the project. This allows portability of our project, as now if we want someone else to run it, they can.

I guess this leads us to the question, how do we use Gradle to run our project? In the LibGDX wiki (https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline), you will find a comprehensive list of commands that can be used while developing your game.

However, for now, we will only cover the desktop project.

What you may not have noticed is that the setup application actually generates a very simple "Hello World" game for us. So, we have something we can run from the command line right away!

Let's go for it!

On our command line, let's run the following:

  • On Windows: gradlew desktop:run
  • On Linux and Mac OS X: ./gradlew desktop:run

    The following screen will appear once you execute the preceding command:

    What is Gradle?

You will get an output similar to the preceding screenshot. Don't worry if it suddenly wants to start downloading the dependencies. This is our dependency management in action! All those JARs and native binaries are being downloaded and put on to classpaths. But, we don't care. We are here to create games!

So, after the command prompt has finished downloading the files, it should then launch the "Hello World" game.

What is Gradle?

Awesome! You have just launched your very first LibGDX game!

Although, before we get too excited, you will notice that not much actually happens here. It is just a red screen with the Bad Logic Games logo.

I think now is the time to look at the code!

You have been reading a chapter from
LibGDX Game Development By Example
Published in: Aug 2015
Publisher:
ISBN-13: 9781785281440
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime