beanz Magazine

Fantasy Computers and TIC-80

Tilemahos Efthimiadis on Flickr

A fun introduction to programming games with fantasy computers. The 70s and 80s are back in style!

Retro gaming has been a big thing over the last few years: 8-and-16-bit style pixel art, chiptune music, and old-school gaming styles like platformers coming back in vogue.

What’s also coming back is the experience of working with the computers of the late 70s and early 80s. Computers like the VIC-20 and Commodore 64 were really popular during this time.

They were neat little machines that ran on operating systems and hardware that were simple enough that you could control almost everything about the computer—including directly programming graphics, sound, and manipulating system memory—with a version of Basic.

Now we have fantasy computers, which are programs that act like they’re a piece of hardware with its own memory, storage, and operating system. These fantasy computers act as an easy way to get started with games programming because you do everything in the fantasy computer itself: draw sprites, write the code, make the music, and create the sound effects. The first version of the puzzle-platformer Celeste started out as a fantasy computer game and based on its success there it’s been released on every major gaming system.

The most well-known fantasy computer is PICO-8, which is both polished and popular but costs $15 and is not open-source. Since I always like promoting open-source software whenever possible we’ll instead talk about TIC-80 which is not only free and open-source but it’s a fantasy computer I absolutely love.

First, you can download TIC-80 or use the in-browser version.

If you start playing with TIC-80 the first thing you’ll notice is that you’re dropped into a text prompt called a command line. If you’ve been following the CLI articles in both this issue and the previous one then you should be pretty comfortable here. Some of the commands you’re already familiar with such as mkdir, cp, cd, and ls are built in and they behave just like they do in OSX or Linux. If you haven’t, that’s okay too. We’ll talk about all the things you need to do to get started.

Now if this is your very first time running TIC-80 the first thing you’ll want to do is type demo in the command line and hit enter. This is going to put a bunch of cartridges, complete TIC-80 programs with graphics and sound, in your home directory. Type ls and hit enter to see them all.

Now type load fire and hit enter. Has anything happened? Well, yes, but not obviously. It’s loaded the cartridge into memory but you need to type run and hit enter in order for it to load. Go ahead and try that and, if everything’s working correctly, you’ll see something like:

Now hit ESC once to end the program and ESC again to enter the editor. This is where all the magic happens!

Here you can edit the program that’s loaded into memory, as well as change things like the color palette, the sprites that are in memory, the background map, sound effects, and music. You can switch between the different tabs by clicking on the icons in the upper-left corner or by pressing f1 through f5 on your keyboard. TIC-80 programs are written in a language called Lua, which we cover in more detail in a separate article this issue.

At this point, you might want to exit the assets editor by hitting ESC and try playing with some of the different demos. As of the time of this writing there’s an implementation of Tetris (though without music added yet), a little top-down adventure game called quest, and both the music and sfx demos show off the sound features. If you’re using a keyboard, like I am, you might notice that the keys that work by default are z, x , s, a and the arrow keys. That’s because TIC-80 is by default optimized for using a controller connected to your computer!

You can learn more about the keymap by going to this page of the TIC-80 documentation.

There’s a lot here to cover in TIC-80 programming, which we’ll cover in future articles, but to start we’ll cover a tiny complete example of the Lua code for a simple program that acts as an interactive bubble sort. Hold down the up key to watch the sorting happen!

local arr = {10, 3, 2, 8, 4, 5}
local index = 1
 
function TIC()
   cls()
   for k,v in ipairs(arr) do
      rect(20+ 10*k,80 - 5*v, 10, 5*v, 2) 
   end
   if btnp(0,20,20) then
      if arr[index] > arr[index+1] then
	 local temp = arr[index+1]
	 arr[index+1] = arr[index]
	 arr[index] = temp
      end
      index = index + 1
      if index == #arr then
	 index = 1
      end
   end
end

To explain what’s happening in this code we’ll break it down into steps:

  1. Creating variables for both the array we’re sorting and the index to keep track of where we are in sorting the array.
  2. We then declare the TIC function, which must be in every TIC-80 program. It functions as the main loop and it runs exactly 60 times per second.
  3. We clear the screen with the cls function, which we have to do so that we’re starting with a fresh canvas every frame drawn.
  4. Then we draw rectangles representing the entries in the array using the rect function. We give it the x-position and y-position of the upper-left corner of the rectangle and then the width, height, and color. The color is just an index into the palette. The palette is set on the same screen as where you draw the sprites! You can even import new palettes from a hex-string. Try this ’60s inspired color palette I like: f94c4efce211952c6e01d2b57cc671ff8726743b82ee2746f5eccffcb043640e130000003d5789c2449eda70751b9cf7
  5. We test to see if the up button is being pressed and, if it is, perform a single step of sorting.

You can read more about all the functions that make up TIC-80 at its extensive and well written documentation page. If you click on any of the entries under “API” you’ll get a description of what all the core functions in TIC-80 do. There’s only a couple dozen needed to make whatever game you want!

If you want to play around with coding you can copy and try to finish the paddle ball game I’ve started here.

Now, the last thing I want to talk about is that you can save, export, and import your games. Saving is done with the save command at the command line but this saves a cartridge to the fake file system TIC-80 uses. Exporting is how you get that file out of TIC-80. Now this gets into about the only weakness I’ve found in TIC-80.

Exporting and importing are a little weird right now, at least in the free version. You can export a playable version of your game, like the one I link to above, or you can export art assets. You can only import art assets and not whole games in the free version. That’s not too big of a problem, though, since you can import the art and then copy & paste the code from another game.

I hope this at least whet your appetite for retro games. I personally have been having a lot of fun with this and look forward to talking about it more.

Learn More

TIC-80 home page

https://tic.computer/

TIC-80 github

https://github.com/nesbox/TIC-80

In-browser TIC-80

https://tic.computer/create

TIC-80 tutorials and documentation

https://github.com/nesbox/TIC-80/wiki

LIKO-12: Another up-and-coming open source fantasy computer

https://github.com/RamiLego4Game/LIKO-12

PICO-8: The big one that started the trend

https://www.lexaloffle.com/pico-8.php

Also In The August 2018 Issue

Use SketchUp to recreate the famous optical illusion that grows and shrinks people around the room.

Learn what makes a good rogue-like game and how to make one yourself.

Unleash your inner digital chef through this simple (but mouth-watering) Python activity.

Dinosaur fossils, STEM podcasts, and a day in the life of a paleontologist.

A fun introduction to programming games with fantasy computers. The 70s and 80s are back in style!

Real life treasure hunts are a way to get outdoors, learn map skills, and have fun finding hidden caches near you.

Tips & trips to help you have a great time on the Internet, even when others don’t behave well.

How scientists finally cracked the code behind the mysterious language of Ancient Egypt.

Two small projects introducing you to Edublocks, a language that bridges the gap between Scratch and Python.

Take your CLI skills to the next level as you combine, redirect, and script commands.

It looks like JavaScript, has a Python aesthetic, and integrates easily with C/C++. Meet Lua: a scripting language for fantasy computers.

How the high-tech LIGO made a huge discovery and won its three founders a Nobel Prize.

An introduction to the devices that’ll make your robots zoom, skitter, and spin.

Learn the secrets behind pixels, image blurs, and all your favourite Instagram filters!

Links from the bottom of all the August 2018 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for August 2018.