Acorn Programs


Bloobers

 
Published in Acorn Programs #7

Richard Wagner reveals the secrets of an entertaining graphics display

Bloobers

Bloobers is based on a program published in September, 1980 for the Pet. This version is for a BBC or Electron. It is the only computer program after which a cat has been named.

It is not a game, more an interactive graphics demonstration. A snake moves across the screen, searching continually for its beginning. The path it takes is selected at random; it remembers where it has been, so that a loop eventually is completed. You can press the SPACE bar to force the snake to untrace itself. On completion of a loop, the program stops for six seconds, untraces itself, waits again, and starts a new snake. The slithering of the snake has a musical accompaniment.

The program makes full use of the power of BBC Basic. The plotting procedure is recursive; that means that it calls itself. Remember that when a procedure, or function, is called, the current value of each of the parameters to the procedure is saved. That also happens to all variables mentioned when a LOCAL statement is encountered.

In Bloobers, each new plot of the snake is initiated by a call to PROCtrace. The parameters, which contain the information about the current snake position and which directions it has tried, are saved. That mechanism allows the program to keep track of its path, without having to save the path details explicitly in a series of arrays. The program will run out of memory in which to store its variables unless the screen size is restricted.

Lines 20, 30 and 40 contain variables whose initial values may be altered:

Mode is the screen mode to be used; it may be 0, 1, 2, 4 or 5. With modes 0, 1 and 2, the size of the screen must be reduced, unless you are using a second processor; with a Model A BBC micro, only modes 4 and 5 may be used.

Width is the width of the frame in which the snake appears.

Depth is the depth of the frame in which the snake appears.

Chan% is the sound channel; change it to &11 and the snake will move faster, as there will be no delay waiting for the previous note to complete.

Amp% is the amplitude of the sound; for some really exotic accompaniment, try defining an envelope and changing this to the envelope number.

Dur% is the duration of the sound; alter this to slow the snake.

The mode, width and depth values are validated in lines 100-120 and the program will halt if they do not contain valid and consistent values.

Other variables are x% which contains the x co-ordinate of the snake's position; y% contains the y co-ordinate of the snake's position; c% contains the snake character number - it is in the range 0 to 3; 251 is added to obtain the value of the character to be output; r% contains 0 or 1 - it determines which way the snake is twisting; t% indicates whether the second of the two possible directions in which the snake can move has yet been tried.

Those five variables are saved when PROCtrace is called and define completely the current state of the snake; ix% and iy% define the initial position of the snake and are used with ic% to determine whether the snake has met its beginning; loopclosed is set once that has occurred; xadj% and yadj% contain values to be added to x% and y% to determine the next snake position for all possible values of c% and r%; n% contains the pitch to be used when plotting each of the four characters used in constructing the snake. They are held in line 2590 and may be altered to change the tune.

PROCsetupvdu sets up character definitions for the frame 255 - and the snake 251-254. The four snake characters are quadrants of a circle, as indicated. You could try changing the definitions of the characters used:

PROCdrawframe draws the frame in which the snake appears. A window is set up so that the co-ordinates of the available area start at 1 for both axes. PROCsetupables initialises the arrays xadj%, yadj% and n%.

A complete trace and untrace of the snake is performed by PROCbloobers. That sets up the initial snake values and calls PROCtrace with those values as parameters. PROCtrace attempts to plot one step of the snake at co-ordinates x%, y% of the frame. It calls FNstoploop to see if it can. It cannot if the character position is already occupied; the snake has met its start point; the space bar is pressed.

PROCtrace immediately exits if FNstoploop returns the value TRUE. It prints the character defined by c% at the defined position and makes a little noise. Line 1240 is the meat of the program; the position to which it must then move is determined by the character last output - c% - and the direction of the twist - r%; arrays xadj% and yadj% are used to determine the new position.

The next character to be output may take one of two values. IF RND(2)=1 selects one at random, calling PROCtrace with the new plot position and a new value for c% and possibly r%; the values selected ensure that the snake is continuous. Line 1250 will not be executed until the snake returns to that position, though it may have been executed as a result of the recursive call at line 1240.

It will then have tried every possible path from that point or have reached the beginning of the snake successfully. At line 1250 the point is unplotted, with some lower-pitched noise. Line 1260 tries under the character not selected when the procedure was called at line 1240, if that has not yet been tried; whether it has been is indicated by t%. If both possibilities have been tried, the procedure exits, having tried every possibility from that point, or met the beginning of the snake.

FNstoploop determines whether the plotting is to continue. If the SPACE bar is pressed, or the snake has already met its beginning, the value TRUE is returned, meaning stop. If the character at the current position is a space, it exits with the value FALSE, meaning continue. That will prevent the snake not only over-writing itself but trying to leave the frame in which it is drawn. The remaining lines of the function check to see if the snake has met its beginning; if it has, loopclosed is set to ensure that TRUE is always returned by FNstoploop, forcing the snake to untrace itself. FNchar returns the ASCII code of the character at the defined co-ordinate.

Disc users should re-locate the program to address &E00 before running it unless they have a second processor.

Richard Warner