Advertisement
WarPie90

Base for Tetris Elektronika 60

May 17th, 2025
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.51 KB | None | 0 0
  1. program new;
  2. (*
  3.   only thing implemented is `z` to rotate
  4. *)
  5.  
  6.  
  7. const
  8.   DEFAULT_START = 5;
  9.   TETRIS_ROWS   = 15;
  10.  
  11. var
  12.   board: array of Char;
  13.   tetroLocation: Int32 = DEFAULT_START;
  14.   thisTetro: Int32 = 0;
  15.   thisRotation: Int32 = 0;
  16.  
  17.  
  18. const
  19.   ui_row_width   = 43;
  20.   ui_board_start = 19;
  21.  
  22.   starting_ui = "
  23. FULL LINES: 1    <! . . . . . . . . . .!>
  24. LEVEL: 1         <! . . . . . . . . . .!>
  25. SCORE: 1         <! . . . . . . . . . .!>
  26. TIME: 00:00      <! . . . . . . . . . .!>
  27.                  <! . . . . . . . . . .!>
  28.                  <! . . . . . . . . . .!>
  29.          XXYYZZ  <! . . . . . . . . . .!>
  30.            LL    <! . . . . . . . . . .!>
  31.            PP    <! . . . . . . . . . .!>
  32.            TT    <! . . . . . . . . . .!>
  33.                  <! . . . . . . . . . .!>
  34.                  <! . . . . . . . . . .!>
  35.                  <! . . . . . . . . . .!>
  36.                  <! . . . . . . . . . .!>
  37.                  <! . . . . . . . . . .!>
  38.                  <! . . . . . . . . . .!>
  39.                  <! . . . . . . . . . .!>
  40.                  <! . . . . . . . . . .!>
  41.                  <! . . . . . . . . . .!>
  42.                  <! . . . . . . . . . .!>
  43.                  <!********************!>
  44.                   \/\/\/\/\/\/\/\/\/\/\/
  45.  
  46.  
  47.                          PADDING
  48. 123456789
  49.  
  50. ";
  51.  
  52.  
  53. const
  54.   tetros: array of array of TByteArray = [
  55.     {|}
  56.     [[ 1, 11, 21, 31], [ 0,  1,  2,  3], [ 1, 11, 21, 31], [ 0,  1,  2,  3]],
  57.  
  58.     {L shapes}
  59.     [[ 2, 10, 11, 12], [ 1, 11, 21, 22], [20, 10, 11, 12], [ 0, 01, 11, 21]],
  60.     [[ 0, 10, 11, 12], [ 2,  1, 11, 21], [10, 11, 12, 22], [30, 11, 21, 31]],
  61.  
  62.     {T}
  63.     [[ 1, 10, 11, 12], [ 1, 12, 11, 21], [ 11, 0, 1, 2], [ 10, 1, 11, 21]],
  64.  
  65.     {cube}
  66.     [[ 1,  2, 11, 12], [ 1,  2, 11, 12], [ 1,  2, 11, 12], [ 1,  2, 11, 12]],
  67.  
  68.     {z}
  69.     [[ 0,  1, 11, 12], [11, 21, 20, 30], [ 0,  1, 11, 12], [11, 21, 20, 30]],
  70.     [[ 1,  2, 10, 11], [10, 20, 21, 31], [ 1,  2, 10, 11], [10, 20, 21, 31]]
  71.   ];
  72.  
  73. function ToString(constref x:array of Char): string; override;
  74. var i: Int32;
  75. begin
  76.   for i:=0 to High(x) do
  77.   begin
  78.     if (i mod ui_row_width = 0) and (i > 0) then Result += LINE_SEP;
  79.     Result += x[i];
  80.   end;
  81. end;
  82.  
  83. procedure SetRandomTetro();
  84. begin
  85.   thisTetro := Random(Length(tetros));
  86. end;
  87.  
  88. procedure RotateTetro();
  89. begin
  90.   thisRotation := (thisRotation + 1) mod 4;
  91. end;
  92.  
  93. function TBL(idx: Int32): Int32;
  94. var x,y: Byte;
  95. begin
  96.   x := idx mod 10;
  97.   y := idx div 10;
  98.   Result := y*ui_row_width+(x*2 + ui_board_start);
  99. end;
  100.  
  101. function TBO(idx: Int32): Int32;
  102. var x,y: Byte;
  103. begin
  104.   x := idx mod 10;
  105.   y := idx div 10;
  106.   Result := y*ui_row_width+(x*2);
  107. end;
  108.  
  109. function VerifyFreeSapce(): Boolean;
  110. var lc := TBL(tetroLocation);
  111. var curr := tetros[thisTetro][thisRotation];
  112. begin
  113.   Result := (board[lc+TBO(curr[0])] in [' ', '.']) and
  114.             (board[lc+TBO(curr[1])] in [' ', '.']) and
  115.             (board[lc+TBO(curr[2])] in [' ', '.']) and
  116.             (board[lc+TBO(curr[3])] in [' ', '.']);
  117. end;
  118.  
  119. procedure DrawTetroBox();
  120. var lc := TBL(tetroLocation);
  121. var curr := tetros[thisTetro][thisRotation];
  122. begin
  123.   board[lc+TBO(curr[0])+0] := '[';
  124.   board[lc+TBO(curr[0])+1] := ']';
  125.   board[lc+TBO(curr[1])+0] := '[';
  126.   board[lc+TBO(curr[1])+1] := ']';
  127.   board[lc+TBO(curr[2])+0] := '[';
  128.   board[lc+TBO(curr[2])+1] := ']';
  129.   board[lc+TBO(curr[3])+0] := '[';
  130.   board[lc+TBO(curr[3])+1] := ']';
  131. end;
  132.  
  133. procedure ClearTetroBox();
  134. var lc := TBL(tetroLocation);
  135. var curr := tetros[thisTetro][thisRotation];
  136. begin
  137.   board[lc+TBO(curr[0])+0] := ' ';
  138.   board[lc+TBO(curr[0])+1] := '.';
  139.   board[lc+TBO(curr[1])+0] := ' ';
  140.   board[lc+TBO(curr[1])+1] := '.';
  141.   board[lc+TBO(curr[2])+0] := ' ';
  142.   board[lc+TBO(curr[2])+1] := '.';
  143.   board[lc+TBO(curr[3])+0] := ' ';
  144.   board[lc+TBO(curr[3])+1] := '.';
  145. end;
  146.  
  147. procedure DropTetro();
  148. begin
  149.   Inc(tetroLocation, 10);
  150. end;
  151.  
  152. procedure UndropTetro();
  153. begin
  154.   Dec(tetroLocation, 10);
  155. end;
  156.  
  157. var
  158.   i:int32;
  159.   TetrisFrame: TImage;
  160.   ticker: TCountDown;
  161. begin
  162.   SetLength(board, Length(starting_ui));
  163.   Move(starting_ui[1], board[0], Length(starting_ui));
  164.  
  165.  
  166.   TetrisFrame := TImage.Create(500,1000); //this could be a form
  167.   TetrisFrame.FontName := 'Consolas';
  168.   tetrisFrame.FontSize := 15;
  169.   TetrisFrame.FontAntialiasing := False; {!!!}
  170.   TetrisFrame.FontBold := False;
  171.  
  172.   TetrisFrame.Show();
  173.  
  174.   SetRandomTetro();
  175.  
  176.   while true do
  177.   begin
  178.     TetrisFrame.DrawColor := 0;
  179.     TetrisFrame.DrawText(ToString(board), [50,50]);
  180.  
  181.     ClearTetroBox();
  182.     DropTetro();
  183.  
  184.     if not VerifyFreeSapce() then
  185.     begin
  186.       UnDropTetro();
  187.       DrawTetroBox();
  188.  
  189.       TetrisFrame.DrawColor := $0077FF;
  190.       TetrisFrame.DrawText(ToString(board), [50,50]);
  191.       TetrisFrame.Show(False);
  192.  
  193.       SetRandomTetro();
  194.       tetroLocation := Random(2,8);// DEFAULT_START;
  195.       thisRotation  := 0;
  196.  
  197.       // still not free!?¤!"¤"!#%"#%
  198.       //if not VerifyFreeSapce() then
  199.       //  TerminateScript('Game over');
  200.     end else
  201.     begin
  202.       DrawTetroBox();
  203.  
  204.       TetrisFrame.DrawColor := $0077FF;
  205.       TetrisFrame.DrawText(ToString(board), [50,50]);
  206.       TetrisFrame.Show(False);
  207.     end;
  208.  
  209.  
  210.     ticker.Start(300);
  211.     while not ticker.IsFinished do
  212.     begin
  213.       if Target.KeyPressed(EKeyCode.Z) then
  214.       begin
  215.          ClearTetroBox();
  216.          TetrisFrame.DrawColor := $0077FF;
  217.          TetrisFrame.Clear();
  218.  
  219.          RotateTetro();
  220.          DrawTetroBox();
  221.          while Target.KeyPressed(EKeyCode.Z) and (not ticker.IsFinished) do sleep(1);
  222.       end;
  223.  
  224.     end;
  225.   end;
  226. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement