先上效果:

准备素材:

素材准备好,使用GDX Texture Packer打包

整体布局采用Stack + Table,每层都使用Table来绘制不同的元素

package com.mygdx.game.ui;import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.viewport.FitViewport;public class GameHUD implements Disposable {private static final String TAG = GameHUD.class.getSimpleName();private GameUIDemo game;private Stage stage;private OrthographicCamera camera;private FitViewport viewport;private TextureAtlas atlas;private BitmapFont font;public GameHUD(GameUIDemo game){this.game = game;//加载资源atlas = new TextureAtlas(Gdx.files.internal("ui/gameui.atlas"));font = new BitmapFont();font.setColor(Color.WHITE);//创建舞台camera = new OrthographicCamera();viewport = new FitViewport(GameUIDemo.V_WIDTH, GameUIDemo.V_HEIGHT, camera);stage = new Stage(viewport, game.batch);//创建多层布局框架Stack layout = new Stack();layout.setFillParent(true);//绘制头像、等级部分外框层Table headTable = new Table();headTable.setFillParent(true);headTable.left().top();headTable.add(new Image(atlas.findRegion("bottom_bg"))).colspan(3).left().width(GameUIDemo.V_WIDTH).height(30);headTable.row();Image headImg = new Image(atlas.findRegion("head"));float hw = GameUIDemo.V_WIDTH * 0.20f;float headScale = hw / headImg.getWidth();headTable.add(headImg).width(hw).height(headImg.getHeight() * headScale).left().top();Image headInner = new Image(atlas.findRegion("head_inner"));headTable.add(headInner).width(GameUIDemo.V_WIDTH - hw - 10).height(headInner.getHeight() * headScale - 1).padRight(-15f).left().top();Image headCorner = new Image(atlas.findRegion("head_corner"));headTable.add(headCorner).width(30).height(headCorner.getHeight() * headScale - 1).padLeft(-5f).left().top();headTable.row();layout.add(headTable);//头部名称、金币和钻石等显示框层Table textTable = new Table();textTable.setFillParent(true);textTable.left().top().padTop(30);TextureAtlas.AtlasRegion textInnerRegion = atlas.findRegion("text_inner");TextureAtlas.AtlasRegion textCornerRegion = atlas.findRegion("text_corner");Image textInner = new Image(textInnerRegion);Image textCorner = new Image(textCornerRegion);float cornerWidth = textCorner.getWidth() * headScale;float offsetLeft = headImg.getWidth() * headScale - 10;float innerWidth = GameUIDemo.V_WIDTH - offsetLeft - cornerWidth * 3 - 40;float innerHeight = textInner.getHeight() * headScale;float innerTop = (headInner.getHeight() - textInner.getHeight()) * headScale / 2;textTable.add(textInner).width(innerWidth / 3).height(innerHeight).padTop(innerTop - 1).padLeft(offsetLeft);textTable.add(textCorner).width(textCorner.getWidth() * headScale).height(innerHeight).padTop(innerTop - 1).left();textInner = new Image(textInnerRegion);textTable.add(textInner).width(innerWidth / 3).height(innerHeight).padTop(innerTop - 1).padLeft(20);textInner = new Image(textInnerRegion);textTable.add(textInner).width(innerWidth / 3).height(innerHeight).padTop(innerTop - 1).padLeft(20);layout.add(textTable);//金币、钻石图标层Table infoTable = new Table();infoTable.setFillParent(true);infoTable.left().top().padTop(30).padLeft(headImg.getWidth() * headScale - 30);Image levelImg = new Image(atlas.findRegion("level_bg"));infoTable.add(levelImg).width(levelImg.getWidth() * headScale).height(levelImg.getHeight() * headScale).left().top();Image goldenImg = new Image(atlas.findRegion("golden"));float goldenOffsetLeft = innerWidth / 3 + 20 - goldenImg.getWidth() * headScale / 2;infoTable.add(goldenImg).width(goldenImg.getWidth() * headScale).height(goldenImg.getHeight() * headScale).padLeft(goldenOffsetLeft).padTop(innerTop).left().top();TextureAtlas.AtlasRegion plusRegion = atlas.findRegion("plus");Image plusImg = new Image(plusRegion);float plusOffsetLeft = innerWidth / 3 - plusImg.getWidth() * headScale - 5;infoTable.add(plusImg).width(plusImg.getWidth() * headScale).height(plusImg.getHeight() * headScale).padLeft(plusOffsetLeft).padTop(innerTop + 1).left().top();Image dimondImg = new Image(atlas.findRegion("dimond"));infoTable.add(dimondImg).width(dimondImg.getWidth() * headScale).height(dimondImg.getHeight() * headScale).padLeft(10).padTop(innerTop + 1).left().top();plusImg = new Image(plusRegion);plusOffsetLeft = innerWidth / 3 - plusImg.getWidth() * headScale - 10;infoTable.add(plusImg).width(plusImg.getWidth() * headScale).height(plusImg.getHeight() * headScale).padLeft(plusOffsetLeft).padTop(innerTop + 1).left().top();layout.add(infoTable);//底部面板、底部背景层Table bottomTable = new Table();bottomTable.setFillParent(true);bottomTable.bottom();bottomTable.add(new Image(atlas.findRegion("bar_corner"))).left().width(30).height(60);bottomTable.add(new Image(atlas.findRegion("bar_inner"))).left().width(GameUIDemo.V_WIDTH - 60).height(60);Image rightCorner = new Image(atlas.findRegion("bar_corner"));rightCorner.setOriginX(15);rightCorner.setScaleX(-1);bottomTable.add(rightCorner).right().width(30).height(60);bottomTable.row();bottomTable.add(new Image(atlas.findRegion("bottom_bg"))).colspan(3).left().width(GameUIDemo.V_WIDTH).height(30);bottomTable.row();layout.add(bottomTable);//底部菜单图标层Table iconTable = new Table();float iconScaleX = 0.45f, iconScaleY = 0.4f;iconTable.setFillParent(true);iconTable.center().bottom().padBottom(50);Button iconHome = new Button(new TextureRegionDrawable(atlas.findRegion("icon_home")));iconTable.add(iconHome).bottom().width(iconScaleX * iconHome.getWidth()).height(iconScaleX * iconHome.getHeight()).pad(0, 5, 0, 10);Button iconHero = new Button(new TextureRegionDrawable(atlas.findRegion("icon_hero")));iconTable.add(iconHero).bottom().width(iconScaleX * iconHero.getWidth()).height(iconScaleX * iconHero.getHeight()).pad(0, 10, 0, 10);Button iconAdventure = new Button(new TextureRegionDrawable(atlas.findRegion("icon_adventure")));iconTable.add(iconAdventure).bottom().width(iconAdventure.getWidth() * iconScaleX).height(iconAdventure.getHeight() * iconScaleX).pad(0, 10, 0, 10);Button iconBag = new Button(new TextureRegionDrawable(atlas.findRegion("icon_bag")));iconTable.add(iconBag).bottom().width(iconScaleX * iconBag.getWidth()).height(iconScaleX * iconBag.getHeight()).pad(0, 10, 0, 10);Button iconUnit = new Button(new TextureRegionDrawable(atlas.findRegion("icon_unit")));iconTable.add(iconUnit).bottom().width(iconScaleX * iconUnit.getWidth()).height(iconScaleX * iconUnit.getHeight()).pad(0, 10, 0, 5);layout.add(iconTable);stage.addActor(layout);Gdx.input.setInputProcessor(stage);}public void draw(float dt){stage.draw();stage.act(dt);}@Overridepublic void dispose() {stage.dispose();}
}

启动入口类

package com.mygdx.game.ui;import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.ScreenUtils;public class GameUIDemo extends Game {public static final float V_WIDTH = 480;public static final float V_HEIGHT = 800;public SpriteBatch batch;public Skin skin;public BitmapFont font;private GameHUD gameHUD;@Overridepublic void create() {skin = new Skin(Gdx.files.internal("ui/uiskin.json"));font = new BitmapFont(Gdx.files.internal("bitmapfont_big.fnt"));batch = new SpriteBatch();gameHUD = new GameHUD(this);}@Overridepublic void render() {super.render();ScreenUtils.clear(Color.WHITE);gameHUD.draw(Gdx.graphics.getDeltaTime());}@Overridepublic void resize(int width, int height) {super.resize(width, height);}@Overridepublic void dispose() {super.dispose();}
}

Libgdx游戏编程之卡牌游戏UI布局相关推荐

  1. C++游戏编程:卡牌游戏

    CardGame 一段时间一直喜欢玩欢乐斗地主~~本来打算做出一个"跑得快"(有的地方叫争上游),技术有点欠缺,于是做出了一个类似打牌的卡牌游戏. 玩家将与两个电脑玩家进行游戏,一 ...

  2. 盛大搅局手游市场:引入日系卡牌游戏_0

    腾讯科技讯(娄池)7月18日消息,盛大游戏宣布热门手游<百万亚瑟王>于今日开启国服公测,这款由日本知名游戏公司Square Enix(SE)研发,盛大游戏韩国子公司Actoz Soft负责 ...

  3. 当年的三国java游戏_三国卡牌类手游塞班 分享问几年前玩的一个三国

    分享问几年前玩的一个三国卡牌手机游戏,好像是塞班...分享问几年前玩的一个三国卡牌手机游戏,好像是塞班,或者安卓的,单机的...是塞班的,之前也玩过.里面有霹雳车,攻城云梯,什么的.骑兵,枪兵,盾兵, ...

  4. SPS | 卡牌游戏Splinterlands,你也可以达到最强王者

    那些最好的买卖,刚开始的时候,从数字上看,几乎都会告诉你不要买.--沃伦·巴菲特 现在的链游,概念预售为多,真正能玩的少.不少项目,游戏还没做出来,就开始炒预期,或者收一笔不少的入场费. 我今天想跟大 ...

  5. 天池 在线编程 卡牌游戏(01背包)

    文章目录 1. 题目 2. 解题 1. 题目 你跟你的朋友在玩一个卡牌游戏,总共有 n 张牌. 每张牌的成本为 cost[i] 并且可以对对手造成 damage[i] 的伤害. 你总共有 totalM ...

  6. 基于QT开发的开源局域网联机UNO卡牌游戏报告(附github仓库地址)

    源代码: https://github.com/yunwei37/UNO-game-oop 目录 1. 需求分析 1.1. UNO卡牌游戏的基本功能 1.2. UNO卡牌游戏的规则 2. 总体设计 3 ...

  7. 计算机卡牌培养游戏,浅谈冒险游戏、卡牌游戏、养成游戏的几个设计要点

    badland-1.jpg (71.19 KB, 下载次数: 16) 2016-1-26 11:56 上传 这几种游戏设计好玩的关键点是: 1.收集:游戏中设计1个主要收集项目,如卡牌本身就是一个很好 ...

  8. 本科课程【虚拟现实引擎Unity3D】实验4 - 卡牌游戏完善

    大家好,我是[1+1=王], 热爱java的计算机(人工智能)渣硕研究生在读. 如果你也对java.人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!! Good better best, never ...

  9. 用js写卡牌游戏(一)

    用js写卡牌游戏(一) 不想看废话的点这 直接看代码的点这 废话(前言) 现在游戏多了,不过总是感觉不太对自己的口味,每个游戏都感觉和自己想象中的要差了那么一点点,所以我决定尝试着自己写一个游戏. 因 ...

  10. 《刀塔传奇》最初不是卡牌游戏——专访龙图COO王彦直

    http://game.donews.com/201407/2814031.shtm "最早是以投资商的身份去合作,后来又转换成发行商的身份去合作." 6月28日,<刀塔传奇 ...

最新文章

  1. CentOS下初次使用MySQL
  2. 【WEB安全】flask不出网回显方式
  3. IT人员健康信号之大脑保养
  4. Object-c基础之一:#import,NSLog(),数据类型
  5. 判断groupbox中所有的edit是否为空,并给出空的哪个edit为空
  6. mysql数据库参考_干货:MySQL数据库优化参考
  7. 深度学习之基于AlexNet实现猫狗大战
  8. pycharm 快捷键
  9. 香农信息熵之可怜的小猪
  10. 管理员账号_MaxCompute项目子账号做超级管理员
  11. ionic自动生成启动页和图标
  12. notepad++自动补全括号
  13. python网络编程相关
  14. 新手入门 | Pr剪辑教程
  15. VS2017无法登录:我们无法刷新此账户的凭证、我们无法添加此账户发送请求时出错、评估期已结束,请登录以解除产品锁定
  16. fireBug网络面板数据说明(转)
  17. 微软面试题--三个灯泡--三个开关
  18. 算法都是套路系列-动态规划模板
  19. 机器学习——集成学习之 AdaBoosting
  20. (四)《数据结构与算法》 青岛大学-王卓 链栈

热门文章

  1. Flink Interval Join Left Join
  2. Android FileProvider详细解析和踩坑指南
  3. 应届生工资排行榜,北上广深杭数据对比(基于320份简历)
  4. 解决169x/hao123浏览器劫持问题
  5. Nature Medicine:肠道菌群代谢组学-苯乙酸PAA能够诱发脂肪性肝病
  6. vue中img本地图片地址的具体使用
  7. Hulugans看什么 | 50多部迪士尼经典动画片带你重温童年
  8. ubuntu18.04安装0.6以上版本的flameshot
  9. 笔记本计算机提升性能,笔记本电脑改装!轻松提升电脑性能!
  10. 苹果计算机访问限制,苹果手机访问限制密码忘了怎么办