Chapter 20
GameLogic

One class remains, but it is the most important. All parts of the game are ready, but you’ll have to bring them together to construct the actual game. This is the purpose of the class gameLogic. Its framework looks like this:

 
class gameLogic 
{ 
private
   // blocks in the game 
5   block currentBlock; 
   block nextBlock   ; 
 
   float forceDownCounter = 0         ; 
   float slideCounter     = SLIDE_TIME; 
10 
   // to move a block completely down 
   bool  toBottom      = false
   float toBottomTimer = 0.05 ; 
 
15   bool canRotate            (C block & b               ) C {} 
   bool canMove              (C block & b, DIRECTION dir) C {} 
   void handleBottomCollision()   {} 
   void changeFocusBlock     ()   {} 
   void checkLoss            () C {} 
20   void handleInput          ()   {} 
 
public
   void create() 
   void update()   {} 
25   void draw  () C {} 
} 
gameLogic GameLogic;  

Let’s look at the variables first:

current block
This is the block you move during the game.
nextBlock
This the next block, which is available on the right hand side.
forceDownCounter
When we don’t move the block down ourselves, it should go down on its With this timer, we arrange how long that takes.
slideCounter
Whenever a block hits the pile, tetris allows you to slide it sideways for a little This means we need a timer for that.
toBottom
When the space bar is pressed, the block should move all the way But you have to see it move, so you can’t just change its position in one go. With this boolean, we can remember if the block should drop down.
toBottomTimer
And that movement also needs a timer for each step.