Last active
December 21, 2015 03:09
-
-
Save usysrc/6240555 to your computer and use it in GitHub Desktop.
a minimal roguelike base in powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.