0

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();



}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sociopaten
  • 144
  • 1
  • 9
  • 1
    Your `spawnBlock` method should do nothing more than create a block and add it to an array. It makes no sense for it to loop through an array and move it some arbitrary amount after spawning it, and then recreate it immediately on the next frame. Remember that the `render` method is already called in a loop continually. If you want something to move, you make it move by a little bit in each call to `render`. Using a for loop to move something doesn't make sense, because the entire for loop will terminate before the item is even drawn to the screen. – Tenfour04 Oct 21 '14 at 17:45
  • That really helped. Now I think I know what I´m doing at least. Feeling stupid about that for-loop though. Thanks! – Sociopaten Oct 21 '14 at 18:41

1 Answers1

0

In two words i can recomend you that approach.

Create Block.java class and move all functions from spawnBlok and render() function inside it. It's help to clear your main class from garbage code. Then do smth like that

{ 
  Block myBlock;

  void onCreate() {
     myBlock = new Block();
     // .....
  }

  void createBlock() {
     myBlock = new Block();
  }

  render() {
    myBlock.render;
  }
}

Last you need call createBlock() from Block.java when your block get bottom.

    if(this.y < 0) {
        myGame.createBlock(); 
    }

I think it's better to recreate you Block not to use one item. Later you can create several blocks falling at the same time (just create Block[] massive of blocks) to complicte your game. It's not an exact working code but i think you can finish it in several minutes. Ask if you hve some questions

Stanislav Batura
  • 420
  • 4
  • 11