Tetris for the RA8875, derived from another users implementation.
Fork of Tetris by
Tetris, adapted to the RA8875 graphics library and display.
As currently implemented, this version is defined for the 800x480 display. A number of macros can adapt it for other screen resolutions.
Further, while presently configured for landscape mode, it should be fairly easy to reconfigure it for portrait mode.
Tetris/Block.cpp@11:2bdc83648d7f, 2020-03-29 (annotated)
- Committer:
- WiredHome
- Date:
- Sun Mar 29 18:21:14 2020 +0000
- Revision:
- 11:2bdc83648d7f
- Parent:
- 7:0e426e81c566
Pick up a bug-fix on jpeg rendering
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sergun2311 | 0:645509d95b8d | 1 | #include "Block.h" |
sergun2311 | 0:645509d95b8d | 2 | #include "mbed.h" |
sergun2311 | 0:645509d95b8d | 3 | #include "playGround.h" |
sergun2311 | 0:645509d95b8d | 4 | #include "Piece.h" |
sergun2311 | 0:645509d95b8d | 5 | #include "Field.h" |
sergun2311 | 4:107d1d5a642e | 6 | #include "Define.h" |
sergun2311 | 4:107d1d5a642e | 7 | #include <ctime> |
sergun2311 | 4:107d1d5a642e | 8 | |
sergun2311 | 4:107d1d5a642e | 9 | // Constructor of the object. Gives the random form and |
sergun2311 | 4:107d1d5a642e | 10 | // defines the position on the field |
WiredHome | 7:0e426e81c566 | 11 | // |
sergun2311 | 1:b4aa36ae11ac | 12 | Block::Block() |
sergun2311 | 4:107d1d5a642e | 13 | { |
WiredHome | 7:0e426e81c566 | 14 | #if 0 |
WiredHome | 7:0e426e81c566 | 15 | srand(clock()); |
WiredHome | 7:0e426e81c566 | 16 | form = rand() % 7; |
WiredHome | 7:0e426e81c566 | 17 | #else |
sergun2311 | 4:107d1d5a642e | 18 | srand (clock()); |
sergun2311 | 2:6b6986c3d2bd | 19 | if (nextForm > 6) |
sergun2311 | 4:107d1d5a642e | 20 | nextForm = (rand() + 1) % 7; |
sergun2311 | 2:6b6986c3d2bd | 21 | form = nextForm; |
WiredHome | 7:0e426e81c566 | 22 | #endif |
sergun2311 | 2:6b6986c3d2bd | 23 | nextForm = rand() % 7; |
sergun2311 | 0:645509d95b8d | 24 | angle = rand() % 4; |
sergun2311 | 0:645509d95b8d | 25 | x = 4 + rand() % 2; |
sergun2311 | 1:b4aa36ae11ac | 26 | y = -1; |
sergun2311 | 1:b4aa36ae11ac | 27 | } |
sergun2311 | 1:b4aa36ae11ac | 28 | |
sergun2311 | 4:107d1d5a642e | 29 | // Destructor of an object |
WiredHome | 7:0e426e81c566 | 30 | // |
sergun2311 | 1:b4aa36ae11ac | 31 | Block::~Block() |
sergun2311 | 1:b4aa36ae11ac | 32 | { |
sergun2311 | 1:b4aa36ae11ac | 33 | } |
sergun2311 | 0:645509d95b8d | 34 | |
sergun2311 | 4:107d1d5a642e | 35 | // Check possibility of rotating of a block and |
sergun2311 | 4:107d1d5a642e | 36 | // rotate it if it is possible |
WiredHome | 7:0e426e81c566 | 37 | // |
sergun2311 | 1:b4aa36ae11ac | 38 | void Block::rotateLeft() |
sergun2311 | 1:b4aa36ae11ac | 39 | { |
sergun2311 | 2:6b6986c3d2bd | 40 | if ( !CheckRotateLeft() ) { |
sergun2311 | 2:6b6986c3d2bd | 41 | if ( angle == 0 ) |
sergun2311 | 2:6b6986c3d2bd | 42 | angle = 3; |
sergun2311 | 2:6b6986c3d2bd | 43 | else |
sergun2311 | 2:6b6986c3d2bd | 44 | angle = ( abs(angle - 1) ) % 4; |
sergun2311 | 2:6b6986c3d2bd | 45 | } |
sergun2311 | 0:645509d95b8d | 46 | } |
sergun2311 | 1:b4aa36ae11ac | 47 | |
sergun2311 | 1:b4aa36ae11ac | 48 | void Block::rotateRight() |
sergun2311 | 1:b4aa36ae11ac | 49 | { |
sergun2311 | 3:36de55e63fdf | 50 | if ( !CheckRotateRight() ) { |
sergun2311 | 2:6b6986c3d2bd | 51 | if ( angle == 3 ) |
sergun2311 | 2:6b6986c3d2bd | 52 | angle = 0; |
sergun2311 | 2:6b6986c3d2bd | 53 | else |
sergun2311 | 2:6b6986c3d2bd | 54 | angle = ( abs(angle + 1) ) % 4; |
sergun2311 | 2:6b6986c3d2bd | 55 | } |
sergun2311 | 1:b4aa36ae11ac | 56 | } |
sergun2311 | 0:645509d95b8d | 57 | |
sergun2311 | 4:107d1d5a642e | 58 | // Check possibility of moving the block and |
sergun2311 | 4:107d1d5a642e | 59 | // moves it if it is possible |
WiredHome | 7:0e426e81c566 | 60 | // |
sergun2311 | 1:b4aa36ae11ac | 61 | void Block::moveLeft() |
sergun2311 | 1:b4aa36ae11ac | 62 | { |
sergun2311 | 1:b4aa36ae11ac | 63 | if ( !CheckLeft() ) |
sergun2311 | 1:b4aa36ae11ac | 64 | x--; |
sergun2311 | 1:b4aa36ae11ac | 65 | } |
sergun2311 | 1:b4aa36ae11ac | 66 | |
sergun2311 | 1:b4aa36ae11ac | 67 | void Block::moveRight() |
sergun2311 | 1:b4aa36ae11ac | 68 | { |
sergun2311 | 1:b4aa36ae11ac | 69 | if ( !CheckRight() ) |
sergun2311 | 1:b4aa36ae11ac | 70 | x++; |
sergun2311 | 1:b4aa36ae11ac | 71 | } |
sergun2311 | 0:645509d95b8d | 72 | |
sergun2311 | 4:107d1d5a642e | 73 | // Check possibility to move block one level below. Checks both, frame and |
sergun2311 | 4:107d1d5a642e | 74 | // blocks on the field ( separately ). |
sergun2311 | 4:107d1d5a642e | 75 | // Returns 1 if it is not possible and |
sergun2311 | 4:107d1d5a642e | 76 | // 0 if it is possible. |
WiredHome | 7:0e426e81c566 | 77 | // |
sergun2311 | 0:645509d95b8d | 78 | bool Block::CheckBottom() |
sergun2311 | 0:645509d95b8d | 79 | { |
sergun2311 | 0:645509d95b8d | 80 | int xx, yy; |
WiredHome | 7:0e426e81c566 | 81 | |
sergun2311 | 0:645509d95b8d | 82 | for ( xx = 0 ; xx < 5 ; xx++ ) { |
sergun2311 | 1:b4aa36ae11ac | 83 | for (yy = 0 ; yy < 5 ; yy++ ) { |
sergun2311 | 1:b4aa36ae11ac | 84 | if ( (Piece[form][angle][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 85 | && (Field[y + yy - 1][x + xx - 2] != 0) |
WiredHome | 7:0e426e81c566 | 86 | && ( y + yy - 1 > 0 ) ) { |
sergun2311 | 1:b4aa36ae11ac | 87 | return 1; |
WiredHome | 7:0e426e81c566 | 88 | } else if ( (Piece[form][angle][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 89 | && ( yy + y == MAXY + 1 ) ) { |
sergun2311 | 0:645509d95b8d | 90 | return 1; |
WiredHome | 7:0e426e81c566 | 91 | } |
sergun2311 | 0:645509d95b8d | 92 | } |
sergun2311 | 0:645509d95b8d | 93 | } |
sergun2311 | 0:645509d95b8d | 94 | return 0; |
sergun2311 | 1:b4aa36ae11ac | 95 | } |
sergun2311 | 1:b4aa36ae11ac | 96 | |
sergun2311 | 4:107d1d5a642e | 97 | // Check possibility to move block to the left. Checks both, frame and |
sergun2311 | 4:107d1d5a642e | 98 | // blocks on the field ( separately ). |
sergun2311 | 4:107d1d5a642e | 99 | // Returns 1 if it is not possible and |
sergun2311 | 4:107d1d5a642e | 100 | // 0 if it is possible. |
WiredHome | 7:0e426e81c566 | 101 | // |
sergun2311 | 1:b4aa36ae11ac | 102 | bool Block::CheckLeft() |
sergun2311 | 1:b4aa36ae11ac | 103 | { |
sergun2311 | 1:b4aa36ae11ac | 104 | int xx, yy; |
WiredHome | 7:0e426e81c566 | 105 | |
sergun2311 | 1:b4aa36ae11ac | 106 | for ( xx = 0 ; xx < 5 ; xx++ ) { |
sergun2311 | 1:b4aa36ae11ac | 107 | for (yy = 0 ; yy < 5 ; yy++ ) { |
sergun2311 | 1:b4aa36ae11ac | 108 | if ( (Piece[form][angle][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 109 | && (Field[y + yy - 2][x + xx - 3] != 0) |
WiredHome | 7:0e426e81c566 | 110 | && ( y + yy - 3 > 0 ) ) { |
sergun2311 | 1:b4aa36ae11ac | 111 | return 1; |
WiredHome | 7:0e426e81c566 | 112 | } else if ( (Piece[form][angle][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 113 | && ( xx + x == 2 ) ) { |
sergun2311 | 1:b4aa36ae11ac | 114 | return 1; |
WiredHome | 7:0e426e81c566 | 115 | } |
sergun2311 | 1:b4aa36ae11ac | 116 | } |
sergun2311 | 1:b4aa36ae11ac | 117 | } |
sergun2311 | 1:b4aa36ae11ac | 118 | return 0; |
sergun2311 | 1:b4aa36ae11ac | 119 | } |
sergun2311 | 1:b4aa36ae11ac | 120 | |
sergun2311 | 4:107d1d5a642e | 121 | // Check possibility to move block to the right. Checks both, frame and |
sergun2311 | 4:107d1d5a642e | 122 | // blocks on the field ( separately ). |
sergun2311 | 4:107d1d5a642e | 123 | // Returns 1 if it is not possible and |
sergun2311 | 4:107d1d5a642e | 124 | // 0 if it is possible. |
WiredHome | 7:0e426e81c566 | 125 | // |
sergun2311 | 1:b4aa36ae11ac | 126 | bool Block::CheckRight() |
sergun2311 | 1:b4aa36ae11ac | 127 | { |
sergun2311 | 1:b4aa36ae11ac | 128 | int xx, yy; |
WiredHome | 7:0e426e81c566 | 129 | |
sergun2311 | 1:b4aa36ae11ac | 130 | for ( xx = 0 ; xx < 5 ; xx++ ) { |
sergun2311 | 1:b4aa36ae11ac | 131 | for (yy = 0 ; yy < 5 ; yy++ ) { |
sergun2311 | 1:b4aa36ae11ac | 132 | if ( (Piece[form][angle][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 133 | && (Field[y + yy - 2][x + xx - 1] != 0) |
WiredHome | 7:0e426e81c566 | 134 | && ( y + yy - 3 > 0 ) ) { |
sergun2311 | 1:b4aa36ae11ac | 135 | return 1; |
WiredHome | 7:0e426e81c566 | 136 | } else if ( (Piece[form][angle][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 137 | && ( xx + x == MAXX + 1 ) ) { |
sergun2311 | 2:6b6986c3d2bd | 138 | return 1; |
WiredHome | 7:0e426e81c566 | 139 | } |
sergun2311 | 2:6b6986c3d2bd | 140 | } |
sergun2311 | 2:6b6986c3d2bd | 141 | } |
sergun2311 | 2:6b6986c3d2bd | 142 | return 0; |
sergun2311 | 2:6b6986c3d2bd | 143 | } |
sergun2311 | 2:6b6986c3d2bd | 144 | |
sergun2311 | 4:107d1d5a642e | 145 | // Check possibility of rotation counter clockwise Checks both, frame and |
sergun2311 | 4:107d1d5a642e | 146 | // blocks on the field ( separately ). |
sergun2311 | 4:107d1d5a642e | 147 | // Returns 1 if it is not possible and |
sergun2311 | 4:107d1d5a642e | 148 | // 0 if it is possible. |
WiredHome | 7:0e426e81c566 | 149 | // |
sergun2311 | 2:6b6986c3d2bd | 150 | bool Block::CheckRotateLeft() |
sergun2311 | 2:6b6986c3d2bd | 151 | { |
sergun2311 | 2:6b6986c3d2bd | 152 | int xx, yy; |
WiredHome | 7:0e426e81c566 | 153 | |
sergun2311 | 2:6b6986c3d2bd | 154 | for ( xx = 0 ; xx < 5 ; xx++ ) { |
sergun2311 | 2:6b6986c3d2bd | 155 | for (yy = 0 ; yy < 5 ; yy++ ) { |
sergun2311 | 2:6b6986c3d2bd | 156 | if ( (Piece[form][( abs(angle - 1) ) % 4][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 157 | && (Field[y + yy - 1][x + xx - 2] != 0) |
WiredHome | 7:0e426e81c566 | 158 | && ( y + yy - 3 > 0 ) ) { |
sergun2311 | 2:6b6986c3d2bd | 159 | return 1; |
WiredHome | 7:0e426e81c566 | 160 | } else if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 161 | && (( xx + x == 1 ) || ( xx + x == MAXX + 1 )) ) { |
sergun2311 | 2:6b6986c3d2bd | 162 | return 1; |
WiredHome | 7:0e426e81c566 | 163 | } |
sergun2311 | 4:107d1d5a642e | 164 | } |
sergun2311 | 2:6b6986c3d2bd | 165 | } |
sergun2311 | 2:6b6986c3d2bd | 166 | return 0; |
sergun2311 | 2:6b6986c3d2bd | 167 | } |
sergun2311 | 2:6b6986c3d2bd | 168 | |
sergun2311 | 4:107d1d5a642e | 169 | // Check possibility of rotation clockwise Checks both, frame and |
sergun2311 | 4:107d1d5a642e | 170 | // blocks on the field ( separately ). |
sergun2311 | 4:107d1d5a642e | 171 | // Returns 1 if it is not possible and |
sergun2311 | 4:107d1d5a642e | 172 | // 0 if it is possible. |
WiredHome | 7:0e426e81c566 | 173 | // |
sergun2311 | 2:6b6986c3d2bd | 174 | bool Block::CheckRotateRight() |
sergun2311 | 2:6b6986c3d2bd | 175 | { |
sergun2311 | 2:6b6986c3d2bd | 176 | int xx, yy; |
WiredHome | 7:0e426e81c566 | 177 | |
sergun2311 | 2:6b6986c3d2bd | 178 | for ( xx = 0 ; xx < 5 ; xx++ ) { |
sergun2311 | 2:6b6986c3d2bd | 179 | for (yy = 0 ; yy < 5 ; yy++ ) { |
sergun2311 | 2:6b6986c3d2bd | 180 | if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 181 | && (Field[y + yy - 1][x + xx - 2] != 0) |
WiredHome | 7:0e426e81c566 | 182 | && ( y + yy - 3 > 0 ) ) { |
sergun2311 | 2:6b6986c3d2bd | 183 | return 1; |
WiredHome | 7:0e426e81c566 | 184 | } else if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) |
WiredHome | 7:0e426e81c566 | 185 | && (( xx + x == 1 ) || ( xx + x == MAXX + 2 )) ) { |
sergun2311 | 1:b4aa36ae11ac | 186 | return 1; |
WiredHome | 7:0e426e81c566 | 187 | } |
sergun2311 | 1:b4aa36ae11ac | 188 | } |
sergun2311 | 1:b4aa36ae11ac | 189 | } |
sergun2311 | 1:b4aa36ae11ac | 190 | return 0; |
sergun2311 | 1:b4aa36ae11ac | 191 | } |