⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rei_35.pas

📁 Delphi经典游戏程序设计40例.pdf 中国铁道出版社出版 含源码
💻 PAS
字号:
unit Rei_35;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Menus, StdCtrls;

type
  TRei40_35 = class(TForm)
    Timer1: TTimer;
    MainMenu1: TMainMenu;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ReDraw(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private 定义 }
    procedure MkMaze;
    procedure MdMov(n1, n2, m1, m2: Byte; Mdpon: array of Byte);
    procedure DiMaze;
  public
    { Public 定义 }
  end;

const
  Mwidth = 25 * 16 + 32;    //整个迷宫的宽度
  Mheight = 19 * 16 + 32;   //整个迷宫的高度

var
  Rei40_35: TRei40_35;
  //  定义绘制用的点阵图
  Make_Bmap: TBitmap;
  //  定义各种变量(Byte类型、TRect类型)
  St, n: Byte;
  Rect_D: TRect;
  //  定义迷宫资料用数组
  Mdata: array[0..24, 0..18] of Byte;
  Md1_1: array[0..6 * 6 - 1] of Byte = (
    0,0,0,0,0,0, 0,1,0,1,0,1, 0,1,0,1,0,1,
    0,1,0,1,0,1, 0,1,0,0,0,1, 0,1,1,1,1,1
  );
  Md1_2: array[0..6 * 6 - 1] of Byte = (
    0,0,0,0,0,0, 0,1,1,1,1,1, 0,1,0,0,0,0,
    0,1,0,1,1,1, 0,0,0,0,0,1, 0,1,1,1,1,1
  );
  Md2_1: array[0..6 * 6 - 1] of Byte = (
    0,0,0,0,0,0, 1,1,1,1,0,1, 0,0,0,0,0,1,
    1,1,1,1,0,1, 0,0,0,0,0,0, 0,1,1,1,1,1
  );
  Md2_2: array[0..6 * 6 - 1] of Byte = (
    0,1,0,0,0,1, 0,1,0,1,0,1, 0,1,0,1,0,1,
    0,1,0,1,0,1, 0,0,0,1,0,0, 1,1,0,1,0,1
  );
  Md3_1: array[0..6 * 6 - 1] of Byte = (
    0,1,0,0,0,0, 0,1,0,1,1,1, 0,0,0,0,0,1,
    0,1,0,1,0,1, 0,1,0,1,0,1, 1,1,0,1,0,1
  );
  Md3_2: array[0..6 * 6 - 1] of Byte = (
    0,0,0,0,0,0, 0,1,1,1,0,1, 0,0,0,0,0,1,
    1,1,0,1,1,1, 0,0,0,0,0,1, 1,1,1,1,0,1
  );
  Md4_1: array[0..7 * 6 - 1] of Byte = (
    0,1,0,0,0,0,0, 0,1,0,1,1,1,0, 0,1,0,1,0,0,0,
    0,1,0,1,1,1,1, 0,0,0,0,0,0,0, 1,1,0,1,1,1,0
  );
  Md4_2: array[0..7 * 6 - 1] of Byte = (
    0,0,0,0,0,0,0, 1,1,1,1,1,1,0, 0,1,0,0,0,1,0,
    0,1,0,1,0,1,0, 0,0,0,1,0,0,0, 0,1,1,1,1,1,0
  );
  Md5_1: array[0..7 * 6 - 1] of Byte = (
    0,1,0,0,0,1,0, 0,1,0,1,0,1,0, 0,1,0,1,0,1,0,
    0,1,0,1,0,1,0, 0,0,0,1,0,0,0, 0,1,1,1,1,1,0
  );
  Md5_2: array[0..7 * 6 - 1] of Byte = (
    0,1,0,0,0,0,0, 0,1,0,1,1,1,1, 0,1,0,0,0,0,0,
    0,1,1,1,1,1,0, 0,0,0,0,0,0,0, 0,1,1,1,0,1,1
  );
  Md6_1: array[0..7 * 7 - 1] of Byte = (
    0,0,0,1,0,1,0, 1,1,0,1,0,1,0, 0,0,0,1,0,0,0,
    0,1,1,1,1,1,1, 0,0,0,0,0,0,0, 1,1,1,1,1,1,0, 0,0,0,0,0,0,0
  );
  Md6_2: array[0..7 * 7 - 1] of Byte = (
    0,1,0,0,0,0,0, 0,1,1,1,1,1,0, 0,0,0,0,0,1,0,
    1,1,1,1,0,1,0, 0,0,0,0,0,1,0, 0,1,1,1,1,1,0, 0,1,0,0,0,0,0
  );
  Md7_1: array[0..6 * 7 - 1] of Byte = (
    0,1,0,0,0,1, 0,1,0,1,0,1, 0,1,0,1,0,1,
    0,1,0,1,0,1, 0,1,0,1,0,1, 0,1,0,1,0,1, 0,0,0,1,0,0
  );
  Md7_2: array[0..6 * 7 - 1] of Byte = (
    0,0,0,0,0,1, 0,1,1,1,1,1, 0,0,0,0,0,1,
    1,1,0,1,0,1, 0,1,0,1,0,1, 0,1,0,1,0,1, 0,0,0,1,0,0
  );
  Md8_1: array[0..6 * 7 - 1] of Byte = (
    0,0,0,0,0,0, 0,1,1,1,0,1, 0,0,0,1,0,1,
    1,1,0,1,1,1, 0,0,0,0,0,1, 1,1,1,1,0,1, 0,0,0,0,0,1
  );
  Md8_2: array[0..6 * 7 - 1] of Byte = (
    0,0,0,0,0,0, 0,1,0,1,1,1, 0,1,0,0,0,1,
    1,1,1,1,0,1, 0,0,0,1,0,1, 0,1,0,1,0,1, 0,1,0,0,0,1
  );
  Md9_1: array[0..6 * 7 - 1] of Byte = (
    0,0,0,0,0,1, 0,1,1,1,1,1, 0,1,0,0,0,1,
    0,1,1,1,0,1, 0,0,0,0,0,1, 0,1,1,1,1,1, 0,0,0,0,0,0
  );
  Md9_2: array[0..6 * 7 - 1] of Byte = (
    0,0,0,1,0,1, 0,1,1,1,0,1, 0,0,0,0,0,1,
    1,1,1,1,0,1, 0,0,0,0,0,1, 1,1,1,0,1,1, 0,0,0,0,0,0
  );
  Md10_1: array[0..6 * 6 - 1] of Byte = (
    0,1,0,0,0,0, 0,1,0,1,1,1, 0,1,0,1,0,1,
    1,1,0,1,0,1, 0,0,0,1,0,1, 0,1,1,1,0,1
  );
  Md10_2: array[0..6 * 6 - 1] of Byte = (
    0,1,0,1,0,0, 0,1,0,1,0,1, 0,0,0,1,0,1,
    1,1,0,1,0,1, 0,0,0,0,0,1, 0,1,1,1,0,1
  );
  Md11_1: array[0..6 * 6 - 1] of Byte = (
    0,1,0,1,0,1, 0,1,0,1,0,1, 0,0,0,0,0,1,
    0,1,1,1,1,1, 0,0,0,0,0,0, 1,1,0,1,1,1
  );
  Md11_2: array[0..6 * 6 - 1] of Byte = (
    0,0,0,0,0,1, 0,1,0,1,1,1, 0,1,0,0,0,1,
    0,1,1,1,0,1, 0,1,0,0,0,0, 1,1,1,1,1,1
  );
  Md12_1: array[0..6 * 6 - 1] of Byte = (
    0,0,0,0,0,1, 0,1,0,1,0,1, 0,1,0,1,0,1,
    1,1,0,1,0,1, 0,0,0,0,0,1, 1,1,1,1,1,1
  );
  Md12_2: array[0..6 * 6 - 1] of Byte = (
    0,0,0,0,0,1, 0,1,1,1,1,1, 0,1,0,0,0,1,
    1,1,1,1,0,1, 0,0,0,0,0,1, 1,1,1,1,0,1
  );

implementation

{$R *.DFM}

procedure TRei40_35.FormCreate(Sender: TObject);
begin
  //  设定Form属性
  Rei40_35.Height := 480;
  Rei40_35.Width := 640;
  Rei40_35.Canvas.CopyMode := cmSrcCopy;
  Button1.Height := 50;
  Button1.Left := 560;
  Button1.Top := 16;
  Button1.Width := 50;
  //  储存绘制用的点阵图
  Make_Bmap := TBitmap.Create;
  Make_Bmap.Width := Mwidth;
  Make_Bmap.Height := Mheight;
  //  初始迷宫制作之指示
  St := 1;
  Randomize;
end;

procedure TRei40_35.Timer1Timer(Sender: TObject);
begin
  case St of
    //  若St=1则显示迷宫的外框
    1: begin
      Make_Bmap.Canvas.Brush.Color := clOlive;
      Rect_D := Rect(0, 0, Mwidth, Mheight);
      Make_Bmap.Canvas.FillRect(Rect_D);
      Make_Bmap.Canvas.Brush.Color := clBlack;
      Rect_D := Rect(16, 16, Mwidth-32, Mheight-32);
      Make_Bmap.Canvas.FillRect(Rect_D);
      Rei40_35.Canvas.Draw(0, 0, Make_Bmap);
      St := 2;
    end;
    //  若St=2则制作新的迷宫并显示出来
    2: begin
      MkMaze;
      DiMaze;
      Rei40_35.Canvas.Draw(0, 0, Make_Bmap);
      St := 0;
    end;
  end;
