TTETRIS
 MARCO BIANCHINI
 bianco@arcanet.it

This Component allow you to easy play tetris in your application.

unit T_TETRIS;

{
                                   \\\\\/////
                                   /  _   _  \
                                  (| (.) (.) |)
 _______________________________.OOOo__( )__oOOO.______________________________
|                                                                              |
|                               This is my Tcomponent                          |
|                                    Ciao Marco                                |
|                              Email bianco@arcanet.it                         |
|                                                                              |
| DATA:      INIZIO:    FINE:      LAVORO:                                     |
|                                                                              |
| 12-07-97,  19:01:38,  21:31:09,  FATTO QUASI TUTTO.                          |
| 13-07-97,  03:39:42,  04:13:11,  ULTIMI DETTAGLI.                            |
| 16-07-97,  14:10:49,  16:05:18,  UN PO' DI OPZIONI NON FANNO MAI MALE...     |
| 30-07-97,  12:20:33,  AGGIUNTO LA FUNZIONE GET_FREE_LINES (10 MINUTI...)     |
| 11-08-97,  10:46:38,  12:08:54,  AGGIUNTO UN PO' DI PEZZI NUOVI.             |
| 25-08-97,  00:12:06,  01:14:23,  AGGIUNTO LA FORM PREVIEW.                   |
|                                                                              |
|_______________________________.oooO__________________________________________|
                                (   )        Oooo.
                                 \ (         (   )
                                  \_)         ) /
                                             (_/

}
interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,winprocs,
  wintypes,extctrls;

CONST
 COLUMNS=9; //DIMENSIONI DEL CAMPO DI GIOCO
 LINES=20;
 COLORI:ARRAY[1..14] OF TCOLOR = (CLMAROON,CLGREEN,CLOLIVE,CLNAVY,
                                CLPURPLE,CLTEAL,CLGRAY,CLSILVER,CLRED,
                                CLLIME,CLYELLOW,CLBLUE,CLFUCHSIA,
                                CLAQUA);
 type
  PEZZO=ARRAY[0..3,0..3] OF INTEGER;
  TTETRIS = class(TGraphicControl)
  private
   { Private declarations }
   P_CAPT:TCAPTION;
   F_PREW:TFORM;   //FORM PREW
   P_SIZE:INTEGER; //DIMENSIONI PREW
   Piece:ARRAY[1..2] OF integer;
   N_TYPE:INTEGER; //NUMERO DI PEZZI
   IMMA:TPICTURE;
   SNP:BOOLEAN;    //VISUALIZZA IL PROSSIMO PEZZO
   SIL:BOOLEAN;    //WISUALIZZA IL CONTORNO DEL PEZZO
   SFC:BOOLEAN;    //VISUALIZZA IL COLORE DI RIEMPIMENTO
   P_COUNT,        //CONTATORE PEZZI
   SIZEX,SIZEY,
   SCR_N,          //TIPO SCHERMO
   DELETED_L,      //ULTIME RIGHE CANCELLATE. <0 PER NUOVO PEZZ0
   CX,CY:INTEGER;  //POSIZIONE DEL PEZZO
   Vet:array[0..COLUMNS,0..LINES] of TCOLOR;
   PEZ:PEZZO;
   NULL_COLOR:TCOLOR;
   FUNCTION  CAN_MOVE(CONST DOVE,X,Y:INTEGER):BOOLEAN;
   FUNCTION  POSI(KX,KY,JX,JY:INTEGER):BOOLEAN;
   FUNCTION  FULL_LINES(DA:INTEGER):INTEGER;
   PROCEDURE P(X,Y:INTEGER;C:TCOLOR);
   procedure MakeObj(const N:integer);
   PROCEDURE SET_P_SIZE(S:INTEGER);
   PROCEDURE DISEGNA_PEZZO(CONST X,Y:INTEGER;CONST VEDI:BOOLEAN);
   PROCEDURE SALVA_PEZZO(KX,KY:INTEGER);
   PROCEDURE RUOTA_PEZZO(CONST N,X,Y:INTEGER;VAR PEZZ:PEZZO);
   PROCEDURE SET_NEXT_PIECE(VALUE:INTEGER);
   PROCEDURE SET_BK_COLOR(VALUE:TCOLOR);
   PROCEDURE SHOW_NEXT;
   PROCEDURE NOT_PIECE_COUNT(VALUE:INTEGER);
   PROCEDURE NOT_DELETED_COUNT(VALUE:INTEGER);
   PROCEDURE SET_SIL(VALUE:BOOLEAN);
   PROCEDURE SET_SFC(VALUE:BOOLEAN);
   PROCEDURE SET_SCREEN(VALUE:INTEGER);
   PROCEDURE MAKE_SCREEN;
   PROCEDURE QUADRATO(X,Y:INTEGER);
   PROCEDURE SET_SNP(VALUE:BOOLEAN);
   PROCEDURE SET_N_TYPE(VALUE:INTEGER);
   PROCEDURE SET_CAPTION(VALUE:TCAPTION);
  protected
    { Protected declarations }
    procedure Paint; override;
  public
    { Public declarations }
   PROCEDURE NEW_GAME; // INIZIALIZZA IL GIOCO
   //RUNTIME PER MUOVERE IL PEZZO
   FUNCTION MOVE_DOWN:INTEGER;
   FUNCTION SEND_BOTTOM:INTEGER;
   FUNCTION MOVE_LEFT:BOOLEAN;
   FUNCTION MOVE_RIGHT:BOOLEAN;
   FUNCTION GET_FREE_LINES(SEE:BOOLEAN):INTEGER;
   PROCEDURE ROTATE_LEFT;
   PROCEDURE ROTATE_RIGHT;
   //RUNTIME PER MODIFICARE LE IMPOSTAZIONI DELLO SCHERMO
   PROCEDURE SET_PIECE(X,Y:INTEGER;C:TCOLOR);
   PROCEDURE INSERT_LINE(X:INTEGER;C:TCOLOR);
   PROCEDURE MOVE_PREVIEW(X,Y:INTEGER);
  published
    { Published declarations }
   Constructor Create(AOwner:TComponent); OVERRIDE;
   PROPERTY NumberOfPieces:INTEGER READ P_COUNT WRITE NOT_PIECE_COUNT;
   PROPERTY NextPiece:INTEGER READ PIECE[2] WRITE SET_NEXT_PIECE;
   PROPERTY LastDeletedLine:INTEGER READ DELETED_L WRITE NOT_DELETED_COUNT;
   PROPERTY BackGroundColor:TCOLOR READ NULL_COLOR WRITE SET_BK_COLOR;
   PROPERTY ScreenNumber:INTEGER READ SCR_N WRITE SET_SCREEN;
   PROPERTY ShowBorderColor:BOOLEAN READ SIL WRITE SET_SIL;
   PROPERTY ShowFillColor:BOOLEAN READ SFC WRITE SET_SFC;
   PROPERTY ShowNextpiece:BOOLEAN READ SNP WRITE SET_SNP;
   PROPERTY NumberOfType:INTEGER READ N_TYPE WRITE SET_N_TYPE;
   PROPERTY PreviewSize:INTEGER READ P_SIZE WRITE SET_P_SIZE;
   PROPERTY PreviewCaption:TCAPTION READ P_CAPT WRITE SET_CAPTION;
  end;

