This is the sixth day of my participation in Gwen Challenge

preface

In the first two chapters (juejin.cn/post/696995…) , (juejin. Cn/post / 697032…). After a brief introduction to how to set up the libGDX development environment, how to run the project, and the role of the modules in the project, we got a glimpse of the libGDX project, so let’s take a look at some of the important apis in libGDX through a simple game.

In order to more clearly describe the functions of each API, we will use a large number of codes and descriptions to explain the functions and uses of each API. Therefore, we will divide the development of small games into four parts. Due to space constraints, we will divide it into three chapters to explain the main contents of each part.

  1. Load resources
  2. Render and clear the screen
  3. Mouse keyboard input and Sprite movement
  4. Small game complete summary

This is the resource loading section

Load resources

In order for the game to use these resources, we must place them in the Androidassets folder. I named these four files droplet.wav, rain-.mp3, Droplet.png, and bucket.png and put them into Android/Assets /. We only need to store the resources once, because both the desktop and HTML5 projects are configured to “view” the folder in different ways.

LibGDX resource directory description

For development without Android platform, the assets directory is in the core directory. If the Android project is included, all resources will be placed in the Assets directory of the Android project and shared with other modules.

Configure initiators for each platform

Desktop module initiator Settings


	package com.badlogic.drop.desktop;

	import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
	import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
	import com.badlogic.drop.Drop;

	public class DesktopLauncher {
	   public static void main (String[] arg) {
	      LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
	      config.title = "Drop";
	      config.width = 800;
	      config.height = 480;
	      new LwjglApplication(newDrop(), config); }}Copy the code

If you are only interested in desktop development, the rest of the platform sections below can be skipped.

Android module initiator Settings

Androidmanifest.xml is modified in the Android (or drop-Android) root directory


	
      
	<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	    package="com.badlogic.drop.android"
	    android:versionCode="1"
	    android:versionName="1.0" >
	
	    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" />
	
	    <application
	        android:allowBackup="true"
	        android:icon="@drawable/ic_launcher"
	        android:label="@string/app_name"
	        android:theme="@style/GdxTheme" >
	        <activity
	            android:name="com.badlogic.drop.android.AndroidLauncher"
	            android:label="@string/app_name"
	            android:screenOrientation="landscape"
	            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
	            <intent-filter>
	                <action android:name="android.intent.action.MAIN" />
	                <category android:name="android.intent.category.LAUNCHER" />
	            </intent-filter>
	        </activity>
	    </application>
	
	</manifest>

Copy the code

Android: screenOrientation set to horizontal (landscape), unless you want to vertical (portrait) can also be set.

Accelerometer and compass

If you want to save battery life and disable the accelerometer and compass, set useAccelerometer and useCompass to false in the launcher code. As shown in the following code:


	package com.badlogic.drop.android;
	
	import android.os.Bundle;
	
	import com.badlogic.gdx.backends.android.AndroidApplication;
	import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
	import com.badlogic.drop.Drop;
	
	public class AndroidLauncher extends AndroidApplication {
	   @Override
	   protected void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
	      config.useAccelerometer = false;
	      config.useCompass = false;
	      initialize(newDrop(), config); }}Copy the code

We cannot define the resolution of the Activity because it is set by the Android operating system.

So here we will simply scale the 800×480 resolution to any resolution on the device

HTML module launcher Settings

Also set 800×480 resolution for rendering images in HTML5 projects.


package com.badlogic.drop.client;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.gwt.GwtApplication;
import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
import com.badlogic.drop.Drop;

public class HtmlLauncher extends GwtApplication {
   @Override
   public GwtApplicationConfiguration getConfig (a) {
      return new GwtApplicationConfiguration(800.480);
   }

   @Override
   public ApplicationListener createApplicationListener (a) {
      return newDrop(); }}Copy the code

Now that the startup classes are configured correctly for all platforms, let’s move on to implementing the game logic part of the code.

Write the code to load the asset

For simplicity and ease of reuse on other platforms, we put all of our game code in core modules.

Our first task is to load assets and store references to them. Assets are usually ApplicationAdapter. The create () method of load, so let’s do this:

	package com.badlogic.drop;
	
	import com.badlogic.gdx.ApplicationAdapter;
	import com.badlogic.gdx.Gdx;
	import com.badlogic.gdx.audio.Music;
	import com.badlogic.gdx.audio.Sound;
	import com.badlogic.gdx.graphics.Texture;
	
	public class Drop extends ApplicationAdapter {
	   private Texture dropImage;
	   private Texture bucketImage;
	   private Sound dropSound;
	   private Music rainMusic;
	
	   @Override
	   public void create(a) {
	      // load the images for the droplet and the bucket, 64x64 pixels each
	      dropImage = new Texture(Gdx.files.internal("droplet.png"));
	      bucketImage = new Texture(Gdx.files.internal("bucket.png"));
	
	      // load the drop sound effect and the rain background "music"
	      dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
	      rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
	
	      // start the playback of the background music immediately
	      rainMusic.setLooping(true);
	      rainMusic.play();
	
	      // ... more to come ...
	   }
	
	   // rest of class omitted for clarity

Copy the code

For each of our assets, we have a field in the Drop class so that we can reference them elsewhere.

1. Load image resources

The first two lines of the create() method load images of raindrops and buckets. Texture indicates the loaded image stored in the video memory. It is usually not possible to draw textures. Texture implements asset file loading by passing FileHandle to the constructor. FileHandle instances can only be obtained through gdx.files. We used different types of asset files, and here we use the “internal” file type to refer to our assets. The internal files are located in the assetsAndroid project’s directory, the same directory that the Desktop and HTML5 projects reference.

Next we load sound effects and background music. Music is usually too large to be kept completely in memory, so there are differences. LibGDX distinguishes between sound effects stored in memory and music streaming from storage. As a rule of thumb, you should use an instance if the sound sample is shorter than 10 seconds, and Music uses an instance for longer audio clips.

2. Load sound resources

A SoundorMusic instance is loaded via gdx.audio. NewSound () and gdx.audio. NewMusic (). Just like the Texture constructor, both methods use FileHandle.

At the end of the create() method, we also tell the Music instance to loop and start playing immediately. If you run the app, you can see a pretty pink background and hear the sound of rain.

At this point all the resources we need for our little game are loaded into the game, and we can call this resource anywhere in the code.

In the next chapter we will explain how to render objects (sprites) onto the screen.

If you think the blogger wrote well, you are welcome to “follow, like, favorites”.