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

📄 rei_23.pas

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

interface

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

type
   //  定义角色管理记录类型
  TPatDt = record
    Used: Byte;
    Sban: Byte;
    Xpos: Integer;
    Ypos: Integer;
    Smov: Byte;
    Scon: Byte;
    Dtim: Byte;
    DlyS: array[1..40] of Byte;
    DlyX: array[1..40] of Integer;
    DlyY: array[1..40] of Integer;
  end;

  TRei40_23 = class(TForm)
    Timer1: TTimer;
    MainMenu1: TMainMenu;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button2MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button3MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button4Click(Sender: TObject);
  private
    { Private 定义 }
    procedure MkDpat(Bmap: TBitmap; ZX: Byte);
    procedure InitChr;
    procedure Mstar;
    procedure ChrDZ(Znum, Xsiz, Ysiz: Byte; Dpon: Word; X1, Y1: Integer;
      Bmap: TBitmap);
    procedure PatDZ(Pnum: Byte; X1, Y1: Integer; Bmap, Zmap: TBitmap);
  public
    { Public 定义 }
  end;

const
  Yoko = 37;           //显示的横向图案数
  Tate = 27;           //显示的纵向图案数
  DYoko = Yoko * 16;   //显示的横向点数
  DTate = Tate * 16;   //显示的纵向点数
  PtFull = 16;         //全面显示(不要重叠显示)的图案数
  MaxSp = 6;           //本次使用的复合图案总数
  Mdot = 3;            //移动单位(点数)
  MaxMd = 1000;        //储存的移动资料总数

var
  Rei40_23: TRei40_23;
  //  定义载入用、去除模版用、绘制用、粉刷用、残留影像用的点阵图
  Load_Bmap: TBitmap;
  Xpat_Bmap: TBitmap;
  Make_Bmap: TBitmap;
  Bpat_Bmap: TBitmap;
  Z1_Bmap: TBitmap;
  Z2_Bmap: TBitmap;
  Z3_Bmap: TBitmap;
  Z4_Bmap: TBitmap;
  Z5_Bmap: TBitmap;
  //  定义各种变量(Byte类型、Word类型、TRect类型、数组类型)
  PX, PY, Mode: Byte;
  Sc, Memo: Word;
  Rect_L, Rect_M, Rect_D: TRect;
  Mdata: array[0..MaxMd] of Byte;
  ChPon: array[0..6] of TPatDt;
  //  各种复合图案的数组
  SpSiz: array[0..(MaxSp * 2 - 1)] of Byte = (
    1,1, 2,2, 1,1, 1,1, 1,1, 1,1
  );
  SpPon: array[0..(MaxSp - 1)] of Word;
  SpDat: array[0..8] of Byte = (
    0,
    78, 79, 94, 95,
    19, 20, 21, 22
   );

implementation

{$R *.DFM}

procedure TRei40_23.FormCreate(Sender: TObject);
var
  //  定义局部变量
  n: Byte;
begin
  //  设定Form属性
  Rei40_23.Height := 480;
  Rei40_23.Width := 640;
  Button1.Height := 25;
  Button1.Left := 603;
  Button1.Top := 16;
  Button1.Width := 25;
  Button2.Height := 25;
  Button2.Left := 603;
  Button2.Top := 56;
  Button2.Width := 25;
  Button3.Height := 25;
  Button3.Left := 603;
  Button3.Top := 96;
  Button3.Width := 25;
  Button4.Height := 25;
  Button4.Left := 603;
  Button4.Top := 160;
  Button4.Width := 25;
  //  载入图案
  Load_Bmap := TBitmap.Create;
  Load_Bmap.LoadFromFile(GetCurrentDir + '\ExData\Pat_Sample.bmp');
  //  零件贴图用指标数组的初始设定
  Sc := 0;
  for n := 0 to (MaxSp - 1) do
  begin
    SpPon[n] := Sc;
    Sc := Sc + SpSiz[n * 2] * SpSiz[n * 2 + 1];
  end;
  //  储存去除用点阵图
  Xpat_Bmap := TBitmap.Create;
  Xpat_Bmap.Width := 256;
  Xpat_Bmap.Height := 256;
  //  制作去除用点阵图
  Rect_L := Rect(0, 0, 256, 256);
  Xpat_Bmap.Canvas.CopyMode := cmSrcCopy;
  Xpat_Bmap.Canvas.CopyRect(Rect_L, Load_Bmap.Canvas, Rect_L);
  Xpat_Bmap.Canvas.Brush.Color := clBlack;
  Xpat_Bmap.Canvas.BrushCopy(Rect_L, Load_Bmap, Rect_L, clWhite);
  Xpat_Bmap.Canvas.CopyMode := cmMergePaint;
  Xpat_Bmap.Canvas.CopyRect(Rect_L, Load_Bmap.Canvas, Rect_L);
  //  储存粉刷用点阵图
  Bpat_Bmap := TBitmap.Create;
  Bpat_Bmap.Width := 8;
  Bpat_Bmap.Height := 8;
  //  储存残留影像用点阵图并制作残留影像图案
  Z1_Bmap := TBitmap.Create;
  Z2_Bmap := TBitmap.Create;
  Z3_Bmap := TBitmap.Create;
  Z4_Bmap := TBitmap.Create;
  Z5_Bmap := TBitmap.Create;
  MkDpat(Z1_Bmap, 3);
  MkDpat(Z2_Bmap, 6);
  MkDpat(Z3_Bmap, 8);
  MkDpat(Z4_Bmap, 10);
  MkDpat(Z5_Bmap, 12);
  //  储存绘制用点阵图
  Make_Bmap := TBitmap.Create;
  Make_Bmap.Width := DYoko + 32;
  Make_Bmap.Height := DTate + 32;
  //  玩模式的指定与初始设定
  Randomize;
  Mdata[0] := Random(256);
  Mode := 1;
  InitChr;
