BBC Micro – My First Program

This tutorial is the third of a set of lessons/tutorials that focus on:

BBC Micro Online Emulator

To complete these lessons, you do not need access to a physical BBC micro computer. You can use the following online emulator and type commands in your browser, without the need to install anything on your computer.

You may also find the following BBC Basic emulator quite handy for testing your own BBC Basic programs.

Writing, Testing and Editing Your First Program

The aim of this tutorial is for you to become confident at writing, editing and testing computer programs written in BBC Basic on a BBC Micro computer.

To do so, we will guide step by step to create a classic Magic 8 Ball program!

By the end of this tutorial you will know how to:

  • Enter a program in BBC BASIC
  • Run and test your program
  • Add, edit and delete lines of your program
  • Get more familiar with some essential BBC Basic instructions
  • Understand how each part of the Magic 8 Ball program works

To create our Magic 8 Ball program using BBC Basic, we will use a step by step approach using the following 15 steps! Make sure to complete all these steps in order and to keep track of your progress!

Step 1 - Starting a Program on the BBC Micro
Starting a Program on the BBC Micro
When you switch on a BBC Micro, you will see:

BBC Computer 32K
>

The > is the command prompt. This means the computer is ready for you to type commands or program lines.

To start writing a program, just type a line number followed by a command, for example:

10 PRINT "HELLO"

Press RETURN after each line.

Running Your Program

To run the whole program, type:

RUN

and press RETURN.

If you change the program, just type RUN again to test it.
To exit a program that is running, press the BREAK on the keyboard.

Step 2 - Adding Lines to a Program
Adding Lines to a Program
To add a new line to a program, just type the line number and the BBC Basic code for this new line.

The line will automatically be added to your program in the right position depending on the line number provided.

For instance, add the following lines to your program to create a banner to your Magic 8 Ball program.

20 PRINT "========================"
30 PRINT "| MAGIC 8 BALL |" 
40 PRINT "========================"
Step 3 - Viewing Your Code
Viewing Your Code
To view your whole BBC Basic code type:

LIST

For a long a program you can filter the lines of code you would like to review:

LIST 20,50

This will only show you program from line 20 to line 50.

Step 4 - Editing/Replacing an Existing Line of a Program
Editing/Replacing an Existing Line of a Program
To edit/replace an existing line of code just retype it using the leine number of the line you wish to replace and the new code you would like the line to be replaced with.

For instance in the code above we will replace line 10 which outputs the word “HELLO” with the following code used to clear the screen.

Using the command prompt, type:

10 CLS
Step 5 - Removing a Line of a Program
Removing a Line of a Program
If you do not require a line within your code just type the line number of the line you would like to remove and press RETURN.

For instance to remove line 10, type:

10

To check that the line is gone, check your code:

LIST

Note that we actually need line 10 so you may want to insert it again.

10 CLS
Step 6 - Adding Annotations to Your Code
Adding Annotations to Your Code
As it is good practice to annotate a program we will add a new line of code at the very top of our code, before line 10. We will use a REM command which is used to annotate the code.

5 REM Magic 8 Ball Program - v1.0

Notice how we inserted our new line of code at line 5. This is the reason why, when numbering lines, we tend to always count in 10: This allows us to then insert new lines between two existing lines.

Check your code listing again to see the end result.

With his approach, line numbering can quickly become untidy. From time to time you can ask the BBC Micro to renumber all the lines of your program, starting at line 10 and counting in 10, using the following instruction.

RENUMBER 10,10

Try it with your own code and notice how line 5 is now renumbered as line 10.


So at this stage your whole code should look like this:

10 REM  Magic 8 Ball Program v1.0 
20 CLS
30 PRINT "========================"
40 PRINT "|     MAGIC  8  BALL   |"
50 PRINT "========================"
Step 7 - Saving and Loading a Program
Saving a Program
This program as not been saved yet, so you are at risk of loosing all your code if the computer crashes. to save your work, use the following command:

SAVE MAGIC8

⚠️ Remember, on a BBC Micro filenames can only contain up to 7 characters.

Now that your program is saved, you will be able to reload it at any time using:

LOAD MAGIC8

There is no auto-save features on the BBC Micro so make sure to save your work every so often as you add more lines to your code.

Step 8 - Displaying Instructions on the Screen
Ok great, so now let’s add more lines of code to our program. By doing so you will learn some very useful BBC Basic instructions.

Displaying Instructions on the Screen
Let’s add the following code:

60 PRINT
70 PRINT ">> Ask a YES/NO question, then press RETURN"
80 PRINT

The PRINT instruction is used to display some text on the screen.
When used on its own, the PRINT instruction leaves an empty line for spacing.

Step 9 - Adding the Main Program Loop
Adding a Main Program Loop
We will use a loop so that the end-user can use the Magic 8 Ball to ask as many questions as they wish.

90 REPEAT

This starts a REPEAT-UNTIL loop. All the code between this line and a line starting with UNTILL will be part of our loop.

Step 10 - Asking the User to Input a Question
Asking the Question

100   PRINT "========================"
110   INPUT "Your question: " Q$
120   IF Q$="" THEN PRINT "You must ask a question!":GOTO 110
  • INPUT lets the user type text.
  • Q$ is a string variable (the $ means text).
  • If the user presses RETURN without typing anything:
    • It prints a warning
    • GOTO 110 sends them back to line 110 to ask the user to enter a question again
