Weekend Projects for Armchair Developers:
TI Calculator Game

February 24th, 2006 by Johnny

[Editor’s Note: This article was written by Johnny as his first contribution to VC&G.]

If you’re a middle-class American between the ages of 14 and 28, chances are high that you or someone you know has access to a Texas Instruments TI-81 graphing calculator or one of its predecessors. I myself own a top-of-the-line TI-84 Silver Edition. And, not too long ago, I learned about the secret underground world that is…TI-BASIC.

To the average High School- or College-level student, the PRGM key is one that goes woefully unused (unless in conjunction with the 2ND key, so as to draw inappropriate images for your friends, or in conjunction with the ALPHA key, to use the letter ‘C’ in inappropriate messages for your friends). Pushing it reveals an esoteric “EXEC – EDIT – NEW” interface, and, really, who needs homebrewed Prgms when you’ve got MirageOS and a rousing game of Tetris?

But it’s this under-appreciated button that harbors the secret portal into the high-stakes hobby of amateur game design. And it will be this button that is the subject of Vintage Computing and Gaming’s very first article in a series of Weekend Projects for Armchair Developers.

…It’s a cumbersome title — I’m no expert at snappy one-lining. I also can’t claim to be an expert at programming. I’m just someone who enjoys his hobby very much. And this article is aimed squarely at people like me.

TI-BASIC won’t get you very far in life. But it’s extremely easy-to-learn, flexible, and a great time-killer. And that’s what WPfADs is all about: With it, I aim to put the “fun” back into “computer-coding”. [So that it becomes “comfunputer-coding” –Ed.] As such, each edition of WPfADs will feature a crash-course in forgotten and/or largely ignored computer languages that are not particularly useful, but a joy to tinker with.

Today we’ll be making a intermediately-complex game, one that I have painstakingly ensured will work on the widest range of TI calculators possible, and have tried to make accessible to a wide range of ability groups. If you own anything between Texas Instruments’ earliest TI-81 and the current TI-84 Plus, you’ll be able to follow along. While making the program, the biggest obstacle was the tiny memory constraints of the earlier models. Working around hardware limitations, though, can simultaneously be one of the most frustrating and exciting/rewarding parts of programming. And it’s part of what gives TI-BASIC some of its charm, I think.

The game is a maze-navigating affair that I’ve been working intermittently on for the past week, and is in as much of a first-person perspective as the calculator’s graphics can allow. For convenience’s sake, I’ve divided it into three parts. Assemble everything in the order presented, and you should have a working game by the end.

Part I: The Main Game Program
Download the Complete Reference Here (.txt file)

The first part of making any program is pressing the PRGM key, followed by the two taps of the right arrow, and culminating with a decisive push of the ENTER key. The name you give your new game doesn’t really matter, but be forewarned that you won’t be able to change the filename later on.

Once I had decided on a name (“LABYRIN“), I punched a few key lines of code:

:ClrDraw
:CoordOff
:GridOff
:AxesOff
:ZStandard
:Full
:

It’s more or less essential that every program start with this. All pictures are displayed in the calculator’s graph area. ClrDraw clears the graph area of any residual equations and inappropriate images for your friends that may be there. CoordOff, GridOff, and AxesOff turn off the clutter of the grid lines and such that normally is seen on the graph area. ZStandard changes the scope and zoom of the graph, so our images appear correctly, and Full is the default setting in which the graph takes up the entire screen, stuck in just in case. If you have trouble finding these commands on your calculator, I’ve stuck an appendix at the end of this article for consultation.

:3->A:3->B
:0->I:0->O
:0->H:0->V
:

Next came variable declaration. On a calculator, all the variables are double values (that is, they store information before and after the decimal point), and they stick around even after the program is finished executing, until you redeclare their value in another program. In this particular program, ‘A’ is the player’s horizontal coordinate in the labyrinth (You can’t use ‘X’ in the default function mode), ‘B’ is the player’s vertical coordinate in the labyrinth (There aren’t any restrictions on the ‘Y’ variable, but B makes more thematic sense), ‘I’ and ‘O’ are used for input and output in the LBRITHM program (detailed later), and ‘H’ and ‘V’ are a player’s horizontal and vertical movement through the maze.
Also, please note that “->” is meant to represent the STO arrow icon.

