5.1. Raindrops game expansion, 5.1 raindrops Expansion
In this tutorial, we will expand the raindrops game. We will try to add a menu and other functions.
(Previous Article:Click to enter)
** Screen interface **
Screen is basic for any game. Screen contains many methods from the ApplicationListener object, but contains two new methods: show and hide. Used to obtain or lose focus.
** Game **
The Game class provides interfaces inherited from ApplicationListener for use, and provides methods to set and control Screen rendering.
By using Screen and Game objects, you can create a simple and robust Game.
Next, create a Game object as the entry to the Game.
The Code is as follows:
Package cn. libgdx. 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 (){
Batch = new SpriteBatch ();
// Use the default Libgdx font
Font = new BitmapFont ();
This. setScreen (new MainMenuScreen (this ));
}
Public void render (){
Super. render (); // This is very important!
}
Public void dispose (){
Batch. dispose ();
Font. dispose ();
}
}
We instantiate a SpriteBatch and BitmapFont when starting an application. SpriteBatch objects are used to render screen objects, such as texture. BitmapFont objects are used together with SpriteBatch to set the rendering font.
Next, we will set the MainMenuScreen object to set parameters at the first runtime.
The common error is that you forget to call the super. render () method in the render () method. Otherwise, the create () method in the Screen class will not be rendered.
Finally, do not forget to delete all objects!
Main Menu
The Code is as follows:
Package cn. libgdx. drop;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. Screen;
Import com. badlogic. gdx. graphics. GL20;
Import com. badlogic. gdx. graphics. OrthographicCamera;
Public class MainMenuScreen implements Screen {
Final Drop game;
OrthographicCamera camera;
Public MainMenuScreen (final Drop gam ){
Game = gam;
Camera = new OrthographicCamera ();
Camera. setToOrtho (false, 800,480 );
}
}
In the code, we add the constructor inherited from the Screen interface. The Screen interface does not provide any create () method, so we use the constructor. The only required parameter for the constructor is an instantiated Drop. Next is the render () method:
Public class MainMenuScreen implements Screen {
@ Override
Public void render (float delta ){
Gdx. gl. glClearColor (0, 0, 0.2f, 1 );
Gdx. gl. glClear (gl1_gl _ COLOR_BUFFER_BIT );
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 (new GameScreen (game ));
Dispose ();
}
}
}
We call the SpriteBatch and BitmapFont we created earlier. Next, check whether the screen is touched. If it is true, set the game to a GameScreen instance and destroy the current instance.
Game screens
Next we will create our game with the following code:
Package cn. libgdx. drop;
Import java. util. Iterator;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. Input. Keys;
Import com. badlogic. gdx. Screen;
Import com. badlogic. gdx. audio. Music;
Import com. badlogic. gdx. audio. Sound;
Import com. badlogic. gdx. graphics. GL20;
Import com. badlogic. gdx. graphics. OrthographicCamera;
Import com. badlogic. gdx. graphics. Texture;
Import com. badlogic. gdx. math. MathUtils;
Import com. badlogic. gdx. math. Rectangle;
Import com. badlogic. gdx. math. Vector3;
Import com. badlogic. gdx. utils. Array;
Import com. badlogic. gdx. utils. TimeUtils;
Public class GameScreen implements Screen {
Final Drop game;
Texture dropImage;
Texture bucketImage;
Sound dropSound;
Music rainMusic;
OrthographicCamera camera;
Rectangle bucket;
Array raindrops;
Long lastDropTime;
Int dropsGathered;
Public GameScreen (final Drop gam ){
This. game = gam;
// Load the image
DropImage = new Texture (Gdx. files. internal ("droplet.png "));
BucketImage = new Texture (Gdx. files. internal ("bucket.png "));
// Load music
DropSound = Gdx. audio. newSound (Gdx. files. internal ("drop.wav "));
RainMusic = Gdx. audio. newMusic (Gdx. files. internal ("rainaudio "));
RainMusic. setLooping (true );
// Create a camera and SpriteBatch
Camera = new OrthographicCamera ();
Camera. setToOrtho (false, 800,480 );
// Create a Rectangle
Bucket = new Rectangle ();
Bucket. x = 800/2-64/2;
Bucket. y = 20;
Bucket. width = 64;
Bucket. height = 64;
Raindrops = new Array ();
SpawnRaindrop ();
}
Private void spawnRaindrop (){
Rectangle raindrop = new Rectangle ();
Raindrop. x = MathUtils. random (0,800-64 );
Raindrop. y = 480;
Raindrop. width = 64;
Raindrop. height = 64;
Raindrops. add (raindrop );
LastDropTime = TimeUtils. nanoTime ();
}
@ Override
Public void render (float delta ){
Gdx. gl. glClearColor (0, 0, 0.2f, 1 );
Gdx. gl. glClear (gl1_gl _ COLOR_BUFFER_BIT );
Camera. update ();
Game. batch. setProjectionMatrix (camera. combined );
Game. batch. begin ();
Game. font. draw (game. batch, "Drops Collected:" + dropsGathered, 0,480 );
Game. batch. draw (bucketImage, bucket. x, bucket. y );
For (Rectangle raindrop: raindrops ){
Game. batch. draw (dropImage, raindrop. x, raindrop. y );
}
Game. batch. end ();
If (Gdx. input. isTouched ()){
Vector3 touchPos = new Vector3 ();
TouchPos. set (Gdx. input. getX (), Gdx. input. getY (), 0 );
Camera. unproject (touchPos );
Bucket. x = touchPos. x-64/2;
}
If (Gdx. input. isKeyPressed (Keys. LEFT ))
Bucket. x-= 200 * Gdx. graphics. getDeltaTime ();
If (Gdx. input. isKeyPressed (Keys. RIGHT ))
Bucket. x + = 200 * Gdx. graphics. getDeltaTime ();
If (bucket. x <0)
Bucket. x = 0;
If (bucket. x> 800-64)
Bucket. x = 800-64;
If (TimeUtils. nanoTime ()-lastDropTime> 1000000000)
SpawnRaindrop ();
Iterator iter = raindrops. iterator ();
While (iter. hasNext ()){
Rectangle raindrop = iter. next ();
Raindrop. y-= 200 * Gdx. graphics. getDeltaTime ();
If (raindrop. y + 64 <0)
Iter. remove ();
If (raindrop. overlaps (bucket )){
DropsGathered ++;
DropSound. play ();
Iter. remove ();
}
}
}
@ Override
Public void resize (int width, int height ){
}
@ Override
Public void show (){
RainMusic. play ();
}
@ Override
Public void hide (){
}
@ Override
Public void pause (){
}
@ Override
Public void resume (){
}
@ Override
Public void dispose (){
DropImage. dispose ();
BucketImage. dispose ();
DropSound. dispose ();
RainMusic. dispose ();
}
}
Here we use the constructor to replace the create () method inherited from ApplicationListener and pass a Drop object.
We added a Sring to show the raindrops collected.
(Www.libgdx.cn is copyrighted. If you need to reprint it, indicate the source)