Musings of a Fondue

Games for the Computer

I hope to run some of the games for the PICO-8 fantasy console on my Homebrew Computer. For example,


The developers have spent a lot of time writing the code, designing the graphics and gameplay, and creating the music to make the games beautiful and fun. While they are not as CPU/GPU intensive as your typical AAA titles, it would be quite an achievement if my computer can run them at a decent frame rate!

To run the games, I need to write code that implements the behaviour specified in PICO-8’s API. In addition, I need to write libraries that implement the following:

  • dictionaries
  • random number generation
  • floating point arithmetic (can also be done via hardware)
  • trigonometry
  • pixel transparency (used by some games like Pico Fox)

I am focusing on running the graphics portion of the games. While the sound portion is also a worthwhile project (requiring both hardware and software additions), it is one for another day.

As usual, Khan Academy’s live editor is my goto prototyping environment. If I can get a PICO-8 game running there, I can get it running on my Homebrew Computer.

I chose to try porting the game Rambo by movaAX13h. It uses a lot of PICO-8’s API, yet is still simple enough that I can understand what is going on and track down problematic code.

Luckily Lua and JavaScript are very similar languages. As such, translating the game’s code (written in Lua) to JavaScript was relatively straightforward. The only major gotcha I encountered was Lua’s use of ‘one-indexed’ lists instead of the standard ‘zero-indexing’.

Most of the time was spent implementing the functions specified by the API. Getting the exact behaviour was tricky as some of the important details were missing from the API. Luckily, the PICO-8 community is vibrant and as such has many great information resources. The Wikia page and BBS were particularly useful.

In the Khan Academy environment, you can only detect one keypress at a time. This is problematic, since many games for the PICO-8 assume that multiple keys can be pressed simultaneously. In the case of Rambo, navigating your inventory requires you to hold down one key (which toggles an ‘inventory selection mode’) while using the arrow keys to select the desired item. Also when moving your character around, you can combine arrow keys to move in diagonals (e.g. towards the bottom left by holding the left and down arrow keys).

I tweaked the game’s code slightly to work around this shortcoming, but it is still evident - especially when you find yourself getting chased by police (who can come diagonally towards you, while you can only run away in one direction at a time). However, this shortcoming is in my favour as the Homebrew Computer (at the moment) can also only detect one keypress at a time.

After a lot of debugging and iteration, I finally succeeded in porting the game! It runs slow as molasses on the Khan Academy environment (likely due to the periodic error checking). As such, I’ve included a standalone version below that is slightly more responsive and playable.

The game requires a keyboard - the controls are z and arrows. Click the image to play:

Though I’ve sorted out the API, I still need to implement the libraries mentioned at the start of this post to get the game running on the Homebrew Computer. I’ll post a follow-up article when I manage to do so.

Code

Comments