Advertisement
WarPie90

Tetro

May 17th, 2025
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.47 KB | None | 0 0
  1. program new;
  2.  
  3. const
  4.   DEFAULT_START = 7;
  5.   TETRIS_ROWS   = 15;
  6.  
  7. var
  8.   column: string := ' '*20;
  9.   board: array of Char;
  10.   tetroLocation: Int32 = DEFAULT_START;
  11.   thisTetro: TByteArray;
  12.  
  13. const
  14.   tetros:array of TByteArray = [[1,16,31,46], [2,15,16,17], [1,15,16,17], [2,15,16,17], [1,2,16,17], [0,1,16,17], [1,2,15,16]];
  15.  
  16. function ToString(constref x:array of Char): string; override;
  17. var i: Int32;
  18. begin
  19.   for i:=0 to High(x) do
  20.   begin
  21.     if (i mod 15 = 0) and (i > 0) then Result += LINE_SEP;
  22.     Result += x[i];
  23.   end;
  24. end;
  25.  
  26. procedure SetRandomTetro();
  27. begin
  28.   thisTetro := tetros[Random(Length(tetros))];
  29. end;
  30.  
  31. function VerifyFreeSapce(blank:Char=' '): Boolean;
  32. begin
  33.                     {lowest pnt}
  34.   if (tetroLocation+thisTetro[3]) div 15 > TETRIS_ROWS then Exit(False);
  35.  
  36.  
  37.   Result := (board[tetroLocation+thisTetro[0]] = blank) and
  38.             (board[tetroLocation+thisTetro[1]] = blank) and
  39.             (board[tetroLocation+thisTetro[2]] = blank) and
  40.             (board[tetroLocation+thisTetro[3]] = blank);
  41. end;
  42.  
  43. procedure DrawTetro(sym:Char='#');
  44. begin
  45.   board[tetroLocation+thisTetro[0]] := sym;
  46.   board[tetroLocation+thisTetro[1]] := sym;
  47.   board[tetroLocation+thisTetro[2]] := sym;
  48.   board[tetroLocation+thisTetro[3]] := sym;
  49. end;
  50.  
  51. procedure DropTetro();
  52. begin
  53.   Inc(tetroLocation, 15);
  54. end;
  55.  
  56. procedure UndropTetro();
  57. begin
  58.   Dec(tetroLocation, 15);
  59. end;
  60.  
  61. var
  62.   I:int32;
  63.   TetrisFrame: TImage;
  64. begin
  65.   SetLength(board, Length(column)*25);
  66.   for i:=0 to 14 do Move(column[1], board[i*20], 20);
  67.  
  68.  
  69.   TetrisFrame := TImage.Create(500,1000); //this could be a form
  70.   TetrisFrame.FontName := 'Consolas';
  71.   TetrisFrame.Show();
  72.  
  73.   SetRandomTetro();
  74.   while true do
  75.   begin
  76.     TetrisFrame.DrawColor := 0;
  77.     TetrisFrame.DrawText(ToString(board), [50,50]);
  78.  
  79.     DrawTetro(#32);
  80.     DropTetro();
  81.  
  82.     if not VerifyFreeSapce() then
  83.     begin
  84.       UnDropTetro();
  85.       DrawTetro('#');
  86.  
  87.       TetrisFrame.DrawColor := $0077FF;
  88.       TetrisFrame.DrawText(ToString(board), [50,50]);
  89.       TetrisFrame.Show(False);
  90.  
  91.       SetRandomTetro();
  92.       tetroLocation := Random(0,12);// DEFAULT_START;
  93.  
  94.       // still not free!?¤!"¤"!#%"#%
  95.       if not VerifyFreeSapce() then
  96.         TerminateScript('Game over');
  97.     end else
  98.     begin
  99.       DrawTetro('#');
  100.  
  101.       TetrisFrame.DrawColor := $0077FF;
  102.       TetrisFrame.DrawText(ToString(board), [50,50]);
  103.       TetrisFrame.Show(False);
  104.     end;
  105.  
  106.     //Sleep(100);
  107.   end;
  108. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement