Skip to content

Instantly share code, notes, and snippets.

@usysrc
Last active December 21, 2015 03:09
Show Gist options
  • Select an option

  • Save usysrc/6240555 to your computer and use it in GitHub Desktop.

Select an option

Save usysrc/6240555 to your computer and use it in GitHub Desktop.
a minimal roguelike base in powershell
Function writetext
{
Param ([int]$x, [int]$y, [string]$text)
[Console]::SetCursorPosition($x, $y)
[Console]::Write($text)
}
# set background color of the shell to black
(Get-Host).UI.RawUI.BackgroundColor = "black"
clear
# player objects factory function
function newPlayer()
{
$player = New-Object PSObject
$player | Add-Member -type Noteproperty -Name x -Value 3
$player | Add-Member -type Noteproperty -Name y -Value 3
$player | Add-Member -type Noteproperty -Name char -Value "@"
return $player
}
#create the player object
$player = newPlayer
#create the map
$width = 20
$height = 20
$map = new-Object 'object[,]' $width, $height
for($i=1; $i -le $width-1; $i++)
{
for($j=1; $j -le $height-1; $j++)
{
$map[$i,$j] = "."
}
}
# main loop
do
{
[Console]::Clear()
for($i=1; $i -le $width-1; $i++)
{
for($j=1; $j -le $height-1; $j++)
{
writetext $i $j $map[$i,$j]
}
}
writetext $player.x $player.y $player.char
$input = [Console]::ReadKey(("NoEcho"))
if ($input.key -eq "RightArrow"){ $player.x = $player.x + 1 }
if ($input.key -eq "LeftArrow"){ $player.x = $player.x - 1 }
if ($input.key -eq "DownArrow"){ $player.y = $player.y + 1 }
if ($input.key -eq "UpArrow"){ $player.y = $player.y - 1 }
} while ($input.keychar -ne "q")
@SteveHiner
Copy link

Cool little demo. Thanks for sharing it.

The current version of the script does not work in PowerShell ISE. I added this to the beginning of the script to launch the normal PowerShell host if it was launched in the ISE:
if ($(Get-Host).Name.Contains("ISE"))
{
Write-Host "Switching to non-ISE PowerShell"
start-process PowerShell.exe -argument $MyInvocation.MyCommand.Definition
Return
}
Note: I didn't make it handle arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment