Article...

Tampilkan postingan dengan label tutorial. Tampilkan semua postingan
Tampilkan postingan dengan label tutorial. Tampilkan semua postingan

Jumat, 16 November 2007

Learn Pascal Programming Tutorial Lesson 2 - Colors, Coordinates, Windows and Sound

Colors

To change the color of the text printed on the screen we use the TextColor command.

program Colors;

uses
crt;

begin
TextColor(Red);
Writeln('Hello');
TextColor(White);
Writeln('world');
end.

The TextBackground command changes the color of the background of text. If you want to change the whole screen to a certain color then you must use ClrScr.

program Colors;

uses
crt;

begin
TextBackground(Red);
Writeln('Hello');
TextColor(White);
ClrScr;
end.

Screen coordinates

You can put the cursor anywhere on the screen using the GoToXY command. In DOS, the screen is 80 characters wide and 25 characters high. The height and width varies on other platforms. You may remember graphs from Maths which have a X and a Y axis. Screen coordinates work in a similar way. Here is an example of how to move the cursor to the 10th column in the 5th row.

program Coordinates;

uses
crt;

begin
GoToXY(10,5);
Writeln('Hello');
end.

Windows

Windows let you define a part of the screen that your output will be confined to. If you create a window and clear the screen it will only clear what is in the window. The Window command has 4 parameters which are the top left coordinates and the bottom right coordinates.

program Coordinates;

uses
crt;

begin
Window(1,1,10,5);
TextBackground(Blue);
ClrScr;
end.

Using window(1,1,80,25) will set the window back to the normal size.

Sound

The Sound command makes a sound at the frequency you give it. It does not stop making a sound until the NoSound command is used. The Delay command pauses a program for the amount of milliseconds you tell it to. Delay is used between Sound and NoSound to make the sound last for a certain amount of time.

program Sounds;

uses
crt;

begin
Sound(1000);
Delay(1000);
NoSound;
end.

Learn Pascal Programming Tutorial Lesson 1 - Introduction to Pascal

About Pascal

The Pascal programming language was created by Niklaus Wirth in 1970. It was named after Blaise Pascal, a famous French Mathematician. It was made as a language to teach programming and to be reliable and efficient. Pascal has since become more than just an academic language and is now used commercially.

What you will need

Before you start learning Pascal, you will need a Pascal compiler. This tutorial uses the Free Pascal Compiler. You can find a list of other Pascal compilers at TheFreeCountry's Pascal compiler list.

Your first program

The first thing to do is to either open your IDE if your compiler comes with one or open a text editor.

We always start a program by typing its name. Type program and the name of the program next to it. We will call our first program "Hello" because it is going to print the words "Hello world" on the screen.

program Hello;

Next we will type begin and end. We are going to type the main body of the program between these 2 keywords. Remember to put the full stop after the end.

program Hello;

begin
end.

The Write command prints words on the screen.

program Hello;

begin
Write('Hello world');
end.

You will see that the "Hello world" is between single quotes. This is because it is what is called a string. All strings must be like this. The semi-colon at the end of the line is a statement separator. You must always remember to put it at the end of the line.

The Readln command will now be used to wait for the user to press enter before ending the program.

program Hello;

begin
Write('Hello world');
Readln;
end.

You must now save your program as hello.pas.

Compiling

Our first program is now ready to be compiled. When you compile a program, the compiler reads your source code and turns it into an executable file. If you are using an IDE then pressing CTRL+F9 is usually used to compile and run the program. If you are compiling from the command line with Free Pascal then enter the following:

fpc hello.pas

If you get any errors when you compile it then you must go over this lesson again to find out where you made them. IDE users will find that their programs compile and run at the same time. Command line users must type the name of the program in at the command prompt to run it.

You should see the words "Hello world" when you run your program and pressing enter will exit the program. Congratulations! You have just made your first Pascal program.

More commands

Writeln is just like Write except that it moves the cursor onto the next line after it has printed the words. Here is a program that will print "Hello" and then "world" on the next line:

program Hello;

begin
Writeln('Hello');
Write('world');
Readln;
end.

If you want to skip a line then just use Writeln by itself without any brackets.

Using commands from units

The commands that are built into your Pascal compiler are very basic and we will need a few more. Units can be included in a program to give you access to more commands. The crt unit is one of the most useful. The ClrScr command in the crt unit clears the screen. Here is how you use it:

program Hello;

uses
crt;

begin
ClrScr;
Write('Hello world');
Readln;
end.

Comments

Comments are things that are used to explain what parts of a program do. Comments are ignored by the compiler and are only there for the people who use the source code. Comments must be put between curly brackets. You should always have a comment at the top of your program to say what it does as well as comments for any code that is difficult to understand. Here is an example of how to comment the program we just made:

{This program will clear the screen, print "Hello world" and wait for the user to press enter.}

program Hello;

uses
crt;

begin
ClrScr;{Clears the screen}
Write('Hello world');{Prints "Hello world"}
Readln;{Waits for the user to press enter}
end.

Indentation

You will notice that I have put 3 spaces in front of some of the commands. This is called indentation and it is used to make a program easier to read. A lot of beginners do not understand the reason for indentation and don't use it but when we start making longer, more complex programs, you will understand.

My Headlines