libGDX を使用して Tetris のクローンを作成するのはクールだと思いました。落下するブロックを作成して画面内に維持する方法をすぐに理解しました。次の課題は、現在のブロックが着陸したらすぐに新しいブロックを「生成」することです。いくつかのチュートリアルを見ましたが、コードの設計が私とは異なるため、理解するのが難しくなっています。オブジェクトを配列に追加してから画面に描画し続ける必要があることはわかっていますが、ここで行き詰まります。
これは、私が書いた Block クラスの一部です。
public void spawnBlock(float delta) {
box = new Rectangle();
box.width = 40f;
box.height = 40f;
this.setPosition(TetrixGame.WIDTH / 2 - box.width / 2, TetrixGame.HEIGHT);
boolean isFalling = true;
for(int i = TetrixGame.HEIGHT; i > 0; --i) {
box.y -= (fallSpeed * delta);
if(Gdx.input.isKeyJustPressed(Keys.LEFT) && isFalling) {
stepLeft();
}
if(Gdx.input.isKeyJustPressed(Keys.RIGHT) && isFalling) {
stepRight();
}
if(Gdx.input.isKeyPressed(Keys.DOWN)) {
setDown();
}
if(box.x < 0) box.x = 0;
if(box.x > TetrixGame.WIDTH - box.width) box.x = TetrixGame.WIDTH - box.width;
if(box.y < 0) {
box.y = 0;
isFalling = false;
blocks.add(box);
}
}
}
public class TetrixGame extends Game {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "TetriX";
private Block block;
private ShapeRenderer renderer;
private OrthographicCamera camera;
@Override
public void create() {
block = new Block();
renderer = new ShapeRenderer();
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
}
@Override
public void render() {
super.render();
Gdx.gl.glClearColor(0, 0, .2f, .8f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setProjectionMatrix(camera.combined);
camera.update();
block.spawnBlock(Gdx.graphics.getDeltaTime());
renderer.begin(ShapeType.Filled);
//I know this part should be in a loop but it´s not working
renderer.rect(block.getX(), block.getY(), block.getWidth(), block.getHeight());
renderer.end();
}