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!