当前位置: 首页 > news >正文

【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 相关链接如下:

  • libGDX 官网:https://libgdx.com
  • libGDX 官方文档:https://libgdx.com/dev
  • libGDX 启动简介:https://libgdx.com/wiki/start/setup
  • libGDX 工具下载:https://libgdx.com/dev/tools
  • libGDX GitHub:https://github.com/libgdx/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

package com.zhyan8.drop;import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;public class DesktopLauncher {public static void main (String[] arg) {Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();config.setForegroundFPS(60);config.setTitle("Drop");new Lwjgl3Application(new Drop(), config);}
}

        AndroidLauncher.java

package com.zhyan8.drop;import android.os.Bundle;import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;public class AndroidLauncher extends AndroidApplication {@Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate(savedInstanceState);AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();initialize(new Drop(), config);}
}

        Drop.java

package com.zhyan8.drop;import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;public class Drop extends ApplicationAdapter {SpriteBatch batch;Texture img;@Overridepublic void create () {batch = new SpriteBatch();img = new Texture("badlogic.jpg");}@Overridepublic void render () {ScreenUtils.clear(1, 0, 0, 1);batch.begin();batch.draw(img, 0, 0);batch.end();}@Overridepublic void dispose () {batch.dispose();img.dispose();}
}

        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

