

These local functions will be described later.

Aside from that, a special local function, drawfield, is also used to generate an image visualization of the current ‘field’ array. forcefalldown function is used to force all hanging blocks to fall down in the third game mode (hence no hanging blocks in this game mode), while checkblockposition checks the validity of the falling object’s position (thus, preventing stacking two blocks in one position). Notice that in the main while loop above, several local function was used other than addblock2field function. function field=addblock2field(field,row,col,color)įield(row(dotID),col(dotID))=color(dotID) After the falling blocks are permanently added, a check was done to see if there is a complete block set. This means that the falling blocks has been added permanently to the ‘field’ array (Later when explaining the nested while loop, you’ll understand why I say permanently). Because of simple representation of ‘field’ array and the falling object’s variables, the addblock2field function is simple and can be written in five lines as shown below.
#CREATING FALLING BLOCKS IN MATHLAB UPDATE#
Then those final conditions are used to update the ‘field’ array by using addblock2field function. At the end of the nested loop, these three variables will represent the falling object’s condition at the most bottom part of the field (final condition). In the nested loop (the while playstat loop), these three variables will be updated over time, as it slowly falls down and as player move/rotate it. Before the nested loop begin, those three variables represent the falling object’s position at the middle top of the field (that’s why ‘c_current’ is initialized by adding it by three, that is to make the falling block placed in the middle top of the field). These variables were initialized by choosing randomly from the ‘shapemodifier_x’ and ‘shapemodifier_y’ array declared previously.


%Updating tetris block into field permanentlyįield=addblock2field(field,r_current,c_current.Īs stated previously, in this code, three variables (‘r_current’, ‘c_current’, and ‘color_current’) were used to define the falling object’s condition. %Checking block position and updating field If ~checkblockposition(r_current,c_current,field) Thus, as the database for the falling blocks’ shape, the following constant variable are declared in cells format as follows: shapemodifier_x Each element on those arrays respectively represent each falling block position in field row and column and it color. Since tetris object is always consist of four blocks (hence, the name is Tetris), I decided to use three 1×4 arrays to represent the falling object, ‘r_current’, and ‘color_current’. 0 means the block is not occupied, while a number of 1 to 7 respectively represent a different block color.Īside from that, a representation of the falling Tetris object, the shape database, and the color database must also be determined. However, to avoid confusion when visualizing the game, I let the first row of the ‘field’ array represent the most-upper blocks on the field instead of the most-lower blocks, and so on. In this ‘field’ array, each elements will represent whether a block in the field is occupied or not and what color that occupied in. This ‘field’ array will be modified throughout the game and will be used for visualization. For simplicity, I used a standard 20×10 array for this ‘field’ array (because Tetris field is 20 blocks in height and 10 blocks in width).
#CREATING FALLING BLOCKS IN MATHLAB HOW TO#
Since Tetris will involve a board or field of blocks, how to represent those fields in the code must be determined first before writing the code. As I promised (after more than a week), here it is, the explanation for my MATLAB Tetris code.
