Blame lazarus/maze/unit1.pas

3f629b
unit Unit1;
3f629b
3f629b
{$mode objfpc}{$H+}
3f629b
3f629b
interface
3f629b
3f629b
uses
3f629b
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, LCLType;
3f629b
3f629b
type
3f629b
3f629b
  { TForm1 }
3f629b
3f629b
  TForm1 = class(TForm)
3f629b
    procedure FormCreate(Sender: TObject);
3f629b
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
3f629b
    procedure FormPaint(Sender: TObject);
3f629b
  private
3f629b
    { private declarations }
3f629b
  public
3f629b
    { public declarations }
3f629b
  end;
3f629b
3f629b
const
3f629b
  rows = 10;
3f629b
  cols = 10;
3f629b
  size = 30;
3f629b
3f629b
var
3f629b
  Form1: TForm1;
3f629b
  board: array[0..rows, 0..cols] of boolean;
3f629b
  px, py: integer;
3f629b
3f629b
implementation
3f629b
3f629b
{$R *.lfm}
3f629b
3f629b
procedure TForm1.FormCreate(Sender: TObject);
3f629b
var
3f629b
  f: textfile;
3f629b
  c: char;
3f629b
  x, y: integer;
3f629b
begin
3f629b
  AssignFile(f, 'level.txt');
3f629b
  Reset(f);
3f629b
  for y:=0 to rows-1 do begin
3f629b
    for x:=0 to cols-1 do begin
3f629b
      Read(f, c);
3f629b
      if c = '.' then board[x, y] := true;
3f629b
      if c = '#' then board[x, y] := false;
3f629b
      if c = 'X' then begin
3f629b
        board[x, y] := true;
3f629b
        px := x;
3f629b
        py := y;
3f629b
      end;
3f629b
    end;
3f629b
    Readln(f, c);
3f629b
  end;
3f629b
  CloseFile(f);
3f629b
end;
3f629b
3f629b
procedure TForm1.FormPaint(Sender: TObject);
3f629b
var
3f629b
  x, y: integer;
3f629b
begin
3f629b
  for y:=0 to rows-1 do begin
3f629b
    for x:=0 to cols-1 do begin
3f629b
      if board[x, y] = false then begin
3f629b
        Canvas.Rectangle(x*size, y*size, (x+1)*size, (y+1)*size);
3f629b
      end;
3f629b
    end;
3f629b
  end;
3f629b
3f629b
  Canvas.Ellipse(px*size, py*size, (px+1)*size, (py+1)*size);
3f629b
end;
3f629b
3f629b
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
3f629b
  Shift: TShiftState);
3f629b
var
3f629b
  x, y: integer;
3f629b
begin
3f629b
  x := px;
3f629b
  y := py;
3f629b
3f629b
  if Key = VK_UP then y := py - 1;
3f629b
  if Key = VK_DOWN then y := py + 1;
3f629b
  if Key = VK_LEFT then x := px - 1;
3f629b
  if Key = VK_RIGHT then x := px + 1;
3f629b
3f629b
  if board[x, y] then begin
3f629b
    px := x;
3f629b
    py := y;
3f629b
  end;
3f629b
3f629b
  if (x < 0) or (y < 0) or (x >= cols) or (y >= rows) then begin
3f629b
    ShowMessage('You are escaped from this maze!');
3f629b
  end;
3f629b
3f629b
  Refresh;
3f629b
end;
3f629b
3f629b
end.
3f629b