Delphi 经典游戏程序设计40例 的学习 例34 RPG 中的敌人的出现时机

unit R34;

interface

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

type
  TRei34 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Rei34: TRei34;
  St,Area,Ptime,Emy : Byte;
  Pcen1,Pcen2,Walks : Word;
  Wsum,Esum : LongInt;
  Base : array[0..19] of Byte = (
    0,0, 0,1, 2,1, 2,2, 3,2, 3,3, 4,3, 4,4, 5,5, 6,6);
  Ememo : array[0..9] of Byte = (
    0,0,0,0,0,0,0,0,0,0);
    


implementation

{$R *.dfm}

procedure TRei34.FormCreate(Sender: TObject);
begin
  Randomize;
  St := 0;
  Area := 0;
  Walks := 0;
  Wsum := 0;
  Esum := 0;
  Pcen1 := Base[Area * 2];
  Ptime := Base[Area * 2 + 1];
  Pcen2 := Pcen1 + Ptime * Walks;
end;

procedure TRei34.Timer1Timer(Sender: TObject);
var
  n : Byte;

begin
  if St = 1 then
  begin
    Walks := Walks + 1;       //步行计数
    Edit4.Text := ' ' + IntToStr(Walks);     //显示步数
    Pcen2 := Pcen1 + Ptime * Walks ;  //计算几率
    Edit5.Text := ' ' + FloatToStr(Pcen2 / 10) + '% ';    //用百分比显示几率
    Edit6.Text := ' ';
    if Random(1000) < Pcen2 then  //敌军出现判断?
    begin
      Emy := Random(9) + 1;        //随机敌军编号
      Edit6.Text := '敌军' + IntToStr(Emy) + '出现了';
      for n := 8 downto 0 do       //出现敌军的步数记录 在数组中
        Ememo[n + 1] := Ememo[n];
      Ememo[0] := Walks;
      n := 0;
      Edit7.Text := ' ';       //显示
      repeat
        Edit7.Text := Edit7.Text + ' ' + IntToStr(Ememo[n]);
        n := n + 1;
      until (n = 10) or (Ememo[n] = 0);

      Wsum := Wsum + Walks;   //总步数
      Esum := Esum + 1;       //出现敌军计数
      Edit8.Text := IntToStr(Wsum div Esum) ;    //计算平均出现敌军步数
      Walks := 0;         //出现敌军步数清零,

      St := 0;            //停止步行
    end;

  end;
end;

procedure TRei34.Button1Click(Sender: TObject);
begin
  St := St xor 1;                    //步行,停止,轮换
end;

procedure TRei34.Button2Click(Sender: TObject);
begin
  St := 0;                        //停止步行 计数
  if Area < 9 then               //调整区域,+ 1
    Area := Area + 1
  else
    Area := 0;
  Pcen1 := Base[Area * 2];         //读取预设的数据 ,基本点
  Ptime := Base[Area * 2 + 1];     //读取预设的数据,系数
  Pcen2 := Pcen1 + Ptime * Walks;  //计算几率
  Edit1.Text := ' ' + IntToStr(Area);
  Edit2.Text := ' ' + IntToStr(Pcen1);
  Edit3.Text := ' ' + IntToStr(Ptime);
  Edit4.Text := ' ' + IntToStr(Walks);
  Edit5.Text := ' ' + FloatToStr(Pcen1 / 10) + '%';
end;

procedure TRei34.Button3Click(Sender: TObject);   //复位初始化
begin
  St := 0;
  Walks := 0;
  Wsum := 0;
  Esum := 0;
  Pcen1 := Base[Area * 2];
  Ptime := Base[Area * 2 + 1];
  Pcen2 := Pcen1 + Ptime * Walks;
  Ememo[0] := 0;
  Edit1.Text := ' ' + IntToStr(Area);
  Edit2.Text := ' ' + IntToStr(Pcen1);
  Edit3.Text := ' ' + IntToStr(Ptime);
  Edit4.Text := ' ' + IntToStr(Walks);
  Edit5.Text := ' ' + FloatToStr(Pcen2 / 10) + '%';
  Edit6.Text := '  ';
  Edit7.Text := '  ';
  Edit8.Text := '  ';
end;

end.

1,结构比较简单,在TIMER中计算变化,可以说步行随时间变化?

2,区域越高,相应的 遇敌概率越大 这个是在 数组中已布置好的

3,遇敌判断是个1000的以下的随机数,比较是小于 计算后的步数乘以系数 则遇敌

 

 Base : array[0..19] of Byte = (
    0,0, 0,1, 2,1, 2,2, 3,2, 3,3, 4,3, 4,4, 5,5, 6,6);

 

Pcen1 := Base[Area * 2]; // area 0-9
Ptime := Base[Area * 2 + 1];
Pcen2 := Pcen1 + Ptime * Walks;

if Random(1000) < Pcen2 then  //敌军出现判断?

可知 0 区不会遇敌
 
 
posted @ 2022-10-19 16:36  D7mir  阅读(89)  评论(0)    收藏  举报