Hungry Dog Roams the ZX81 Screen
A hungry dog takes over the ZX81 screen while a simple PRINT AT routine preserves a changing message behind the image.
For this program, I wanted to display a hungry dog over a message filling the ZX81 screen. Unlike most of my image programs, the text remains visible behind the dog, almost as though the image has a transparent background.
# Filling the screen with text.
The hungry dog displayed over a message on the ZX81
My original plan was to use one of the image compression or plotting routines I have used in previous programs. That approach would not work well this time.
As with my MONO LISA program, I expand the display using POKE 16418,0. Unfortunately, that also prevents the normal PLOT command from working.
Even if plotting had worked, I did not want the dog to erase the message behind it. The goal was to treat the spaces surrounding the image as transparent, leaving the existing screen contents untouched.
Rather than getting too fancy, I fell back to using PRINT AT to draw only the visible parts of the dog.
# Making the message fit.
The ZX81 BASIC screen-filling routine
The part where I did get a little fancy was filling the screen with the message. Since the text can vary in length, I needed a routine that could repeat any string until the entire display was covered.
40 LET T=768
50 LET C=LEN M$
60 PRINT M$;
70 LET T=T-C
80 IF T>C THEN GOTO 60
90 PRINT M$( TO T);
The expanded display contains 768 character positions, so T starts at 768. The program stores the length of the message in C, prints the complete string, and subtracts its length from the available space.
As long as more than one complete copy will fit, the program returns to line 60 and prints it again. Once the display is nearly full, line 90 uses a string slice to print only the characters needed to fill the remaining space.
It is a small routine, but I like how elegantly it handles messages of different lengths. There is no need to pad the strings or adjust the routine for each one.
# Choosing a random message.
The ZX81 BASIC setup and random message selection
To add a little variety, I included five different messages. The program selects one using a random number and a calculated GOTO.
440 GOTO 500+INT (RND*4+.5)*20
The calculation produces one of five line numbers from 500 through 580, with each message beginning 20 lines after the previous one.
500 LET M$="AHUNGRYDOGBELIEVESINNOTHINGBUTMEAT"
510 RETURN
520 LET M$="MERCYISAWORDNOHUNGRYDOGUNDERSTANDS"
530 RETURN
540 LET M$="BEWARETHEDOGTHATHASFORGOTTENITSMASTER"
550 RETURN
560 LET M$="ASTARVINGDOGTRUSTSONLYITSTEETH"
570 RETURN
580 LET M$="HUNGERMAKESAFAITHFULDOGWILD"
590 RETURN
After selecting the message, the program returns to the screen-filling routine. The dog is then printed over the text one row at a time.
Because the strings used to draw the dog contain spaces around its visible parts, those spaces leave the existing screen contents untouched. This allows the message to show through without needing a separate transparency or masking routine.
The result is a simple effect, but it feels quite different from my other ZX81 image programs. Sometimes falling back to PRINT AT turns out to be exactly what the program needs.
Want to see it in action? You can run the program, or view the code if you’d like to see how it works.
[tags]zx81, retro, basic, graphics[/tags]