Programming Pascal 3 - Text & Colors
 

Crt unit does not consist of Clrscr on
ly. It has a lot of interesting functions and procedures. The ones we will discuss this time are :



  1. TextColor and TextBackGround

  2. TextAttr

  3. GotoXY

  4. TextMode

  5. Sound, Delay, and NoSound

Let's give color to our text on screen ! We use Crt unit's procedures: TextColor and TextBackGround. Both accepts value ranging 0 to 15. Example :

TextColor(14);
TextBackground(1);


It will set the foreground color to yellow and blue background for the next Write or Writeln. Yep ! Easy ! Let's look at full example :




uses crt;

begin
TextColor(14);
TextBackGround(1);
Writeln('Hello there !');
end.



 Use TextAttr instead. It's a kind of a 'combo' variable. You just assign a value that is :

(background * 16)+foreground

Suppose you want a cyan background (or light blue), the number is 3, and black foreground, that is 0. So you would write this:

TextAttr:=(3*16)+0;

Then, subsequent writes on screen will be in black over cyan.


If you clear screen AFTER you assign colors, you will found that the entire screen will be wiped to the last assigned color.


Want to have big characters on screen ? Try adding TextMode(CO40);.
Well, TextMode is used for changing text mode. Valid values to pass is CO80, that is for return back to normal mode, or LastMode -- that is back to last mode visited, and refer help for more information! Example :




uses crt;

begin
TextMode(CO40);
Writeln('A Real BIG Characters on screen !');
Readln;
TextMode(CO80);
Writeln('Back to normal');
Readln;
end.



PC Screen has a resolution. In text mode, screen has either 80 or 40 horizontal resolution (generally) and 25 vertical resolution. It means, the screen could be 80 cols by 25 lines or 40 cols by 25 lines. The setting of TextMode reflects to this : CO40 -- sets the screen to 40 characters wide and 25 lines -- and CO80 -- sets the screen to 80 characters wide and 25 lines too. 40 characters wide causes the screen to "stretch" horizontally, so in result, WIDE characters appear on screen.


EGA and VGA has a special feature that is also supported by crt unit : an enhanced text mode consists of 43 (when EGA) or 50 lines, instead of 25. We put the value Font8x8 inside TextMode parameter.


We can also directs our text output at specified location by using GotoXY. It accepts parameters X and Y (both are bytes). Note that X reflects to column position, and Y reflects to line position. Both X and Y must not exceed the range of the current text mode. Run and view this program GOTOXY.PAS to see what's going.




uses crt;

Begin
TextMode(CO40);
GotoXY(8,12);
Writeln('At 8,12');
Readln;
GotoXY(35,8);
Writeln('At 35,8');
Readln;
GotoXY(40,20);
Writeln('At 40,20');
Readln;
GotoXY(30,30);
Writeln('Guess');
Readln;
TextMode(CO80);
Writeln('Back to normal');
End.



Let' s play with sounds ! It's easy ! Start playing the sound with Sound, delay for several times, then NoSound. Let's practice ! Cut and paste this program. Run it in BP.




uses crt;
begin
Sound(440); Delay(100); NoSound;
Sound(550); Delay(100); NoSound;
end


Sound accepts frequency number and it's a word. It is the value of frequency you want to play. Delay accepts value of words. It reflects how many milliseconds (1/100th of a second) you want to delay. NoSound tells the PC to stop playing the note. If you omit Delay, you may not hear any voices. Try it and figure it out !


Pascal is able to calculate things and it made programming much easier. OK guys, here is the convention:



  • Addition (+), Subtraction(-), and Multiplication(*) :

    If both numbers are integers, it yields an integer result. Otherwise, even there's only one is real, the result becomes real.

    • integer with integer = integer

    • integer with real = real

    • real with real = real

  • Division(/) : Always yields real result.

  • Special Division (Div) : It's quite the same as (/), but it always yields integer result.
Fine, fine. But how can I apply this ?

















Real life equation Becomes
y = 5 x 3 y:=5*3; (y is either integer or real)
z = 5 + 4 x 3 z:=5+4*3; (z is either integer or real)
a = 3.14 x 7 x 7 a:=3.14*7*7; (a is always real)
b = 14 x (5 + a) b:=14*(5+a);
(b depends on a, if a real, b is always real. If a integer, b may be real or integer).

Yeah, that's quite an example. But you should know it though. How about the special division :









a:=22/7; when a is real, it yields result 3.142856 .... so on otherwise, error.
b:=22 div 7; b is always integer, and it hold 3. Div always round to the floor, I mean not to the top nor not to the nearest integer.Even 98 div 3 will result 32 though normal people consider 32.66666666..... to 33.

Pascal could convert the real numbers to integers using Trunc and Round. Trunc behaves similarly like Div, it rounds to the floor always. Round is more moderate though, it rounds to the nearest integer. Evaluate the expressions below and see the result.




uses crt;
begin
Clrscr;
Writeln(Trunc(12.31));
Writeln(Round(12.31));
Writeln(Trunc(31.49));
Writeln(Round(31.49));
Writeln(Trunc(44.59));
Writeln(Round(44.59));
Readln;
end;

(c) Shilpa Sayura Foundation 2006-2017