The Pascal programming language is often used as an introductory language for teaching computer programming to students. There are many versions of it, but the most common are Borland's Turbo Pascal and Free Pascal. This tutorial was written using Free Pascal, but all versions of Pascal share the same features, so the code should transfer without modification between different versions. Pascal is an imperative, structured programming language. The animation features are provided by the "Graph" function library that comes with the language. It is fully described in Chapter 18 of the Run-Time Library Reference Guide linked to in the first reference.
- Skill level:
- Moderately Easy
Create a next file in your favourite text editor. Any plain text editor will do, including Windows Notepad, though you may prefer to use a dedicated Pascal editor like the free program Lazarus.
Paste the following header data into the text editor to initialise the variables that will be used in the animation tutorial:
Uses Crt, Graph;
Var video Driver, gMode : Integer;
Var x, y, w, h : Integer;
Var loop : Boolean;
Var colour : Integer;
This imports both the CRT (for dealing with the console) and graphics libraries. Then it declares variables for the graphics driver, the graphics mode, the position and height of a graphic element along with its colour, and a boolean to control the animation loop.
Paste the following code immediately after the last code to initialise the values of the variables:
video Driver := DetectGraph; InitGraph(video Driver, gMode, ''); x := 40; y := 40; w := 10; h := 10; colour := Red; loop := True;
The video driver provides the interface to the screen and gMode holds the current resolution and colour depth. X, Y, W, and H will hold the X and Y coordinates on the screen to draw the animation, as well as its width and height. Colour will hold the current colour (in this case, Red).
Paste the following code to create the main loop, which will draw an ellipse, move it a little, and clear the screen to draw it again. When done repeatedly, it will produce an animation of a ball moving across the screen. It will also check for the user to quit by press the "Q" key.
while (loop) begin ClrScr; setColor(colour); FillEllipse(x, y, w, h); x := x + 1; y := y + 1; if (ReadKey == 'Q') then loop := False; end
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for