Hello. When your BBC Micro arrived, I would imagine that the first thing that you did
was to read through the welcome booklet that came with it. And having read through that,
I expect you played all the programmes on the welcome cassette. These will have given
you a fair idea of just what the machine is capable of. Since then, you may have bought
other programmes, games perhaps, or business programmes. But sooner or later, you're going
to want to write your own programmes to do something that you're interested in. Obviously,
I can't tell you what to write programmes about. That will depend on you. But what I
can do during the next hour or so is set you on the right road as regards designing programmes
and writing them in BASIC. Now, before we start looking at the individual statements that make
up the BASIC language, I think it'll be useful if we spend just a couple of minutes looking at the
inside of the machine to see what goes on in there. And the best way to do that, I think,
will be on a piece of paper. Now, inside your machine is a large area of memory. Memory in
which information is stored. Now, some of this is available for you to use, and some of it is
already full. Let's divide it up like this. In part of the memory, there is what we call the
machine operating system. And this is a series of instructions whose job it is to control the
machine, to control your television display, to control your cassette recorder. Now, we also have
what's called the BASIC interpreter. And this is a programme whose job it is to understand the
BASIC statements that you write to make sure that they are correct and then to execute them.
And then in the rest of this storage, we have your programme. If we write your programme here,
remember these are the statements that you will write. We'll also require an area called the screen
memory. And into this part of the memory will go all the information that needs to be shown on the
television screen. Now, we don't have to be concerned too much at the moment about what's
happening in the machine operating system and what's happening in the BASIC interpreter. What
we're going to be doing is writing the BASIC statements to go into the programme area of the
memory. And those BASIC statements will, among other things, make things appear on the screen.
Now, let's look in a little more detail at what happens inside that programme area. Now, your
programme will be written as a series of lines of coding, as a series of statements. And every
one of those statements is going to be numbered. For example, we might number them 10, 20, 30,
40. You'll see in a moment why I haven't numbered them one, two, three, four, but why I've left
gaps between those numbers. Now, these numbers are very important because it's only by looking at
these numbers that the BASIC interpreter knows in which order these statements are to be used.
Here, we've got number 10 before number 20, before number 30, before number 40. Well, that's the
theory. Now, let's look to see what that's like on the screen. What you can see here is the first
six lines of a much larger programme. The programme is at the moment sitting in the computer's memory,
and I've asked it just to list the first six lines. And you can see the line numbers I was
talking about 10, 20, 30, 40, 50 and 60 at the beginning of each line. Now, the way I asked the
computer to list the programme is by using a list command. Down here on the left-hand side,
you can see the flashing cursor and you can see a little arrowhead. When you see these two together,
it means the computer is ready to accept a command. And the command I used was a list command,
simply L-I-S-T. List by itself would ask the computer to print out the whole programme on
the screen. Watch. As soon as I press the return key to show the end of the command,
every line in the programme was printed on the screen. And there were far too many of them to
be seen on the screen at the same time. And so the ones I was interested in were lost from the top
of the screen. If I only wanted to list a few of the lines in my programme, then I have to use a
slightly more complicated list statement. List followed by the first line, 10, a comma, and the
last line, 60. This command says, list out my programme starting at line 10 and finishing at
line 60. Now, when I press return at the end of the command, I get exactly those lines printed out
and no others. Now, the way they are at the moment, it's slightly difficult to read because
there's no space between the line number and the line of basic. I can ask the computer to insert
an extra space each time it lists out my programme by using another command, a list O command. List O
seven. Now, after I've pressed return, we still have the flashing cursor and the little arrowhead
to tell me it's ready for another command. But if I list again, I should get some extra spaces put
into my listing. Let's try it. List 10 to 60. And you see we now have a column of spaces following
the line number. Now, these line numbers, I said, are used by basic to decide in which order the
lines in your programme should occur. And we use these line numbers when we want to insert a new
line or change a line or delete a line from our programme. For example, suppose we want to insert
a new line of coding between line 10 and line 20. All we need to do is think of a number between
10 and 20. Let's say 15. So we type in a command starting with the line number. Let's suppose the
command will be print. Don't worry too much about what each of these commands does, because we'll
be going through them in later parts of this video session. Now, when I've entered that line
that says 15 print, this new line has been inserted into my programme between 10 and 20.
And if I list 10 to 60 again, we should see it in the right place. List 10 to 60. And as you see,
line 15 has been put correctly between 10 and 20. If we want to delete a line, then all we have to
do is type its line number. Let's suppose I want to delete line 10. By simply typing 10 and then
return, I've removed line 10 from the programme. List again, and it's gone. And if we want to
replace a line of coding with another line, then we can simply type in the new version of the line.
Let's suppose we want to replace line 50. We simply need to start with the line number,
50, and then type in whatever text we want to be in the line. This new line 50 has now replaced
the old line 50 in our programme. We can list again to confirm that it, in fact, has happened.
As you see, the old line 50 has gone, and we have our new one. Now, that's all very well if we want
to replace a line completely with brand new coding. But if we had to retype every line in
which we'd made a typing mistake, we'd have an awful lot of typing to do. And so, when we've
only got a small change to make in a line, we use the editing keys, which you see on the right hand
side of your keyboard. Now, let's see what happens when we use these arrow keys. If we push the up
arrow key once, you'll see the two things happen. Firstly, the flashing cursor has moved up one line,
and the space where it was has been replaced by a square white block. If we continue to push it up,
that flashing cursor line moves up a line each time. Similarly, with the down arrow key, pushing
it once moves it down one line, and it continues to move down the more we push it. The right arrow
key and the left arrow key move the cursor left and right. Notice that these keys, like all the
other keys on the keyboard, repeat automatically after half a second. Hold the key down, and the
cursor automatically goes to the right. Notice that it returns on the next line down. Similarly,
if we continue to go up, when it gets to the top of the screen, it comes back to the bottom. Now,
what's the use of this movable cursor? Well, let's suppose we wanted to change line 20. Instead of
saying 20 repeat, suppose we wanted to say 20 please repeat. Down here, the very bottom right
corner of the keyboard is a key marked copy, and this does exactly that. When we press it,
the character that was under the flashing cursor is copied to the line where our typing input
normally goes. Every time we press copy, one character is copied. As it stands, we've now
copied the whole of line 20 to a new line. If we pressed return then and listed the program,
of course you'd find that there was no difference. We've simply replaced line 20 with an identical
line 20. But if we reposition that copy cursor to the 20 again, press the copy key twice,
we can then type new information in. We wanted to say please. The new information that we've
typed goes onto this line as normal. The copy cursor remains where it was. And now, if we press
the copy key again, it will continue to copy the old line 20. Pressing return enters this as a new
line 20. And if we list the program again, we see that the old line 20 has been replaced by our new
changed version. Of course, this isn't basic. This is rubbish, which is a very important point,
that when we're typing in lines of basic, they don't have to be valid. We can type in anything,
English, French, or gibberish, because it's not checked to see if it's valid basic until we try
to use it as a program. So we've seen the use of the copy key. The one next to it is marked delete,
and that can be used to delete the character we've just typed. Let's suppose we were typing in a new
line. Let's suppose it was line 17, and we were going to say don't drop litter. But now we see
we've made a spelling mistake. Instead of saying drop, we've said D-R-O-O. Every time we press the
delete key, the last character entered is deleted. So we can delete back to where we made the mistake,
and then continue typing. Similarly, because the delete and copy keys are next to each other,
it makes it very easy to change a wrong letter. For example, let's suppose we were going to change
line 40. Instead of saying proc, we wanted to say prog. We've now copied the C from here to here,
but we don't want the C in our final answer. Pressing the delete key once deletes the C
from here. We can now type in the letter we wanted, G, and continue to copy. So the combination of
delete and copy allows us to edit the text that we're typing along this line. The cursor keys
allow us to move around the text that we've already typed to copy parts of it that we're going to need
again. What we've looked at in this first part of the videotape are the basic principles of putting
the program into the computer, making sure the lines are numbered, and editing the program to
make changes to it, inserting new lines and deleting lines. Now at the end of this part of
the course, as with all of them, the first thing you should do is look in your user guide at the
things we've discussed, because here you'll find them all described in full. And the other thing
you want to do is practice what we've talked about. So in order to give you a chance to practice
putting lines of coding in and modifying them, stop the tape now. When we come back, we'll start
work on our first complete program. Now there are many different types of program that you could
write, from simple question and answer programs up to arcade games where you spend all your time
killing aliens or perhaps digging holes for monsters to fall into. And some of the techniques
used in writing these programs will differ according to the type of program, but the majority of the
techniques are the same. We're going to start by writing a simple question and answer program that
does some calculations. We're going to write a program that, given the length of the box,
its depth and its height, will calculate the capacity of the box, its volume, and the total
surface area, but without a lid. Now if that's what the program is going to do, it's going to
have four basic stages. The first stage is going to be to show the questions that we want the user
to answer. The second stage is to accept his answers that he types in from the keyboard. The
third stage will be to do the calculations on the numbers he's typed in, and then when we've done
the calculations and we have the answers, and the fourth stage will be to show the results back to
the user. Now there's one facility that BBC Basic has which is very important when you're dividing
your program up into separate sections, and every well-designed program is divided up like this,
and this is the facility of procedures. Now in BBC Basic, a procedure is a separate section of
coding to perform a separate job within the program. The very first line of a procedure
defines the procedure, def proc name. Define the procedure, and then when I have name in small
letters, you can put the name of your procedure, whatever you decide to call it. At the end of the
procedure, we have a line that says end proc, and this tells basic that's the end of the procedure.
And when we want to use the procedure, when we want our program to perform the work that the
procedure does, we use a proc statement. We say proc followed by the name we gave the procedure.
Now that may seem a bit abstract, so let's look at a particular example. Now let's suppose that
during our program at a certain point, we want to clear the screen completely and put page heading
at the top. Now we could write the coding to do that into a procedure called proc title,
and line 50 in our program to use that procedure would simply say proc title. Somewhere else in
the program, we would define that procedure. There's our def proc statement, and there's
our end proc statement. The two lines of coding within that procedure are the work that's to be
done whenever we use the line proc title. In this case, we have two basic statements, CLS,
which clears the screen, and a print statement, which prints out the two words, page heading.
Now once we've defined that procedure, we can use it from anywhere we like in the program. For example,
at line 170, we might have another reference to proc title. Again, as soon as our program reached
this point, we'd come here and execute these two statements again. So finally, let's have a look
to see what proc title does. Well, it clears the screen and prints page heading at the top. Now our
program has four separate stages in it. We ask the user the questions, we take in his answers,
we do the calculations, and then we show him the results. So we'll have four procedures in our
program. Let's look at the first few lines of the coding. Here we have seven lines of basic,
four of those lines are proc statements, the ones we were discussing a moment ago. Let's just look
at these first three lines. Line 10 starts REM, which stands for a remark. Now this isn't really
part of the program. The basic interpreter won't try to do anything with a line like this, but it's
a very useful way of putting into your program a comment that'll help you to understand it,
or maybe help somebody else to understand it. I've put in a remark that says a box program.
Secondly, I set a mode. Now the BBC computer can run in several different modes. These affect the
number of characters you can get onto a line, the number of colors, the number of lines there
are on the screen. I've said this program is going to run in mode four, which means we'll have 40
characters across the screen and 32 lines vertically. Remember mode four will work on any size of BBC
computer. The next line, 30, doesn't do anything. That colon you see there is the character that
basic uses to separate one statement from another, but on this line we have no statements. It's very
useful however to have that on a line because it allows us to separate the different parts of the
program. It makes it easier for us to see what part starts where and where it finishes. So our
four procedures, PROC questions, PROC answers, PROC calculate, and PROC results. Well that's
defined the logic of our program as a series of four procedures, and now we can go ahead and write
the coding for each of those separate procedures. Let's start with PROC questions. Remember this is
the one that's going to display for the user the description of the information he has to enter,
and so that's going to contain a series of print statements. Here you see the coding for it,
DEF PROC questions and END PROC to define the beginning and end of the procedure, and in between
three print statements. Very simple statements. We've asked to print the height of the box,
the length of the box, and the depth of the box, describing the three items of information we want
him to type in. Note these quotation marks at the beginning and the end of everything we want to
print. These will always be the same, so these are constant values with quotation marks around them.
Well that's PROC questions. Now let's look at PROC answers. Now printing that information out
is fairly straightforward because it's going to be the same information every time. It's constant
information, but when we come on to looking at the values that the user of the program is going to
type in, now those are going to be different every time, so we can't use constants. What we have to do
is use what BASIC refers to as variables. Perhaps it'll be simplest if we look at the coding for
PROC answers and you'll see what I mean. Here's our DEF PROC statement and our END PROC statement.
The three statements within this procedure are all input statements, input statements which will
ask the user to type something in. What he types in is going to be put into height for the first
number, length for the second number, and depth for the third number. Now height, length, and depth
are names that we've thought up to describe these values that the user is going to put in. When the
BASIC interpreter finds this input statement, input height, it doesn't recognize the word height
as being a word in the BASIC language, so it allocates a piece of memory to store a number in,
and it calls that piece of memory height. Now after we've used the word height here in an input
statement, whenever we refer to it again later, the BASIC interpreter knows that we want to look
at the number that's in that piece of memory called height. So as the result of these three input
statements, we've asked the BASIC interpreter to set up three areas of memory, one called height,
one called length, one called depth, and the three numbers that the user is going to type in will be
placed in those areas of memory, and we'll be able to use them later as part of our calculations.
Well we have our three numbers in, and so the next thing to do is the calculation,
and we're doing that in PROC CALCULATE. So let's have a look at the coding for that to see how it
works. Now if we list the program, first thing we see is the fact that it's now too big to go onto
the screen. Lines 10 to 30 have gone off the top of the screen, and we're left with just 40 down to
270. Now as our programs get bigger and bigger, this is going to be more of a problem. So perhaps
this is a good time to introduce the idea of putting the screen into PAGED mode, and we do
that by using a key we haven't used yet, the CONTROL key. Now the CONTROL key is rather unusual,
because it only works in conjunction with other keys. If we use it by itself, it has no effect at
all. Now we're going to use it in conjunction with the N key. We're going to type CONTROL N.
We hold down the CONTROL key and type N. It's most important that you hold the CONTROL key down
while you type N. The screen is now in PAGED mode, which means that it won't allow the listing to
fly off the top of the screen without giving you a chance to see what it says. Let's prove that by
listing the program again. As you can see, we still have the beginning of the program at the top of
the screen, but at the bottom we haven't listed it to the end of the program. Now you can tell
when the computer is waiting to show the rest of the listing, because these two lights are on,
the caps lock and the shift lock lights. And the way that you tell the computer that it can continue
and show you the next page of the listing is by pressing the SHIFT key. As you can see,
it's now continued the listing to the end of the program. So CONTROL N puts the screen into PAGED
mode and you'll find that CONTROL O takes it out of PAGED mode again and leaves it as it was before.
There are several other control codes that you can use to obtain different effects,
and there's a full list of them in your user guide. However, let's go back to our calculations.
Here's PROC CALCULATE with the DEF PROC statement at the beginning and the END PROC at the end.
Now you'll recognize these names, LENGTH, DEPTH and HEIGHT. These were the variables where we put the
numbers that our user typed in. However, these names here are new ones, BASE, SIDE, END, VOLUME,
AREA. That's another five variables that we're asking BASIC to define as part of our calculation.
It'll take the value that's in LENGTH, multiply it by the value that's in DEPTH. That asterisk
is used in BASIC to mean multiply, and it'll put the answer in a piece of storage which it will call
BASE. Similarly, multiplying LENGTH and HEIGHT puts the answer into SIDE, DEPTH and HEIGHT into END.
This now calculates the volume of the complete box, multiplying the LENGTH by the DEPTH by the
HEIGHT, and the AREA consists of two sides, two ends and a base, plus signs to indicate addition.
And although I've put brackets around these calculations to make it look a lot clearer,
they're not actually necessary. It would have done the calculation the same way even without the
brackets. Notice that I've used lowercase letters for all these variable names.
You don't have to. BASIC does allow you to use uppercase or lowercase letters.
But I think it's a good idea because it does allow you to see very easily which words in
your program are your variable names and which are the basic keywords, which must be in uppercase.
Okay, back to our program. We now have our two answers in VOLUME and AREA.
And the only thing that remains to be done is to print those answers, which we're going to do in
PROC RESULTS. So we'll just see the rest of the program by listing from line 280 onwards.
And there you see PROC RESULTS. Very simple, only two statements in the procedure. They're both print statements.
We're printing out that the VOLUME is, and then we're printing the VOLUME that we calculated.
The second print statement prints AREA is, and then prints the AREA that we calculated.
Now, before we go to run this program, I think we should just do a final check of the logic to make
sure we haven't forgotten anything. It's always worth doing a final check of the logic before you
run the program, just in case there is something silly that you've left out. Here's the beginning
of the program. We went into mode four. We executed the procedure, and now we're going to run the
program. We went into mode four. We executed the procedure to ask the questions, the procedure to
obtain the answers, the one to do the calculation, and the one to print the results. And then what?
Well, our program will continue executing, and it'll carry on down here and try to execute PROC
questions again. Well, if we're going to make this a very simple program, we really should make it
stop at this point, once it's done the job once. So we need to insert a new statement there,
an END statement. Let's make it line 80. Remember, we can insert a new line simply by typing its
number and then the text of the line. We can check that it's gone into the right place
by listing that same set of lines again.
And as you can see, we now have line 80 end. Well, only one thing remains to be done,
and that's to run the program to see what it looks like. To run the program, R-U-N for run,
and return. Here are the first three lines to be printed by our first procedure,
and then we have a question mark followed by a cursor. This tells us that the computer is
waiting for us to type in the first of those three numbers, the height of the box. Well,
let's say that's 25. 25 and press return. Length of the box, let's say it's 30,
and for the depth, let's put 40. Press return for the third time, and there are our results
from volume is and the value for the volume, area is and the value for the surface area.
Our little arrowhead and the flashing cursor tell us that the program has finished. It's
done the job once and terminated. Well, the program seems to work, though I don't think
anybody would claim that the output looks very elegant or very pretty, but that we'll fix in the
next session on this tape. However, we have created our first complete working program,
and what we've done is to record that program on the soundtrack of this videotape following this
little session, and at the same time you'll see a diagram giving you instructions for how to copy
the soundtrack from your videotape onto your own tape recorder so that you can then
load it into your program and play with the coding yourself.
Well, that's the program as we left it at the end of the last session. It's not very impressive,
but as I said, it does work. It does produce the answers we asked for. Now, our first job is going
to be to tidy it up, to improve the quality of the output, because if you're going to ask somebody
to use one of your programs, then it's got to look right. They've got to look at the screen,
and they've got to want to use it. Now, compare the output from that version of the program
with the output from this version.
Well, as you can see, that's quite an improvement over our first version of the program.
Various things have improved the appearance of the screen. For a start, the answers to the questions
go in opposite the questions, so it's much easier to see which question and which answer relate to
one another. The numbers that we type in and the numbers that are the result are printed in reverse
as black letters on a white background instead of white on black, and this makes them easier to
distinguish. And then that final point, asking the user if he wants to do another calculation,
giving him the chance to say yes or no. Well, that looked better. How do we achieve that in
BASIC? Well, here are the first two procedures, PROC questions and PROC answers. If we look at
the coding, we'll see some significant changes from the original version. On these three print
statements, we now have the word tab followed by numbers in brackets, and on the three input
statements, again, we have tab followed by two numbers in brackets. Now, tab, when used in a
print statement or an input statement, allows us to position our coding to anywhere we like within
the screen. Now, if you remember, this was being done in mode four, so we have 40 characters
horizontally and we have 32 lines vertically. Tab five comma three means move to position five
along the line on line three. Semicolon means print this immediately following that position.
So the second print statement, position five along the line but on line five, will cause
depth of box to be printed two lines below length of box but in the same position along the line.
Similarly, five seven makes height of box be printed another two lines down.
When we look at our input statements, the first one is tab 22, three. That's position 22 along
the line on line three, and you notice that line three occurs in both of these statements.
On the print statement saying length of box and on the input statement asking for the length,
which means that length of box is printed here on the screen and then the input statement positions
the cursor immediately following that text for the user to type in his number. Similarly, when we
input the depth, that's position 22 on line five, which is the same line that we used to type depth
of box. Similarly, the height is on line seven on the same line as the text height of box. So that's
the first difference. Using the tab in a print or input statement to be able to position you exactly
on the screen. In mode four, that's anywhere from position zero at the very left of the screen
up to position 39 at the very right hand side of the screen. So that's the first difference.
Now the second difference comes here in lines 200 and 240. In each of these two lines,
we've put two color statements here, color zero, color 129, and here color one and color 128.
Now the color statement is used to change the color of the text that we're printing.
In mode four, color zero means black and color one means white. So here we've said print in black,
but of course our background is black and if we printed black numbers on a black background,
we wouldn't be able to see them. Color 129 changes the color of the background
to color number one. If the number you put in the color statement is bigger than 127,
then Basic assumes you mean the background color. Color 128 as we've used here
means make the background color zero. Color 129 means make the background color one.
So while we're inputting our three numbers, what we type at the keyboard appears on the screen
in black letters on a white background. And as soon as we finished typing in those three numbers,
we switch back to using white letters on a black background. All right, so those are two of the
changes allowing us to position ourselves to a precise location on the screen and changing
between white letters on a black background and black letters on a white background. Now the other
change that we made was in the control of the program, asking the user if he wanted to do
another calculation. Now there are two things about this extra part of the program. One is
the fact that we need some way of persuading the program to repeat over and over again.
The second thing is that the user had to type in a letter y or n instead of a number.
So we're going to have to learn how to handle variables that won't hold numbers,
but will hold strings of other characters. Let's look at the coding for the beginning of the
program. It's basically the same as the original version except for line 30, line 80, and line 90.
Now line 30 and line 90 go together. They form what's called a repeat until loop,
and the effect of them is to ensure that this coding is repeated over and over again until
something happens. Well let's see what that something is. ans$ equals n. Now that's in
small letters, so that's a variable name, but it's got a dollar sign following it.
That dollar sign is very important. This tells basic that ans is not a variable that's going
to hold a number. It's going to be a character variable. It's going to hold a string of characters.
Where does it get a value from? Well it comes from here. This input statement in 80,
the tab we've just seen used, another y or n. Now if we put a string in quotation marks in an input
statement, then basic will print it out for us before it tries to input the value.
So what we said is move to position 515 on the screen, print out another y or n,
and then take the result, whatever the user typed in, as a string of characters and put it into ans$.
Now we hope the user is only going to type one character, either a y or an n.
If it's a y, we want to go back to the beginning and repeat proc questions,
proc answers, proc calculate, and proc results. If it's an n, it means the user doesn't want to
run the program again, and so we can continue to the end statement. That's exactly what this
repeats until does. It repeats, it goes around and around, executing these again and again until
the user has typed in an n into the variable ans$. That becomes true, ans$ is equal to n,
at which point we can come through to the end statement and the program will finally finish.
So that's a new sort of variable, a character variable, and the only way that basic can tell
that a variable is for holding character strings is because of that dollar sign on the end of it.
If it doesn't have a dollar sign on the end, then basic will expect to hold a number in that
variable. So if you need to handle strings of characters, remember to put that dollar
on the end of your variable name. Now we've seen input used to input a numeric value
and a string value, but in fact input isn't the only way of accepting information from the keyboard.
Now watch me run this program again and see if you can see the difference between this version
and the last version. We type the digits of the first number, 13 perhaps, the second number,
45, pressing return after each, the third number, 6 perhaps, and press return.
We're asked do we want another, so we press Y and immediately without waiting for us to press return,
it's accepted the Y and gone to run the program again. So what's the difference? We didn't use an
input statement at that time, we used something else. If we look at the listing of the program,
we'll see how that worked. The difference is that we've replaced the input statement
by these two statements. Now this print statement simply prints out the same message as before,
another Y or N. This statement puts a value into ANS dollar, not by inputting it,
but by giving it the value that GET dollar provides. Now GET dollar is a function in BASIC.
BASIC contains many functions to do many different things.
GET does one thing, it waits for somebody to type a single key on the keyboard
and then gives you that one character back. So as soon as we've printed that prompt, another Y or N,
as soon as any key is pressed on the keyboard, the one character string corresponding to that key
is placed in ANS dollar. So we don't have to press return, as soon as the user presses Y or N,
that value goes into ANS dollar and can then be tested by this until following.
Sometimes it's useful to save the user having to type return after every question,
if all the answers are going to be single letters, Y, N, U, D. And so in that sort of situation,
using the GET function and putting the value into a character variable can be more convenient
than using an input statement. Now that's got the layout of the program right, it's got the
format of the output to the user right, but after all the BBC Micro is a colour machine.
It seems rather a shame to have it printing simple black and white letters. Can't we make
the program work in colour? Well the answer is of course that we can. How about this?
That's the colour called magenta. Now in order to see how we've managed to achieve that we'll
have to look back at the coding at the beginning of the program. There's the answer. In line 25
I've put a VDU statement. Now the VDU statement does many different things
and of course there's a list of them in your user guide, but VDU 19 is used to change colours.
Now we are in mode 4. Mode 4 is what's called a two colour mode. We can only have two colours
on the screen at one time and they're called colour 0 and colour 1 and we've already used
them in our colour statement. What this VDU statement does is to say when I say colour 1
what I really mean is colour number 5 and looking again in your user guide you'll find that colour
5 is magenta. So until we do another mode change or until the program terminates
everything that we print in colour 1 will come out in colour 5 which is magenta.
Colour 0 would be the background and we can change the colour of the background as well
by inserting a new VDU statement. That particular combination is perhaps not one that I would choose
but you can pick any pair of colours you like out of the palette of 8 different ones. To get
the green background there we used this statement. There it is. Statement 26, another VDU 19 statement
this time changing colour 0, that's the normal background colour, to colour 2 which is that
green. You'll notice that on both of these VDU 19 statements I've got three zeros following the two
colour numbers. If you don't put those in then your program won't work. Those three extra noughts
are required by the format of a VDU 19 so don't try and cut corners by leaving them out.
Well we're going to take a break there at the end of this program so that you can copy the
program from the soundtrack following this onto your own tape recorder as we did with the previous
program and while you're looking at it make sure that you understand what each of those statements
does. Look them up in the user guide and change the program yourself,
improve it, make it do something different and in the next part of this video tape we'll look at
graphics.
Well I said we were going to look at graphics in this session
and so the simplest way to start with graphics is to extend the program we've been using
by getting it to draw what we've been talking about like this. Now this much you should be
familiar with but on entering the third number this time we've got a nice perspective picture
of the box and in fact that box will change its shape according to the numbers we put in.
Let's put in one that isn't so long. Let's put in one that's only 100 long,
20 deep and 50 high. Hardly worth calling a box at all. All right how did we get that drawing done
and how did we make sure that the top half of the screen was used for text
and the bottom half for the graphics. Well we put two extra procedures into the program.
Right the things that are new in this coding are line 30 here, PROC SETUP. That's a call to a
different procedure and note that it's outside the repeat until loop so we'll only do that once at
the beginning of the program. CLS you've seen for clear screen, CLG stands for clear graphics
screen. We need that because our text and graphics are going to go into different parts of the screen
and down here at line 100 we're referring to a new procedure PROC DRAWBOX.
Well let's take PROC SETUP first to see what we need to do in the way of initialization in this program.
Well there's the procedure called PROC SETUP and it contains two new VDU calls. VDU 24 and VDU 28.
Let's deal with VDU 28 first because that's a little closer to what we've been working with.
VDU 28 sets up what we call a text window that says only print text in part of the screen. Don't
print it in the whole screen. These four numbers tell basic which part of the screen it's to use
for text. The left-hand side means starting at column 0. 12 here means down to line 12.
39 means all the way across the column 39 and 0 means up to line 0. So those four numbers define
the four sides of the text window. Now VDU 24 is used to define a graphics window and that's the
part of the screen in which we're going to draw graphics. Now whereas with text this top left-hand
corner is thought of as position 0 on line 0, with graphics the 0 position is right down at the bottom
of the screen, at the bottom left. To specify a text position, for example when we're using tab,
we used column number and line number. When we specify graphics, we say the number of points
to the right and the number of points up. And instead of being perhaps 40 characters,
and instead of being perhaps 40 characters by 32 characters, when we use graphics the very
left-hand position is 0, the right-hand position is 1279. The bottom is 0 and the top is 1023.
So this VDU 24 statement says the graphics window should be 0 at the left, that's all the way to the
left of the screen, 0 downwards which means to the bottom of the screen, 1279 which means all the way
to the right of the screen, but 512 up. So that's only halfway up the screen. So we've got a little
less than half the screen for text, and the bottom half of the screen for graphics.
Color 129, color 0, simply says that for text we're going to use white background and black
text printing on that background. Well now we've got ourselves set up to do the drawing, let's look
at the procedure which actually does it, PROC DRAWBOX. Now we've listed the program like this
so that we can see the basic statements in part of the screen and the drawing they produced in
the other part of the screen. If we go from the beginning of the procedure we have clear graphics
which you've seen used before. The next three statements I'll skip over for the moment because
they're involved in drawing this perspective back to the box. And we come down to line 550
which is the first statement that's directly concerned with the drawing.
Now a MOVE statement will move the graphics cursor to a particular point on the screen.
In the same way that we used a TEXT cursor to show where the next character should be printed,
there's a GRAPHICS cursor that can be moved around to show where the next line should be drawn from.
MOVE 00 takes us to the very bottom left hand corner of the graphics screen.
The DRAW statement will draw a line from the current position of the cursor, remember that's
down here, to the point described by these two numbers, LENGTH 0. Now LENGTH is the distance of
the point from the left hand side and ZERO is the distance up from the bottom. That's that point.
This drawing was done with a length of 400 so this point is the point 400 comma 0.
The second DRAW statement will draw another line. It won't start at the beginning again,
it'll start where the last line finished. And that'll draw to the point identified by these two
numbers, LENGTH, HEIGHT. Well LENGTH takes us this far in from the left of the screen
and HEIGHT takes us this far up. So this second DRAW statement will draw a line from here.
Where the first DRAW statement finished, up to this corner. The third DRAW statement
draw a line to the point 0 comma HEIGHT. That's the point 0 HEIGHT, 0 meaning it's at the very
left hand side of the screen and HEIGHT meaning it's this far up from the bottom. And so we get
this line drawn. And then the fourth DRAW statement draw a line back to the same place that we started
from, 0 0. So that completes the fourth line of this front of the box. Now that's fairly
straightforward, drawing a simple rectangle. It's a little more complicated to draw these
sloping lines and the back of the box. That's where this calculation came in.
We need to use the depth of the box to decide how far back these lines are going to go.
I've decided to use 30 degrees as this angle and I've put 30 degrees converted to radians
into the variable called angle. And up and right are the distance up from here to the back corner
of the box and right is the distance from here to the back side of the box. Let's look at this
second sequence of MOVE and DRAW statements. MOVE LENGTH 0. That moves to this point here.
Remember the second corner of the box we drew. MOVE doesn't draw at all. It won't leave a line
behind it. It simply moves the cursor to this point. The next statement DRAW draws to the point
LENGTH plus RIGHT. Well that's LENGTH and that's RIGHT.
Followed by UP, which is that distance. So it draws a diagonal line. The lines we draw don't have to be
vertical or horizontal. They always go from the last place we visited to the point given in the
DRAW statement. And if you follow the logic of these DRAW statements through, you'll find that
we've drawn round the back of this box. If we can just MOVE the listing on, then we can draw
on. So we can see the last two statements. Those are the statements to draw this final line in
to complete the box. Now in this videotape we've only had time to cover a few of the
aspects of writing programs in BASIC on the BBC Micro. In future programs we hope to be covering
advanced graphics, sound, advanced string handling and many of the other facilities that this machine
has to offer. But for the moment it's up to you to go away and to practice the things we've talked
about during this tape. Till then, good programming.