Tetris Loop

Hello everyone,

I'm currently making a tetris using SDL as a part of my coursework and I have a question.
My tetris consists of a class TetrisWindow that contains all the useful functions like drawLine etc., but most importantly it contains a predefined function work, that acts as a loop and that's where I insert my pieces so they fall, the function work gets called infinetly unless there is a condition to stop it. I also have a parent class Block that acts as an abstract class and contains drawBlock method. It's children are all the pieces of tetris, so basically a Piece "I" would look like this:
1
2
3
4
5
6
7
void PieceI::drawPiece()
{
  drawBlock(x1,y1);
  drawBlock(x2,y2);
  drawBlock(x3,y3);
  drawBlock(x4,y4);
}


And to move any Piece down I just use:

1
2
3
4
5
6
7
8
9
10
void PieceI::movePieceDown()
{
  if (y1 !=20 && y2 != 20 && y3 != 20 && y4 != 20) //makes the piece stop at the bottom
  {
  drawBlock(x1,y1++);
  drawBlock(x2,y2++);
  drawBlock(x3,y3++);
  drawBlock(x3,y4++);
  }
}


To randomly pick pieces I also have a method in Block, that randomly returns new PieceI or new PieceO:

1
2
3
4
5
6
7
8
9
Block* Block::choosePiece()
{
  switch(rand() %6)
  {
    case 0: return new PieceI; break;
    case 1: return new PieceO; break;
    etc......
   }
}


So basically, in class TetrisWindow I have a pointer to the class Block that I name "current", so in class TetrisWindow I have:

1
2
3
4
5
6
7
8
9
void TetrisWindow::work()
{
  counter++
  if(counter == 50)
  {
    current->choosePiece()->movePieceDown()
    counter = 0;
  }
}


(I use the counter to slow down the work function, because without it the pieces are falling too fast.)

Finally, the problem is that my pieces just keep switching at the top without even getting a change to drop down. So everytime choosePiece() just returns a newPiece and that's it. I just don't know which condition I need to put so the pieces start falling.

I'm sorry if something is unclear I am ready to clarify everything.

Thank you very much in advance!
Last edited on
For your timing problem you may use this:

https://wiki.libsdl.org/SDL_GetTicks?highlight=%28\bCategoryAPI\b%29|%28SDLFunctionTemplate%29
https://wiki.libsdl.org/SDL_TICKS_PASSED


When a new piece is generated (with choosePiece()) you need to store it in an array or vector. Move/show the pieces only that are in that container.
Thanks for the GetTicks! Very neat!

As for the array, I created one to store the pieces that already fell, so later I can detect collision. I thought maybe I need to create a separate method that only chooses a new piece and another one that loops movePieceDown ()...
As for the array, I created one to store the pieces that already fell, so later I can detect collision.
Yes, that's ok.

In that case you need a pointer to the falling piece.

If the falling piece pointer is null -> choosePiece()

otherwise

falling piece -> movePieceDown().

If you detect a collision set falling piece pointer -> null

[EDIT]
There shouldn't be another object Block. Either Piece or Block.
I suggest that you use const arrays of x/y coordinates to describe the shape of the piece. Together with the offset you can do the collision check for each x/y coordinate.
Last edited on
OK I got it, I will create a pointer to a falling piece. Thanks :)

As for the arrays, I thought about it, it would be easier, but the teacher wants us to avoid storing pieces in arrays, he says it'seems too C. I know it'sounds weird, but I am trying to stick to the plan. Thank you very much for the help! I will try to implement what you suggested!
Topic archived. No new replies allowed.