:Lbl 0
:prgmLBPICS
:

The LBPICS program will draw and refresh the room the player is currently in, and is also detailed later. It’s not necessary to make this function a second program, but it’s much more convenient and clutter-free this way.

:Lbl 1
:0->I
:
:Repeat I
:getKey->I
:End
:
:0->H:0->V
:
:If I=25:Then
:1->I:-1->V
:End
:
:If I=34:Then
:2->I:1->V
:End
:
:If I=24:Then
:3->I:-1->H
:End
:
:If I=26:Then
:4->I:1->H
:End
:
:If I=22
:Goto 2
:

This chunk of code takes input from the player and assigns values to variables for use by the LBRITHM program a bit later. The player moves with the arrow keys. If the player presses the QUIT button, the program exits out.

Input on the TI-81 and its brethren is very simple. After assigning the value “getKey” to I, I becomes a value corresponding to the button pressed on the calculator, using an easy-to-understand numbering system:

It’s important to put the getKey command inside a loop, using the Repeat command. Otherwise, the window of opportunity for the player’s input would be less than a second.

One more thing: The calculator uses a different symbol for negative numbers and for subtracting. Make sure you use the smaller dash under the ‘3’ button when denoting negative numbers.

:prgmLBRITHM
:0->I
:If O=1:Then
:A+V->A:B+H->B:Goto 0
:End
:
:Goto 1
:

The LBRITHM (short for Labyrin Algorithm…I’m rather clever) program is essential in compressing the memory requirements of this game down to as small a space as possible. Information about the labyrinth is stored in a matrix, each room represented by a four-digit decimal number between 0 and 1, and the Labyrin Algorithm turns those numbers into something meaningful. It’s kind of hard to explain, we’ll talk about it when we code LBRITHM.

:Lbl 2
:ClrDraw
:ClrHome

When exiting a program, it’s usually a good idea to clear the calculator’s display, to prevent confusion and have a generally cleaner presentation.

We’ve finished coding the meat of the program. Now, it’s time to make the helper program that draws the image of the labyrinth room on the calculator. To save a lot of memory, and make the organization of the game’s resources much cleaner and more convenient for the user, the room is drawn using individual Line() and Pt-On() commands (as opposed to storing and recalling actual Pic files). This has the adverse effect, however, of dropping speed a bit. Whether or not this is a fair trade-off is entirely your call.

Although, keep in mind that if you would rather use Pic files, I think you’re a dope. [Insulting the audience is not a good idea. I think they have guns. –Ed.]

Part II: Graphics
Download the Complete Reference Here (.txt file)

Make a new program and name it whatever you’d like. Mine is named “LBPICS” (Labyrin Pictures…you get it?).

:”Draws a room”
:”and its exits”
:
:”Empty Room”
:Line(-10,-8,10,-8)
:Line(-10,-8,-5,-1)
:Line(10,-8,5,-1)
:Line(-5,-1,5,-1)
:Line(-5,-1,-5,10)
:Line(5,-1,5,10)
:

Words in quotes are the closest thing TI-BASIC has to comments. There’s probably a cleaner and more efficient way to do it, but this method works and I haven’t noticed any problems.

When writing out the Line commands, your code won’t look as clean as this. Each line of code will be so long that it stretches over into the next, and you’ll end up with a lot of white space. This has no effect on program performance.

:1->I:prgmLBRITHM
:
:If O≠1
:Goto 1
:

“=/=” in the text file listings is supposed to be the “does not equal” sign (≠), from the TEST menu. Also, I didn’t mention this earlier, but if you want to put two commands on the same line, you must separate them with a “:”

:”North Door”
:Line(-2,-1,-2,7)
:Line(2,-1,2,7)
:Line(-2,7,2,7)
:
:Lbl 1
:
:2->I:prgmLBRITHM
:
:If O≠1
:Goto 2
:

