Turbo Pascal 2.0 course cover
← Back to Courses
module
28

The Worker Moves

Introduction

A warehouse you can only look at is not much of a game. This section puts the worker under your control: the arrow keys walk him around, and the walls stop him. Crates stop him too, for now - pushing them is the next section.

A New Stage From The Last

Each stage of the game starts as a copy of the one before. S always saves under the work file's own name, so the copy is made the other way round: a new work file, with the old one read into it. The editor can read a whole file into the text:

  1. W, and the new name, WARE2. Turbo says New File.
  2. E to open the empty editor.
  3. CTRL-K CTRL-R. The top line asks Read block from file:. Type WARE1 and ENTER.

The whole of WARE1 appears. Change it into WARE2, and WARE1 stays on the disc as it was - if a change goes badly wrong, you can always start this stage again.

The Game Loop

repeat
  Read(Kbd, Ch);
  case UpCase(Ch) of
    '^', '[', ']', #10: Go(Ch);
  end;
until UpCase(Ch) = 'Q';

This is the heart of the game, and it is S24's key-driven loop: wait for a key, act on it, go round again. The arrow keys arrive as ^, [, ] and the character numbered 10 (S24); any of them is handed to Go. Q stops. Any other key falls through the case and does nothing.

Nothing happens between keys. The game costs nothing while you think, and it needs no {$U+} to be stoppable: Q is always there.

Which Way?

Delta turns an arrow into a direction - how far to move in rows (DR) and in columns (DC):

KeyDRDC
UP ^-10
DOWN (10)10
LEFT [0-1
RIGHT ]01

DR and DC are var parameters (S15): Delta sets the caller's own variables. Working in steps like this means the rest of the game never has to care which of the four keys was pressed - moving up is the same arithmetic as moving right.

A Step

Go works out the square the worker would step onto - NR, NC, for next row and next column - and looks at what is there:

if L.Grid[NR, NC] in [Floor, Goal]
then

Only floor and goals can be walked on. A wall, a crate or a crate on a goal stops him, and Go does nothing at all.

If he can go, he moves, and exactly two squares are redrawn: the one he left - Show draws it as whatever tile is there, now that he is not - and the one he arrived on. The rest of the screen is untouched, and the move is instant.

The Code

Start WARE2 from WARE1 as above, then make these changes.

Add Ch to the variables:

var
  L: Level;
  N, R, C: Integer;
  LeftCol, TopRow: Integer;
  Wide, High: Integer;
  Ch: Char;

At the end of DrawAll, after Squares;, put the keys on the bottom line:

  GotoXY(1, 24);
  Write('Arrows move  Q quit');
end;

After DrawAll, add the two new procedures:

procedure Delta(Key: Char;
                var DR, DC: Integer);
begin
  DR := 0;
  DC := 0;
  case Key of
    '^': DR := -1;
    #10: DR := 1;
    '[': DC := -1;
    ']': DC := 1;
  end;
end;

procedure Go(Key: Char);
var
  DR, DC, NR, NC: Integer;
begin
  Delta(Key, DR, DC);
  NR := R + DR;
  NC := C + DC;
  if L.Grid[NR, NC] in [Floor, Goal]
  then
  begin
    R := NR;
    C := NC;
    Show(R - DR, C - DC);
    Show(R, C);
  end;
end;

And the main program becomes:

begin
  N := 1;
  MakeLevel(N);
  R := L.StartR;
  C := L.StartC;
  DrawAll;
  repeat
    Read(Kbd, Ch);
    case UpCase(Ch) of
      '^', '[', ']', #10: Go(Ch);
    end;
  until UpCase(Ch) = 'Q';
  ClrScr;
end.

Starting from

WARE1 from S27, read into a new work file WARE2 as above. Make the changes, CTRL-K CTRL-D, S to save, then R.

What you should see

The first level, with Arrows move Q quit on the bottom line. The arrow keys walk the worker around the floor, and he goes over the goal square without spoiling it. He will not go into the walls, and he will not go into the crate. Q clears the screen and returns to Turbo.

The worker has moved

In MAME: the arrow keys on your keyboard are the Einstein's arrow keys.

Change One Thing

  • Take out the line Show(R - DR, C - DC);. Walk around. What does the worker leave behind him, and why?
  • In Go, change in [Floor, Goal] to <> Wall. Walk through the crate, and out the other side. What happens to it?
  • In Delta, swap the -1 and the 1 on the '^' and #10 lines. Which way does UP go now?

Exercises

28.1 Make SPACE send the worker straight back to where he started.

28.2 Show the worker's row and column on the top line after every step, as Row 4 Col 5.

28.3 Let the letters E, S, D and X move the worker as well as the arrows - up, left, right and down, as in the editor (S5).

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
The worker does not move at allThe case is missing an arrow, or Go is not called. Check the characters '^', '[', ']' and #10.
He moves the wrong wayA sign in Delta is the wrong way round.
A trail of workersThe square he left is not redrawn: Show(R - DR, C - DC) is missing.
Q does nothinguntil UpCase(Ch) = 'Q' is missing or mistyped.

Summary

A new stage starts as a new work file with the last stage read in by CTRL-K CTRL-R. The game loop waits for a key, acts on it, and goes round until Q. Delta turns an arrow into a step in rows and columns, and Go takes the step only onto floor or a goal, redrawing just the two squares that changed.

Next

S29, Pushing A Crate - the one thing the worker is for.

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.