• 【libGDX】初识libGDX


    1 前言

            libGDX 是一个开源且跨平台的 Java 游戏开发框架,于 2010 年 3 月 11 日推出 0.1 版本,它通过 OpenGL ES 2.0/3.0 渲染图像,支持 Windows、Linux、macOS、Android、iOS、Web 等平台,提供了统一的 API,用户只需要写一套代码就可以在多个平台上运行,官方介绍见 → Features

            libGDX 相关链接如下:

    2 libGDX 环境搭建

            1)下载 gdx-setup

            官方下载链接:gdx-setup.jar,如果网速较慢,用户也可以从这里下载:libGDX全套工具包

            2)生成项目

            双击 gdx-setup.jar 文件,填写 Project name、Package name、Game Class、Output folder、Android SDK、Supported Platforms 等信息,点击 Generate 生成项目。官方介绍见 → Generate a Project

            注意:JDK 最低版为 11,见官方说明 → Set Up a Dev Environment

            3)打开项目

            使用 Android Studio 打开生成的 Drop 项目,等待自动下载依赖,项目结构如下。

            注意:如果选择了 Android 启动,需要在 gradle.properties 文件中添加 AndroidX 支持,如下。

    android.useAndroidX=true

            DesktopLauncher.java

    1. package com.zhyan8.drop;
    2. import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
    3. import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
    4. public class DesktopLauncher {
    5. public static void main (String[] arg) {
    6. Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
    7. config.setForegroundFPS(60);
    8. config.setTitle("Drop");
    9. new Lwjgl3Application(new Drop(), config);
    10. }
    11. }

            AndroidLauncher.java

    1. package com.zhyan8.drop;
    2. import android.os.Bundle;
    3. import com.badlogic.gdx.backends.android.AndroidApplication;
    4. import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
    5. public class AndroidLauncher extends AndroidApplication {
    6. @Override
    7. protected void onCreate (Bundle savedInstanceState) {
    8. super.onCreate(savedInstanceState);
    9. AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    10. initialize(new Drop(), config);
    11. }
    12. }

            Drop.java

    1. package com.zhyan8.drop;
    2. import com.badlogic.gdx.ApplicationAdapter;
    3. import com.badlogic.gdx.graphics.Texture;
    4. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    5. import com.badlogic.gdx.utils.ScreenUtils;
    6. public class Drop extends ApplicationAdapter {
    7. SpriteBatch batch;
    8. Texture img;
    9. @Override
    10. public void create () {
    11. batch = new SpriteBatch();
    12. img = new Texture("badlogic.jpg");
    13. }
    14. @Override
    15. public void render () {
    16. ScreenUtils.clear(1, 0, 0, 1);
    17. batch.begin();
    18. batch.draw(img, 0, 0);
    19. batch.end();
    20. }
    21. @Override
    22. public void dispose () {
    23. batch.dispose();
    24. img.dispose();
    25. }
    26. }

            4)运行项目(点击操作)

            Desktop:

            Android:

            运行效果如下。

            5)运行项目(通过命令)

            可以通过在 Terminal 中运行以下命令来运行项目,见官方介绍 → Importing & Running

            Desktop:

    ./gradlew desktop:run

            Android:

    ./gradlew android:installDebug android:run

            iOS:

    ./gradlew ios:launchIPhoneSimulator

            HTML:

    ./gradlew html:superDev

    3 libGDX 官方案例

            官方接水游戏见 → A Simple Game

            在第二节的基础上,修改 Drop.java,如下。

            Drop.java

    1. package com.zhyan8.drop;
    2. import java.util.Iterator;
    3. import com.badlogic.gdx.ApplicationAdapter;
    4. import com.badlogic.gdx.Gdx;
    5. import com.badlogic.gdx.Input.Keys;
    6. import com.badlogic.gdx.audio.Music;
    7. import com.badlogic.gdx.audio.Sound;
    8. import com.badlogic.gdx.graphics.OrthographicCamera;
    9. import com.badlogic.gdx.graphics.Texture;
    10. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    11. import com.badlogic.gdx.math.MathUtils;
    12. import com.badlogic.gdx.math.Rectangle;
    13. import com.badlogic.gdx.math.Vector3;
    14. import com.badlogic.gdx.utils.Array;
    15. import com.badlogic.gdx.utils.ScreenUtils;
    16. import com.badlogic.gdx.utils.TimeUtils;
    17. public class Drop extends ApplicationAdapter {
    18. private Texture dropImage;
    19. private Texture bucketImage;
    20. private Sound dropSound;
    21. private Music rainMusic;
    22. private SpriteBatch batch;
    23. private OrthographicCamera camera;
    24. private Rectangle bucket;
    25. private Array raindrops;
    26. private long lastDropTime;
    27. @Override
    28. public void create() {
    29. // load the images for the droplet and the bucket, 64x64 pixels each
    30. dropImage = new Texture(Gdx.files.internal("droplet.png"));
    31. bucketImage = new Texture(Gdx.files.internal("bucket.png"));
    32. // load the drop sound effect and the rain background "music"
    33. dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"));
    34. rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
    35. // start the playback of the background music immediately
    36. rainMusic.setLooping(true);
    37. rainMusic.play();
    38. // create the camera and the SpriteBatch
    39. camera = new OrthographicCamera();
    40. camera.setToOrtho(false, 800, 480);
    41. batch = new SpriteBatch();
    42. // create a Rectangle to logically represent the bucket
    43. bucket = new Rectangle();
    44. bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
    45. bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
    46. bucket.width = 64;
    47. bucket.height = 64;
    48. // create the raindrops array and spawn the first raindrop
    49. raindrops = new Array();
    50. spawnRaindrop();
    51. }
    52. private void spawnRaindrop() {
    53. Rectangle raindrop = new Rectangle();
    54. raindrop.x = MathUtils.random(0, 800-64);
    55. raindrop.y = 480;
    56. raindrop.width = 64;
    57. raindrop.height = 64;
    58. raindrops.add(raindrop);
    59. lastDropTime = TimeUtils.nanoTime();
    60. }
    61. @Override
    62. public void render() {
    63. // clear the screen with a dark blue color. The
    64. // arguments to clear are the red, green
    65. // blue and alpha component in the range [0,1]
    66. // of the color to be used to clear the screen.
    67. ScreenUtils.clear(0, 0, 0.2f, 1);
    68. // tell the camera to update its matrices.
    69. camera.update();
    70. // tell the SpriteBatch to render in the
    71. // coordinate system specified by the camera.
    72. batch.setProjectionMatrix(camera.combined);
    73. // begin a new batch and draw the bucket and
    74. // all drops
    75. batch.begin();
    76. batch.draw(bucketImage, bucket.x, bucket.y);
    77. for(Rectangle raindrop: raindrops) {
    78. batch.draw(dropImage, raindrop.x, raindrop.y);
    79. }
    80. batch.end();
    81. // process user input
    82. if(Gdx.input.isTouched()) {
    83. Vector3 touchPos = new Vector3();
    84. touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    85. camera.unproject(touchPos);
    86. bucket.x = touchPos.x - 64 / 2;
    87. }
    88. if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
    89. if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();
    90. // make sure the bucket stays within the screen bounds
    91. if(bucket.x < 0) bucket.x = 0;
    92. if(bucket.x > 800 - 64) bucket.x = 800 - 64;
    93. // check if we need to create a new raindrop
    94. if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
    95. // move the raindrops, remove any that are beneath the bottom edge of
    96. // the screen or that hit the bucket. In the latter case we play back
    97. // a sound effect as well.
    98. for (Iterator iter = raindrops.iterator(); iter.hasNext(); ) {
    99. Rectangle raindrop = iter.next();
    100. raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
    101. if(raindrop.y + 64 < 0) iter.remove();
    102. if(raindrop.overlaps(bucket)) {
    103. dropSound.play();
    104. iter.remove();
    105. }
    106. }
    107. }
    108. @Override
    109. public void dispose() {
    110. // dispose of all the native resources
    111. dropImage.dispose();
    112. bucketImage.dispose();
    113. dropSound.dispose();
    114. rainMusic.dispose();
    115. batch.dispose();
    116. }
    117. }

            音频和图片资源放在 assets 目录下面,如下。

            Desktop 运行效果如下:

            Android 运行效果如下:

  • 相关阅读:
    入学四个月反思
    设计模式之单例和原型
    @Slf4j打印异常信息打印出堆栈信息
    数据链路层主要问题及源于课本的答案
    MS5192T/MS5193T——低噪声、低功耗、16/24 位∑-ΔADC
    ubuntu配置java环境jdk
    [Codeforces] number theory (R1600) Part.8
    go实现异常捕捉
    DeepMind 推出 OPRO 技术,可用于优化 ChatGPT 提示
    微信小程序案例2-1:学生信息
  • 原文地址:https://blog.csdn.net/m0_37602827/article/details/134388619