This is the 10th day of my participation in Gwen Challenge
preface
In the previous five chapters (juejin.cn/post/696995…) , (juejin. Cn/post / 697032…). , (juejin. Cn/post / 697068…). , (juejin. Cn/post / 697107…). , (juejin. Cn/post / 697130…). , (juejin. Cn/post / 697162…). We fully covered how to set up the libGDX development environment and how to run the project, as well as the role of modules in the project. And by loading resource article, graphics rendering and touch screen, mouse and keyboard to monitor implementation elves moving, basically covers libGDX resource management, graphics rendering, audio playback, and touch screen, the mouse and keyboard input device to monitor the relevant API content, you have a basic understanding of how to use libGDX developed a simple little game.
In this chapter, we’ll expand on the simple game “Drop” that we created in the previous article. Add a menu screen and some features to this small game to make it more comprehensive.
Of course, some of the more advanced libGDX apis will be included in the extension process.
Screen interface
The screen is the foundation of any game with a graphical interface. Screens contains a number of methods used from the ApplicationListener object and includes several new methods: show and hide, which are called when Screen gains or loses focus, respectively. Screens handle (that is, process and render) the game’s menu screen, Settings screen, game screen, etc.
Game entry class implementation
The Game class handles multiple screens and provides helper methods for doing so, along with an implementation of ApplicationListener that you can use. Screen and Game objects are used together to create a simple and powerful infrastructure for games.
We start by creating a Game object whose create() method is the entry point to our Game. See the following code:
package com.badlogic.drop;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Drop extends Game {
public SpriteBatch batch;
public BitmapFont font;
public void create(a) {
batch = new SpriteBatch();
font = new BitmapFont(); // use libGDX's default Arial font
this.setScreen(new MainMenuScreen(this));
}
public void render(a) {
super.render(); // important!
}
public void dispose(a) { batch.dispose(); font.dispose(); }}Copy the code
We launch the application by instantiating SpriteBatch and BitmapFont. It is a bad practice to create multiple objects that can be shared. The SpriteBatch object is used to render objects (such as textures) to the screen; And the BitmapFont object is used with SpriteBatch to render text to the screen.
Next, we set up the screen of the game as a MainMenuScreen object with the Drop instance as its first and only argument.
A common mistake: If you forget to call the Game’s super.render() implementation. If the create() method is not called, the Screen set in the Game class will not be rendered!
Build the main menu screen
package com.badlogic.drop;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class MainMenuScreen implements Screen {
final Drop game;
OrthographicCamera camera;
public MainMenuScreen(final Drop game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false.800.480);
}
/ /... Rest of class omitted for succinctness.
}
Copy the code
In this code snippet, we implement the Screen interface for MainMenuScreen and create a constructor. The Screen interface does not provide any type of create() method, so we use the constructor to create it. The only argument to the constructor required by the game is the Drop instance so that we can call the method and field if needed.
Render the main menu interface
Let’s finally implement the Render (float) method in MainMenuScreen:
public class MainMenuScreen implements Screen {
final Drop game;
OrthographicCamera camera;
public MainMenuScreen(final Drop game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false.800.480);
}
@Override
public void render(float delta) {
ScreenUtils.clear(0.0.0.2 f.1);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch, "Welcome to Drop!!! ".100.150);
game.font.draw(game.batch, "Tap anywhere to begin!".100.100);
game.batch.end();
if (Gdx.input.isTouched()) {
game.setScreen(newGameScreen(game)); dispose(); }}// Rest of class still omitted...
}
Copy the code
The code here is simple, using the game’s SpriteBatch and BitmapFont to render the text to the screen game.font. Draw (SpriteBatch, String, float, float). LibGDX comes with a prefabricated font: Arial, so we can use the font using the default constructor.
We then check to see if the screen is touched, if so, set the GameScreen to a GameScreen instance, and then process the current instance of MainMenuScreen. The rest of the methods that need to be implemented in MainMenuScreen are left blank, so I’ll continue to omit them (there’s nothing to work with in this class).
conclusion
So far, we’ve improved the entry to the game and added a main menu interface, so in the next chapter, we’re going to integrate this chapter with the game to make it look like a little game with the full flow.
If you feel that the blogger wrote good, welcome to “pay attention to, favorites, like” three even one key!