Delphi 经典游戏程序设计40例 的学习 例35 半自动制作迷宫

 

 

 

unit R35;

interface

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

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

const
  Mwidth = 25 * 16 + 32;         //规定的16*16的方块,25*19个,
  Mheight = 19 * 16 + 32;
  
var
  Rei35: TRei35;
  MakeBmap : TBitmap;
  RectD : TRect;
  St,n : Byte;
  Mdata : array[0..24,0..18] of Byte;        //25*19 的数组 ,

  Md011 : array[0..6 * 6 - 1] of Byte = (     //分为12格,预设的数据,这个得预先设置好 6*6方块
    0,0,0,0,0,0, 0,1,0,1,0,1, 0,1,0,1,0,1,    //编号XX + 变化X
    0,1,0,1,0,1, 0,1,0,0,0,1, 0,1,1,1,1,1);
  Md012 : 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);
  Md021 : 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);
  Md022 : 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);
  Md031 : 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);
  Md032 : 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);
  Md041 : array[0..7 * 6 - 1] of Byte = (       //7*6方块
    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);
  Md042 : 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);
  Md051 : 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);
  Md052 : 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);
  Md061 : array[0..7 * 7 - 1] of Byte = (       //7*7方块
    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);
  Md062 : 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);
  Md071 : 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);
  Md072 : 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);
  Md081 : 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);
  Md082 : 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);
  Md091 : 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);
  Md092 : 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);
  Md101 : 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);
  Md102 : 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);
  Md111 : 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);
  Md112 : 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);
  Md121 : 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);
  Md122 : 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 TRei35.FormCreate(Sender: TObject);
begin
  Rei35.Canvas.CopyMode := cmSrcCopy;
  MakeBmap := TBitmap.Create;      //少写这句程序可以编译出来,但是运行显示错误
  MakeBmap.Width := Mwidth;
  MakeBmap.Height := Mheight;

  St := 1;
  Randomize;
end;

procedure TRei35.MkMaze;
begin     //12格随机选择图案
  if Random(2) = 0 then
    MdMov(0,5,0,5,md011)   //对格搬运数据
  else
    MdMov(0,5,0,5,Md012);
  if Random(2) = 0 then
    MdMov(6,11,0,5,Md021)
  else
    MdMov(6,11,0,5,Md022);
  if Random(2) = 0 then
    MdMov(12,17,0,5,md031)
  else
    MdMov(12,17,0,5,Md032);
  if Random(2) = 0 then
    MdMov(18,24,0,5,Md041)
  else
    MdMov(18,24,0,5,Md042);
  if Random(2) = 0 then
    MdMov(18,24,6,11,md051)
  else
    MdMov(18,24,6,11,Md052);
  if Random(2) = 0 then
    MdMov(18,24,12,18,Md061)
  else
    MdMov(18,24,12,18,Md062);
   if Random(2) = 0 then
    MdMov(12,17,12,18,md071)
  else
    MdMov(12,17,12,18,Md072);
  if Random(2) = 0 then
    MdMov(6,11,12,18,Md081)
  else
    MdMov(6,11,12,18,Md082);
   if Random(2) = 0 then
    MdMov(0,5,12,18,md091)
  else
    MdMov(0,5,12,18,Md092);
  if Random(2) = 0 then
    MdMov(0,5,6,11,Md101)
  else
    MdMov(0,5,6,11,Md102);
   if Random(2) = 0 then
    MdMov(6,11,6,11,md111)
  else
    MdMov(6,11,6,11,Md112);
  if Random(2) = 0 then
    MdMov(12,17,6,11,Md121)
  else
    MdMov(12,17,6,11,Md122);




end;

procedure TRei35.MdMov(n1,n2,m1,m2:Byte;Mdpon:array of Byte);
var
  x,y : Byte;
begin
  n := 0;
  for y := m1 to m2 do          //m1,m2 对应12大格Y格标
    for x := n1 to n2 do        //n1,n1对饮12格的X坐标
    begin
      Mdata[x,y] := Mdpon[n];     //搬运 预设数据
      n := n + 1;
    end;
end;

procedure TRei35.DiMaze;
var
  x,y : Byte;

begin
  for x := 0 to 24 do         //根据数据画出 16*16的黑格
    for y := 0 to 18 do
      begin
        if Mdata[x,y] = 0 then
          MakeBmap.Canvas.Brush.Color := clBlack
        else
          MakeBmap.Canvas.Brush.Color := clOlive;
        RectD := Rect(x * 16 + 16,y * 16 + 16,x * 16 + 32,y * 16 + 32);
        //RectD := Rect(x * 16 + 0,y * 16 + 0,x * 16 + 16,y * 16 + 16);  //0,0坐标绘制
        MakeBmap.Canvas.FillRect(RectD);
      end;
end;
procedure TRei35.Timer1Timer(Sender: TObject);
begin
  case St of
    1:begin                                        //画面初始化
      MakeBmap.Canvas.Brush.Color := clOlive;     //刷褐色 ,大一圈,形成边界图案
      RectD := Rect(0,0,Mwidth,Mheight);
      MakeBmap.Canvas.FillRect(RectD);
      MakeBmap.Canvas.Brush.Color := clBlack;     //刷黑色,
      RectD := Rect(16,16,Mwidth - 32,Mheight - 32);
      MakeBmap.Canvas.FillRect(RectD);
      Rei35.Canvas.Draw(0,0,MakeBmap);      //将制作好的图案画到界面上
      St := 2;
      end;
    2:begin
      MkMaze;                          //将制作好的图案画到界面上
      DiMaze;
      Rei35.Canvas.Draw(0,0,MakeBmap);
      St := 0;                         //绘制完成进入状态0,
    end;
  end;
end;

procedure TRei35.Button1Click(Sender: TObject);
begin
  St := 2;
end;

procedure TRei35.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MakeBmap.Free;
end;

end.

1,预设好数据,必须考虑到块和块之间的接口

2,块的划分,不是相同大小的,对应数据大小也不一样

3,迷宫数据用数组表示。

4,

MakeBmap := TBitmap.Create;      //少写这句程序可以编译出来,但是运行显示错误

5,继续坚持

posted @ 2022-10-23 21:31  D7mir  阅读(140)  评论(0)    收藏  举报