CopilotDemo2.ps1
#What is snake game? #build snake game in PowerShell # PowerShell Snake Game $snake = @(,@(10,10)) $direction = 'Right' $fruit = @(,@(Get-Random -Minimum 1 -Maximum 20), @(Get-Random -Minimum 1 -Maximum 20)) while ($true) { Clear-Host if ($snake[0] -eq $fruit) { $fruit = @(,@(Get-Random -Minimum 1 -Maximum 20), @(Get-Random -Minimum 1 -Maximum 20)) $snake += ,@($snake[-1]) } $snake = @(,@($snake[0][0], ($snake[0][1] % 20) + 1)) + $snake if ($snake[0] -in $snake[1..($snake.Length-2)]) { break } 1..20 | ForEach-Object { $y = $_ 1..20 | ForEach-Object { $x = $_ if ($snake -contains @(,$x,$y)) { Write-Host -NoNewline 'O' } elseif ($fruit -eq @(,$x,$y)) { Write-Host -NoNewline 'X' } else { Write-Host -NoNewline ' ' } } Write-Host } switch ($host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode) { 37 { if ($direction -ne 'Right') { $direction = 'Left' } } 38 { if ($direction -ne 'Down') { $direction = 'Up' } } 39 { if ($direction -ne 'Left') { $direction = 'Right' } } 40 { if ($direction -ne 'Up') { $direction = 'Down' } } } switch ($direction) { 'Left' { $snake[0][0]-- } 'Up' { $snake[0][1]-- } 'Right' { $snake[0][0]++ } 'Down' { $snake[0][1]++ } } Start-Sleep -Milliseconds 100 } |