2023-09-18 20:26:16 +02:00
|
|
|
|
<#
|
2023-09-17 19:35:15 +02:00
|
|
|
|
.SYNOPSIS
|
2023-09-18 20:26:16 +02:00
|
|
|
|
Play the Pong game
|
2023-09-17 19:35:15 +02:00
|
|
|
|
.DESCRIPTION
|
2023-09-18 20:26:16 +02:00
|
|
|
|
This PowerShell script lets 2 players play the famous Pong game.
|
2023-09-18 12:22:26 +02:00
|
|
|
|
NOTE: Player 1: <W> moves up, <S> moves down | Player 2: <UP> or <DOWN> | <ESC> to quit
|
2023-09-17 19:35:15 +02:00
|
|
|
|
.EXAMPLE
|
2023-09-18 20:26:16 +02:00
|
|
|
|
PS> ./play-pong.ps1
|
2023-09-17 19:35:15 +02:00
|
|
|
|
.LINK
|
2023-09-18 20:26:16 +02:00
|
|
|
|
https://github.com/fleschutz/PowerShell
|
2023-09-17 19:35:15 +02:00
|
|
|
|
.NOTES
|
2023-09-18 20:26:16 +02:00
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
2023-09-17 19:35:15 +02:00
|
|
|
|
#>
|
2023-09-17 12:18:49 +02:00
|
|
|
|
|
|
|
|
|
function DrawScores {
|
2023-09-18 12:22:26 +02:00
|
|
|
|
$x = [System.Console]::WindowWidth / 2 - 4
|
|
|
|
|
[System.Console]::SetCursorPosition($x, 1)
|
2023-09-17 12:18:49 +02:00
|
|
|
|
[System.Console]::Write("$($script:scorePlayer1) - $($script:scorePlayer2)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function DrawFooter {
|
2023-09-18 12:22:26 +02:00
|
|
|
|
$text = "| Player 1: <W> moves up, <S> moves down | Player 2: <UP> or <DOWN> | <ESC> to quit |"
|
2023-09-17 19:35:15 +02:00
|
|
|
|
$x = ([System.Console]::WindowWidth - $text.Length) / 2
|
|
|
|
|
$y = [System.Console]::WindowHeight - 1
|
|
|
|
|
[System.Console]::SetCursorPosition($x, $y)
|
2023-09-17 12:18:49 +02:00
|
|
|
|
[System.Console]::Write($text)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function DrawPaddle($y, $isLeft) {
|
2023-09-19 20:34:00 +02:00
|
|
|
|
if ($isLeft) {
|
|
|
|
|
$x = 0
|
|
|
|
|
} else {
|
|
|
|
|
$x = [System.Console]::WindowWidth - 1
|
|
|
|
|
}
|
|
|
|
|
for ($i = 0; $i -lt 5; $i++) {
|
|
|
|
|
[System.Console]::SetCursorPosition($x, $y + $i)
|
|
|
|
|
[System.Console]::Write("▌")
|
|
|
|
|
}
|
2023-09-17 12:18:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ClearPaddle($y, $isLeft) {
|
|
|
|
|
if ($isLeft) {
|
|
|
|
|
$x = 0
|
|
|
|
|
} else {
|
|
|
|
|
$x = [System.Console]::WindowWidth - 1
|
|
|
|
|
}
|
|
|
|
|
for ($i = 0; $i -lt 5; $i++) {
|
|
|
|
|
[System.Console]::SetCursorPosition($x, $y + $i)
|
|
|
|
|
[System.Console]::Write(" ")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
[System.Console]::SetCursorPosition($x, $y)
|
2023-09-18 20:26:16 +02:00
|
|
|
|
Write-Host "🔴" -noNewline
|
2023-09-17 12:18:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
[System.Console]::SetCursorPosition($x, $y)
|
|
|
|
|
[System.Console]::Write(" ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function UpdateBall {
|
|
|
|
|
$nextX = $script:ball.X + $script:ball.Dx
|
|
|
|
|
$nextY = $script:ball.Y + $script:ball.Dy
|
|
|
|
|
|
|
|
|
|
if ($nextY -lt 0 -or $nextY -ge [System.Console]::WindowHeight) {
|
|
|
|
|
$script:ball.Dy = -$script:ball.Dy
|
|
|
|
|
$nextY = $script:ball.Y
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($nextX -eq 1 -and ($nextY -ge $player1 -and $nextY -le ($player1 + 4))) {
|
|
|
|
|
$script:ball.Dx = -$script:ball.Dx
|
|
|
|
|
$nextX = $script:ball.X
|
|
|
|
|
} elseif ($nextX -eq ([System.Console]::WindowWidth - 2) -and ($nextY -ge $player2 -and $nextY -le ($player2 + 4))) {
|
|
|
|
|
$script:ball.Dx = -$script:ball.Dx
|
|
|
|
|
$nextX = $script:ball.X
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 20:34:00 +02:00
|
|
|
|
if ($nextX -lt 0) {
|
2023-09-17 12:18:49 +02:00
|
|
|
|
$script:scorePlayer2++
|
|
|
|
|
DrawScores
|
|
|
|
|
$nextX = [System.Console]::WindowWidth / 2
|
|
|
|
|
} elseif ($nextX -ge [System.Console]::WindowWidth) {
|
|
|
|
|
$script:scorePlayer1++
|
|
|
|
|
DrawScores
|
|
|
|
|
$nextX = [System.Console]::WindowWidth / 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$script:ball.X = $nextX
|
|
|
|
|
$script:ball.Y = $nextY
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 19:35:15 +02:00
|
|
|
|
$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
|
2023-09-17 12:18:49 +02:00
|
|
|
|
|
2023-09-17 19:35:15 +02:00
|
|
|
|
$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
|
2023-09-17 12:18:49 +02:00
|
|
|
|
[System.Console]::TreatControlCAsInput = $true
|
|
|
|
|
|
|
|
|
|
while ($true) {
|
|
|
|
|
if ([System.Console]::KeyAvailable) {
|
|
|
|
|
$key = [System.Console]::ReadKey($true).Key
|
|
|
|
|
|
|
|
|
|
if ($key -eq "W" -and $player1 -gt 0) {
|
|
|
|
|
ClearPaddle $player1 $true
|
|
|
|
|
$player1--
|
|
|
|
|
DrawPaddle $player1 $true
|
|
|
|
|
} elseif ($key -eq "S" -and $player1 -lt [System.Console]::WindowHeight - 5) {
|
|
|
|
|
ClearPaddle $player1 $true
|
|
|
|
|
$player1++
|
|
|
|
|
DrawPaddle $player1 $true
|
|
|
|
|
} elseif ($key -eq "UpArrow" -and $player2 -gt 0) {
|
|
|
|
|
ClearPaddle $player2 $false
|
|
|
|
|
$player2--
|
|
|
|
|
DrawPaddle $player2 $false
|
|
|
|
|
} elseif ($key -eq "DownArrow" -and $player2 -lt [System.Console]::WindowHeight - 5) {
|
|
|
|
|
ClearPaddle $player2 $false
|
|
|
|
|
$player2++
|
|
|
|
|
DrawPaddle $player2 $false
|
|
|
|
|
} elseif ($key -eq "Escape") {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClearBall $ball.X $ball.Y
|
|
|
|
|
UpdateBall
|
|
|
|
|
DrawBall $ball.X $ball.Y
|
|
|
|
|
|
|
|
|
|
DrawPaddle $player1 $true
|
|
|
|
|
DrawPaddle $player2 $false
|
|
|
|
|
|
|
|
|
|
Start-Sleep -Milliseconds 100
|
|
|
|
|
}
|
2023-09-18 12:22:26 +02:00
|
|
|
|
[System.Console]::Clear()
|
|
|
|
|
exit 0 # success
|