如何在libgdx中创建非常简单的暂停/恢复游戏

huangapple go评论60阅读模式
英文:

How to create very simple pause/resume game in libgdx

问题

我是libgdx的新手
如何在libgdx中创建一个非常简单的暂停/恢复游戏??

我正在尝试使用libgdx实现一个简单的游戏我想要创建一个暂停游戏但不知道如何根据用户输入暂停和恢复游戏请提供一些想法以及一些实际的代码来实现相同我正在使用libgdx库中演示的简单游戏代码谢谢

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    float x;
    float y;
    
    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        x = x + 4;

        if (Gdx.input.isKeyPressed(Input.Keys.P)) {
            // 暂停游戏
        }

        batch.begin();
        batch.draw(img, x, y);
        batch.end();
    }

    @Override
    public void dispose () {
        batch.dispose();
        img.dispose();
    }
}

按下P键来暂停游戏

if (Gdx.input.isKeyPressed(Input.Keys.P)) {
    // if (game.pause()) {
    //     game.resume();
    // } else {
    //     game.pause();
    // }
}
有什么想法吗谢谢
英文:

i am newbie for libgdx,
How to create very simple pause/resume game in libgdx??

I'm trying to implement simple game using libgdx. I have want to create pause the game but don't know how to pause and resume game based on user input.Kindly suggest idea as well as some practical code to implement the same.I am using simple game code demonstrated in libgdx library. Thank you.

public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
float x;
float y;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
x= x+4;
if(Gdx.input.isKeyPressed(Input.Keys.P)){
// pause
}
batch.begin();
batch.draw(img, x, y);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
}

}

Press P key to press the game

if(Gdx.input.isKeyPressed(Input.Keys.P)){
// if(game.pause()){
game.resume();
}else{
game.pause();
}
}

any idea??? thanks

答案1

得分: 1

你的渲染方法被称为可重复调用的,而“暂停”的含义取决于你。你必须实现处理“暂停”状态的逻辑,因为这对于libGDX来说没有任何意义。

如果你希望在游戏暂停时一切都冻结,但仍然绘制图形,最好的方法(我个人认为)是将绘制与计算(移动等)完全分开。因此,例如,当调用你的渲染方法时,你将调用绘制方法一次(绘制图形),但你还将计算你的计算方法应该被调用多少次(以便在不同帧速率下拥有相同的游戏速度),然后调用它那么多次。

然后,当游戏暂停时,你将跳过调用计算方法,只调用绘制方法。这样,没有任何内容会移动,直到解除暂停,相同的图形将被绘制。

为了保持暂停状态,你可以设置一些布尔变量。每次按下 p 键时,你应该将其反转,例如:

if(Gdx.input.isKeyPressed(Input.Keys.P)){
    paused = !paused;
}

或类似的方式(如 @Tenfour04 所建议的)。

英文:

Your render method is called repeatable and what "paused" means is up to you. You have to implement your logic that handles "paused" state because that doesn't mean anything to libGDX.

If you want everything to freeze when game is paused but still to be drawn best way is (IMHO) to fully separate drawing from calculating (movements and stuff). So i.e. when your render method is called you will call your drawing method once (to draw the graphics) but you will also calculate how many times your calculation method should be call (to have same game speed independently on frame rate) and call it that many times.

Then when your game is paused you will just skip calling your calculation method and call only drawing method. So nothing will move and until you end your pause the same graphics will be drawn.

And to keep paused state you can have some boolean variable. Every time p key is pressed you should invert it, like:

if(Gdx.input.isKeyPressed(Input.Keys.P)){
paused = !paused;
}

or something (as @Tenfour04 suggested).

huangapple
  • 本文由 发表于 2020年7月24日 20:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63073722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定