end;

procedure TRei40_23.Timer1Timer(Sender: TObject);
var
  //  定义局部变量
  n, Cn: Byte;
begin
  //  控制所有的角色并显示在Form上
  Mstar;
  Make_Bmap.Canvas.Brush.Color := clBlack;
  Rect_M := Rect(16, 16, DYoko + 16, DTate + 16);
  Make_Bmap.Canvas.FillRect(Rect_M);
  Make_Bmap.Canvas.CopyMode := cmSrcCopy;
  for Cn := 0 to 6 do
    with ChPon[Cn] do
      if (Used <> 0) and (Sban <> 0) then
      begin
        if Dtim <> 0 then
          for n := 5 downto 1 do
            if (DlyX[n * Dtim] <> Xpos) or (DlyY[n * Dtim] <> Ypos) then
              ChrDZ(n, SpSiz[Sban * 2], SpSiz[Sban * 2 + 1],
                SpPon[DlyS[n * Dtim]], DlyX[n * Dtim] + 16,
                DlyY[n * Dtim] + 16, Make_Bmap);
        ChrDZ(0, SpSiz[Sban * 2], SpSiz[Sban * 2 + 1],
          SpPon[Sban], Xpos + 16, Ypos + 16, Make_Bmap);
      end;
  Rei40_23.Canvas.CopyMode := cmSrcCopy;
  Rect_D := Rect(0, 0, DYoko, DTate);
  Rei40_23.Canvas.CopyRect(Rect_D, Make_Bmap.Canvas, Rect_M);
end;

procedure TRei40_23.Mstar;
var
  //  定义局部变量
  n, Cn: Byte;
