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.
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:
Applying to our alien, we get:
VDU 23,CharNumber,Row0Total, ... ,Row7Total
Let's try this in a program:
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.
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
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.
The WAIT instruction is a small delay, adjust to taste, to prevent the whole thing being rendered in Flic-o-vision.
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
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:
However, because this such a common requirement, BBC BASIC allows using OFF to switch off the cursor and ON to put it back again.
VDU 23,1,0;0;0;0; : REM Disable cursor VDU 23,1,1;0;0;0; : REM Enable cursor
We can apply this to our walking alien program.
Wasn't that better? As with the text colours, it's usually considered good manners to restore the cursor before we leave the 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
|Tip: Larger user defined characters|
* 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:
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.
will tell you if I% is an odd or even number.
I% MOD 2