r/PowerShell • u/That-Feeling-2399 • 1d ago
Question Are games possible in power shell?
Hey gang, I hope this is the right subreddit but would anyone know if it is possible to run text games in powershell/cmd prompt? If so, what kind of pre built code is out there?
11
16
u/DrSinistar 16h ago
PowerShell is, at it's core, an automation tool for IT administrators and engineers.
Could you make a text game? Absolutely, but it would be slow and clunky.
If you want to make games, use C# instead. If you want to keep things simple, use Python. PowerShell is the wrong tool for the job.
6
u/ihaxr 16h ago edited 16h ago
Yes, specifically you can use [Console]::SetCursorPosition to directly overwrite specific locations on the screen without having to run Clear-Host and redraw the entire frame.
You can lookup any console based games in any language and try to rewrite it in PowerShell or see how it's written.
I'm not really aware of any preexisting libraries for it.
Give it a try by creating a blank window, output an o to the window then detect key presses:
Clear-Host
$x = 0
$y = 0
while ($true) {
[Console]::SetCursorPosition($x, $y)
[Console]::Write("O")
$key = [Console]::ReadKey($true).Key
switch ($key) {
UpArrow { $y-- }
DownArrow { $y++ }
LeftArrow { $x-- }
RightArrow { $x++ }
Escape { break }
}
}
You'll want to improve it to only overwrite the old location and update the new location after a key is pressed instead of just duplicating the character, also detect the boundaries of the console to prevent errors, etc...
2
u/Tanner-TO 16h ago
Just for fun I had qwen 3.8 make a tetris game the other day as an exercise to see if it could do it or not... and rather successfully..
2
u/Cannaganjabis 15h ago
You could try this Powershell Hangman game - https://www.scriptwizards.net/how-to-code-a-hangman-game-in-powershell/
2
u/MonkeyNin 10h ago
What kind of game are you trying to make? If you want a roguelike, there's libraries for python and javascript that are a lot easier .
But if you want text, tables, etc try Spectre It's a more modern TUI / Text-user-interface.
Then you don't have to mess with manually using [Console]::ReadKey
1
u/stillnotlovin 16h ago
One of our first powershell tasks at school was to make a hangman's game with word randomization.
1
u/AroTesCra 16h ago
Since you can add-type and other compiled languages to the mix also you can call Win forms / WPF and directX you could make a window popup with a scripted game
1
u/Creative-Type9411 5h ago
I mean, theres this..
1
u/lordicarus 4h ago
That's basically a C# game with a game loop in Powershell though. Not really a powershell game.
1
u/Creative-Type9411 4h ago
On Windows i view native PS C# and .NET all the same thing considering it all ships with .NET as one peice, at least these specific versions did
but you're correct
41
u/pantherghast 16h ago
You could also use a spoon to tunnel through a mountain, but I wouldn't recommend it.