Space Invaders

Home  >>  Cool  >>  Space Invaders

Space Invaders

On January 2, 2007, Posted by , In Cool,Invaders,PowerShell, With 8 Comments

In a previous post I said that I always like to try to write space invaders in every language that I learn. Well, PowerShell in the standard Windows console window looked like a great candidate so I set about the task in my spare time. My colleagues Richy King, Nik Crabtree and Brian Long joined in the efforts and between us, after 37 revisions, we came up with something that was:

a) Silly
b) Inappropriate use of PowerShell
c) Quite a bit of fun.

Download it here: psinvaders r94 Update: this version now includes sound.

Unpack the zip file, then from the directory where psinvaders.ps1 is, type ./psinvaders.ps1. Enjoy!

Any future updates may appear on the downloads page.

Here are screen shots of the play screen and the score advance table.

How it works

All the drawing is based on drawing ‘sprites’ on the console screen. No back buffering takes place, so when a sprite is drawn, it erases itself from its most recent position, updates the x,y value, the draws itself in the new position.

The drawing itself is done using [Console]::Write. Since writing this we discovered the joys of BufferCells, and one of the reasons for taking this project no further.

Reading the key events proved to be quite simple: we just check if a key event is available, then consume all the event types, maintaining the down/up state for movement keys and just observing down events for firing and putting in new credits.

The game is quite playable, speed-wise, because very few sprites are moved in the inner loop: the base, one space invader, and any bombs and missiles. This means that:

a) The game moves at a reasonable pace.
b) The alien block gets quicker as more are killed
c) An alien bomb will overdraw an lower alien until next time.

The rest of it is fairly basic game loop logic.

What’s next?

I’m done with psinvaders now. I’ve started working on a better framework for doing back-buffered drawing of sprites, images, tiled backgrounds and lines/points.

Currently, it’s pure PowerShell and unusably slow, but I may start doing some C# or JScript inlining to speed it up. Thanks for Lee Holmes for that inspiration.

Or maybe I’ll just wait for the anticipated performance inprovements in PowerShell 1.1 ;-)

8 Comments so far:

  1. Поиграем в PowerShell?…

    Для чего только не используют PowerShell. Это только подчеркивает мощность и уни…

  2. Space Invaders…

    Space Invaders…

  3. PowerShell Space Invaders…

    This one I got from Paul Flaherty’s blog (aka Flaphead). Someone had the bright idea of writing Space…

  4. PowerShell Space Invaders…

  5. […] Anyways, a popular link is Space Invaders in PowerShell. […]

  6. […] images to text (ASCII art)Space InvadersMaking noisesWhat colour is yours?Console screen grabs in […]

  7. […] machine copying with PowerShell Believe it or not, PowerShell is more than just fun and games. You can even do productive things with it! A common scenario I run into is the need to create a […]

  8. Divertimenti innocui per sistemisti…

    PowerShell Invaders!…