Exploring Truchet Tiles on the ZX81 with BASIC


Exploring Truchet tiles on the ZX81 using BASIC and a bit of clever screen control. A fun little animation project with surprising results.

Sometimes ideas drop right into your lap. In the case of this month’s program, it was a mix of a post and a video that led me to this Truchet animation. A simple concept, but it worked way better on the ZX81 than I expected.

# So those diagonal lines were Truchet tiles.

I’d read and written here before about the single-line “10 PRINT” program. What I didn’t realize was that the diagonal lines used in the Commodore version were actually the Labyrinth variation of Truchet tiles. The video I watched opened my eyes, and I was intrigued to do something similar—but with my own twist!

Since the ZX81 doesn’t natively support user-defined graphics (UDGs), I took a different approach. This is where the BASIC program that was shared sparked an idea. Instead of a single character like before, I decided to use a 2x2 tile. This gave me a bit more flexibility… but only a little. The ZX81’s character set is still pretty limited.

# I kept the code simple and fast.

Using the quarter-circle form of Truchet decoration, I landed on using two different tiles. In real life, it would just be a single tile rotated. I thought of a couple different ways to print the design, but ultimately went with the straightforward approach: generate a random number and use a conditional. Here’s the rough code used:

LET R=INT (RND*2)
IF R=0 THEN PRINT AT X,Y;”/ “;AT X+1,Y;” /”;
IF R=1 THEN PRINT AT X,Y;” ";AT X+1,Y;”\ “;

I used the ZX81’s graphics characters instead of the slashes, but you get the idea. The rest of the program is a loop that fills the display with tiles. I would’ve preferred more circular graphics, but had to work with what was available. To my surprise, the patterns looked really good.

Truchet, ZX81 Screenshot, 2025 by Steven ReidTruchet, ZX81 Screenshot, 2025 by Steven Reid

# Why those last two lines had to go.

Unlike some of my previous programs that scrolled the display, this one redraws the entire screen with new random tiles each time through the loop. The result is an ever-changing pattern, an idea I borrowed from the example I saw. I hadn’t tried that before, and it worked well with the simplicity of the design.

The two blank lines at the bottom of the screen bugged me. There is a way to remove them, but I usually don’t bother. This time, though, filling the screen felt important. I considered poking the display memory with tile characters, but discarded that idea—it would have required math that would slow things down.

Fortunately, there’s another way.

On the ZX81, there’s a system variable that controls how many lines are reserved at the bottom of the screen. Setting that variable to zero with POKE 16418,0 effectively expands the vertical display by two lines. I hardly ever use this trick in BASIC since it can cause issues with scrolling. But in this case, scrolling wasn’t a problem.

This solution was elegant: the only change I had to make was adjusting the FOR loop range. I ran a few tests to see if breaking out of the program would crash the system. It didn’t—and I decided it was worth using. This trick has been around since I was a teenager and shows up in a few of my old books.

# Wrapping up with more tile ideas.

And with that, I had a very simple tile program. I did try a few other designs just to see what worked. It’s not overly hard to add more tiles—I even made a version with four—but the ZX81’s limited character set didn’t play nicely with most of them. Feel free to try your own variations!

As mentioned earlier, I could’ve also used an array to avoid the IF conditionals. I didn’t go that route, but I’ll leave it as a challenge for someone else.

Now, something I might do is convert this into a machine language version. Not only would it run faster, but I could use it inside a larger, more complex game. Interestingly, I learned after the fact that a few strategy games have used these same quarter-circle Truchet tiles. The patterns also seem like they’d make great backgrounds for other games.

I’ll end this one with more ideas running through my head. Let me know what you’d like to see me build next!

Want to try it out? You can run the program, or view the code if you’d like to see how it works.



Comments on this article:

No comments so far.

Write a comment:

Type The Letters You See.
[captcha image][captcha image][captcha image][captcha image][captcha image][captcha image]
not case sensitive