Update play-pong.ps1

This commit is contained in:
Markus Fleschutz 2023-09-17 19:35:15 +02:00
parent 9a5a9cefec
commit f864fa1d3a

View File

@ -1,16 +1,15 @@
$console = [System.Console]::BufferWidth = [System.Console]::WindowWidth = 50 <#
[System.Console]::BufferHeight = [System.Console]::WindowHeight = 20 .SYNOPSIS
Play the Pong game
$player1 = 5 .DESCRIPTION
$player2 = 5 This PowerShell script lets you play the Pong game.
$ball = @{ .EXAMPLE
X = 25 PS> ./play-pong.ps1
Y = 10 .LINK
Dx = 1 https://github.com/fleschutz/PowerShell
Dy = 1 .NOTES
} Author: Markus Fleschutz | License: CC0
$scorePlayer1 = 0 #>
$scorePlayer2 = 0
function DrawScores { function DrawScores {
$middle = [System.Console]::WindowWidth / 2 $middle = [System.Console]::WindowWidth / 2
@ -19,11 +18,10 @@ function DrawScores {
} }
function DrawFooter { function DrawFooter {
$text = "Created in PS only with GPT-4" $text = "| Player 1: <W> for up, <S> for down | Player 2: <UP> or <DOWN> | ESC key to quit |"
$rightAlignX = [System.Console]::WindowWidth - $text.Length $x = ([System.Console]::WindowWidth - $text.Length) / 2
$bottomY = [System.Console]::WindowHeight - 1 $y = [System.Console]::WindowHeight - 1
[System.Console]::SetCursorPosition($x, $y)
[System.Console]::SetCursorPosition($rightAlignX, $bottomY)
[System.Console]::Write($text) [System.Console]::Write($text)
} }
@ -51,7 +49,6 @@ function ClearPaddle($y, $isLeft) {
} }
} }
function DrawBall($x, $y) { function DrawBall($x, $y) {
if ($x -lt 0 -or $x -ge [System.Console]::WindowWidth - 1 -or $y -lt 0 -or $y -ge [System.Console]::WindowHeight) { if ($x -lt 0 -or $x -ge [System.Console]::WindowWidth - 1 -or $y -lt 0 -or $y -ge [System.Console]::WindowHeight) {
return return
@ -60,7 +57,6 @@ function DrawBall($x, $y) {
[System.Console]::Write("O") [System.Console]::Write("O")
} }
function ClearBall($x, $y) { function ClearBall($x, $y) {
if ($x -lt 0 -or $x -ge [System.Console]::WindowWidth - 1 -or $y -lt 0 -or $y -ge [System.Console]::WindowHeight) { if ($x -lt 0 -or $x -ge [System.Console]::WindowWidth - 1 -or $y -lt 0 -or $y -ge [System.Console]::WindowHeight) {
return return
@ -69,12 +65,6 @@ function ClearBall($x, $y) {
[System.Console]::Write(" ") [System.Console]::Write(" ")
} }
DrawPaddle $player1 $true
DrawPaddle $player2 $false
DrawScores
DrawFooter
function UpdateBall { function UpdateBall {
$nextX = $script:ball.X + $script:ball.Dx $nextX = $script:ball.X + $script:ball.Dx
$nextY = $script:ball.Y + $script:ball.Dy $nextY = $script:ball.Y + $script:ball.Dy
@ -106,10 +96,26 @@ function UpdateBall {
$script:ball.Y = $nextY $script:ball.Y = $nextY
} }
$ui = (Get-Host).ui
$rui = $ui.rawui
$console = [System.Console]::BufferWidth = [System.Console]::WindowWidth = $rui.MaxWindowSize.Width
[System.Console]::BufferHeight = [System.Console]::WindowHeight = $rui.MaxWindowSize.Height
$player1 = 5
$player2 = 5
$ball = @{
X = 25
Y = 10
Dx = 1
Dy = 1
}
$scorePlayer1 = 0
$scorePlayer2 = 0
[System.Console]::Clear()
DrawPaddle $player1 $true
DrawPaddle $player2 $false
DrawScores
DrawFooter
[System.Console]::TreatControlCAsInput = $true [System.Console]::TreatControlCAsInput = $true
while ($true) { while ($true) {
@ -144,9 +150,6 @@ while ($true) {
DrawPaddle $player1 $true DrawPaddle $player1 $true
DrawPaddle $player2 $false DrawPaddle $player2 $false
Start-Sleep -Milliseconds 100 Start-Sleep -Milliseconds 100
} }
[System.Console]::Clear() [System.Console]::Clear()