libGDX游戏开发之菜单界面(四)

libGDX系列,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm…国内用libgdx比较少,多数情况需要去官网和google找资料,相互学习的可以加我联系方式。

在游戏中,我们知道在镜头中的菜单是固定的。下面演示如何在libgdx中添加菜单和菜单点击事件

基本的代码如下,前面的地图是我拿前面的文章来用的嘿嘿。

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.ScreenUtils; /**
* @author lingkang
* @date 2021/10/6 23:49
* @description
*/
public class MyMenu extends ApplicationAdapter {
// 精灵批处理
public SpriteBatch batch;
// 地图相关
private TmxMapLoader maploader;
private TiledMap map;
private OrthographicCamera camera;// 相机
private OrthogonalTiledMapRenderer renderer; private float mapX = 128f, mapY = 128f; // 地图的位置,用于相机观察这个位置
private float moveSpeed = 3f; // 相机移动的速度 // 玩家
private TextureRegion player; // 舞台,与javafx类似,可以在上面添加标签
private Stage stage;
private Table table;
private Label time; // 用于动态更新这个标签 @Override
public void create() {
batch = new SpriteBatch();
stage = new Stage(); // 加载地图和地图渲染
maploader = new TmxMapLoader();
// 地图是 块=20 像素大小:1280x960
map = maploader.load("worldmap/worldmap.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1);// 将地图单元设置为 1 camera = new OrthographicCamera();
// 相机可看到的宽高
camera.setToOrtho(false, 500, 500);
camera.position.x = mapX;
camera.position.y = mapY;
camera.update();
// --- 加载地图是为了让我们的演示更直观 // 玩家 大小 50*50 随意找一张图片
player = new TextureRegion(new Texture("worldmap/badlogic.jpg"), 50, 50); // libgdx 默认的样式是不支持中文的,要自己去制作样式的,可以参考前面的文章
Label.LabelStyle labelStyle = new Label.LabelStyle(new BitmapFont(), Color.RED);
table = new Table();
table.top();
table.setFillParent(true);
table.add(new Label("fenshu", labelStyle)).expandX().padTop(10);
table.add(new Label("xueliang", labelStyle)).expandX().padTop(10);
table.add(new Label("time", labelStyle)).expandX().padTop(10);
table.row();// 添加新行
table.add(new Label("666", labelStyle)).expandX();
// 使用图片
table.add(new Image(new TextureRegion(new Texture("worldmap/badlogic.jpg"), 20, 20))).expandX();
// 用于更新这个标签的效果
time=new Label(String.valueOf(mapY), labelStyle);
table.add(time).expandX(); time.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("鼠标点击这个标签: "+time.getText());
return super.touchDown(event, x, y, pointer, button);
}
// 还能添加 移入等事件
}); //添加输出
stage.addActor(table);
// 激活点击事件,否则不生效的
Gdx.input.setInputProcessor(stage);
} @Override
public void render() {
// 清除相机内容
ScreenUtils.clear(225f, 225f, 225f, 0.4f);
// 用户的输入
userInput(); // 更新相机
camera.position.x = mapX;
camera.position.y = mapY;
camera.update();// 一定要记得更新这个相机的视图
// 摄影机查看并渲染内容
renderer.setView(camera);
renderer.render(); // 绘制玩家, -128 是因为绘制的原点是中央
batch.begin();
batch.draw(player, Gdx.graphics.getWidth() / 2 - 25, Gdx.graphics.getHeight() / 2 - 25);
batch.end(); // 将绘制与相机组合
batch.setProjectionMatrix(stage.getCamera().combined);
// 绘制舞台
stage.draw();
} /**
* 轮询,用户输入
*/
private void userInput() {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A)) {
if (mapX < 64) {
mapX = 64; // 不给玩家走出屏幕外
} else
mapX -= moveSpeed;
} if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D)) {
if (mapX > 1280 - 64) {
mapX = 1280 - 64;// 不给玩家走出屏幕外
} else
mapX += moveSpeed;
}
if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W)) {
if (mapY > 960 - 64) {
mapY = 960 - 64;// 不给玩家走出屏幕外
} else
mapY += moveSpeed;
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) {
if (mapY < 64) {
mapY = 64;// 不给玩家走出屏幕外
} else
mapY -= moveSpeed;
// 按下的时候更改时间标签
time.setText(String.valueOf(mapX));
}
}
}

效果如下,按S会更新时间这个标签,点击时间还会控制台打印内容。


启动入口:

/**
* @author lingkang
* @date 2021/10/7 0:00
* @description
*/
public class MyMenuApp{
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = 1024;
config.height = 576;
new LwjglApplication(new MyMenu(), config);
}
}

