Let's Extend !

Hi !

We meet again ! Glad to see you ! Now, let's extend our first program with a little variation. Crt unit does not consist of Clrscr only. 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

This chapter is intended to add your interests in programming so the lesson wouldn't be so tiresome. If you want to skip this chapter it's OK since it has no important thing to spot. But beware that every jargon and commands I use in this lesson will be brought to the next ones.

OK ! 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.

Run it and see what happens.

O ho ! That's great ! Yellow over Blue. Try to figure all color values ! A lot of variation could be applied and it's all yours to choose. There's even more convenient than above : 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. Easy, eh ?

If you clear screen AFTER you assign colors, you will found that the entire screen will be wiped to the last assigned color. Suppose you have set color black over cyan, if you clear the screen, the entire screen will be black over cyan ! It's useful if you want alternative background instead of black -- It's boring. Yeah ! Easy !

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.

Before we continue, I would like to introduce the screen behavior of PCs in text mode. 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.

Now, 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:

Fine, fine. But how can I apply this ?
Real life equationBecomes
y = 5 x 3y:=5*3; (y is either integer or real)
z = 5 + 4 x 3z:=5+4*3; (z is either integer or real)
a = 3.14 x 7 x 7a:=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;

OK ! That's all for now ! Shall we have the quiz ?


Where to go ?

Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 1, 'Hello, World !'
To Chapter 3 about branching (IF)
My page of programming link
Contact me


By : Roby Joehanes, © 1996, 2000