begin
  //  所有角色的动作管理
  for Cn := 0 to 6 do
    if ChPon[Cn].Used = 1 then
    begin
      with ChPon[Cn] do
      begin
        for n := 39 downto 1 do
        begin
          DlyS[n + 1] := DlyS[n];
          DlyX[n + 1] := DlyX[n];
          DlyY[n + 1] := DlyY[n];
        end;
        DlyS[1] := Sban;
        DlyX[1] := Xpos;
        DlyY[1] := Ypos;
      end;
      case Cn of
        //  星舰的动作管理
        0: if Memo = MaxMd + 1 then
          begin
            Mode := 0;
            ChPon[0].Used := 0;
          end
          else begin
            case Mode of
              1: begin
                Mdata[Memo] := ChPon[0].Smov;
                Memo := Memo + 1;
              end;
              2: if Mdata[Memo] <> 255 then
                begin
                  ChPon[0].Smov := Mdata[Memo];
                  Memo := Memo + 1;
                end
                else begin
                  Mode := 0;
                  ChPon[0].Used := 0;
                end;
            end;
            case ChPon[0].Smov of
              1: if ChPon[0].Xpos < DYoko - 32 - Mdot then
                ChPon[0].Xpos := ChPon[0].Xpos + Mdot;
              5: if ChPon[0].Xpos > Mdot then
                ChPon[0].Xpos := ChPon[0].Xpos - Mdot;
            end;
          end;
        //  星星的动作管理
        1..6: begin
          ChPon[Cn].Sban := ((ChPon[Cn].Sban + 1) and 3) + 2;
          if ChPon[Cn].Scon <> 0 then
            ChPon[Cn].Scon := ChPon[Cn].Scon - 1
          else begin
            ChPon[Cn].Smov := (ChPon[Cn].Smov and 7) + 1;
            ChPon[Cn].Scon := Random(30) + Cn * 5;
          end;
          case ChPon[Cn].Smov of
            1: if ChPon[Cn].Xpos < DYoko - 16 - Mdot then
                ChPon[Cn].Xpos := ChPon[Cn].Xpos + Mdot;
            2: if (ChPon[Cn].Xpos < DYoko - 16 - Mdot) and
                (ChPon[Cn].Ypos > Mdot) then
              begin
                ChPon[Cn].Xpos := ChPon[Cn].Xpos + Mdot;
                ChPon[Cn].Ypos := ChPon[Cn].Ypos - Mdot;
              end;
            3: if ChPon[Cn].Ypos > Mdot then
                ChPon[Cn].Ypos := ChPon[Cn].Ypos - Mdot;
            4: if (ChPon[Cn].Xpos > Mdot) and (ChPon[Cn].Ypos > Mdot) then
              begin
                ChPon[Cn].Xpos := ChPon[Cn].Xpos - Mdot;
                ChPon[Cn].Ypos := ChPon[Cn].Ypos - Mdot;
              end;
            5: if ChPon[Cn].Xpos > Mdot then
                ChPon[Cn].Xpos := ChPon[Cn].Xpos - Mdot;
            6: if (ChPon[Cn].Xpos > Mdot) and
                (ChPon[Cn].Ypos < DTate - 16 - Mdot) then
              begin
                ChPon[Cn].Xpos := ChPon[Cn].Xpos - Mdot;
                ChPon[Cn].Ypos := ChPon[Cn].Ypos + Mdot;
              end;
            7: if ChPon[Cn].Ypos < DTate - 16 - Mdot then
                ChPon[Cn].Ypos := ChPon[Cn].Ypos + Mdot;
            8: if (ChPon[Cn].Xpos < DYoko - 16 - Mdot) and
                (ChPon[Cn].Ypos < DTate - 16 - Mdot) then
              begin
                ChPon[Cn].Xpos := ChPon[Cn].Xpos + Mdot;
                ChPon[Cn].Ypos := ChPon[Cn].Ypos + Mdot;
              end;
          end;
        end;
      end;
    end;
end;

procedure TRei40_23.Button1Click(Sender: TObject);
begin
  //  指定玩的模式
  Mdata[0] := Random(256);
  Mode := 1;
  InitChr;
end;

procedure TRei40_23.Button2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //  指定星舰向左移动(於玩模式时)
  if Mode = 1 then
    ChPon[0].Smov := 5;
end;

procedure TRei40_23.Button2MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //  指定星舰停止移动
  if Mode = 1 then
    ChPon[0].Smov := 0;
end;

procedure TRei40_23.Button3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //  指定星舰向右移动(於玩模式时)
  if Mode = 1 then
    ChPon[0].Smov := 1;
end;

procedure TRei40_23.Button3MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //  指定星舰停止移动
  if Mode = 1 then
    ChPon[0].Smov := 0;
end;

procedure TRei40_23.Button4Click(Sender: TObject);
begin
  //  指定重玩模式
  if Mode = 1 then
    Mdata[Memo] := 255;
  Mode := 2;
  InitChr;
end;

procedure TRei40_23.InitChr;
var
  //  定义局部变量
  n, Cn: Byte;
begin
  //  乱数与动作记忆指标的初始设定
  RandSeed := Mdata[0];
  Memo := 1;
  //  角色用数组的初始化
  ChPon[0].Used := 1;
  ChPon[0].Sban := 1;
  ChPon[0].Xpos := 280;
  ChPon[0].Ypos := 300;
  ChPon[0].Smov := 0;
  ChPon[0].Scon := 0;
  ChPon[0].Dtim := 0;
  for Cn := 1 to 6 do
  begin
    ChPon[Cn].Used := 1;
    ChPon[Cn].Sban := Random(4) + 2;
    ChPon[Cn].Xpos := Random(100) + Cn * 80;
    ChPon[Cn].Ypos := Random(200) + 10;
    ChPon[Cn].Smov := Cn;
    ChPon[Cn].Scon := Random(10);
    ChPon[Cn].Dtim := Random(2) + 2;
  end;
  for Cn := 0 to 6 do
    for n := 1 to 40 do
      with ChPon[Cn] do
      begin
        DlyS[n] := Sban;
        DlyX[n] := Xpos;
        DlyY[n] := Ypos;
      end;