libGDX游戏开发之菜单界面(四)的更多相关文章

  1. [libgdx游戏开发教程]使用Libgdx进行游戏开发(6)-添加主角和道具

    如前所述,我们的主角是兔子头.接下来我们实现它. 首先对AbstractGameObject添加变量并初始化: public Vector2 velocity; public Vector2 term ...

  2. Libgdx游戏开发(2)——接水滴游戏实现

    原文:Libgdx游戏开发(2)--接水滴游戏实现 - Stars-One的杂货小窝 本文使用Kotlin语言开发 通过本文的学习可以初步了解以下基础知识的使用: Basic file access ...

  3. 《C++游戏开发》笔记十四 平滑过渡的战争迷雾(二) 实现:真正的迷雾来了

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9712321 作者:七十一雾央 新浪微博:http:/ ...

  4. cocos2d-x 游戏开发之有限状态机(FSM) (四)

    cocos2d-x 游戏开发之有限状态机(FSM) (四) 虽然我们了解了FSM,并且可以写自己的FSM,但是有更好的工具帮我们完成这个繁琐的工作.SMC(http://smc.sourceforge ...

  5. Unity3D游戏开发从零单排(四) - 制作一个iOS游戏

    提要 此篇是一个国外教程的翻译,尽管有点老,可是适合新手入门. 自己去写代码.debug,布置场景,能够收获到非常多.游戏邦上已经有前面两部分的译文,这里翻译的是游戏的最后一个部分. 欢迎回来 在第一 ...

  6. [libGDX游戏开发教程]使用libGDX进行游戏开发(12)-Action动画

    前文章节列表:  使用libGDX进行游戏开发(11)-高级编程技巧   使用libGDX进行游戏开发(10)-音乐音效不求人,程序员也可以DIY   使用libGDX进行游戏开发(9)-场景过渡   ...

  7. [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计

    声明:<使用Libgdx进行游戏开发>是一个系列,文章的原文是<Learning Libgdx Game Development>,大家请周知.后续的文章连接在这里 使用Lib ...

  8. 精通libGDX游戏开发-RPG实战-开发游戏的基本前提

    说起RPG,大概国人是不会陌生的. 这不得不从中国单机游戏市场说起,由于早期软件市场被盗版杀死,顺带的,单机游戏软件作为软件市场的分支,也没赚什么钱,养不活公司纷纷倒闭,只到RPG游戏<仙剑奇侠 ...

  9. [libgdx游戏开发教程]使用Libgdx进行游戏开发(10)-音乐和音效

    本章音效文件都来自于公共许可: http://files.cnblogs.com/mignet/sounds.zip 在游戏中,播放背景音乐和音效是基本的功能. Libgdx提供了跨平台的声音播放功能 ...

  10. [libgdx游戏开发教程]使用Libgdx进行游戏开发(11)-高级编程技巧 Box2d和Shader

    高级编程技巧只是相对的,其实主要是讲物理模拟和着色器程序的使用. 本章主要讲解利用Box2D并用它来实现萝卜雨,然后是使用单色着色器shader让画面呈现单色状态:http://files.cnblo ...

随机推荐

  1. Blazor Server 发起HttpPost请求,但是多参数

    一.介绍 今天突然想起之前工作上遇到的一个问题,在做Blazor 开发时后端给的一个接口请求方式是Post ,但是他需要携带多个参数,新建一个公共类又觉得麻烦,我就尝试着怎么在Post请求中携带多个参 ...

  2. Github 组合搜索开源项目 (超详细)

    例如搜索 Spring Boot 相关项目  spring boot (最简单最常用) in:name spring boot (匹配项目名字)  in:name spring boot stars: ...

  3. pandas(进阶操作)-- 处理非数值型数据 -- 数据分析三剑客(核心)

    博客地址:https://www.cnblogs.com/zylyehuo/ 开发环境 anaconda 集成环境:集成好了数据分析和机器学习中所需要的全部环境 安装目录不可以有中文和特殊符号 jup ...

  4. [NSSCTF 2022 Spring Recruit]ezgame

    打开题目,发现是一个网页小游戏,就开始F12 提示到,需要分数超过65,才会得到flag 但不可能用手点吧(不怕麻烦还是可以) flag肯定是藏在了某个地方,仔细找找 发现有一个css,js文件,依次 ...

  5. HCTF 2023 wp

    HCTF 2023 wp 一.Misc 1.玩原神玩的 分析:附件为一张图片 观察最后一行,明显有flag的格式 搜索得知是 对照得flag为:hctf{yuanlainiyewanyuanshenh ...

  6. 21.2 Python 使用Scapy实现端口探测

    Scapy 是一款使用纯Python编写的跨平台网络数据包操控工具,它能够处理和嗅探各种网络数据包.能够很容易的创建,发送,捕获,分析和操作网络数据包,包括TCP,UDP,ICMP等协议,此外它还提供 ...

  7. CSP2021游记

    题外话 中午十二点半到了考场.没到时间不让进,恰巧发现 lhm 在对面饭店于是去讨论了一下上午 J 组的题,复习了线段树板子( 等到进考场坐好的时候已经两点半了,看考号本来以为我们同机房三个同学会坐一 ...

  8. 如何通过Python代码旋转PDF页面

    前言 日常处理 PDF 文档时,我们时常会遇到页面颠倒.很难正常阅读或打印的情况. 在这种情况下,我们可以通过旋转页面来调整文档的方向.旋转时,也可以根据具体情况,选择顺时针或逆时针旋转特定的角度,以 ...

  9. 一篇适合躺收藏夹的 Nexus3 搭建 NuGet&Docker 私有库的安装使用总结

    前言 Nexus 是支持 Nuget.Docker.Npm 等多种包的仓库管理器,可用做私有包的存储分发,缓存官方包.本篇将手把手教学使用 Nexus 搭建自己的 NuGe t& Docker ...

  10. 《最新出炉》系列初窥篇-Python+Playwright自动化测试-26-处理单选和多选按钮-下篇

    1.简介 今天这一篇宏哥主要是讲解一下,如何使用Playwright来遍历单选和多选按钮.大致两部分内容:一部分是宏哥在本地弄的一个小demo,另一部分,宏哥是利用JQueryUI网站里的单选和多选按 ...