1.7 Fancier Output
So far we've been printing our output one line at a time. We've always been using black
characters on a white background. It is possible to use different colours as well as
go to specific locations on the screen to print things. In this section we'll investigate
how to do these things.
The screen (in the default graphics mode that Turing will start in) consists of 25 rows by 80
columns of text. You can tell the next put statement to start at any one of those locations
by using the locate procedure. You specify the row and column that you want. Here
is a program that prints a message near the centre of the screen.
locate(12, 30)
put "Another great program"
locate(13, 39)
put "by"
locate(14, 33)
put "Your name here"
|
|
Here is what the output window looks like:
The first statement in the program places the cursor at row 12 and column 30. It then prints
Another great program. The cursor will be at row 13 column 1 after the print statement
is executed (since there is no .. after the put). That is why we need the second
locate statement so that by will be centred instead of lined up
on the left side of the page.
Here is another example to see if you've got the idea of how locate works. See if you
can predict what the output will look like.
locate(3, 5)
put "line "..
put "three"
put "line four"
locate(2, 5)
put "line two"
locate(1, 3)
put "line one to the left a little"
|
|
Try running it. You should get the following output window:
You specify what colour a character should be printed in using the color (since
Turing was made by a Canadian company you can also use colour) procedure. You specify
a colour by specifying an int between 0 and the maximum colour. You can find the
value of this maximum colour by calling the function maxcolor (or maxcolour).
On some computers this value will be 255. To see the value on your computer just execute
the statement put maxcolor. You can also specify
the background colour of a character by using the
colorback procedure. The following program will show you all the colours you
have available for character graphics:
% We will explain how the for loop works in detail in chapter 4.
% It repeats until it has printed one space in all possible colours.
for i : 0 .. maxcolor
colorback(i)
put " "..
end for
|
|
Trying to keep track of colour numbers can be a bit tricky. Fortunately there are a number
of predefined constants for some of the more common colours. Here is a program that puts
together all the ideas we've seen in this section so far.
color(green)
put "GGGGG"
color(blue)
put "BBBBB"
color(red)
put "RRRRR"
locate(12, 40)
colorback(yellow)
put "12345"..
locate(13, 40)
colorback(white)
color(magenta)
put "MMMMM"
colorback(brown)
put " "..
locate(14, 5)
put " "..
locate(15, 2)
put " "..
locate(15, 4)
put " "..
locate(16, 3)
put " "..
locate(17, 2)
put " "..
locate(17, 4)
put " "..
locate(18, 1)
put " "..
locate(18, 5)
put " "..
|
|
It produces the following output window:
After you've been doing character graphics for a while in a program, you may want to clear
the screen and start again. There is a predefined procedure in Turing that will do that.
The procedure is called cls. Putting that statement in a program will erase
anything that was on the screen before you exectued that statement.
Exercise 1.7
- Write a program that will print your name and address as it would appear
on an envelope, centred on the screen.
- Write a program that will draw a simple picture using different coloured
background space characters.
- Write a program that prompts for the user's favourite animal. Print this
animal in red starting at row 4 and column 20. Print again in green 10
rows under the first one. Print in blue with a yellow background starting
at row 8 and column 45.
|
|