PowerShell Game 04

PowerShell Game 04

 07.02.2016 -  Sebastian Pech -  ~2 Minuten

Nachdem die Spielwelt für das PowerShell Game existiert, wäre es doch schön wenn sich etwas darin bewegen könnte. Üblicherweise ist das die Spielfigur. In diesem Fall ein freundlicher Smiley.

Variablen

Alle Informationen die eine Spielfigur benötigt bekommt diese nun als Attribute.

$player = New-Object PSObject
Add-Member -InputObject $player -MemberType NoteProperty -Name x -Value 1
Add-Member -InputObject $player -MemberType NoteProperty -Name y -Value 0
Add-Member -InputObject $player -MemberType NoteProperty -Name symbol -Value '☺'

Bewegung

Im GameLoop sollen die Tasten n/e/s/w (North/East/South/West) eine Bewegung der Spielfigur ermöglichen.

switch($action)
{
    'q' { $runGame = $false; }
    'e' { $player.x++ }
    'w' { $player.x-- }
    'n' { $player.y-- }
    's' { $player.y++ }
}

Neben der Bewegung ist es außerdem hilfreich wenn der Spieler das Spielfeld nicht verlassen kann.

if($player.x -lt 0) { $player.x = 0 }
if($player.x -ge $global:mapWidth ) { $player.x = $global:mapWidth -1 }
if($player.y -lt 0) { $player.y = 0 }
if($player.y -ge $global:mapHeight ) { $player.y = $global:mapHeight -1 }

Spielfeld

Beim Spielfeld wird nun vor der Ausgabe eines Feldes ermittelt ob der Spieler auf diesem Feld steht. Falls dies der Fall ist, ist das angezeigte Symbol der Smiley in der Farbe des Spielers. Der Hintergrund erhält die Farbe des ursprünglichen Feldes.

function DrawMap()
[...]
# Farbe ermitteln
    $char = $global:map[$x + $y * $global:mapWidth]
        $foregroundColor = "Black"
        $backgroundColor = "White"

# Feldspezifische Farbe und Symbol ermitteln		
    switch($char)
    {
        'E' { $foregroundColor = "Black"; $backgroundColor = "DarkRed"; $char = '^' }
        '#' { $foregroundColor = "Gray"; $backgroundColor = "DarkGray"; $char = '#' }
        'L' { $foregroundColor = "DarkGreen"; $backgroundColor = "DarkGreen"; $char = ' ' }
        'S' { $foregroundColor = "Yellow"; $backgroundColor = "Yellow"; $char = ' ' }
        'B' { $foregroundColor = "White"; $backgroundColor = "White"; $char = ' ' }
        'D' { $foregroundColor = "Black"; $backgroundColor = "Black"; $char = ' ' }
        'G' { $foregroundColor = "DarkGreen"; $backgroundColor = "Green"; $char = '*' }
        'J' { $foregroundColor = "DarkGray"; $backgroundColor = "Gray"; $char = '''' }
        'W' { $foregroundColor = "DarkBlue"; $backgroundColor = "Blue"; $char = '~' }
    }
    
# Wenn der Spieler auf dem Feld ist - Symbold und Vordergrundfarbe ersetzen		
    if($x -eq $player.x -and $y -eq $player.y)
    {
        $foregroundColor = "Blue"
        $char = $player.symbol
    }
    
# Aktuelle Symbol-/Farbkombination ausgeben
    Write-Host -NoNewline -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor $char
[...]