Python Nibbles

In 1990, QBasic shipped standard with MS-DOS, and included a handful of example programs. One of these was Nibbles, a version of the "snake game" also seen on other popular and equally-capable operating systems, such as the TRS-80 and Nokia phones.

This Python version reflects the original QBasic code, and does not necessarily prescribe proper Python programming patterns. Comments in nibbles.py which are prefixed by ## are my own. Others are from the original code, although they have been updated to reflect the surrounding code and environment. (The notes near the top of the program detailing how to run the program or exit QBasic have been removed entirely.)

Troubleshooting / known bugs

ImportError: No module named _curses

Most likely: you are using Windows. Ironically, this won't run in Windows. Please note that the most commonly suggested drop-in replacement for the curses module under Windows (UniCurses) is licensed under the GPL version 3, which may be incompatible with the license used here.

If you are not running Windows, you will probably need to find out how to install the Python curses module for your distribution. Note that it "should" be shipped with Python, and if that is not the case, your distro is shipping broken packages. ;)

FileNotFoundError for /dev/dsp

If you encounter an error about /dev/dsp, and you are using Linux, try the following:

$ sudo modprobe snd_pcm_oss

If modprobe fails, or if you're using another operating system, I have no idea what you might need to do.

Font issues

Nibbles requires a window that is at least 80x25 characters, and a suitable font containing support for the Unicode block-drawing elements (namely characters U+2580 UPPER HALF BLOCK, U+2584 LOWER HALF BLOCK, and U+2588 FULL BLOCK). Here are some fonts which I have confirmed to support these characters, roughly in decreasing order of quality:

  • Fira Mono
  • Consolas
  • Lucida Console
  • Liberation Mono
  • Deja Vu Sans Mono
  • Source Code Pro
  • Nimbus Mono
  • Fixedsys Excelsior 3.01-L

Fonts that use placeholder glyphs, or worse, show no character at all:

  • OCR A Std / OCR A Extended / OCR B
  • Anonymous Pro
  • Droid Sans Mono
  • Ubuntu Mono
  • Terminus
  • Inconsolata
  • Luxi Mono
  • Courier
  • TopazPlus (That's an Amiga font, not DOS, you silly!)

Some Linux distributions, unfortunately, use some of the fonts in the latter list as either the default monospace font or as a recommended terminal font. Please make better font choices!

Bottom-right corner issues

If your terminal is exactly 80x25 characters, the bottom-right position will never be filled due to scrolling issues with curses. This is intended. While it's sometimes possible to "sneak" a character there by combining insch() and addch() (and possibly also inch()), Python's curses wrapper seems not to support ins_wch() or in_wch(), so this technique won't work here.

Sound issues

The audio library (Mammal) is a bit of a hack that I did in a hurry, and notes are sometimes a little bit choppy. This shouldn't interfere with gameplay, though.

Unimplemented behavior

Since QBasic had no readily-accessible timer functions, programs typically used busy-wait loops to delay execution. Nibbles had a machine speed test which amusingly crashed with a division by zero if the CPU was too fast. This version does not replicate that feature. :)

Challenges for new programmers

I learned a lot from QBasic, and specifically, Nibbles. You can, too! Try a few of the following:

  • Change the names of the snakes
  • Change the colors
  • Add more color schemes selectable at the start of the game, in place of the current "monochrome" and "color"
  • Make the snake colors change depending on what direction you're moving
  • Make each "piece" of the snake randomly colored (a bit more difficult)
  • Pause the game with spacebar as well as the 'p' key
  • Add a key that exits the game, without having to lose all of your lives
  • ... with a yes/no prompt first (hint: look at StillWantsToPlay())
  • Change the "sparkles" on the title screen to spell out N I B B L E S instead of asterisks
  • Give a bigger score bonus for getting to points faster
  • Add a high score table and list it on startup
  • Add an "attract mode" showing a level being played if the title screen is up for more than a few seconds
  • Add more levels
  • Introduce "portals" along the edges of the play field that warp the snake to the other side
  • Change the sound effects
  • Add more sound effects (maybe a little blip when the snake moves, a different sort of blip when it changes direction, a little flourish when starting a level...)

Author and copyright

nibbles.py is a nearly line-by-line translation of NIBBLES.BAS which was bundled with QBasic in MS-DOS 5.0 and greater.

NIBBLES.BAS contains the following copyright notice:

Copyright (C) Microsoft Corporation 1990

I definitely do not purport to have written Nibbles, nor do I claim any rights to the code; I am merely responsible for bringing it to Python.

The other two files, basic.py and mammal.py, are entirely of my own creation; these I have placed under a BSD license for flexibility.

-- Star Taylor -- star@startaylor.net -- http://startaylor.net/