8

I am writing a console app that would be kicking off long running processes. So rather than let the user stare at the screen for several minutes, I'd love to throw my processing on a background thread and let the user play a game meanwhile.

If you are my age, you definitely remember the Nibbles game written in QBasic that shipped with DOS for years. I remember reading several years ago that someone rewrote it in C# console mode. But I can't find it. Anyone know where I could grab it?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • Just don't get sued by [namco](http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=5718632.PN.&OS=PN/5718632&RS=PN/5718632) <-- The patient for a minigame during a loading screen – Scott Chamberlain Oct 04 '10 at 21:28
  • lol, there is a patent for everything nowdays. Just ignore them. – o0'. Oct 04 '10 at 23:17

1 Answers1

12

I saw your question and immediately went ahead and translated the original NIBBLES.BAS directly into C#.

Of course, the code is full of Basicisms; in particular, arrays start at 1. I have changed the sammy and colorTable arrays so that they start at 0, but not the arena array (this one now has an unused index 0).

Many things could be done more “properly” in C# (e.g. one should probably use enums instead of the numbers 1,2,3,4 for directions up,down,left,right; one should use the ConsoleColor enum instead of integers for the colors).

I did use a few C#isms though: the sparkle effect on the initial screen is done in a separate thread so that I can just use Console.ReadKey() to wait for user input.

I had to comment out the code that sets/unsets Num Lock, Caps Lock and Scroll Lock because C#’s Console only lets me read the state of those, not change them. I would have had to use WinAPI for this, which I decided would have been over the top.

All the comments are from the original.

EDIT: By now the finished version of this is on github. I’ve changed the above link to go to the github repo instead of a pastebin. For those still interested in the original first version, here’s the original pastebin link.

Timwi
  • 65,159
  • 33
  • 165
  • 230
  • One complaint. It works well if in the properties for the DOS window you've selected Lucida Console fonts. But if you have the Raster Fonts selected (which I think is the default), the screen is an absolute mess. – AngryHacker Oct 05 '10 at 00:58
  • @AngryHacker: I know. It’s very unfortunate. If you know of a way to fix this, I would love to hear. I don’t know how to change the console font programmatically, or even to determine what the font is. If I could at least determine it, then I could use the old DOS codepage when it is Raster Fonts. – Timwi Oct 05 '10 at 11:40
  • @Timwi: I fixed it to work with both raster and TT fonts. I also added a few other minor changes like properly restoring the console. http://pastebin.com/LjXn7246 – Tergiver Oct 14 '10 at 15:56
  • There's also a bug in `InitColors`. The background color needs to be set (black) before calling `Console.Clear`. There's a small chance (I saw it once in about 25 levels played) that the background color is non-black prior to a level change. – Tergiver Oct 14 '10 at 16:40
  • ...That would be colorTable[3], not black. – Tergiver Oct 14 '10 at 16:51
  • Why does pastebin give you a new URL for corrections? That's not very usable. Here is the corrected one: http://pastebin.com/AzECdgYw – Tergiver Oct 14 '10 at 16:57
  • @Tergiver: Thanks, I’ll take a look at it tomorrow. By now I have considerably “fixed” the code on my end here, removing all the Basicisms and making it more C#-like. I’ll try to merge your changes and then upload it to Codeplex or something, OK? – Timwi Oct 14 '10 at 23:12
  • @Timwi: Cool. I thought about cleaning it up myself, it's even more messy with the changes I made, but I got distracted by writing a QBasic PLAY routine so I could put the sounds in. I have something working now which uses DirectSound, but it uses SlimDX and I want to remove any 3rd party DX wrapper (no SlimDX/MDX). There's only a handful of DX calls, but doing them without unsafe code requires a ton of wrapper code. – Tergiver Oct 15 '10 at 00:30
  • @Tergiver: [Done.](http://csnibbles.codeplex.com/) (I haven’t copied all of your code — the character code changes weren’t necessary. I’ve implemented my own rudimentary version of PLAY, which doesn’t really work very well — feel free to replace it completely if you like.) – Timwi Oct 15 '10 at 08:55
  • @Timwi: Nice. I made several issue reports. These notes are primarily for myself as I looked it over, but can't work on it right now. – Tergiver Oct 15 '10 at 13:53
  • @Tergiver: Are you working on those issues you submitted? A friend of mine is strongly suggesting that we move this to BitBucket which is supposedly better, what do you think? — Also, if you would like to get in contact so we don’t have to use this StackOverflow thread, please contact me on Skype (timwiterby), thanks! – Timwi Oct 18 '10 at 19:35
  • @Timwi: Yes, check Source Code > Patches. There is where all the fixes are. (I knew that patches section was too out of the way to see). – Tergiver Oct 18 '10 at 20:57
  • awesome. modifying this game to get endlessly growing snakes that would ultimately die from exhausted level space must have been one of my first programming experiences ever. and funny, that the emulation for QBASIC's `PLAY` instruction takes up more C# source than everything else. – Cee McSharpface Feb 09 '17 at 21:46