LibGDX簡單的示例程序(二)

在本教程中,我們將在之前的教程的基礎上擴展簡單的“Drop”游戲。 我們將添加一個菜單屏幕,以及幾個功能,使這款游戲更加全面。

讓我們開始介紹我們游戲中的幾個更高級的課程。

游戲界面

游戲界面是游戲中眾多組件的基礎。

Game類

Game抽象類提供了一個ApplicationListener的實現(xiàn)供您使用,以及一些幫助方法來設置和處理屏幕渲染。
一起使用Screen和Game對象來創(chuàng)建一個簡單而強大的游戲結構。
我們將從創(chuàng)建一個Game對象開始,這將是我們游戲的切入點。
讓我們來看一些代碼:

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() {
        batch = new SpriteBatch();
        //Use LibGDX's default Arial font.
        font = new BitmapFont();
        this.setScreen(new MainMenuScreen(this));
    }

    public void render() {
        super.render(); //important!
    }
    
    public void dispose() {
        batch.dispose();
        font.dispose();
    }
}

在程序的最開始部分,我們實例化了一個SpriteBatch 和 BitmapFont,創(chuàng)建多個全局變量并不是 一個很好的做法(具體可以參見DRY),SpriteBatch 用于渲染內容到屏幕上,比如紋理;使用BitmapFont和SpriteBatch,可以將文本繪制到屏幕上,我們將在Screen 課程中更加詳細的了解這部分的內容。

接下來我們設置當前的屏幕為MainMenuScreen(菜單頁面),其中MainMenuScreen需要傳遞一個Drop 的實例.

一個比較常見的錯誤是,render()方法經(jīng)常忘記調用super.render(),這樣會導致create()方法中設置的屏幕將不會被渲染!

最后提醒一下,別忘了釋放 本地資源(dispose),Further reading.。

菜單頁面

現(xiàn)在,我們來看看MainMenuScreen類的細節(jié):

package com.badlogic.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 game) {
        this.game = game;

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);

    }


        //...Rest of class omitted for succinctness.

}

在這個代碼片段中,MainMenuScreen 實現(xiàn)了Screen 接口,并重寫了構造函數(shù),該構造函數(shù)傳遞了Drop的一個實例,所以,如果有需要的話,我們可以調用Drop的方法及字段。

接下來,我們來看看MainMenuScreen類中比較重要的方法:render(float)

public class MainMenuScreen implements Screen {

        //public MainMenuScreen(final Drop game)....        

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.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();
        }
    }
        //Rest of class still omitted...
}

這里的代碼非常簡單, game.font.draw(SpriteBatch,String,float,float)是文本如何呈現(xiàn)到屏幕上。 LibGDX附帶一個預制字體,Arial,以便您可以使用默認的構造函數(shù)獲得一個字體。

然后我們檢查一下屏幕是否被觸摸過,如果有的話,我們檢查將游戲畫面設置為GameScreen實例,然后處理當前的MainMenuScreen實例。 MainMenuScreen中沒有需要釋放的本地資源,因此我們可以忽略垃圾回收處理。

游戲屏幕

現(xiàn)在我們的主菜單屏幕已經(jīng)完成了,是時候來實現(xiàn)我們的游戲業(yè)務邏輯了,我們將盡量從原始的游戲中提取大量的代碼來避免冗余。并避免采取其他的思路來實現(xiàn)Drop這樣簡單的游戲實現(xiàn).

package com.badlogic.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<Rectangle> raindrops;
    long lastDropTime;
    int dropsGathered;

    public GameScreen(final Drop game) {
        this.game = game;

        // 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"));
        rainMusic.setLooping(true);

        // create the camera and the SpriteBatch
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);

        // create a Rectangle to logically represent the bucket
        bucket = new Rectangle();
        bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
        bucket.y = 20; // bottom left corner of the bucket is 20 pixels above
                        // the bottom screen edge
        bucket.width = 64;
        bucket.height = 64;

        // create the raindrops array and spawn the first raindrop
        raindrops = new Array<Rectangle>();
        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) {
        // clear the screen with a dark blue color. The
        // arguments to glClearColor are the red, green
        // blue and alpha component in the range [0,1]
        // of the color to be used to clear the screen.
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // tell the camera to update its matrices.
        camera.update();

        // tell the SpriteBatch to render in the
        // coordinate system specified by the camera.
        game.batch.setProjectionMatrix(camera.combined);

        // begin a new batch and draw the bucket and
        // all drops
        game.batch.begin();
        game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
        game.batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height);
        for (Rectangle raindrop : raindrops) {
            game.batch.draw(dropImage, raindrop.x, raindrop.y);
        }
        game.batch.end();

        // process user input
        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();

        // make sure the bucket stays within the screen bounds
        if (bucket.x < 0)
            bucket.x = 0;
        if (bucket.x > 800 - 64)
            bucket.x = 800 - 64;

        // check if we need to create a new raindrop
        if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
            spawnRaindrop();

        // move the raindrops, remove any that are beneath the bottom edge of
        // the screen or that hit the bucket. In the later case we increase the 
        // value our drops counter and add a sound effect.
        Iterator<Rectangle> 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() {
        // start the playback of the background music
        // when the screen is shown
        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();
    }

}

此代碼與原始實現(xiàn)95%幾乎是相同的,但現(xiàn)在我們使用構造函數(shù)而不是ApplicationListener的create()方法,并傳入一個Drop對象,就像在MainMenuScreen類中一樣。 一旦Screen設置為GameScreen,我們也開始播放音樂。
我們還在游戲的左上角添加了一個字符串,它跟蹤收集的雨滴數(shù)量。

請注意,GameScreen類的dispose()方法不會自動調用,請參閱Screen API。 你有責任去調用它。 如果GameScreen類將對本身的引用傳遞給Game類,則可以從Game類的dispose()方法中調用此方法。 執(zhí)行此操作非常重要,否則GameScreen的資源即使在退出應用程序后也可能會持續(xù)存在并占用內存。

就這樣,你完成了完整的游戲。 那就是所有關于Screen界面和Game抽象類的知識。
你可以訪問該地址獲取源碼:This Github Gist

進階
現(xiàn)在你已經(jīng)掌握了多個屏幕的使用方法,利用這個機會了解Scene2d,Scene2D.ui和Skins,使您的主菜單變得美麗,這將使你的游戲下載量獲得爆炸性增長。

如果您還閱讀了上一個Drop教程中的下一步,您應該可以自己制作游戲了。 最好的做法是去那里做,所以去做下一件大事!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 在深入了解LibGDX詳細的API之前,讓我們先創(chuàng)建一個簡單的游戲,它會對每個模塊都有接觸,我們將介紹一些概念,但...
    天神Deity閱讀 3,945評論 0 4
  • This article is a record of my journey to learn Game Deve...
    蔡子聰閱讀 4,098評論 0 9
  • 因為項目中涉及到Libjdx,不管是考慮到以后項目的維護還是自身能力的提升,對這一塊的了解非常有必要。通過查閱網(wǎng)上...
    拉貝閱讀 187評論 0 1
  • 你曾經(jīng)是光 相公癡 你曾經(jīng)是光, 在駭浪翻涌的大海上, 佇立于孤單燈塔, 為每一艘巨輪、每一條帆船指引方向。 如此...
    相公癡閱讀 102評論 0 2
  • 1.難過的時候允許自己難過,煩惱即菩提,把煩惱當做清理的機會,你就是菩提。 2.發(fā)善心,當你帶著幫助別人得想法的時...
    愛看書的路大膽閱讀 193評論 0 1

友情鏈接更多精彩內容