procedure Register;

implementation
  
 ....
  
New Property:

 BackGroundColor:TColor
  Get/Set the background color for the component and the form preview.
 
 LastDeletedLine:Integer
  Run time, read-only, 
  This property show the number of full lines deleted.
  It will be updated when the current piece stop.
    
 NextPiece:Integer
  Set/get the next piece.   
  This value must be betveen 1 and 15.
 
 NumberOfPieces:Integer
  RunTime Read-Only.
  This property return the current number of piece-1 you are moving.
 
 NumberOfType:Integer 
  Get/Set how many kind of piece will be used.
  This value must be betveen 8 and 15.
 
 PreviewCaption:TCaption
  Get/Set the caption of the preview form.

 PreviewSize:Integer
  Get/Set the size of square for the preview form.
  In the preview form there is 16 square (4x4).
  Changing this property TTetris will update ClientWindth and 
  ClientHeight of the preview form.

 ScreenNumber:Integer
  Get/Set the screen number
  This value must be betveen 0 and 5.

 ShowBorderColor:Boolean
  If True 
   Canvas.Pen.Color=Default piece color
  If False
   Canvas.Pen.Color:=BackgroundColor 
 
 ShowFillColor:Boolean
  If True 
   Canvas.Brush.Color=Default piece color
  If False
   Canvas.Brush.Color:=BackgroundColor 

 ShowNextPiece:Boolean
  Show/Hide [True/False] the preview form