Step 11 - Choosing a Random Answer
Choosing a Random Answer

130   R = RND(20)

RND(20) gives a random number from 1 to 20.
This number is stored in variable called R (for Response)

This decides which answer the Magic 8 Ball gives.

Step 12 - Displaying the Result
Displaying the Result

140   PRINT
150   PRINT "The Magic 8 Ball says:"
160   PRINT

Just formatting — makes the output look nicer.


The 20 Possible Answers

170   IF R=1  THEN PRINT "It is certain."
180   IF R=2  THEN PRINT "It is decidedly so."
190   IF R=3  THEN PRINT "Without a doubt."
200   IF R=4  THEN PRINT "Yes - definitely."
210   IF R=5  THEN PRINT "You may rely on it."
220   IF R=6  THEN PRINT "As I see it, yes."
230   IF R=7  THEN PRINT "Most likely."
360   IF R=8  THEN PRINT "Outlook good."
240   IF R=9  THEN PRINT "Yes."
250   IF R=10 THEN PRINT "Signs point to yes."
260   IF R=11 THEN PRINT "Reply hazy, try again."
270   IF R=12 THEN PRINT "Ask again later."
280   IF R=13 THEN PRINT "Better not tell you now."
290   IF R=14 THEN PRINT "Cannot predict now."
300   IF R=15 THEN PRINT "Concentrate and ask again."
310   IF R=16 THEN PRINT "Don't count on it."
320   IF R=17 THEN PRINT "My reply is no."
330   IF R=18 THEN PRINT "My sources say no."
340   IF R=19 THEN PRINT "Outlook not so good."
350   IF R=20 THEN PRINT "Very doubtful."

Each IF checks the random number. Only one of these will match. That matching answer is then displayed on screen.

Step 13 - Asking to Play Again?
Asking to Play Again
We will end our program by asking if the user would like to ask another question. This will be useful to use the user answer as a stopping condition for our main program loop.

360   PRINT
370   INPUT "Do you want to ask another question (Y/N)? " A$
380 UNTIL A$="N" OR A$="n"

In the above code:

  • A$ stores the user’s answer
  • The loop continues until the end-user types N or n
    So typing:

      Y → play again, the user will be able to enter another question
      N → stop
Step 14 - Ending the Program
Ending the Program

390 CLS
400 PRINT "Goodbye... the Magic 8 Ball rests!"

The above code will:

  • Clear the screen (CLS)
  • Show a goodbye message (PRINT)
Step 15 - Saving Your Work
Saving your work
You can now test your program using the RUN command and do not forget to save your work using the SAVE command!

SAVE MAGIC8
Full BBC Basic Code
Full BBC Basic Code for the Magic 8 Ball Program:

10 REM  Magic 8 Ball Program v1.0 
20 CLS
30 PRINT "========================"
40 PRINT "|     MAGIC  8  BALL   |"
50 PRINT "========================"
60 PRINT
70 PRINT ">> Ask a YES/NO question, then press RETURN"
80 PRINT
90 REPEAT
100   PRINT "========================"
110   INPUT "Your question: " Q$
120   IF Q$="" THEN PRINT "You must ask a question!":GOTO 110
130   R = RND(20)
140   PRINT
150   PRINT "The Magic 8 Ball says:"
160   PRINT
170   IF R=1  THEN PRINT "It is certain."
180   IF R=2  THEN PRINT "It is decidedly so."
190   IF R=3  THEN PRINT "Without a doubt."
200   IF R=4  THEN PRINT "Yes - definitely."
210   IF R=5  THEN PRINT "You may rely on it."
220   IF R=6  THEN PRINT "As I see it, yes."
230   IF R=7  THEN PRINT "Most likely."
360   IF R=8  THEN PRINT "Outlook good."
240   IF R=9  THEN PRINT "Yes."
250   IF R=10 THEN PRINT "Signs point to yes."
260   IF R=11 THEN PRINT "Reply hazy, try again."
270   IF R=12 THEN PRINT "Ask again later."
280   IF R=13 THEN PRINT "Better not tell you now."
290   IF R=14 THEN PRINT "Cannot predict now."
300   IF R=15 THEN PRINT "Concentrate and ask again."
310   IF R=16 THEN PRINT "Don't count on it."
320   IF R=17 THEN PRINT "My reply is no."
330   IF R=18 THEN PRINT "My sources say no."
340   IF R=19 THEN PRINT "Outlook not so good."
350   IF R=20 THEN PRINT "Very doubtful."
360   PRINT
370   INPUT "Do you want to ask another question (Y/N)? " A$
380 UNTIL A$="N" OR A$="n"
390 CLS
400 PRINT "Goodbye... the Magic 8 Ball rests!"

In the above code, you will noticed that the code within the REPEAT UNTIL loop has been indented (2 spaces at the front of each line of code, between the line number and the code for the line). Though this is not mandatory, it is good practice to indent your code when using loops or IF statements as it makes the code easier to read.

Did you like this challenge?

Click on a star to rate it!

Average rating 3.2 / 5. Vote count: 13

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: ,