If LBRITHM doesn’t return 1 (meaning there’s no door in that direction), the program skips drawing the door and tries the next one.

:”South Door”
:Pt-On(.4,-5)
:Pt-On(-.4,-5)
:Pt-On(1,-5)
:Pt-On(-1,-5)
:Pt-On(1.25,-5.5)
:Pt-On(-1.25, -5.5)
:Pt-On(1.5, -6)
:Pt-On(-1.5, -6)
:Pt-On(1.75, -7)
:Pt-On(-1.75,-7)
:

A door to the south looks like a bunch of dots systematically spread on the floor. I tried to simulate the effect of light drifting lazily in from a open doorway behind the player. Feel free to change this segment to your own liking.

:Lbl 2
:
:3->I:prgmLBRITHM
:
:If O≠1
:Goto 3
:
:”East Door”
:Line(8,-4.75,8,3.25)
:Line(8,3.25,6,6)
:Line(6,-2,6,6)
:
:Lbl 3
:
:4->I:prgmLBRITHM
:
:If O≠1
:Goto 4
:
:”West Door”
:Line(-8,-4.75,-8,3.25)
:Line(-8,3.25,6,6)
:Line(-6,-2,-6,6)
:
:Lbl 4
:

That wraps up the LBPICS prgm. All that’s left is the very short LBRITHM program, and we’ll have a working game.

Part III: LBRITHM

This small program evolved from the need to compress the memory usage of this game to a small enough space that the TI-81 could handle. Previously, I had stored data about the labyrinth’s rooms in a gargantuan 90 x 4 matrix. Each value would represent whether or not a doorway existed in that room. Needless to say, this system was painfully inefficient, a memory hog to boot.

The new system stores each room’s data into one decimal number, up to four digits long. The first digit is the north door (0 means no doorway, 1 means there is a doorway), the second digit is the south door, third is east, and fourth is west.

The LBRITHM takes the variable ‘I’ as input, specifying which digit to look at, and outputs whether or not that digit is a zero or a one. You may have noticed I’m saddling variable I with two different uses (taking key input from the player as well as using it in the LBRITHM program). This isn’t recommended unless you’re absolutely certain the program won’t blow up on you because of it.

:[A](A,B)->O
:
:While I>0
:10*(O-int(O))->O
:
:I-1->I
:End
:int(O)->O

…That’s the entire program. [A] is the name of the matrix I used to store all the room values. Coding the matrix is a bit tricky. Each value you enter into your matrix represents a room, formatted in the way described above, and each value has a set of coordinates on the matrix grid. For example, if you entered this as your matrix:

You’d end up with this a maze that looks sorta like this:

Hopefully your maze will be quite a bit more imaginative than that, but this example should give you a rough approximation of how to construct your matrix.

Once you work out your matrix, you’re done. Test out your program by executing your main program. If you’ve followed the instructions exactly, your program should work fine.

You’re free to tinker with this program, and, in fact, I encourage it. You’ve probably noticed there’s no way to win or lose the game. Try programming an exit to the maze. Or perhaps add some kind of wandering monster that the player must avoid, like a minotaur or a burlesque St. Bernard that walks like a man and breathes poisonous gas.

If you’d read this far, I’d also love to hear some constructive feedback on the readability/usefulness of this article, so that future ones may be made better.

Appendix: Where to Find the Commands Used in This Program

