How to make a game using PowerShell Old School Style – Dragon Slayer
I love messing around from time to time and recently I’ve been working on a new book which I’ll be releasing on Amazon which focuses on writing fun games using PowerShell! If you want to keep up to date on its development you should subscribe to the notification list here. Back in the day I spent many hours writing ASCII based games at school and college and I wanted to give it a try again only this time put together a book which teaches PowerShell scripting in a fun way.
I’m going to share one of the lessons from the book with you now, its a text based role-play game where you have to make decisions based on the story line. Your decisions will directly effect the outcome of any future decisions made, so in order to put this together we need to learn a few basics first.
- Controlling the PowerShell host (Resizing the window if its running as a command prompt)
- Displaying text on the screen
- Reading characters from the keyboard
- Storing and reading information in variables
- Structuring code so its easy to manage and read
Let’s start by writing a method we can call to set the size of the PowerShell window to known values. We need to do this so our title screens, menu’s and levels display correctly when run on other machines. Here I setup a function called Setup-Display() which sets the width and height of the PowerShell window to known values, in this case 55 characters top to bottom and 110 characters from left to right.
$phost = get-host
$pwindow = $phost.ui.rawui
$newsize = $pwindow.windowsize
$newsize.height = 55
$newsize.width = 110
$pwindow.windowsize = $newsize
Once we have called the Setup-Display() method we can call our second method Title-Screen which obviously displays the title screen of the game. This is a cut down version of the code but don’t worry you can download the whole script at the end of this post.
There are several key parts to this method, first is the cmdlet Clear-Host which wipes everything from the window. If someone runs our script after running other commands then it wont display correctly so clear everything from the window using Clear-Host.
Next we can use the Write-Host cmdlet to display text on a new line in the window. Here we ask the user to press any key to play or press q to quit then next we create a variable $Continue which saves the key the user presses. Here we call ReadKey which waits for the user to press a key which then returns a key press object which we save into the $continue variable. Finally we use an if statement and compare the Character value to the q character. If they match then we set a global variable runGame to $false which stops the game. You will see from the code download that global variables are stored at the top of the script and are prefixed with $global: which simply states that they can be used anywhere within our script where as other variables we use such as $continue are created within a method and can only be used within that method and not called from another method later on.
Write-Host “Press Any Key to Play or Q to quit”
$continue = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)
$global:runGame = $false
PowerShell isn’t just an administration tool it can be fun too! Download the code and expand on this example and if you create something amazing send it to me and I’ll post it here for others to play! Maybe we should have a games programming competition!? Comment below if you would be interested.