From anonymous, 6 Months ago, written in Perl.
This paste is a reply to tetris.pl from anonymous
- view diff
Embed
  1. use Term::ReadKey;
  2. ReadMode 5;
  3. $| = 1;
  4. print "\ec\e[2J\e[?25l\e[?7l\e[1;1H\e[0;0r";
  5. @FLAG = split //, '####H#V#2#0#{#h#t#t#p#s#:#/#/#w#w#w#.#y#o#u#t#u#b#e#.#c#o#m#/#w#a#t#c#h#?#v#=#d#Q#w#4#w#9#W#g#X#c#Q#}####';
  6. @SHAPES = ( 89, 51, 30, 27, 75, 294 );
  7. $width  = 11;
  8. $height  = 23;
  9. print(  "\e[1;1H\e[103m"
  10.       . ( ' ' x ( 2 * $width + 2 ) )
  11.       . "\e[0m\r\n"
  12.       . (
  13.         ( "\e[103m \e[0m" . ( ' ' x ( 2 * $width ) ) . "\e[103m \e[0m\r\n" ) x $height )
  14.       . "\e[103m"
  15.       . ( ' ' x ( 2 * $width + 2 ) )
  16.       . "\e[2;1H\e[0m" );
  17.  
  18. sub print_block {
  19.     ( $shape, $text, $color, $x, $y ) = @_;
  20.     for $yy ( 0 .. 2 ) {
  21.         for $xx ( 0 .. 5 ) {
  22.             print(  "\e[${color}m\e["
  23.                   . ( $yy + $y + 2 ) . ";"
  24.                   . ( $xx + $x * 2 + 2 )
  25.                   . "H${text}" )
  26.               if ( ( ( $shape & ( 0b111 << ( $yy * 3 ) ) ) >> ( $yy * 3 ) ) &
  27.                 ( 4 >> ( $xx >> 1 ) ) );
  28.         }
  29.     }
  30. }
  31.  
  32. sub blockbits {
  33.     $_ = shift;
  34.     ( $_ & 4 ) << 6 | ( $_ & 32 ) << 2 | ( $_ & 256 ) >> 2 | ( $_ & 2 ) << 4 |
  35.       ( $_ & 16 ) | ( $_ & 128 ) >> 4 | ( $_ & 1 ) << 2 | ( $_ & 8 ) >> 2 |
  36.       ( $_ & 64 ) >> 6;
  37. }
  38.  
  39. sub stack_block {
  40.     ( $b, $bc, $x, $y ) = @_;
  41.     for $yy ( 0 .. 2 ) {
  42.         for $xx ( 0 .. 5 ) {
  43.             substr( $f[ $yy + $y ], ( $xx + $x ), 1 ) = $bc
  44.               if (
  45.                 (
  46.                     ( ( $b & ( 0b111 << ( $yy * 3 ) ) ) >> ( $yy * 3 ) ) &
  47.                     ( 4 >> $xx )
  48.                 )
  49.               );
  50.         }
  51.     }
  52.     $Q = 'QcXgWw9d4';
  53.     @f = grep { / / } @f;
  54.     unshift @f, ( " " x $width ) while ( @f < $height );
  55.     solidify();
  56. }
  57.  
  58. sub is_collision {
  59.     $_Q = 'ljhc0hsA5';
  60.     ( $b, $x, $y ) = @_;
  61.     for $yy ( 0 .. 2 ) {
  62.         for $xx ( 0 .. 2 ) {
  63.             return 1
  64.               if (
  65.                 (
  66.                     ( ( $b & ( 0b111 << ( $yy * 3 ) ) ) >> ( $yy * 3 ) ) &
  67.                     ( 4 >> $xx )
  68.                 )
  69.                 && (   ( $yy + $y >= $height )
  70.                     || ( $xx + $x < 0 )
  71.                     || ( $xx + $x >= $width )
  72.                     || ( substr( $f[ $yy + $y ], ( $xx + $x ), 1 ) ne ' ' ) )
  73.               );
  74.         }
  75.     }
  76. }
  77.  
  78. sub solidify {
  79.     for $yy ( 0 .. $#f ) {
  80.         ;print( "\e[" . ( $yy + 2 ) . ";2H\e[0m" );
  81.         $_ = $f[$yy];
  82.         s/./$&$&/gg;
  83.         print;
  84.     }
  85. };
  86. sub readkey { $k = ''; $k .= $c while ( $c = ReadKey(-1) ); $k; };
  87.  
  88. sub load_block {
  89.     $bx = 5;
  90.     $by = 0;
  91.     $shapeindex = int( rand( scalar @SHAPES ) );
  92.     $shape = $SHAPES[$shapeindex];
  93.     $shapetext = $FLAG[$flagindex];
  94.     $flagindex > 77 && $flagindex < 98 && $flagindex != 82 && eval( '$shapetext' . "=~y#$Q#$_Q#" )
  95.       || $flagindex == 98 && $shapetext =~ s/./0/;
  96.     $flagindex++;
  97. }
  98. @f = ( " " x $width ) x $height;
  99. solidify();
  100. load_block();
  101. while (1) {
  102.     $key = readkey();
  103.     last if ( $key =~ /q/ );
  104.     $key = substr( $key, 2, 1 );
  105.     $dx = ( $key eq 'C' ) - ( $key eq 'D' );
  106.     $bx += $dx unless ( is_collision( $shape, $bx + $dx, $by ) );
  107.     if ( $key eq 'A' ) {
  108.         unless ( is_collision( blockbits($shape), $bx, $by ) )      { $bits = blockbits($bits) }
  109.         elsif  ( !is_collision( blockbits($shape), $bx + 1, $by ) ) { $bits = blockbits($bits); $bx++ }
  110.         elsif  ( !is_collision( blockbits($shape), $bx - 1, $by ) ) { $bits = blockbits($bits); $bx-- }
  111.     }
  112.     print_block( $shape, $shapetext, 101 + $shapeindex, $bx, $by );
  113.     select( undef, undef, undef, 0.05 );
  114.     if ( is_collision( $shape, $bx, ++$by ) ) {
  115.         last if ( $by < 2 );
  116.         stack_block( $shape, $shapetext, $bx, $by - 1 );
  117.         load_block();
  118.     }
  119.     else { print_block( $shape, " ", 0, $bx, $by - 1 ); }
  120. }
  121. sleep(0.5);
  122. ReadMode 0;
  123. print "\ec";
  124.