ClrDraw Draw Menu (2ND / PRGM / 1)
CoordOff Format Menu (2ND / ZOOM)
GridOff Format Menu (2ND / ZOOM)
AxesOff Format Menu (2ND / ZOOM)
ZStandard Zoom Menu (ZOOM / 6)
Full Mode Menu (MODE)
Lbl Program Menu (PRGM / 9)
prgm Program Menu (PRGM / ALPHA / D)
Repeat Program Menu (PRGM / 6)
getKey Input/Output Menu (PRGM)
End Program Menu (PRGM / 7)
If Program Menu (PRGM / 1)
Then Program Menu (PRGM / 2)
Goto Program Menu (PRGM / 0)
Line( Draw Menu (2ND / PRGM / 2)
Pt-On( Points Menu (2ND / PRGM)
[A] Matrix Menu (2ND / X^-1 / 1)
While Program Menu (PRGM – 5)
int( Number Menu (MATH)


17 Responses to “Weekend Projects for Armchair Developers:
TI Calculator Game”

  1. MegaKitsune Says:

    Or you could just get a cable and download the Wolfenstein cart… or get a GBA Micro and DOOM…

  2. Goz Says:

    Obviously someone misses the point…

    This is pretty cool, too bad to many people are just consumers and lack the desire for actually creating something.

  3. MegaKitsune Says:

    It’s not that I don’t like creating things…and it’s not that I don’t realize the coolness factor of this. I just don’t see what good a maze game is without monsters…

  4. Fleeno Jackson Says:

    Well if you like creating things, then why don’t you create some monsters?

  5. LSK Says:

    Ah, yes, gotta love TI-Basic. Underused programming language. The need for optimization is the charm.

  6. Johnny Says:

    Actually, while writing this article, I was working on a version that included monsters, but deemed it too “rough” to publish. The code was excessively large, too confusing to follow, and wasn’t nearly as efficient as it eventually could be.

    And I completely agree with everything LSK said. TI-Basic’s limitations are, in a strange way, definitely part of what makes it a “fun” language to code for.

  7. timekeeper Says:

    the thing thats great about Ti -Basic is that when you get bored in math class you can make it at least SEEM like your doing something 🙂

  8. RedWolf Says:

    Shh…don’t tell anybody, but I used to write programs for my TI-81 to solve my math, chemistry, and physics problems for me in high school and college. It saved me a lot of work. 🙂

  9. Lord_Richard_2@yahoo Says:

    I think that Ti-BASIC is somewhat slow, but it is great for non computer access in making games or programs. You also don’t have to worry about alot of formatting with other computer languages. I’m gad of that, and Assembly language (ASM) can be somewhat compressed, and can do a lot more, but it requires an OS, i use MirageOS.

  10. asdf Says:

    Knowing C++ and C+/phb programming really helps. I’m actually working on making a movie with my TI-84+ and it’s going fairly well. I’ve gotten it 18 minutes long but eventually i’m hoping to get it at least an hour!

  11. kevin Says:

    yeah, i love making my own ti-84 programs.

    right now im making a text adventure based off the old “hitch-hiker’s guide to the galaxy” DOS game

    great time-waster in physics class

    🙂

  12. Sethbeastalan Says:

    Some of your graphics code is bugged. You put.
    “West Door”
    :Line(-8,-4.75,-8,3.25)
    :Line(-8,3.25,6,6)
    :Line(-6,-2,-6,6)
    Where it should be
    West Door”
    :Line(-8,-4.75,-8,3.25)
    :Line(-8,3.25,-6,6)
    :Line(-6,-2,-6,6)
    Just mentioning because it might confuse people that just want the game and don’t know much about code.

  13. SouthPark_calc.guru Says:

    this isnt a really big deal but whatever i will comment anyway- (first paragraph):
    “If you’re a middle-class American between the ages of 14 and 28, chances are high that you or someone you know has access to a Texas Instruments TI-81 graphing calculator or one of its predecessors.”

    … the TI-81 graphing calculator had no predecessors. I think the word you are looking for is successor?

  14. Some Guy Says:

    How do you actually run this? I went to PRGM, then click the program under “EXEC” and it just said “prgm____”

  15. Alice Says:

    The T1-81 predecessor is the TI-*0, right? Tell me if I’m wrong.

  16. al h Says:

    What about the people who don’t have TI calculators (like me)? Could there be versions for other calculators (such as the Casio fx-9860G, Casio ClassPad or Sharp EL-9900 )?

  17. ghest1138 Says:

    This does not work on a TI-81. It does’nt have getKey, or Return. However, you can use a program called Unity to program in assembly for TI-81.

Leave a Reply