Advertisement
Hadlock

powershell tetris - broken

Oct 26th, 2014
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Filename “tetrisInPowershell.ps1″
  3. # Example for rendering screen.
  4. #
  5. # Created by:    Magic Mao
  6. # Created date:  2011–07–14
  7. #
  8.  
  9. # http://magicmao.wordpress.com/2011/08/05/tetris-in-powershell/#comment-61
  10.  
  11.  
  12.  
  13. $speaker = New–Object –ComObject “SAPI.SpVoice”;
  14. $voices = $speaker.GetVoices();
  15. $winPosition = $host.ui.rawui.WindowPosition;
  16. $winSize = $host.ui.rawui.WindowSize;
  17.  
  18. $blockX = 10; $blockY = 10; $blockW = 16;$blockH = 20;
  19. $nextBlockX = $blockX + $blockW + 10; $nextBlockY = $blockY;
  20. $framework = New–Object Management.Automation.Host.Rectangle;
  21. $blockFrame =  New–Object Management.Automation.Host.Rectangle;
  22. $point = New–Object Management.Automation.Host.Coordinates;
  23. $rect = New–Object Management.Automation.Host.Rectangle;
  24.  
  25. $fgc = $Host.UI.RawUI.ForegroundColor;
  26. $bgc = $Host.UI.RawUI.BackgroundColor;
  27.  
  28. $border = $null; # handle for border buffer
  29. $blocks = @(); # seven basic blocks;
  30. [System.ConsoleColor[]]$blockColors = @(
  31. [System.ConsoleColor]::Gray,       #The color gray.
  32. #[System.ConsoleColor]::Blue,        #The color blue.
  33. [System.ConsoleColor]::Green,       #The color green.
  34. [System.ConsoleColor]::Cyan,       #The color cyan (blue–green).
  35. [System.ConsoleColor]::Red,        #The color red.
  36. [System.ConsoleColor]::Magenta,    #The color magenta (purplish–red).
  37. [System.ConsoleColor]::Yellow,      #The color yellow.
  38. [System.ConsoleColor]::White        #The color white.
  39. ); # handle of block colors
  40. $blockChar =#”;
  41. $nextBlock = $null; # handle for next block;
  42. [Int32]$nextBlockRotation = 0; # rotation of the next block;
  43. $nextBlockBuffer = $null; # handle for next block buffer;
  44. [Int32]$nextBlockColor =1; # next block color index
  45. $currentBlock = $null; #handle for current block;
  46. [Int32]$currentBlockRotation = 0; # rotation of the current block;
  47.  
  48. [Int32]$currentBlockX = 0; [Int32]$currentBlockY = 0; # location of current block
  49. [Int32]$currentBlockColor =1; # current block color index
  50. $turndelay = 400;
  51. $currentRunFrameBuffer = $null; # handle for current run block
  52. [Int32]$totalScore = 0;
  53.  
  54. function main(){
  55.   Introduction;
  56.   Start–Sleep –s 5;
  57.   InitEnvironment;
  58.   # Backup UI
  59.   $script:uiBackup = $Host.UI.RawUI.GetBufferContents($framework);
  60.   StartGame;
  61.   # Restore UI
  62.   Draw 0 0 $uiBackup;
  63. }
  64.  
  65. #
  66. # Brief introduction
  67. #
  68. function Introduction(){
  69.   Speak 02 “What should we do next?”;
  70.   Speak 1 0 “How about PC games?”;
  71.   Speak 0 2 “Yeah! i love it!”;
  72. }
  73. function Speak([Int32]$index, [Int32]$rate, [String]$words){
  74.   $speaker.Rate = $rate;
  75.   $speaker.Voice = $voices.Item($index);
  76.   $color = switch($index){0 {“Red”} default {“Yellow”}};
  77.   $speaker.Speak($words, 1) | Out–Null;
  78.   Write–Host $words –ForegroundColor $color
  79.   $speaker.WaitUntilDone(60000) | Out–Null;
  80. }
  81.  
  82. #
  83. # Draw the formated buffer start from the [x,y]
  84. #
  85. function Draw($x, $y, $buffer){
  86.   $point.x = $x; $point.y = $y;
  87.   $Host.UI.RawUI.SetBufferContents($point, $buffer);
  88. }
  89. function DrawText($x, $y, $message){
  90.   $point.x = $x; $point.y = $y;
  91.   $Host.UI.RawUI.CursorPosition = $point;
  92.   Write–Host $message;
  93. }
  94.  
  95. function SelectNewBlock(){
  96.   $script:currentBlock = $nextBlock;
  97.   $script:currentBlockRotation = $nextBlockRotation;
  98.   $script:currentBlockColor = $nextBlockColor;
  99.   $index = random(7);
  100.   $script:nextBlock = $blocks[$index];
  101.   $script:nextBlockRotation = random(4);
  102.   $script:nextBlockColor = random($blockColors.Length);
  103.   $nextBlockBuffer = $Host.UI.RawUI.NewBufferCellArray(@(”    “,”    “,”    “,”    “),[System.ConsoleColor]‘Green’,$bgc);
  104.   0..3 | %{$char = $nextBlock[$nextBlockRotation][$_];$nextBlockBuffer[$char[1], $char[0]] = $Host.UI.RawUI.NewBufferCellArray(@($blockChar),$blockColors[$nextBlockColor],$bgc)[0,0];}
  105.   $script:nextBlockBuffer = $nextBlockBuffer;
  106.  
  107.   $script:currentBlockX = $blockX + $blockW/22; $script:currentBlockY = $blockY
  108.  
  109.   $script:currentRunFrameBuffer = $Host.UI.RawUI.GetBufferContents($blockFrame);
  110.   DrawStaticInfo;
  111. }
  112.  
  113. #
  114. # Prepare invironment variables
  115. #
  116. function InitEnvironment(){
  117.   cls;
  118.   $script:totalScore = 0;
  119.   # init framework
  120.   $framework.Left = $framework.Top = 0;
  121.   $framework.Right =  $framework.Left + $winSize.Width;
  122.   $framework.Bottom = $framework.Top + $winSize.Height;
  123.   # build border
  124.   [String[]]$border = @();
  125.   $line1 =|”; $line2 = “\”;
  126.   1..$blockW | %{ $line1 += ” “; $line2 +==”; }
  127.   $line1 +=|”; $line2 +=/”;
  128.   1..$blockH | %{ $border += $line1; }
  129.   $border += $line2;
  130.   $script:border = $Host.UI.RawUI.NewBufferCellArray($border,[system.consolecolor]‘Green’,$script:bgc);
  131.   Draw $blockX $blockY $script:border;
  132.   $script:blockFrame.Left = $blockX; $script:blockFrame.Top = $blockY; $script:blockFrame.Right = $blockX + $blockW; $script:blockFrame.Bottom = $blockY + $blockH;
  133.   #init blocks
  134.   [Int32[][][][]]$blocks = @(
  135.     @(((1,0),(1,1),(1,2),(1,3)), ((0,1),(1,1),(2,1),(3,1)), ((1,0),(1,1),(1,2),(1,3)), ((0,1),(1,1),(2,1),(3,1))),
  136.     @(((1,1),(1,2),(2,1),(2,2)), ((1,1),(1,2),(2,1),(2,2)), ((1,1),(1,2),(2,1),(2,2)), ((1,1),(1,2),(2,1),(2,2))),
  137.     @(((1,0),(1,1),(1,2),(2,1)), ((1,0),(0,1),(1,1),(2,1)), ((1,0),(0,1),(1,1),(1,2)), ((0,1),(1,1),(2,1),(1,2))),
  138.     @(((1,0),(1,1),(2,1),(2,2)), ((1,1),(2,1),(0,2),(1,2)), ((1,0),(1,1),(2,1),(2,2)), ((1,1),(2,1),(0,2),(1,2))),
  139.     @(((2,0),(1,1),(2,1),(1,2)), ((0,1),(1,1),(1,2),(2,2)), ((2,0),(1,1),(2,1),(1,2)), ((0,1),(1,1),(1,2),(2,2))),
  140.     @(((1,0),(2,0),(1,1),(1,2)), ((0,0),(0,1),(1,1),(2,1)), ((1,0),(1,1),(0,2),(1,2)), ((0,1),(1,1),(2,1),(2,2))),
  141.     @(((0,0),(1,0),(1,1),(1,2)), ((0,1),(1,1),(2,1),(0,2)), ((1,0),(1,1),(1,2),(2,2)), ((2,0),(0,1),(1,1),(2,1)))
  142.     );
  143.   $script:blocks = $blocks;
  144.   #init current and next block;
  145.   SelectNewBlock;
  146.   SelectNewBlock;
  147. }
  148.  
  149. function StartGame(){
  150.   DrawStaticInfo;
  151.   MoveBlock;
  152. }
  153.  
  154. function DrawStaticInfo(){
  155.   #Draw $blockX $blockY $currentRunFrameBuffer;
  156.   DrawText $nextBlockX $nextBlockY (“Total Score:” + $totalScore);
  157.   DrawText $nextBlockX ($nextBlockY + 1) “Next Block:”;
  158.   Draw ($nextBlockX + 2) ($nextBlockY + 2) $nextBlockBuffer;
  159. }
  160.  
  161. function CheckAndDrawCurrentBlock(){
  162.   $isStuck = $false;
  163.  
  164.   $buffer = $currentRunFrameBuffer.Clone();
  165.   0..3 | %{
  166.     $x = $currentBlockX + $currentBlock[$currentBlockRotation][$_][0]$blockX; $y = $currentBlockY + $currentBlock[$currentBlockRotation][$_][1]$blockY;
  167.     $bufferCell = $buffer[$y, $x];
  168.     if( $bufferCell.Character –ne ” ” ) { $isStuck = $true; }
  169.     else { $bufferCell.Character = $blockChar; $bufferCell.ForegroundColor = $blockColors[$currentBlockColor]; $buffer[$y, $x] = $bufferCell; }
  170.   };
  171.   if ($isStuck –eq $true) {
  172.     return $false;
  173.   } else {
  174.     Draw $blockX $blockY $buffer
  175.     return $true;
  176.   }
  177. }
  178.  
  179. #
  180. # Remove full lines
  181. #
  182. function ClearBlocks(){
  183.   $buffer = $Host.UI.RawUI.GetBufferContents($blockFrame);
  184.   $x = $blockFrame.Left; $y = $blockFrame.Top;
  185.   #$blockW;$blockH;
  186.   $lastLine = “”;
  187.   [Int32[]]$lines = @();
  188.   foreach($i in $blockH..0){
  189.     $isPassed = $true;
  190.     foreach($j in 1..$blockW){
  191.       if($buffer[$i,$j].Character –ne $blockChar) { $isPassed = $false; }
  192.     }
  193.     if($isPassed){
  194.       $lines += $i;
  195.     }
  196.   }
  197.   if($lines.Length –gt 0){
  198.     # Blink the lines
  199.     0..3 | %{
  200.       foreach($i in $lines){
  201.         foreach($j in 1..$blockW){
  202.           $bufferCell = $buffer[$i, $j];
  203.           $bufferCell.Character =*”;
  204.           $buffer[$i, $j] = $bufferCell;
  205.         }
  206.       }
  207.       Draw $x $y $buffer;
  208.       Start–Sleep –m 100;
  209.       foreach($i in $lines){
  210.         foreach($j in 1..$blockW){
  211.           $bufferCell = $buffer[$i, $j];
  212.           $bufferCell.Character = “X”;
  213.           $buffer[$i, $j] = $bufferCell;
  214.         }
  215.       }
  216.       Draw $x $y $buffer;
  217.       Start–Sleep –m 100;
  218.     }
  219.     #Clear the lines
  220.     $sourceLine = $lines[0];
  221.     for($k = $sourceLine; $k –gt 0; $k-){
  222.       if($sourceLine –gt 0){
  223.         $sourceLine-–;
  224.       }
  225.       while($lines –Contains $sourceLine){
  226.         if($sourceLine –gt 0){
  227.           $sourceLine-–;
  228.         }
  229.       }
  230.       foreach($l in 1..$blockW){
  231.         $bufferCell = $buffer[$k, $l];
  232.         $bufferCell.Character = $buffer[$sourceLine, $l].Character;
  233.         $buffer[$k, $l] = $bufferCell;
  234.       }
  235.     }
  236.     Draw $x $y $buffer;
  237.     $script:totalScore += 100 * [Math]::Pow(2, $lines.Length);
  238.   }
  239. }
  240.  
  241. function KeyToCommand(){
  242.   $code = 0;
  243.       #$Host.UI.RawUI.FlushInputBuffer();
  244.       #Start–Sleep –milliseconds 50;
  245.   if ($Host.UI.RawUi.KeyAvailable){
  246.     $key = $Host.UI.RawUI.ReadKey(“NoEcho, IncludeKeyUp”);
  247.     $code = switch($key.VirtualKeyCode){
  248.       81 {1 } # ‘q’ quit the game
  249.       32 { 32 } # map space to down arrow
  250.       {(37..40) –contains $_ } {$key.VirtualKeyCode} # arrow key
  251.       default { 0 } # do nothing
  252.     };
  253.   }
  254.   return $code;
  255. }
  256.  
  257. function Rotate($tempBlock){
  258.   $tempBlock
  259.   0..3 | %{
  260.     $y = $tempBlock[$_][0]1; $x = $tempBlock[$_][1]1;
  261.     $x = $x –BXOR $y; $y = $x –BXOR $y; $x = $x –BXOR $y;
  262.     if(((1$y) –lt 0) –or (($x + 1) –lt 0)) {exit; 1$y; $x + 1;}
  263.     $tempBlock[$_][0] = 1$y; $tempBlock[$_][1] = $x + 1
  264.     };
  265.  
  266.   return $tempBlock;
  267. }
  268.  
  269. function MoveBlock(){
  270.   #$leftKey = 37; $upKey = 38; $rightKey = 39; $downKey = 40;
  271.   $code = KeyToCommand;
  272.   $startTime=Get–Date
  273.   while($code –ne –1){
  274.     do{
  275.       $isDown = $false;
  276.       $code = KeyToCommand;
  277.       $x = $currentBlockX;
  278.       $lastBlockRotation = $currentBlockRotation;
  279.       switch($code){
  280.         37 { $script:currentBlockX–-; } #<=
  281.         38 { $script:currentBlockRotation = ($currentBlockRotation + 1) % 4; } #rotate
  282.         39 { $script:currentBlockX++; } #=>
  283.         40 { $isDown = $true;} #strait down
  284.         32 { $isDown = $true;
  285.           $result = CheckAndDrawCurrentBlock;
  286.           $y = $currentBlockY;
  287.           while($result){
  288.             $y = $script:currentBlockY++;
  289.             $result = CheckAndDrawCurrentBlock;
  290.             }
  291.           $script:currentBlockY = $y;
  292.           }
  293.         default {}
  294.       }
  295.  
  296.       $result = CheckAndDrawCurrentBlock;
  297.       if($result –ne $true){
  298.         $script:currentBlockX = $x;
  299.         $script:currentBlockRotation = $lastBlockRotation;
  300.       }
  301.       $elapsed=((Get–Date).Subtract($startTime)).TotalMilliseconds;
  302.       if($isDown –OR (($isDown –eq $false) –AND ($elapsed –ge $turndelay))){
  303.         $isDown = $true; $script:currentBlockY++; $startTime=Get–Date;
  304.         $result = CheckAndDrawCurrentBlock;
  305.         if($result –ne $true){
  306.           ClearBlocks;
  307.           SelectNewBlock;
  308.           $result = CheckAndDrawCurrentBlock;
  309.           if($result –ne $true) {
  310.             break;
  311.           }
  312.         }
  313.       }
  314.  
  315.       Start–Sleep –m 10
  316.     } until($code –eq –1)
  317.  
  318.     InitEnvironment;
  319.     #CheckAndDrawCurrentBlock;
  320.  
  321.   }   # end of main loop
  322.   #exit 1;
  323.   return;
  324. }
  325.  
  326. . main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement