Tutorial in Pascal Programming Animation

Updated February 21, 2017

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.

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:

Program Pascal_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:

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.

Cite this Article A tool to create a citation to reference this article Cite this Article


About the Author

Kevin Walker is a computer programmer who decided to take a few years out from the corporate life and see the world. He spent a total of six years living abroad and teaching English in China, Korea and Mexico before returning to his home in Texas. He uses his programming and teaching experience to write easy-to-understand computer tutorials.