libGDX游戏开发之实现血条、进度条、血量(十七)
lobgdx
系列,使用java开发游戏纯属业余爱好,当前也想进入游戏兴业。机遇使然进入了Java开发。
绘制原理:
用两个矩形进行绘制,第一个矩形背景为白色,第二个矩形背景为红色,作为血量显示
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import top.lingkang.pwo.utils.BitmapFontUtils;
/**
* @author lingkang
* Created by 2022/10/23
*/
public class LifeLine {
private Texture bgTexture, lifeTexture;
private BitmapFont bitmapFont;
public LifeLine() {
Pixmap pixmap = new Pixmap(100, 15, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
bgTexture = new Texture(pixmap);
bitmapFont = BitmapFontUtils.loadBitmapFont("0123456789/", 16, Color.WHITE);
Pixmap pixmap1 = new Pixmap(100, 15, Pixmap.Format.RGBA8888);
pixmap1.setColor(Color.RED);
pixmap1.fill();
lifeTexture = new Texture(pixmap1);
}
public void draw(SpriteBatch batch, int x, int y, int life, int maxLife) {
// 绘制关键,先绘制背景,再绘制血量,后绘制会覆盖前绘制
batch.draw(bgTexture, x, y, 100, 15);
batch.draw(lifeTexture, x, y, (float) life / maxLife * 100, 15);
bitmapFont.draw(batch, life + " / " + maxLife, x + 110, y + 15);
}
public void dispose() {
bgTexture.dispose();
lifeTexture.dispose();
bitmapFont.dispose();
}
}