Tetris game on mikroTFT touchscreen and LPC1768
Diff: Block.cpp
- Revision:
- 2:6b6986c3d2bd
- Parent:
- 1:b4aa36ae11ac
- Child:
- 3:36de55e63fdf
--- a/Block.cpp Sat Feb 25 23:52:28 2017 +0000 +++ b/Block.cpp Fri Mar 03 13:19:26 2017 +0000 @@ -5,8 +5,11 @@ #include "Field.h" Block::Block() -{ - form = rand() % 7; +{ + if (nextForm > 6) + nextForm = rand() % 7; + form = nextForm; + nextForm = rand() % 7; angle = rand() % 4; x = 4 + rand() % 2; y = -1; @@ -18,18 +21,22 @@ void Block::rotateLeft() { - if ( angle == 0 ) - angle = 3; - else - angle = ( abs(angle - 1) ) % 4; + if ( !CheckRotateLeft() ) { + if ( angle == 0 ) + angle = 3; + else + angle = ( abs(angle - 1) ) % 4; + } } void Block::rotateRight() { - if ( angle == 3 ) - angle = 0; - else - angle = ( abs(angle + 1) ) % 4; + if ( !CheckRotateLeft() ) { + if ( angle == 3 ) + angle = 0; + else + angle = ( abs(angle + 1) ) % 4; + } } void Block::moveLeft() @@ -66,7 +73,7 @@ for ( xx = 0 ; xx < 5 ; xx++ ) { for (yy = 0 ; yy < 5 ; yy++ ) { if ( (Piece[form][angle][xx][yy] != 0) - && (Field[y + yy - 1][x + xx - 3] != 0) && + && (Field[y + yy - 2][x + xx - 3] != 0) && ( y + yy - 3 > 0 ) ) return 1; if ( (Piece[form][angle][xx][yy] != 0) && ( xx + x == 2 ) ) @@ -82,10 +89,42 @@ for ( xx = 0 ; xx < 5 ; xx++ ) { for (yy = 0 ; yy < 5 ; yy++ ) { if ( (Piece[form][angle][xx][yy] != 0) - && (Field[y + yy - 1][x + xx - 1] != 0) && + && (Field[y + yy - 2][x + xx - 1] != 0) && ( y + yy - 3 > 0 ) ) return 1; - if ( (Piece[form][angle][xx][yy] != 0) && ( (xx + x) == 11) ) + if ( (Piece[form][angle][xx][yy] != 0) && ( xx + x == 11 ) ) + return 1; + } + } + return 0; +} + +bool Block::CheckRotateLeft() +{ + int xx, yy; + for ( xx = 0 ; xx < 5 ; xx++ ) { + for (yy = 0 ; yy < 5 ; yy++ ) { + if ( (Piece[form][( abs(angle - 1) ) % 4][xx][yy] != 0) + && (Field[y + yy - 1][x + xx - 2] != 0) && + ( y + yy - 3 > 0 ) ) + return 1; + if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) && (( xx + x == 1 ) || ( xx + x == 11 )) ) + return 1; + } + } + return 0; +} + +bool Block::CheckRotateRight() +{ + int xx, yy; + for ( xx = 0 ; xx < 5 ; xx++ ) { + for (yy = 0 ; yy < 5 ; yy++ ) { + if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) + && (Field[y + yy - 1][x + xx - 2] != 0) && + ( y + yy - 3 > 0 ) ) + return 1; + if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) && (( xx + x == 1 ) || ( xx + x == 12 )) ) return 1; } }