end;

procedure TRei40_23.MkDpat(Bmap: TBitmap; ZX: Byte);
begin
  //  制作残留影像用图案
  Bmap.Width := 256;
  Bmap.Height := 256;
  Bpat_Bmap.Canvas.CopyMode := cmSrcCopy;
  Rect_L := Rect(ZX * 8 + 32, 64, ZX * 8 + 40, 72);
  Rect_D := Rect(0, 0, 8, 8);
  Bpat_Bmap.Canvas.CopyRect(Rect_D, Load_Bmap.Canvas, Rect_L);
  Bmap.Canvas.Brush.Bitmap := Bpat_Bmap;
  Bmap.Canvas.CopyMode := cmMergeCopy;
  Rect_L := Rect(0, 0, 256, 256);
  Bmap.Canvas.CopyRect(Rect_L, Load_Bmap.Canvas, Rect_L);
  Bmap.Canvas.CopyMode := cmMergePaint;
  Bmap.Canvas.CopyRect(Rect_L, Xpat_Bmap.Canvas, Rect_L);
  Bmap.Canvas.Brush.Bitmap := Nil;
end;

procedure TRei40_23.ChrDZ(Znum, Xsiz, Ysiz: Byte; Dpon: Word;
  X1, Y1: Integer; Bmap: TBitmap);
var
  //  定义局部变量
  CDX, CDY: Byte;
  Z_Bmap: TBitmap;
begin
  //  将指定角色绘制至指定的点阵图上(重叠显示、残留影像图案适用)
  case Znum of
    0: Z_Bmap := Load_Bmap;
    1: Z_Bmap := Z1_Bmap;
    2: Z_Bmap := Z2_Bmap;
    3: Z_Bmap := Z3_Bmap;
    4: Z_Bmap := Z4_Bmap;
    5: Z_Bmap := Z5_Bmap;
  end;
  for CDY := 0 to (Ysiz - 1) do
    for CDX := 0 to (Xsiz - 1) do
    begin
      if (X1 + CDX * 16 >= 0) and (X1 + CDX * 16 <= DYoko + 16) and
        (Y1 + CDY * 16 >= 0) and (Y1 + CDY * 16 <= DTate + 16) then
          PatDZ(SpDat[Dpon], X1 + CDX * 16, Y1 + CDY * 16, Bmap, Z_Bmap);
      Dpon := Dpon + 1;
    end;
end;

procedure TRei40_23.PatDZ(Pnum: Byte; X1, Y1: Integer; Bmap, Zmap: TBitmap);
begin
  //  将指定图案绘制至指定的点阵图上(重叠显示、残留影像图案适用)
  PX := (Pnum and $F) * 16;
  PY := Pnum and $F0;
  Rect_L := Rect(PX, PY, PX + 16, PY + 16);
  Rect_D := Rect(X1, Y1, X1 + 16, Y1 + 16);
  if Pnum <> 0 then
    if Pnum >= PtFull then
    begin
      Bmap.Canvas.CopyMode := cmSrcPaint;
      Bmap.Canvas.CopyRect(Rect_D, Xpat_Bmap.Canvas, Rect_L);
      Bmap.Canvas.CopyMode := cmSrcAnd;
      Bmap.Canvas.CopyRect(Rect_D, Zmap.Canvas, Rect_L);
    end
    else begin
      Bmap.Canvas.CopyMode := cmSrcCopy;
      Bmap.Canvas.CopyRect(Rect_D, Zmap.Canvas, Rect_L);
    end;
end;

procedure TRei40_23.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //  将载入用、去除用、绘制用、粉刷用与残留影像用的点阵图释放掉
  Load_Bmap.Free;
  Xpat_Bmap.Free;
  Make_Bmap.Free;
  Bpat_Bmap.Free;
  Z1_Bmap.Free;
  Z2_Bmap.Free;
  Z3_Bmap.Free;
  Z4_Bmap.Free;
  Z5_Bmap.Free;
end;

end.

⌨️ 快捷键说明

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