BASH Games

Get serious about your shell scripting skills and maybe you can pull this one off. It’s a game of snake played in a BASH shell. It seems like a coding nightmare, but the final product turns out to be organized well enough for us to understand and took less than 250 lines of code.

[Martin Bruchanov] started on the project after pining for an old DOS game called Housenka. It’s another version of the classic Snake game which we’ve coded ourselves and seen in several projects including this head-to-head version using musical recorders as controllers. When using a terminal emulator capable of ANSI sequences the game is displayed in color using extended characters.

We give [Martin] bonus points for the way he wrote about his project. It describes the mechanics most would be interested in, like how the user input is captured and what drives the update function and food generation. The rest of the details can be gleaned by reading through the code itself.

18 thoughts on “BASH Games

  1. I guess well done… but really it is just another example of bash being used where a compiled application should have been. I wonder how much quicker a Linux machine would be if all it’s start-up scripts were replaced with real applications.

    1. Pretty quick actually. At least right up until you wanted to customize something, in which case “I can’t edit it at all” roughly translates to a big zero for speed.
      (If you’ll never get to where you want, it’s hard to claim progress is being made)

      A good comparison of speed and feature would be to install a fresh Windows system.
      Boots really fast! But we’ve all been there when banging our heads against a stone wall trying to get the OS to do something the makers didn’t intend :P

  2. And here is one that also draws to the eink framebuffer, and also reads the binary input events for the Kindle Touch touchscreen, letting you draw on the screen with your finger (and save the results out to a display list file):

    The point is, a number of people had fun contributing to that as a sort of puzzle to see just how much you can bend a shell script to your will, doing things that many thought was limited only to “real” programming languages.

  3. this has been done before… I made a hearts game in a CS class using escape codes to change the background and foreground to make the output look like cards….
    there are also ascii demos that kick ass. they use an ascii filter on a bitmap.
    hell, speaking of ascii filters, you can make this kind of stuff in gimp

  4. How is he getting away with $[] in lieu of $(())? I can’t find the $[ ] syntax documented anywhere. If I find that it words without side effects, though, I’m stealing that idea for use in my own code.

    He also reminded me that the “:” nop can take args, so I can hide arithmetic side effects there.

    You learn something new every day.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.