I thought it would be cool to use libGDX to make a Tetris clone. I figured out pretty quickly how to make a falling block and maintain it inside the screen. My next challenge is to "spawn" a new block as soon as the current block has landed. I watched a couple of tutorials but they design their code different than I do which makes it hard for me to figure it out. I know I have to add the objects to an array and then keep painting them on the screen but this is where I get stuck.
This is part of a Block class I've written.
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();
}