BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
19

Colour And Where Text Goes

Introduction

Until now, every line has gone under the one before, and the screen has scrolled up when it was full. A game wants more control than that: a title in the middle, the room description at the top, the air running out in a status line at the bottom, in colour. This section is the screen.

The Screen

MODE 0 clears the screen and gives 40 columns and 24 rows of text - the screen BBC BASIC starts in, and the one this course uses throughout. (MODE 1 gives 32 columns of wider letters.) Columns are numbered 0 to 39 from the left, and rows 0 to 23 from the top.

CLS clears the screen too, without changing anything else, and puts the cursor back at the top left.

When printing reaches the bottom row, the whole screen moves up a line to make room. And a line that fills all 40 columns exactly leaves an empty row after it: the cursor has already moved to the next line when the PRINT adds its own. Keep lines to 39 characters when that matters.

Putting Text Anywhere

TAB with two numbers moves the cursor to a column and row before printing:

PRINT TAB(15,4);"D R I F T"

prints at column 15 of row 4. It prints over whatever is there. Text that runs past column 39 carries on at the start of the next row.

POS and VPOS say where the cursor is now - its column and its row.

Colour

COLOUR sets the colour of what is printed next. A number from 0 to 15 sets the text; the same number plus 128 sets its background:

0 transparent4 dark blue8 medium red12 dark green
1 black5 light blue9 light red13 magenta
2 medium green6 dark red10 dark yellow14 grey
3 light green7 cyan11 light yellow15 white

So COLOUR 11 is light yellow text, and COLOUR 129 a black background behind it. The screen starts as white text on transparent - and transparent shows the dark blue behind everything. Set the colours back to COLOUR 15 and COLOUR 128 when you have finished with them.

CLS clears the screen to the current background colour: COLOUR 130:CLS turns it medium green (the border round the edge stays blue).

Tidying Up

Two control codes, sent with VDU, clear part of the screen:

  • VDU &15 clears from the cursor to the end of its line,
  • VDU &16 clears from the cursor to the end of the screen.

And VDU &14 hides the cursor, VDU &11 brings it back. (& means the number is written in hexadecimal - &15 is 21. You only need to copy them.)

The Code

10 MODE 0
20 COLOUR 11
30 PRINT TAB(15,4);"D R I F T"
40 COLOUR 15
50 PRINT TAB(9,8);"A SHIP ADRIFT IN SPACE"
60 COLOUR 129
70 PRINT TAB(0,20);"AIR: 40   ";
80 COLOUR 128
90 PRINT TAB(0,22);

Starting from

A fresh boot, or NEW.

What you should see

The screen clears. D R I F T in light yellow near the top, a white line under it, and AIR: 40 on a black band near the bottom. Line 90 moves the cursor down out of the way, so the > appears below the band.

DRIFT's title in colour

Change One Thing

  • Change line 20 to COLOUR 9. What colour is the title now?
  • Change line 50's TAB(9,8) to TAB(30,8). What happens to a line that does not fit?
  • Add 15 COLOUR 130:CLS. What does the screen look like - and what colour is the line the > is on, and why?

The same, after COLOUR 130:CLS

Exercises

19.1 Ask for a name, clear the screen, and print the name in the middle of row 12.

19.2 Print a strip of all sixteen background colours, two spaces of each.

19.3 Turn the whole text area green.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
An empty row appears after a lineThe line filled all 40 columns. Keep it to 39.
Text breaks in the middle of a wordIt ran past column 39. Start it further left, or split it yourself.
Everything after is in the wrong colourCOLOUR lasts until changed. Set COLOUR 15 and COLOUR 128 back.
A patch of blue on a coloured screenTransparent (COLOUR 128) shows the blue backdrop, not the colour you cleared to.
The > appears in the middle of your pictureThe program ended with the cursor there. PRINT TAB(0,22); at the end moves it.

Summary

MODE 0 is 40 by 24; CLS clears it. PRINT TAB(x,y); puts text anywhere. COLOUR n sets the text colour and COLOUR 128+n its background. VDU &15 and VDU &16 clear to the end of the line and the screen.

Next

Part three: the game. S20, The Map - DRIFT's four rooms and the ways between them.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.