gpt4 book ai didi

java - LibGdx-无法关闭的空白屏幕

转载 作者:行者123 更新时间:2023-12-01 13:19:13 25 4
gpt4 key购买 nike

我想在度假时制作一个简单的 flappy Bird 克隆(这是我从高中以来第一次尝试游戏编程),所以在查看 ligdx wiki 并调整代码以满足我的需求之后,我得到了类似的东西这个:

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

public class FlappyBall implements ApplicationListener {
Texture ballImage;
Texture chaoImage;
Texture canoImage;
OrthographicCamera camera;
SpriteBatch batch;
Rectangle ball;
int score = 0;
final float gravity = 9.0f;

@Override
public void create() {

ballImage = new Texture(Gdx.files.internal("crystalball_small.png"));
//chaoImage = new Texture(Gdx.files.internal("Flappy-Ground.png"));


camera = new OrthographicCamera(800,480);
camera.setToOrtho(false, 800, 480);


batch = new SpriteBatch();


ball = new Rectangle();
ball.x = 128;
ball.y = 20;
ball.width = 64;
ball.height = 64;

}

@Override
public void dispose() {
ballImage.dispose();
//chaoImage.dispose();

}

@Override
public void render() {
// cleans the screen
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

// rendenring ball
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(ballImage, ball.x, ball.y);
batch.end();

// jump(not tested)
if (Gdx.input.isTouched()) {
ball.y += 10;
}

// apply gravity(not tested)
while (ball.y < 480)
ball.y -= gravity * Gdx.graphics.getDeltaTime();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
}

但是这段代码最终显示了一个空白屏幕,当我按下 X 按钮时甚至无法关闭它(为了关闭它,我需要在 eclipse 中终止其进程或使用 xkill 命令:/)

PS:这是我的 Main.java 文件:

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Flappy Ball";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;

new LwjglApplication(new FlappyBall(), cfg);
}
}

Ps².:执行此代码时编译器没有显示错误

谢谢:D

最佳答案

你的渲染方法可能永远不会完成它的第一帧。 while 循环将继续进行,直到 ball.y 变得大于或等于 480。这永远不会发生,因为您正在减去一个正数。

        while (ball.y < 480)
ball.y -= gravity * Gdx.graphics.getDeltaTime();

我宁愿把它改成这样:

float change = gravity * Gdx.graphics.getDeltaTime();
if(ball.y - change >= 0)
ball.y -= change;

关于java - LibGdx-无法关闭的空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22181587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com