Tetris for the RA8875, derived from another users implementation.

Dependencies:   RA8875

Fork of Tetris by Sergejs Popovs

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.

Committer:
sergun2311
Date:
Sat Mar 18 14:45:16 2017 +0000
Revision:
4:107d1d5a642e
Parent:
3:36de55e63fdf
Tetris V1

Who changed what in which revision?

UserRevisionLine numberNew 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
sergun2311 1:b4aa36ae11ac 11
sergun2311 1:b4aa36ae11ac 12 Block::Block()
sergun2311 4:107d1d5a642e 13 {
sergun2311 4:107d1d5a642e 14 srand (clock());
sergun2311 2:6b6986c3d2bd 15 if (nextForm > 6)
sergun2311 4:107d1d5a642e 16 nextForm = (rand() + 1) % 7;
sergun2311 2:6b6986c3d2bd 17 form = nextForm;
sergun2311 2:6b6986c3d2bd 18 nextForm = rand() % 7;
sergun2311 0:645509d95b8d 19 angle = rand() % 4;
sergun2311 0:645509d95b8d 20 x = 4 + rand() % 2;
sergun2311 1:b4aa36ae11ac 21 y = -1;
sergun2311 1:b4aa36ae11ac 22 }
sergun2311 1:b4aa36ae11ac 23
sergun2311 4:107d1d5a642e 24 // Destructor of an object
sergun2311 4:107d1d5a642e 25
sergun2311 1:b4aa36ae11ac 26 Block::~Block()
sergun2311 1:b4aa36ae11ac 27 {
sergun2311 1:b4aa36ae11ac 28 }
sergun2311 0:645509d95b8d 29
sergun2311 4:107d1d5a642e 30 // Check possibility of rotating of a block and
sergun2311 4:107d1d5a642e 31 // rotate it if it is possible
sergun2311 4:107d1d5a642e 32
sergun2311 1:b4aa36ae11ac 33 void Block::rotateLeft()
sergun2311 1:b4aa36ae11ac 34 {
sergun2311 2:6b6986c3d2bd 35 if ( !CheckRotateLeft() ) {
sergun2311 2:6b6986c3d2bd 36 if ( angle == 0 )
sergun2311 2:6b6986c3d2bd 37 angle = 3;
sergun2311 2:6b6986c3d2bd 38 else
sergun2311 2:6b6986c3d2bd 39 angle = ( abs(angle - 1) ) % 4;
sergun2311 2:6b6986c3d2bd 40 }
sergun2311 0:645509d95b8d 41 }
sergun2311 1:b4aa36ae11ac 42
sergun2311 1:b4aa36ae11ac 43 void Block::rotateRight()
sergun2311 1:b4aa36ae11ac 44 {
sergun2311 3:36de55e63fdf 45 if ( !CheckRotateRight() ) {
sergun2311 2:6b6986c3d2bd 46 if ( angle == 3 )
sergun2311 2:6b6986c3d2bd 47 angle = 0;
sergun2311 2:6b6986c3d2bd 48 else
sergun2311 2:6b6986c3d2bd 49 angle = ( abs(angle + 1) ) % 4;
sergun2311 2:6b6986c3d2bd 50 }
sergun2311 1:b4aa36ae11ac 51 }
sergun2311 0:645509d95b8d 52
sergun2311 4:107d1d5a642e 53 // Check possibility of moving the block and
sergun2311 4:107d1d5a642e 54 // moves it if it is possible
sergun2311 4:107d1d5a642e 55
sergun2311 1:b4aa36ae11ac 56 void Block::moveLeft()
sergun2311 1:b4aa36ae11ac 57 {
sergun2311 1:b4aa36ae11ac 58 if ( !CheckLeft() )
sergun2311 1:b4aa36ae11ac 59 x--;
sergun2311 1:b4aa36ae11ac 60 }
sergun2311 1:b4aa36ae11ac 61
sergun2311 1:b4aa36ae11ac 62 void Block::moveRight()
sergun2311 1:b4aa36ae11ac 63 {
sergun2311 1:b4aa36ae11ac 64 if ( !CheckRight() )
sergun2311 1:b4aa36ae11ac 65 x++;
sergun2311 1:b4aa36ae11ac 66 }
sergun2311 0:645509d95b8d 67
sergun2311 4:107d1d5a642e 68 // Check possibility to move block one level below. Checks both, frame and
sergun2311 4:107d1d5a642e 69 // blocks on the field ( separately ).
sergun2311 4:107d1d5a642e 70 // Returns 1 if it is not possible and
sergun2311 4:107d1d5a642e 71 // 0 if it is possible.
sergun2311 4:107d1d5a642e 72
sergun2311 0:645509d95b8d 73 bool Block::CheckBottom()
sergun2311 0:645509d95b8d 74 {
sergun2311 0:645509d95b8d 75 int xx, yy;
sergun2311 0:645509d95b8d 76 for ( xx = 0 ; xx < 5 ; xx++ ) {
sergun2311 1:b4aa36ae11ac 77 for (yy = 0 ; yy < 5 ; yy++ ) {
sergun2311 1:b4aa36ae11ac 78 if ( (Piece[form][angle][xx][yy] != 0)
sergun2311 1:b4aa36ae11ac 79 && (Field[y + yy - 1][x + xx - 2] != 0) &&
sergun2311 1:b4aa36ae11ac 80 ( y + yy - 1 > 0 ) )
sergun2311 1:b4aa36ae11ac 81 return 1;
sergun2311 3:36de55e63fdf 82 if ( (Piece[form][angle][xx][yy] != 0) && ( yy + y == MAXY + 1 ) )
sergun2311 0:645509d95b8d 83 return 1;
sergun2311 0:645509d95b8d 84 }
sergun2311 0:645509d95b8d 85 }
sergun2311 0:645509d95b8d 86 return 0;
sergun2311 1:b4aa36ae11ac 87 }
sergun2311 1:b4aa36ae11ac 88
sergun2311 4:107d1d5a642e 89 // Check possibility to move block to the left. Checks both, frame and
sergun2311 4:107d1d5a642e 90 // blocks on the field ( separately ).
sergun2311 4:107d1d5a642e 91 // Returns 1 if it is not possible and
sergun2311 4:107d1d5a642e 92 // 0 if it is possible.
sergun2311 4:107d1d5a642e 93
sergun2311 1:b4aa36ae11ac 94 bool Block::CheckLeft()
sergun2311 1:b4aa36ae11ac 95 {
sergun2311 1:b4aa36ae11ac 96 int xx, yy;
sergun2311 1:b4aa36ae11ac 97 for ( xx = 0 ; xx < 5 ; xx++ ) {
sergun2311 1:b4aa36ae11ac 98 for (yy = 0 ; yy < 5 ; yy++ ) {
sergun2311 1:b4aa36ae11ac 99 if ( (Piece[form][angle][xx][yy] != 0)
sergun2311 2:6b6986c3d2bd 100 && (Field[y + yy - 2][x + xx - 3] != 0) &&
sergun2311 1:b4aa36ae11ac 101 ( y + yy - 3 > 0 ) )
sergun2311 1:b4aa36ae11ac 102 return 1;
sergun2311 1:b4aa36ae11ac 103 if ( (Piece[form][angle][xx][yy] != 0) && ( xx + x == 2 ) )
sergun2311 1:b4aa36ae11ac 104 return 1;
sergun2311 1:b4aa36ae11ac 105 }
sergun2311 1:b4aa36ae11ac 106 }
sergun2311 1:b4aa36ae11ac 107 return 0;
sergun2311 1:b4aa36ae11ac 108 }
sergun2311 1:b4aa36ae11ac 109
sergun2311 4:107d1d5a642e 110 // Check possibility to move block to the right. Checks both, frame and
sergun2311 4:107d1d5a642e 111 // blocks on the field ( separately ).
sergun2311 4:107d1d5a642e 112 // Returns 1 if it is not possible and
sergun2311 4:107d1d5a642e 113 // 0 if it is possible.
sergun2311 4:107d1d5a642e 114
sergun2311 1:b4aa36ae11ac 115 bool Block::CheckRight()
sergun2311 1:b4aa36ae11ac 116 {
sergun2311 1:b4aa36ae11ac 117 int xx, yy;
sergun2311 1:b4aa36ae11ac 118 for ( xx = 0 ; xx < 5 ; xx++ ) {
sergun2311 1:b4aa36ae11ac 119 for (yy = 0 ; yy < 5 ; yy++ ) {
sergun2311 1:b4aa36ae11ac 120 if ( (Piece[form][angle][xx][yy] != 0)
sergun2311 2:6b6986c3d2bd 121 && (Field[y + yy - 2][x + xx - 1] != 0) &&
sergun2311 1:b4aa36ae11ac 122 ( y + yy - 3 > 0 ) )
sergun2311 1:b4aa36ae11ac 123 return 1;
sergun2311 3:36de55e63fdf 124 if ( (Piece[form][angle][xx][yy] != 0) && ( xx + x == MAXX + 1 ) )
sergun2311 2:6b6986c3d2bd 125 return 1;
sergun2311 2:6b6986c3d2bd 126 }
sergun2311 2:6b6986c3d2bd 127 }
sergun2311 2:6b6986c3d2bd 128 return 0;
sergun2311 2:6b6986c3d2bd 129 }
sergun2311 2:6b6986c3d2bd 130
sergun2311 4:107d1d5a642e 131 // Check possibility of rotation counter clockwise Checks both, frame and
sergun2311 4:107d1d5a642e 132 // blocks on the field ( separately ).
sergun2311 4:107d1d5a642e 133 // Returns 1 if it is not possible and
sergun2311 4:107d1d5a642e 134 // 0 if it is possible.
sergun2311 4:107d1d5a642e 135
sergun2311 2:6b6986c3d2bd 136 bool Block::CheckRotateLeft()
sergun2311 2:6b6986c3d2bd 137 {
sergun2311 2:6b6986c3d2bd 138 int xx, yy;
sergun2311 2:6b6986c3d2bd 139 for ( xx = 0 ; xx < 5 ; xx++ ) {
sergun2311 2:6b6986c3d2bd 140 for (yy = 0 ; yy < 5 ; yy++ ) {
sergun2311 2:6b6986c3d2bd 141 if ( (Piece[form][( abs(angle - 1) ) % 4][xx][yy] != 0)
sergun2311 2:6b6986c3d2bd 142 && (Field[y + yy - 1][x + xx - 2] != 0) &&
sergun2311 2:6b6986c3d2bd 143 ( y + yy - 3 > 0 ) )
sergun2311 2:6b6986c3d2bd 144 return 1;
sergun2311 3:36de55e63fdf 145 if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) && (( xx + x == 1 ) || ( xx + x == MAXX + 1 )) )
sergun2311 2:6b6986c3d2bd 146 return 1;
sergun2311 4:107d1d5a642e 147 }
sergun2311 2:6b6986c3d2bd 148 }
sergun2311 2:6b6986c3d2bd 149 return 0;
sergun2311 2:6b6986c3d2bd 150 }
sergun2311 2:6b6986c3d2bd 151
sergun2311 4:107d1d5a642e 152 // Check possibility of rotation clockwise Checks both, frame and
sergun2311 4:107d1d5a642e 153 // blocks on the field ( separately ).
sergun2311 4:107d1d5a642e 154 // Returns 1 if it is not possible and
sergun2311 4:107d1d5a642e 155 // 0 if it is possible.
sergun2311 4:107d1d5a642e 156
sergun2311 2:6b6986c3d2bd 157 bool Block::CheckRotateRight()
sergun2311 2:6b6986c3d2bd 158 {
sergun2311 2:6b6986c3d2bd 159 int xx, yy;
sergun2311 2:6b6986c3d2bd 160 for ( xx = 0 ; xx < 5 ; xx++ ) {
sergun2311 2:6b6986c3d2bd 161 for (yy = 0 ; yy < 5 ; yy++ ) {
sergun2311 2:6b6986c3d2bd 162 if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0)
sergun2311 2:6b6986c3d2bd 163 && (Field[y + yy - 1][x + xx - 2] != 0) &&
sergun2311 2:6b6986c3d2bd 164 ( y + yy - 3 > 0 ) )
sergun2311 2:6b6986c3d2bd 165 return 1;
sergun2311 3:36de55e63fdf 166 if ( (Piece[form][( abs(angle + 1) ) % 4][xx][yy] != 0) && (( xx + x == 1 ) || ( xx + x == MAXX + 2 )) )
sergun2311 1:b4aa36ae11ac 167 return 1;
sergun2311 1:b4aa36ae11ac 168 }
sergun2311 1:b4aa36ae11ac 169 }
sergun2311 1:b4aa36ae11ac 170 return 0;
sergun2311 1:b4aa36ae11ac 171 }