This model shows a re-visit of the classic tetris game which has been shipping with Stateflow® to use some of the more modern programming paradigms and features. It shows the use of the
hasChanged functional operator to query changes in the value of inputs, MATLAB® function to abstract algorithmic sub-components and some useful semantic abstractions such as hierarchical states and inner transitions.
The Stateflow chart TetrisLogic implements the logic behind the game. The arena or the playing field is represented by a [21x12] array which represents the playing arena. The chart updates this array at each simulation step and only uses MATLAB to display the array as an image.
The tetris game flow is represented intuitively by the sub-state
MainArea which represents the current state of the playing field. The game starts by getting a "new shape" (or tetronimo). The piece then keeps moving down (or sideways) depending on user input. When the piece touches something from below (the
isTouching condition), then the piece stops moving. If the piece has stopped too high, then the game is over. Otherwise, we "freeze" the shape, if necessary move to the next level, get a new shape and continue.
Graphically abstracting the game logic at this level provides a good way to hide details at various levels. Each of the sub-states can hide its inner complexity.
For example, the
Moving substate is responsible for moving the tetronimo based on the user input. Once again, we can abstract the game flow by using states. A tetronimo either moves slowly (default) or falls fast (when the user presses the space bar and makes
hasChanged(drop) return true).
Notice the use of
hasChanged to gather user input. Each time the user presses a key, the MATLAB UI increments the input to the chart, which makes
hasChanged return true during that time-step.
Parallel substates of
MoveSlowly capture the fact that we want to process multiple user keystrokes per time step.
Finally, the UI is implemented using a simple MATLAB script which also captures keystrokes from the user.
'j' or LEFT for moving left
'l' or RIGHT for moving right
'i' or UP for rotating clockwise
'k' or DOWN for rotating counter clockwise
SPACE for dropping very fast
'p' for play/pause
'q' for quit
The smaller square axis on the right shows the next tetronimo.