delphi - 如何使 Delphi 中的事件只发生一次?

标签 delphi if-statement pascal

我是一名初级程序员,我正在尝试使用 Delphi Pascal 语言制作一款基于文本的 RPG 游戏(如 Zork)。 我做了一个事件,主角打开箱子并找到一些元素:

begin
  text1.text := 'You see a chest. It is unlocked.';
       end;
  if edit1.Text = 'Open Chest' then
  text1.Text := 'You found 50 Gold Pieces, a Short Sword, a Cloth Armor and a Satchel Bag.';
end;

但我想以一种方式做到这一点,每当有人在第一次之后打开箱子时,箱子就会是空的,因为玩家已经拿走了元素。 换句话说,当有人第二次在 TEdit 中输入“打开箱子”时,它会显示类似“它是空的”之类的内容。

但是如何呢?

最佳答案

你必须使用额外的变量来告诉你箱子是否已经被打开。

var
  ChestOpened: boolean;

// initialize at beginning
ChestOpened := false;

...

if Edit1.text = 'Open Chest' then
begin
  if ChestOpened then
    Text1.Text := 'Chest is empty'
  else
  begin
    ChestOpened := true;
    Text1.Text := 'You found 50 Gold Pieces, a Short Sword, a Cloth Armor and a Satchel Bag.'
  end;
end;

关于delphi - 如何使 Delphi 中的事件只发生一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28705670/

相关文章:

Python if 表达式无法正确处理原始输入

c++ - OR (||) 运算符在 C++ 中无法正常工作

dll - Inno 脚本从互联网异步下载文件

delphi - '0。 0' is not a valid timestamp when accessing OldValue of TClientDataSet' s TDateField

delphi - FireDac 连接问题

c++ - 将 WM_MOUSEWHEEL Delphi 代码转换为 C++ Builder

powershell - 在 PowerShell 的 Active Directory 中测试计算机对象是否存在

delphi - 这里的观察者实现是否存在内存泄漏?

delphi - 在lazarus或delphi中调整图像大小?

python - Pascal 类型化二进制文件的 C++ 和 Python 类似物