end;

procedure TRei40_35.MkMaze;
begin
  //  迷宫资料的制作
  if Random(2) = 0 then
    MdMov(0, 5, 0, 5, Md1_1)
  else
    MdMov(0, 5, 0, 5, Md1_2);
  if Random(2) = 0 then
    MdMov(6, 11, 0, 5, Md2_1)
  else
    MdMov(6, 11, 0, 5, Md2_2);
  if Random(2) = 0 then
    MdMov(12, 17, 0, 5, Md3_1)
  else
    MdMov(12, 17, 0, 5, Md3_2);
  if Random(2) = 0 then
    MdMov(18, 24, 0, 5, Md4_1)
  else
    MdMov(18, 24, 0, 5, Md4_2);
  if Random(2) = 0 then
    MdMov(18, 24, 6, 11, Md5_1)
  else
    MdMov(18, 24, 6, 11, Md5_2);
  if Random(2) = 0 then
    MdMov(18, 24, 12, 18, Md6_1)
  else
    MdMov(18, 24, 12, 18, Md6_2);
  if Random(2) = 0 then
    MdMov(12, 17, 12, 18, Md7_1)
  else
    MdMov(12, 17, 12, 18, Md7_2);
  if Random(2) = 0 then
    MdMov(6, 11, 12, 18, Md8_1)
  else
    MdMov(6, 11, 12, 18, Md8_2);
  if Random(2) = 0 then
    MdMov(0, 5, 12, 18, Md9_1)
  else
    MdMov(0, 5, 12, 18, Md9_2);
  if Random(2) = 0 then
    MdMov(0, 5, 6, 11, Md10_1)
  else
    MdMov(0, 5, 6, 11, Md10_2);
  if Random(2) = 0 then
    MdMov(6, 11, 6, 11, Md11_1)
  else
    MdMov(6, 11, 6, 11, Md11_2);
  if Random(2) = 0 then
    MdMov(12, 17, 6, 11, Md12_1)
  else
    MdMov(12, 17, 6, 11, Md12_2);
end;

procedure TRei40_35.MdMov(n1, n2, m1, m2: Byte; Mdpon: array of Byte);
var
  //  定义局部变量
  X, Y: Byte;
begin
  //  传送迷宫资料
  n := 0;
  for Y := m1 to m2 do
    for X := n1 to n2 do
    begin
      Mdata[X, Y] := Mdpon[n];
      n := n + 1;
    end;
end;

procedure TRei40_35.DiMaze;
var
  //  定义局部变量
  X, Y: Byte;
begin
  //  绘制迷宫
  for X := 0 to 24 do
    for Y := 0 to 18 do
      begin
        if Mdata[X, Y] = 0 then
          Make_Bmap.Canvas.Brush.Color := clBlack
        else
          Make_Bmap.Canvas.Brush.Color := clOlive;
        Rect_D := Rect(X * 16 + 16, Y * 16 + 16, X * 16 + 32, Y * 16 + 32);
        Make_Bmap.Canvas.FillRect(Rect_D);
      end;
end;

procedure TRei40_35.Button1Click(Sender: TObject);
begin
  //  制作迷宫的指示
  St := 2;
end;

procedure TRei40_35.ReDraw(Sender: TObject);
begin
  //  重绘Form
  Rei40_35.Canvas.Draw(0, 0, Make_Bmap);
end;

procedure TRei40_35.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //  将绘制用的点阵图释放掉
  Make_Bmap.Free;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -