Tetris in BGI in Turbo C (Beta Demo)

Introduction

The Source code that follows is for Tetris using BGI under Turbo C/C++ DOS compiler. It is still in the preliminary stages of development and there is a long way to go.

Now, this is a demo version where the user isnt supposed to 'play' the game but be a mute spectator - it just demonstrates the Graphics User Interface and to some extent logic in the program.

Now, I hav achieved vertical motion logic - i.e. till what point can a Tetris Block proceed and when it should stop. While there is horizontal motion in the program, it is uncontrolled - so far, I hav been unable to find a logic that controls horizontal motion of blocks and also fits with the way I hav coded.

I hav provided adequate comments throughout the code, so u guys will hav a fair idea.

PS : Once the Tetris Blocks begin falling, it may seem like its taking too long. There are in all 30 blocks which will fall as part of the demo .... so be patient .... otherwise simply reduce the value of the "NUM_OF_BLCKS" macro

The Code ...

Source Code:

Code: C

/* Tetris in C++ using BGI ==> DEMO Version 1.0 */

// Execute this in a TC/C++ DOS Compiler// THIS IS JUST A DEMO VERSION & IS AS SUCH INCOMPLETE

/* Looping as many times as user desires .Each time, one Tetris-Block is produced randomly at the top of Main Window, slides in Free-fall and settles at floor of the Tetris Window. In the meanwhile, next piece is produced randomly(displayed in "Next Piece Window"), which is used as the Falling Tetris-Block in next iteration. Please note, here: 'T' - the invoking object from main() is object for Tetris Main Window 'Temp' - Is the Object for "Next Piece Window" */for(r=0;r<NUM_OF_BLCKS;r++){if(r==0)// Very 1st Looping{// Hence, Generating shape to be used currently/initially by using Next_Piece Object Temp.gen_randshape();

// Iterating for Current Piece for Free-Fall Motion to floor of Tetris Window// Here:// 1. 'j' will control the FREE-FALLING Motion of Tetris Block// 2. 'channel' will control Horizontal/Sideways Motion of Tetris Blockfor(j=y;(j<HYCh-2&&boxtype==23)||(j<HYCh-1&&boxtype==32) ||(j<HYCh-3&&boxtype==14)||(j<HYCh-1&&boxtype==22) ||(j<HYCh&&boxtype==41);j++){ prevchan=channel;

if(channel==0)// We have reached the LEFTMOST boundary// Hence, we may either let 'channel' be or Increment it// We CANNOT and MUST NOT Decrement 'channel'{switch(random(2)){case0:break;case1:channel++;}}

elseif((channel==VXCh-2&&boxtype==23) || (channel==VXCh-3&&boxtype==32) ||(channel==VXCh-1&&boxtype==14) || (channel==VXCh-4&&boxtype==41) ||(channel==VXCh-2&&boxtype==22))// We have reached the RIGHTMOST boundary// Hence, we may either let 'channel' be or Decrement it// We CANNOT and MUST NOT Increment 'channel'{switch(random(2)){case0:break;case1:channel--;}}else// We are somewhere in the middle// Hence, we may move leftwards, rightwards or stay as it is{switch(random(3)){case0:channel++;break;case1:channel--;break;case2:break;}}

printf("\n\n\aWelcome to Tetris 2.0C (BETA) DEMO!!\n\n\a");printf("\n\n\aThis Demo is a glimpse into a preliminary Version and just shows the Movement of Various Tetris Pieces along the Tetris Play Space\a\n\n");printf("\n\n\aUser I/P is NOT Expected\a\n\n");printf("\n\n\n\aPress ENTER to goto Graphics Mode and See the DEMO\a\n\n\n"); getch();clrscr(); T.regulate_play(); getch();}