Advertisement
timsstuff

Enter-Matrix.ps1

Aug 4th, 2022
1,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Note: I've discovered that PowerShell 5.0 can't run this script if it's loading from a folder with an apostrophe in the name. It's a bug that
  2. #has since been resolved in PS7. Easy solution is to move the script out of the folder with the apostrophe
  3. #ref: https://superuser.com/questions/1323501/bug-in-powershell-classes-when-script-is-in-a-folder-containing-a-single-quote
  4. class Glyph {
  5.     [int]$LastPosition
  6.     [int]$CurrentPosition
  7.     [int]$Velocity
  8.     [int]$Intensity
  9.     [double]$IntensityChange
  10.     [char]$Current
  11.     [char]$Last
  12.    
  13.     Glyph() {
  14.         $this.Setup()
  15.     }
  16.  
  17.     [void]Setup()
  18.     {
  19.         $this.CurrentPosition = $script:rand.Next(-$script:ScreenHeight,.6*$script:ScreenHeight)
  20.         $this.Velocity = 1
  21.         $this.Intensity=0
  22.         $this.IntensityChange = ($script:rand.Next(1,20)/100)
  23.         $this.Current=$script:PossibleGlyphs[$script:rand.Next($script:glyphCount+1)]
  24.         $this.Last=$script:PossibleGlyphs[$script:rand.Next($script:glyphCount+1)]
  25.        
  26.     }
  27.  
  28.     [void]Move() {
  29.         $this.LastPosition=$this.CurrentPosition
  30.         $this.Intensity+=[Math]::Floor(255*$this.IntensityChange)
  31.         if ($this.Intensity -gt 255){$this.Intensity = 255}
  32.  
  33.         $this.CurrentPosition+=$this.Velocity
  34.        
  35.         $this.Last = $this.Current
  36.         if ($this.Current -ne ' '){$this.Current=$script:PossibleGlyphs[$script:rand.Next($script:glyphCount+1)]}
  37.        
  38.         #out of bounds
  39.         if ($this.CurrentPosition -gt $script:ScreenHeight -1)
  40.         {
  41.             $this.Setup()
  42.  
  43.         }
  44.     }
  45. }
  46.  
  47. #not using this any longer to blank the screen. It fills the screen with black periods on a black background.
  48. function Blank-Screen()
  49. {
  50.     $line="$script:e[38;2;0;0;0m$script:e[48;2;0;0;0m" + "".PadLeft($script:ScreenWidth,".")
  51.    
  52.     for ($l = 0;$l -lt $script:ScreenWidth;$l++)
  53.     {
  54.         Write-Host -NoNewline $line
  55.     }
  56. }
  57.  
  58. $script:rand = [System.Random]::new()
  59. $script:ScreenWidth=$host.UI.RawUI.WindowSize.Width
  60. $script:ScreenHeight=$host.UI.RawUI.WindowSize.Height
  61.  
  62. #[char[]]$script:PossibleGlyphs="     `"ACBDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()<>?{}[]<>~".ToCharArray()
  63. [char[]]$script:PossibleGlyphs="   +=1234567890!@#$%^&*()<>?{}[]<>~".ToCharArray() #this seems a little less 'human' than the version above with letters
  64. #[string[]]$script:PossibleGlyphs = ("   サカホヨヲロッβαγπΣθΩΞφµ@#$%&8953" -split "").Where({$_ -ne ""})
  65. $glyphCount = $script:PossibleGlyphs.Count
  66.  
  67.  
  68. $script:e=[char]27 #escape
  69.  
  70. #create array of glyphs, one for each column
  71. [Glyph[]]$AllGlyphs=[Glyph[]]::new($script:ScreenWidth)
  72. for ($i=0;$i -lt $AllGlyphs.Count;$i++)
  73. {
  74.     $AllGlyphs[$i]=[Glyph]::new()
  75. }
  76.  
  77. #hide the cursor and capture the
  78. [Console]::CursorVisible=$false
  79. $originalBG = [Console]::BackgroundColor
  80. $originalFG = [Console]::ForegroundColor
  81. #$originalOE = [Console]::OutputEncoding
  82.  
  83. #[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  84.  
  85. #Blank-Screen
  86. Write-Host "$e[38;5;16m$e[48;5;16m$e[H$e[Jm" -NoNewline #blanks the screen with some ANSI escape sequences
  87.  
  88. $stopwatch =[System.Diagnostics.Stopwatch]::StartNew()
  89.  
  90. #loop until a key is pressed
  91. while (-not [System.Console]::KeyAvailable)
  92. {
  93.     if ($stopwatch.Elapsed.TotalMilliseconds -gt 33.33)
  94.     {
  95.         #loop through each glyph and move it
  96.         for ($i = 0; $i -lt $script:ScreenWidth;$i++)
  97.         {
  98.             $AllGlyphs[$i].Move()
  99.  
  100.             #draw the 'leader'
  101.             if ($AllGlyphs[$i].CurrentPosition -ge 0)
  102.             {
  103.                 [Console]::CursorLeft=$i
  104.                 [Console]::CursorTop=[Math]::Floor($AllGlyphs[$i].CurrentPosition)
  105.                 [Console]::Write("$e[48;5;16m$e[38;5;15m$($AllGlyphs[$i].Current)")            
  106.             }
  107.  
  108.                     #draw the 'faint' trail
  109.             if ($AllGlyphs[$i].LastPosition -ge 0)
  110.             {
  111.                 [Console]::CursorLeft=$i
  112.                 [Console]::CursorTop=[Math]::Floor($AllGlyphs[$i].LastPosition)
  113.                 [Console]::Write("$e[48;5;16m$e[38;2;0;$($AllGlyphs[$i].Intensity);0m$($AllGlyphs[$i].Last)")
  114.             }
  115.  
  116.         }
  117.  
  118.         $stopwatch.Restart()
  119.     }
  120.  
  121. }
  122.  
  123. #set the console colors back
  124. [Console]::BackgroundColor = $originalBG
  125. [Console]::ForegroundColor = $originalFG
  126.  
  127. #disregard the keystroke that was pressed to exit the loop
  128. $null = [Console]::ReadKey($true)
  129.  
  130. #take us back home
  131. Clear-Host
  132. #[console]::OutputEncoding = $originalOE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement