Computer Science · Graphics & Games

ASCII: Text That Became Games

Character Encoding Binary ASCII Art Roguelikes

A computer cannot store the letter A. It can only store numbers. ASCII is the 1963 agreement that says A is 65, B is 66, and so on for 128 characters. Programmers soon found two surprising uses for those characters. They used them as pixels to draw pictures, and they used them as symbols to build whole game worlds. Our showcase has projects that do both.

 Live: a 3D donut made of nothing but text 
  1. Letters are numbers
  2. The ASCII table
  3. Characters as pixels
  4. Characters as a map
  5. Our ASCII family tree
  6. Check yourself
Part 1

Every letter is secretly a number

Inside the computer, everything is made of bits, 0s and 1s. To store text, people had to agree on a character encoding: a table that gives every character a number. Before 1963, different companies used different tables, so a message typed on one machine could come out as nonsense on another.

ASCII fixed that. It uses 7 bits, and 7 bits can make 27 = 128 different patterns, so ASCII has exactly 128 characters, numbered 0 to 127. The number for a character is its code point.

Try it: type anything

Each character with its decimal, hexadecimal, and binary code
CharDecimalHexBinary (7 bits)

Key idea: the word "Hi" is not stored as letters. It is stored as 1001000 1101001. The screen draws the letter shapes only when a human needs to read them.
Part 2

The ASCII table has a plan

The 128 codes are not in random order. The designers grouped them so that simple math works on them. Click any character below to see its code.

  • 0–31 and 127: control characters. They print nothing. Code 10 means "new line," and code 7 rings a bell.
  • 48–57: the digits 0–9. Subtract 48 from the code to get the digit's value.
  • 65–90: A–Z.
  • 97–122: a–z, exactly 32 after each capital.
Click a character.
Why 32? 32 is 25, so it is a single bit. A is 1000001 and a is 1100001. Changing capitals to lowercase is just switching one bit on. Press the button above and watch the whole alphabet change case at once.

ASCII was built for English. Today computers use Unicode, which has room for every alphabet and every emoji. But Unicode's first 128 characters are exactly ASCII, so the table you just explored is still inside every phone and computer.

Part 3

Idea one: characters as pixels

Early printers and terminals could print only text. But look closely and you will notice that @ covers much more of its box than . does. From across the room, a page full of @ looks dark and a page full of . looks light. So a character can stand in for a shade of gray.

That works only in a monospace font, where every character has the same width, so the characters line up in a grid like pixels on a screen.

  1. Shrink the picture so each pixel becomes one character cell.
  2. Measure each pixel's luminance: 0.299·R + 0.587·G + 0.114·B.
  3. Pick a character from a character ramp, sorted from least ink to most ink. Dark pixels get a space, bright pixels get @.

Try it: shade a 3D shape with text

Shape
Ramp
64
 

Ramp in use:

Showcase link: ASCII City. ASCII City does exactly this, 60 times a second. It renders a real 3D city at a tiny 160 × 62 size, one pixel per character, then turns every pixel into one of 19 characters:  .'`,:;-~=+*!?%#&$@. It adds one trick this demo doesn't have: the brightness picks the character, and the pixel's hue tints its color.

The spinning donut is based on Andy Sloane's famous donut.c (2006), a C program whose own source code is shaped like a donut.

Part 4

Idea two: characters as a map

In 1980, two students at UC Santa Cruz, Michael Toy and Glenn Wichman, wanted to make an adventure game with a map, but their terminals could only show text. They used a new library called curses, written by Ken Arnold at UC Berkeley, which could place any character at any row and column. They called the game Rogue.

In Rogue, a character does not mean a shade. It means a thing. @ is you. # is a tunnel. . is floor. Capital letters are monsters: B for bat, Z for zombie. Rogue also created rules that games still borrow today:

  • Procedural generation: a brand-new dungeon every time you play.
  • Permadeath: if you die, the game is over. No saves.
  • Turn-based: monsters move only when you do, so you always have time to think.

Games built on these rules are called roguelikes. To build a whole one yourself, with seeds, monsters and dice math, go on to the Roguelike Builder.

Try it: a tiny Rogue

Click the map, then move with the arrow keys, WASD, or Rogue's own keys (h j k l). Walk into a monster to fight it. Find > to go deeper. Rooms are laid out Rogue's way: a 3 × 3 grid of rooms joined by tunnels.

Draw as
  

Try the "Draw as" buttons. The game doesn't change at all, only the picture does. The dungeon is really a grid of values, a tile map. Rogue drew it with characters because a terminal was all it had. That split between game data and how you draw it is the most important idea in this lesson.
Part 5

Our ASCII family tree

Three projects in our showcase grew out of the ideas above. Each one takes the grid of characters and does something different with it. Click a picture to enlarge it.

Side by side

Rogue (1980)ASCII CityCrawlspaceRetro Dungeon Quest
What a character meansA thing (@ = you)A brightnessA thing, on the map onlyA thing, stored as a number
The world is stored asA grid of squares3D shapesA grid of squaresA grid of numbers
What you see on screenCharactersCharacters3D stone + an ASCII-style mapPixel-art tiles
New level each play?YesYes, a new cityYesNo, hand-built
Part of this lessonPart 4Part 3Parts 3 and 4Part 4

The wider family

  1. 1963ASCII is published as an American standard.
  2. 1976–77Colossal Cave Adventure (Will Crowther, then Don Woods) is a game made entirely of sentences. You type GO NORTH and read what happens.
  3. 1980Rogue turns characters into a map. It spreads to universities with the BSD version of Unix.
  4. 1987NetHack, grown from Rogue's descendant Hack, is still being updated by volunteers today.
  5. 2006Dwarf Fortress (Tarn and Zach Adams) simulates entire civilizations using the 256-character IBM PC set, an extended ASCII.
  6. 2006Andy Sloane's donut.c shows that a 3D shape can be lit using nothing but text.
  7. TodayGames like Spelunky, Hades, and Slay the Spire are called roguelikes, even with no ASCII in sight. The rules survived after the characters were gone, just as they did in Crawlspace.

Build your own

Remix ASCII City

Swap in your own character ramp. What happens with emoji, or with only 01?

ASCII map for William's dungeon

Rewrite Retro Dungeon Quest's number map using #, ., +, and $. Is it easier to read?

Add a monster letter

Design a new Rogue monster. Pick its letter, its hit points, and how it moves.

Check yourself

Practice

EasyASCII uses 7 bits. How many different characters can it hold?

2 × 2 × 2 × 2 × 2 × 2 × 2.

EasyA is 65. What is the code for H?

Count: A B C D E F G H. H is 7 letters after A.

MediumD is 68. What is the code for lowercase d?

Lowercase is always 32 more than its capital.

MediumWhich character sits at code 64, the binary number 1000000? (It's the one Rogue used for the player.)

It comes right before A (65) in the table.

HardWith the ramp  .:-=+*#%@, which character does the brightest possible pixel get?

The ramp runs from least ink to most ink. Brightest means the end of the ramp.

HardCrawlspace shows 3D stone and Rogue shows #, yet they can use the same dungeon. What do they share: the picture, or the underlying ___?

Think about the "Draw as" buttons in Part 4. What stayed the same?