MazePlus

Crt game by Nikhil (Y9, Age ~14)

Introduction

The aim of the game (now available as a web version) is to use the arrow keys to navigate the maze and land upon the "O" in as short a time as possible. Asterisks indicate teleporting locations. Just take a quick look at the code to see the maze structure.

This program demonstrates the extraction of data from the source file (usually a temporary measure because you must remember to keep the source with the executable). The use of the WhereX and WhereY Crt functions makes the maze navigation code straightforward.

We have tweaked the code to make it run on the Raspberry Pi. Follow the link below for a screenshot and the Pi code.

Nikhil acknowledges some help from PPS to implement his own fine ideas.

The Program

program MazePlus;
{
    Copyright (c) 2013 Nikhil
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
{$mode objfpc}{$H+}
uses
  crt, sysutils;
var
  chars : array[1 .. 80, 1 .. 24] of char;
  c : char;
  start, finish : TDateTime;
  seconds : real;

procedure maze1;
begin
  textColor(yellow);
  textBackground(cyan);
  clrscr;

  write('################################################################################');  //1
  write('#                     #                                                         ');  //2
  write('#   ###############   ###############   #############   ########################');  //3
  write('#   #   ##  ##    #   ###############   #   #########   #                     ##');  //4
  write('#   #             #   ###############   #   #########   #   ###############   ##');  //5
  write('#   #   ###########   ###############   #   #########   #   ###############   ##');  //6
  write('#                                       #                   #######       #   ##');  //7
  write('#   #########################   #########   #########################   ###   ##');  //8
  write('#   #                       #   #########   #########################         ##');  //9
  write('#   #   #################   #   #       #   ####################################');  //10
  write('#   #                   #   #   #   #   #   #                       ############');  //11
  write('#   #################   #   #   #   #   #   #   #################   ############');  //12
  write('#                       # * #       #   #   #   #######      #  #   ###       ##');  //13
  write('###############   ###########   #########   #   ###  ##   #  #  #   ###   #   ##');  //14
  write('###############   ###########   #########   #   ###  ##   #  #  #   ###   #   ##');  //15
  write('#             #   ####   ####       #####   #   ###  ##   #  #  #   ###   #   ##');  //16
  write('#   #######   #   ##       ##   #   #####   #   ###  ##   #  #  #         #   ##');  //17
  write('#   #    O#   #   ##   #   ##   #     ###   #   ###  ##   #  #  #   ###   #   ##');  //18
  write('#   #   ###   ######   #   ##   #########   #   ###  ##   #  #  #   ###   #   ##');  //19
  write('#   #           * ##   #   ##   #########   #   ###  ##   #  #  #   ###   #   ##');  //20
  write('#   ################   #   ##   #                     #   #     ###########   ##');  //21
  write('#   ################   #   ############################   #################   ##');  //22
  write('#                      #                                                      ##');  //23
  write('################################################################################');  //24
end;

procedure readmaze1;
var
  f : text;
  CurrentLine : string;
  i, j : integer;
begin
  assignfile(f, 'MazePlus.lpr');
  reset(f);
  //Read lines up to maze
  for i := 1 to 23 do
    readln(f, CurrentLine);
  //Read maze into chars array
  for i := 1 to 24 do
    begin
      readln(f, CurrentLine);
      for j := 1 to 80 do
        chars[j, i] := CurrentLine[j + 9];
    end;
  closefile(f);
end;

begin
  maze1;
  readmaze1;
  GoToXY(79, 2);
  start := time;
  repeat
    if keypressed = true then
      begin
        c := readkey;
        if ord(c) = 0 then //Special Key
          begin
            c := readkey;
            case ord(c) of
              75 : if chars[WhereX - 1, WhereY] <> '#' then
                     GoToXY(WhereX - 1, WhereY); //left arrow = 75

              77 : if chars[WhereX + 1, WhereY] <> '#' then
                     GoToXY(WhereX + 1, WhereY); //right arrow = 77

              80 : if chars[WhereX, WhereY + 1] <> '#' then
                     GoToXY(WhereX, WhereY + 1); //down arrow = 80

              72 : if chars[WhereX, WhereY - 1] <> '#' then
                     GoToXY(WhereX, WhereY - 1); //up arrow = 72
            end; //case
          end; //if  ord
      end; //if keypressed

    if (whereX = 27) and (whereY = 13) then
      begin
        gotoxy (WhereX - 11, WhereY + 7);
      end;

    if (whereX = 18) and (whereY = 20) then
      begin
        gotoxy (WhereX + 10, WhereY - 7);
      end;
  until (whereX = 10) and (whereY = 18);
  finish := time;
  clrscr;
  gotoxy(36, 12);
  seconds := (finish - start) * 24 * 60 * 60;
  write('You win in ', seconds : 6 : 2, ' seconds!');
  sleep(3000);
end.

Remarks

Could you write a crt program with teleporting?

Programming - a skill for life!

Three programs including MazePlus by Nikhil