package com.zhyan8.drop;import java.util.Iterator;import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;public class Drop extends ApplicationAdapter {private Texture dropImage;private Texture bucketImage;private Sound dropSound;private Music rainMusic;private SpriteBatch batch;private OrthographicCamera camera;private Rectangle bucket;private Array<Rectangle> raindrops;private long lastDropTime;@Overridepublic void create() {// load the images for the droplet and the bucket, 64x64 pixels eachdropImage = 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.mp3"));rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));// start the playback of the background music immediatelyrainMusic.setLooping(true);rainMusic.play();// create the camera and the SpriteBatchcamera = new OrthographicCamera();camera.setToOrtho(false, 800, 480);batch = new SpriteBatch();// create a Rectangle to logically represent the bucketbucket = new Rectangle();bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontallybucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edgebucket.width = 64;bucket.height = 64;// create the raindrops array and spawn the first raindropraindrops = 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();}@Overridepublic void render() {// clear the screen with a dark blue color. The// arguments to clear are the red, green// blue and alpha component in the range [0,1]// of the color to be used to clear the screen.ScreenUtils.clear(0, 0, 0.2f, 1);// tell the camera to update its matrices.camera.update();// tell the SpriteBatch to render in the// coordinate system specified by the camera.batch.setProjectionMatrix(camera.combined);// begin a new batch and draw the bucket and// all dropsbatch.begin();batch.draw(bucketImage, bucket.x, bucket.y);for(Rectangle raindrop: raindrops) {batch.draw(dropImage, raindrop.x, raindrop.y);}batch.end();// process user inputif(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 boundsif(bucket.x < 0) bucket.x = 0;if(bucket.x > 800 - 64) bucket.x = 800 - 64;// check if we need to create a new raindropif(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 latter case we play back// a sound effect as well.for (Iterator<Rectangle> iter = raindrops.iterator(); iter.hasNext(); ) {Rectangle raindrop = iter.next();raindrop.y -= 200 * Gdx.graphics.getDeltaTime();if(raindrop.y + 64 < 0) iter.remove();if(raindrop.overlaps(bucket)) {dropSound.play();iter.remove();}}}@Overridepublic void dispose() {// dispose of all the native resourcesdropImage.dispose();bucketImage.dispose();dropSound.dispose();rainMusic.dispose();batch.dispose();}
}

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

        Desktop 运行效果如下:

        Android 运行效果如下:

相关文章:

【libGDX】初识libGDX

1 前言 libGDX 是一个开源且跨平台的 Java 游戏开发框架&#xff0c;于 2010 年 3 月 11 日推出 0.1 版本&#xff0c;它通过 OpenGL ES 2.0/3.0 渲染图像&#xff0c;支持 Windows、Linux、macOS、Android、iOS、Web 等平台&#xff0c;提供了统一的 API&#xff0c;用户只需要…...

VIVADO+FPGA调试记录

vivadoFPGA调试记录 vitis编译vivado导出的硬件平台&#xff0c;提示xxxx.h file cant find vitis编译vivado导出的硬件平台&#xff0c;提示’xxxx.h file cant find’ 此硬件平台中&#xff0c;包含有AXI接口类型的ip。在vitis编译硬件平台时&#xff0c;经常会报错&#xf…...

Android——Gradle插件gradle-wrapper.properties

一、Android Studio版本&#xff0c;Android Gradle插件版本&#xff0c;Gradle版本 Android Studio 通过Android Gradle插件 使用 Gradle来构建代码&#xff1b; Android Studio每次升级后&#xff0c; Android Gradle 插件自动更新&#xff0c;对应的Gradle版本也会变动&…...

iOS应用加固方案解析:ipa加固安全技术全面评测

在移动应用开发领域&#xff0c;iOS应用的安全性一直备受关注。ipaguard作为一款专业的iOS应用加固方案&#xff0c;采用混淆加密技术&#xff0c;旨在保护应用免受破解、逆向和篡改等风险。本文将深入探讨ipaguard的产品功能、安全技术及其在iOS应用加固领域中的核心优势和特色…...

过滤器模式 rust和java的实现

文章目录 过滤器模式实现 过滤器模式实现javarustjavarust rust代码仓库 过滤器模式 过滤器模式&#xff08;Filter Pattern&#xff09;或标准模式&#xff08;Criteria Pattern&#xff09;是一种设计模式&#xff0c;这种模式允许开发人员使用不同的标准来过滤一组对象&…...

Feature Pyramid Networks for Object Detection(2017.4)

文章目录 Abstract1. Introduction3. Feature Pyramid NetworksBottom-up pathwayTop-down pathway and lateral connections 7. Conclusion FPN Abstract 特征金字塔是识别系统中检测不同尺度物体的基本组成部分。但最近的深度学习对象检测器避免了金字塔表示&#xff0c;部分…...

Python3基础模块 random

Python3基础模块 random import random #作用&#xff1a;生成随机数使用dir(module)查看模块内容 >>> import random >>> dir(random) [BPF, LOG4, NV_MAGICCONST, RECIP_BPF, Random, SG_MAGICCONST, SystemRandom, TWOPI, _BuiltinMethodType, _MethodT…...

ubuntu安装pgsql16

ubuntu安装postgresSQL 官网地址&#xff1a; https://www.postgresql.org/download/ 1.安装 # 添加源 sudo sh -c echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list # 安装数字签名 w…...

数据管理70个名词解析

数据标准化70个名词解析 1、数据 是指任何以电子或者其他方式对信息的记录。在计算机科学技术中&#xff0c;“数据”是客观事物的符号表示&#xff0c;指所有可被输入到计算机中并可被计算机程序处理的符号的总称;在管理科学技术中&#xff0c;“数据”是描述事件或事物的属性…...

线性代数本质系列(二)矩阵乘法与复合线性变换,行列式,三维空间线性变换

本系列文章将从下面不同角度解析线性代数的本质&#xff0c;本文是本系列第二篇 向量究竟是什么&#xff1f; 向量的线性组合&#xff0c;基与线性相关 矩阵与线性相关 矩阵乘法与复合线性变换 三维空间中的线性变换 行列式 逆矩阵&#xff0c;列空间&#xff0c;秩与零空间 克…...

Linux-CentOS重要模块

软件包管理器&#xff1a;CentOS使用Yum&#xff08;Yellowdog Updater, Modified&#xff09;作为其包管理器。Yum提供了一种方便的方式来安装、更新和删除软件包&#xff0c;并自动解决依赖关系。 RPM&#xff1a;RPM&#xff08;RPM Package Manager&#xff09;是CentOS中…...

posix定时器的使用

POSIX定时器是基于POSIX标准定义的一组函数&#xff0c;用于实现在Linux系统中创建和管理定时器。POSIX定时器提供了一种相对较高的精度&#xff0c;可用于实现毫秒级别的定时功能。 POSIX定时器的主要函数包括&#xff1a; timer_create()&#xff1a;用于创建一个定时器对象…...

安科瑞煤矿电力监控系统的研究与应用

摘要&#xff1a;作为一个巨大的能源消耗国家&#xff0c;我国每年对煤炭的市场需求巨大。煤炭作为我国点力气和供暖企业的重要原材料&#xff0c;煤矿的开采过程存在着难以消除的风险&#xff0c;我国的煤炭安全问题长期困扰着相关企业和监督部门&#xff0c;也受到社会的广泛…...

高教社杯数模竞赛特辑论文篇-2023年A题:基于机理分析法的定日镜场优化设计模型(附获奖论文及MATLAB代码实现)

目录 摘要 一、 问题重述 1 . 1 问题背景 1 . 2 问题要求 二、 问题分析...

缩点+图论路径网络流:1114T4

http://cplusoj.com/d/senior/p/SS231114D 重新梳理一下题目 我们先建图 x → y x\to y x→y&#xff0c;然后对点分类&#xff1a;原串出现点&#xff0c;原串未出现点。 假如我们对一个原串出现点进行了操作&#xff0c;那么它剩余所有出边我们立刻去操作必然没有影响。所…...

Go语言fyne开发桌面应用程序-环境安装

环境安装 参考https://developer.fyne.io/started/#prerequisites网站 之前的文章介绍了如何安装GO语言这里不在叙述 msys2 首先安装msys2&#xff0c;https://www.msys2.org/ 开始菜单打开MSYS2 执行 $ pacman -Syu$ pacman -S git mingw-w64-x86_64-toolchain注意&#…...

JavaWeb——CSS3的使用

目录 1. CSS概述 2. CSS引入方式 3. CSS颜色显示 4. CSS选择器 4.1. 元素&#xff08;标签&#xff09;选择器 4.2. id选择器 4.3. 类选择器 4.4. 三者优先级 5. 盒子模型 1. CSS概述 CSS&#xff0c;全称为“Cascading Style Sheets”&#xff0c;中文译为“层叠样式…...

AR导览小程序开发方案

一、背景介绍 随着科技的不断发展&#xff0c;虚拟现实&#xff08;VR&#xff09;和增强现实&#xff08;AR&#xff09;技术逐渐被应用于各个领域。其中&#xff0c;AR导览小程序作为一种新兴的导览方式&#xff0c;以其独特的视觉体验和互动性受到了广泛的关注。AR导览小程…...

继承、多态

复习 需求&#xff1a; 编写一个抽象类&#xff1a;职员Employee,其中定义showSalary(int s)抽象方法&#xff1b;编写Employee的子类&#xff0c;分别是销售员Sales和经理Manager,分别在子类中实现对父类抽象方法的重写&#xff0c;并编写测试类Test查看输出结果 package cn.…...

贪吃蛇小游戏代码

框架区 package 结果;import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.util.ArrayList; import java.util.List; import java.util.Random;import javax.s…...

3.3.1_1 检错编码(奇偶校验码)

从这节课开始&#xff0c;我们会探讨数据链路层的差错控制功能&#xff0c;差错控制功能的主要目标是要发现并且解决一个帧内部的位错误&#xff0c;我们需要使用特殊的编码技术去发现帧内部的位错误&#xff0c;当我们发现位错误之后&#xff0c;通常来说有两种解决方案。第一…...

解决Ubuntu22.04 VMware失败的问题 ubuntu入门之二十八

现象1 打开VMware失败 Ubuntu升级之后打开VMware上报需要安装vmmon和vmnet&#xff0c;点击确认后如下提示 最终上报fail 解决方法 内核升级导致&#xff0c;需要在新内核下重新下载编译安装 查看版本 $ vmware -v VMware Workstation 17.5.1 build-23298084$ lsb_release…...

LeetCode - 394. 字符串解码

题目 394. 字符串解码 - 力扣&#xff08;LeetCode&#xff09; 思路 使用两个栈&#xff1a;一个存储重复次数&#xff0c;一个存储字符串 遍历输入字符串&#xff1a; 数字处理&#xff1a;遇到数字时&#xff0c;累积计算重复次数左括号处理&#xff1a;保存当前状态&a…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序

一、开发准备 ​​环境搭建​​&#xff1a; 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 ​​项目创建​​&#xff1a; File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...

屋顶变身“发电站” ,中天合创屋面分布式光伏发电项目顺利并网!

5月28日&#xff0c;中天合创屋面分布式光伏发电项目顺利并网发电&#xff0c;该项目位于内蒙古自治区鄂尔多斯市乌审旗&#xff0c;项目利用中天合创聚乙烯、聚丙烯仓库屋面作为场地建设光伏电站&#xff0c;总装机容量为9.96MWp。 项目投运后&#xff0c;每年可节约标煤3670…...

比较数据迁移后MySQL数据库和OceanBase数据仓库中的表

设计一个MySQL数据库和OceanBase数据仓库的表数据比较的详细程序流程,两张表是相同的结构,都有整型主键id字段,需要每次从数据库分批取得2000条数据,用于比较,比较操作的同时可以再取2000条数据,等上一次比较完成之后,开始比较,直到比较完所有的数据。比较操作需要比较…...

PHP 8.5 即将发布:管道操作符、强力调试

前不久&#xff0c;PHP宣布了即将在 2025 年 11 月 20 日 正式发布的 PHP 8.5&#xff01;作为 PHP 语言的又一次重要迭代&#xff0c;PHP 8.5 承诺带来一系列旨在提升代码可读性、健壮性以及开发者效率的改进。而更令人兴奋的是&#xff0c;借助强大的本地开发环境 ServBay&am…...

脑机新手指南(七):OpenBCI_GUI:从环境搭建到数据可视化(上)

一、OpenBCI_GUI 项目概述 &#xff08;一&#xff09;项目背景与目标 OpenBCI 是一个开源的脑电信号采集硬件平台&#xff0c;其配套的 OpenBCI_GUI 则是专为该硬件设计的图形化界面工具。对于研究人员、开发者和学生而言&#xff0c;首次接触 OpenBCI 设备时&#xff0c;往…...

Ubuntu Cursor升级成v1.0

0. 当前版本低 使用当前 Cursor v0.50时 GitHub Copilot Chat 打不开&#xff0c;快捷键也不好用&#xff0c;当看到 Cursor 升级后&#xff0c;还是蛮高兴的 1. 下载 Cursor 下载地址&#xff1a;https://www.cursor.com/cn/downloads 点击下载 Linux (x64) &#xff0c;…...

深度学习之模型压缩三驾马车:模型剪枝、模型量化、知识蒸馏

一、引言 在深度学习中&#xff0c;我们训练出的神经网络往往非常庞大&#xff08;比如像 ResNet、YOLOv8、Vision Transformer&#xff09;&#xff0c;虽然精度很高&#xff0c;但“太重”了&#xff0c;运行起来很慢&#xff0c;占用内存大&#xff0c;不适合部署到手机、摄…...