Methods:

 Procedure New_Game; 
  You must call this methods to start a new game.
  This procedure initialize all variables.
  Example:
   Tetris1.New_Game.

 Procedure Rotate_Left;
  Call this methods to rotate the current piece.
  Example:
   Tetris1.Rotate_Left;
  Effects:
   Before:
    00
   00
   After
    0
    00
     0 

 Procedure Rotate_Right;
  Call this methods to rotate the current piece.
  Example:
   Tetris1.Rotate_Right;
  Effects:
   Before:
    00
   00
   After
     0
    00
    0 
   It is the same to call for 3 times Rotate_Left method.

  Function Move_Down:Integer;
   Call this method do move down the piece.
   It return 0 if the piece can move:
    before           After
     |          |      |          |
     |          |      |          |
     |  00      |      |          |
     |  00      |      |  00      |
     |*         |      |* 00      |
     |*         |      |*         |
     |*  **     |      |*  **     |
     |*___**____|      |*___**____|
 
   It return 1 if the piece can not move:
    before           After          After
     |          |      |          |  |   0000 <-New piece
     |          |      |          |  |          |
     |          |      |          |  |          |
     |          |      |          |  |          |
     |  00      |      |  00      |  |  **      |
     |  00      |      |  00      |  |  **      |
     |***       |      |***       |  |***       |
     |  ** ***  |      |  ** ***  |  |  ** ***  |
     |***** *** |      |***** *** |  |***** *** |
     |***_***_**|      |***_***_**|  |***_***_**|
    In this case will be automatically saved the position of
    the piece, updated the NumberOfPieces property, deleted
    the full lines, updated LastDeletedLines property. Then
    the next piece become the current piece and CPU choose the
    next piece (You can set here the NextPiece property...),
    and the preview form will be updated.
  It return 2 if the piece can not move and the game can not continue:
    before           After          After
     |  00      |      |  00      |  |  ** <-I can't insert a new piece
     |  00      |      |  00      |  |  **      |
     |   *      |      |   *      |  |   *      |
     |   *      |      |   *      |  |   *      |
     |****      |      |****      |  |****      |
     |  ** ***  |      |  ** ***  |  |  ** ***  |
     |***** *** |      |***** *** |  |***** *** |
     |***_***_**|      |***_***_**|  |***_***_**|
    In this case will be automatically saved the position of
    the piece, updated the NumberOfPieces property, deleted
    the full lines, updated LastDeletedLines property. 

  Function Send_Bottom:Integer;
   Call this method to send to the bottom of the screen the current
   piece. Is like to call x times Move_Down method and it return x.
   Example:  
    before           After
     |          |      |          |
     |  00      |      |          |
     |  00      |      |          |
    1|          |      |          |
    2|          |      |          |
    3|          |      |          |
    4|          |      |          |
    5|          |      |          |
    6|*         |      |* 00      |
    7|*         |      |* 00      |
     |*  **     |      |*  **     |
     |*___**____|      |*___**____|
   In this example the function return 7
     
  Function Move_Left/Move_Right:Boolean;
   They returns True if you can move left or right the piece
   Example:
    before           After
     |          |      |          |
     |          |      |          |
     |  00      |      | 00       |
     |  00*     |      | 00 *     |
     |*   *     |      |*   *     |
     |*   *     |      |*   *     |
     |*  **     |      |*  **     |
     |*___**____|      |*___**____|
    In this example:
     Move_Left=TRUE
     Move_Right=FALSE

  Function Get_Free_Lines(See:Boolean):Integer;
   This function returns the number for free lines
   starting from the top. If See = true, the function draw a
   white rectangle for any free line.
   Example:  
    See=False         See=True
    1|          |      |1111111111|
    2|          |      |1111111111|
    3|          |      |1111111111|
    4|          |      |1111111111|
     |   *      |      |   *      |
     |   *      |      |   *      |
     |   *      |      |   *      |
     |  **      |      |  **      |
     |_**_______|      |_**_______|
   In this example the function return 4
   
  Procedure Set_Piece(X,Y:Integer,C:TColor);
   Call this method to set the [X,Y] square's value = C
   If y=-1 this function find the last free square starting from
   the top.
   Example:
   X=3  
    Y=5               Y=-1
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |   0      |      |          |
     |          |      |          |
     |          |      |          |
     |*         |      |*         |
     |*         |      |*  0      |
     |*  **     |      |*  **     |
     |*___**____|      |*___**____|
  
 
  Procedure Insert_Line(X:Integer;C:TColor);
   Call this method to insert a not-full-line on the bottom of the
   screen. The X square is empty, the others are C.
   Example:
   X=3;  
    Before            After
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |          |      |          |
     |          |      |*         |
     |*         |      |*         |
     |*         |      |*  **     |
     |*  **     |      |*   **    |
     |*___**____|      |000_000000|
 
  Procedure Move_Preview(X,Y:Integer);
   Call this method to move the previev form to the x,y screen's
   coordinates...

  HHAAAHHUU....
  It's late...
  I must sleep...
  I hope this file help you, if not, look the demo
  or Mail me any question (bianco@arcanet.it) about it.

  History:
  
   1993:Pascal 3.0 (TextMode,Nice)
   1995:Pascal 7.0 (GraphMode 640x480 256 Colours, Better but much slow.
   1996:Visual Basic 4.0 (Bleaahh...)
   1997: ____    ___   _      ____    _   _   _
        |  _ \  |  _| | |    |  _ \  | | | | | |
        | | . | | |_  | |    | |_| | | |_| | | |
        | | | | |  _| | |    |  __/  |  _  | | |          
        | |_. | | |_  | |__  | |     | | | | | |                      
        |____/  |___| |____| |_|     |_| |_| |_|  2.0
                                     
   199x:Tetris 3D (I Hope).

 Ciao,
  Marco Bianchini
  Via Tassi 6 San Felice S/P (MO) ITALY
  41038
  Tel. +39 535 82534
       +39 535 96300
       +39  51 983142                                               
                                                     
                                                   
                                                    
                                                    
                                                    
                                                    


                                                    



 
  



                                                    

