使用 Public 整数变量作为计数器 C#

Using a Public Integer Variable as a Counter C#

我正在 Visual Studio 中创建一个口袋妖怪类型的游戏。我现在正在处理库存表。

我的问题是我可以使用 public 计数变量来跟踪库存吗?

正在设置:

public int healthPotionCount = 0;

然后如果玩家遇到 healthPotion 增加它的价值

if(picPlayer.Bounds.IntersectsWith(hPotion.Bounds)
      {
          healthPotionCount++;
      }

最后使用相同的 public 变量来确定是否在清单中显示它:

if(healthPotionCount > 0)
   {
       picBox.BackgroundImage = (the health potion image);
       lblQuantity.Text = ""+healthPotionCount; 
   }

有了这个,我的问题是我的解决方案中有很多表单,我希望所有表单都可以访问这个变量。这个逻辑是不是太坑爹了?

With this, my issue is that I have a lot of forms across my solution and I want this variable to be accessible by all forms.

您可以将其设为 static,这意味着它属于 class 而不是特定的表单对象,因此您可以在多个表单中访问它。

public static int healthPotionCount { get; set;}