Turbo Pascal 2.0 course cover
← Back to Courses
module
23

Where Text Goes

Introduction

Until now, everything your programs printed has come out a line at a time, scrolling up the screen like a typewriter. A game cannot work like that. It needs a picture that stays still while pieces of it change: the worker moves, a crate slides, the move count ticks up. This section is about putting text exactly where you want it, and changing only what you must.

ClrScr And GotoXY

ClrScr clears the screen and puts the cursor in the top-left corner.

GotoXY(Col, Row) moves the cursor to column Col and row Row, and the next Write starts there. Columns run from 1 to 40, across; rows from 1 to 24, down; (1, 1) is the top-left corner:

ClrScr;
GotoXY(10, 5);
Write('X');

puts an X ten columns in and five rows down. Note the order: across first, then down.

ClrEol - clear to end of line - rubs out everything from the cursor to the right-hand edge, on that line only.

The Cell Never To Write In

Do not write in the bottom-right cell, (40, 24). Printing a character there makes the whole screen scroll up one line, and your careful picture slides up with it. Keep games to 23 rows, or leave that one cell alone.

Waiting

Delay(1000) waits one second; the number is in thousandths of a second. It is how a program pauses long enough for someone to see something happen.

Change Only What Changes

Printing to this screen takes time - a little under two thousandths of a second a character. That sounds fast, but a board of 320 characters takes over half a second to redraw - and Warehouse's squares, as you will see, are four characters each. A game that redrew everything after every move would feel like wading through mud.

So don't. When the worker moves one square to the right, only two squares have changed: the one it left and the one it arrived at. Draw those two - GotoXY to each and write what is there - and the move is instant. It is the most important habit in screen programming on a machine like this, and Warehouse is built on it.

Two That Do Nothing Here

Turbo has a procedure to delete a line from the screen, DelLine, and one to write in dim characters, LowVideo. They depend on what the screen was set up to do, and the Einstein's set-up has no way to do either, so here they do nothing at all. The course does not use them.

The Code

program Draw;
type
  Str20 = string[20];
const
  Level: array[1..5] of Str20 = (
    '#######',
    '#     #',
    '# $ . #',
    '#  @  #',
    '#######');
var
  R: Integer;
begin
  ClrScr;
  GotoXY(1, 1);
  Write('WAREHOUSE');
  GotoXY(20, 1);
  Write('Moves: 0');
  for R := 1 to 5 do
  begin
    GotoXY(5, R + 2);
    Write(Level[R]);
  end;
  Delay(1000);
  GotoXY(8, 6);
  Write(' ');
  GotoXY(9, 6);
  Write('@');
  GotoXY(20, 1);
  Write('Moves: 1');
  GotoXY(1, 20);
end.

The level is drawn with its top-left corner at column 5, row 3. The worker is in the level's row 4, column 4, so on the screen it is at column 5 + 3 = 8, row 3 + 3 = 6. After a second it moves one square right: a space where it was, an @ where it is now, and the move count changed - three small writes, not a redraw.

Starting from

Turbo freshly started, Y, a new work file called DRAW.

What you should see

A cleared screen, with this at the top:

WAREHOUSE          Moves: 0

    #######
    #     #
    # $ . #
    #  @  #
    #######

and a second later, the worker one square to the right and Moves: 1.

Change One Thing

  • Change GotoXY(5, R + 2) to GotoXY(1, R). What happens to the title, and where does the moving worker end up?
  • Take out the ClrScr. What is left on the screen underneath your drawing?
  • Just before the last line, add GotoXY(40, 24); Write('!');. What happens to everything else?

Exercises

23.1 Clear the screen and put your name roughly in the middle of it.

23.2 Draw a box of #, 20 wide and 10 high, with its top-left corner at column 10, row 5.

23.3 Count down from 5 to 1 in the same place on the screen, one number a second, each replacing the last, then show Go!.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Everything has moved up a lineSomething was written in the bottom-right cell, (40, 24).
Text appears in the wrong placeThe column and row are the wrong way round: GotoXY(Col, Row) - across first.
Old text shows throughThe screen was not cleared, or a longer word was overwritten by a shorter one. Write spaces over what should go.
The game feels slowIt redraws too much. Draw only the squares that changed.
DelLine or LowVideo does nothingThey cannot work on the Einstein's set-up.

Summary

ClrScr clears the screen; GotoXY(Col, Row) puts the next text at a column from 1 to 40 and a row from 1 to 24. Never write in (40, 24). Delay(n) waits n thousandths of a second. Printing is slow enough that a game must redraw only the squares that change.

Next

S24, Keys Without ENTER - reading one key the moment it is pressed, and moving something round the screen with it.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.