R.T.Russell Home Page

Chapter 18 - User Defined Characters




BBC BASIC allows you to make your own graphic characters and this section shows you how. It assumes you know a little about binary numbers. If you don't, there's an appendix you can go and read. Return here when you're comfortable with base 2.

Every character is represented by a grid of 8 by 8 bits. This is how we would represent a letter 'A'.

This does depend on which character set or font you are using, but I'm sure you get the idea. By treating each column as a bit in an 8-bit byte, we get the following representation by adding the value of each column that is set. Row 0 is 32+16+8+4=60.

A

By redefining these numbers, we can alter which bits are set in which rows and hence draw our own characters. We could make our very own alien like this:

BBC BASIC allows us to redefine the characters in the range 32 to 255. Usually, it's better to stick to ones that don't interfere with the ordinary characters below 128 and most programs start around 200. To redefine a character, we use a VDU command. There are dozens of these commands, have a look in the help to see what they can do for you. The one we are concerned with here is number 23. This allows us to redefine a character (amongst other things). We do it like this:

VDU 23,CharNumber,Row0Total, ... ,Row7Total
Applying to our alien, we get:
VDU 23,240,153,189,219,126,36,60,36,36
Let's try this in a program:
REM Making aliens
MODE 6
PRINT "Before adjustment ";CHR$(240)
VDU 23,240,153,189,219,126,36,60,36,36
PRINT "After adjustment ";CHR$(240)
END
In the default mode, the new character comes out very small, so I've used MODE 6 which makes the characters larger so we can see them better.

Obviously, you can apply different colours to our alien, because it is just another text character as far as BASIC is concerned. Now we've got our character, we can make a simple animation sequence. By printing the character, erasing it by printing space and reprinting in a different position, we can make our alien walk across the screen.

REM Walking alien
MODE 6
VDU 23,240,153,189,219,126,36,60,36,36
PRINT TAB(0,10);CHR$(240)
FOR I%=1 TO 19
  PRINT TAB(I%-1,10);" "
  PRINT TAB(I%,10);CHR$(240)
  WAIT 25
NEXT I%
END
The WAIT instruction is a small delay, adjust to taste, to prevent the whole thing being rendered in Flic-o-vision.

User defined characters have endless uses, not just in games, but for lots of general purpose programs where you want a character that is not available in the standard character set.

The cursor can be a distraction at times, so it's nice to be able to turn it off and on at will. Inspection of the help for VDU 23 reveals that it can do this for us if we call it like this:

VDU 23,1,0;0;0;0; : REM Disable cursor
VDU 23,1,1;0;0;0; : REM Enable cursor
However, because this such a common requirement, BBC BASIC allows using OFF to switch off the cursor and ON to put it back again.

We can apply this to our walking alien program.

REM Walking alien
MODE 6
OFF
VDU 23,240,153,189,219,126,36,60,36,36
PRINT TAB(0,10);CHR$(240)
FOR I%=1 TO 19
  PRINT TAB(I%-1,10);" "
  PRINT TAB(I%,10);CHR$(240)
  WAIT 25
NEXT I%
ON
END
Wasn't that better? As with the text colours, it's usually considered good manners to restore the cursor before we leave the program.

Tip:  Larger user defined characters
 8 * 8 not big enough for you? There's nothing to stop you defining several characters, each containing a segment of the overall picture them building a string with the larger picture in it like this:
REM Composite Characters
MODE 6
OFF
VDU 23,240,58,69,130,137,149,73,34,28
VDU 23,241,92,162,65,145,169,146,68,56
Butterfly$=CHR$(240)+CHR$(241)
REM Print 20 butterflies at random positions
FOR I%= 1 TO 20
  PRINT TAB(RND(30+5),RND(15+5));Butterfly$
  WAIT 25
NEXT I%
ON
END

Exercises

1) Make the alien walk back across the screen, right to left when it has reached the right-hand side.
2) Create another alien with its arms pointing down using character 241. Modify the animation from 1) to alternate aliens as it moves across the screen.

I% MOD 2
will tell you if I% is an odd or even number.

Left CONTENTS

CHAPTER 19 Right


Best viewed with Any Browser Valid HTML 4.0!
© Peter Nairn 2006