桌面角色扮演游戏的点购买系统



我正在尝试使用数字向上/向下创建一个点数购买系统。这个想法是这样的:有六个数字向上/向下,每个特征一个(力量,灵巧,体质,智力,智慧和魅力(。每个特征从 10 分开始。你不能带来低于 7 或高于 18 的特质。

我是一个彻头彻尾的菜鸟,但我设法做到了这一点:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            numericUpDown1.Maximum = 18;
            numericUpDown1.Minimum = 7;
        }

我做了六次。在我的表格中,现在有六个数字向上/向下。现在我正在尝试做一些对我微不足道的知识来说

太多的事情。

我想要一个系统,其中六个数字向上下降的值是组合在一起并且不能超过的,这意味着在这种情况下,我们将有 60 分,除非我们减少一个,否则无法增加任何分数。我会在"积分池"中添加 15 分,这样用户就不必为了增加另一个统计数据而立即减少统计数据。

示例:我还剩 1 分,我的分数如下:15、15、14、10、10、10。我将第三个分数提高 1 分。我现在有这个:

15, 15, 15, 10, 10, 10.

现在我什么都没有了,但我希望我的第四分是15分。为了实现这一目标,我必须降低第五和第六分,直到我腾出 5 分。我现在有这个:

15, 15, 15, 15, 7, 8.

在我的表单中有一个 Lil' 框来显示剩余的点数将是顶部的樱桃。

我尽力解释这一点。请注意,英语不是我的母语,我有时会为此而挣扎。

我不知道如何实现这一目标,因为我几乎不了解 C#。代码会丢失什么?

如果你创建一个角色类会更容易

您可以为构造函数中的每个属性定义默认值,以及增加或减少其点的单个方法。

public class Character
{
    private int totalPointsMax = 60;
    private int maxPoints = 18;
    private int minPoints = 7;
    public int Strength { get; set; }
    public int Dexterity { get; set; }
    public int Constitution { get; set; }
    public int Intelligence { get; set; }
    public int Wisdom { get; set; }
    public int Charisma { get; set; }
    public Character()
    {
        // create new Character defaults...
        Strength = 10;
        Dexterity = 10;
        Constitution = 10;
        Intelligence = 10;
        Wisdom = 10;
        Charisma = 10;
    }
    private int GetTotalCharacterPoints()
    {
        return Strength + Dexterity + Constitution + Intelligence + Wisdom + Charisma;
    }
    //example of method to increase Strength
    public void IncreaseStrength()
    {
        int availablePoints = totalPointsMax - GetTotalCharacterPoints();
        if (availablePoints > 0 && Strength < maxPoints)
            Strength++;
    }
    //example of method to decrease Strength
    public void DecreaseStrength()
    {
        if (Strength >= minPoints)
            Strength--;
    }
    //missing the other increase/decrease methods for the rest of features...
}

你只需要在开始时实例化,你的UI按钮只需要调用CharacterFoo.IncreaseStrength((或CharacterFoo.ReductionWisdom(( ...等。

此外,使用此选项,您可以在游戏的任何部分重复使用它。(例如:如果你的角色发现任何特殊的药水..那么CharacterFoo.IncreaseStrength((

希望这有帮助...

有几种方法可以做到这一点。

首先,将它们移到事件函数之外:

numericUpDown1.Maximum = 18;
numericUpDown1.Minimum = 7;

我的建议是设置一个具有最大点数的变量:

private int MAX_POINTS = 60;

然后,当其中一个被更改时,您可以调用另一个将所有当前框相加的方法,并确定它是否超过限制:

private bool TotalOfStatsIsOverLimit() {
    int total = GetTotalOfStats();
    return total > MAX_POINTS;
}
private int GetTotalOfStats() {
    int total = 0;
    // TODO: Go through each number box and add to the total
    return total;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
    if(TotalOfStatsIsOverLimit()) {
         // TODO: Decrease the value of the stat box that was updated
    }
}

这样做的一个优点是,您可以对所有 6 个统计信息框重复使用相同的事件方法numericUpDown1_ValueChanged

如果我是你,我会选择角色类,因为它会大大简化你未来的工作,但如果你是新手,一开始可能会更难。不过,您可以按如下方式遵循基本方法:

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    private const int totalPoints = 60 + 15; // sum of each trait plus the pool bonus
    public Form1()
    {
        InitializeComponent();
        foreach (Control control in this.Controls)
        {
            if (control is NumericUpDown)
            {
                NumericUpDown numControl = control as NumericUpDown;
                numControl.Minimum = 7;
                numControl.Maximum = 18;
                numControl.Value = 10;
                numControl.ValueChanged += nud_ValueChanged;
                lblPointsLeft.Text = "15"; // bonus points
            }
        }
    }
    private void nud_ValueChanged(object sender, EventArgs e)
    {
        int sum = (int)(nudCha.Value + nudCon.Value + nudDex.Value + nudInt.Value + nudStr.Value + nudWis.Value);
        int pointsLeft = totalPoints - sum;
        NumericUpDown nudSender = (NumericUpDown)sender;
        if (pointsLeft < 0)
        {
            MessageBox.Show("No points left");
            // restore last change
            // undo the last change 
            nudSender.Value = nudSender.Value - 1;
            pointsLeft++;

        }
        lblPointsLeft.Text = pointsLeft.ToString();
    }
}
}
下面是

一些伪代码:您希望创建一个变量来保存当前点。 创建一些标签来保存该变量,并确保执行 AJAX 调用,否则每次更新时都将再次从服务器调用此变量。 这可能在Javascript/Jquery中做得更好。

int pointsUsed = numericUpDown1.value + numericUpDown2.value + numericUpDown6.value; //add all 6 of your values.
//for your textbox:
label1.text = "points left is:" 
label2.text = 75 - pointsUsed;
private void checkBox1_Click(Object sender, EventArgs e)
{//to add points
   if (pointsUsed < 75)
   {
      numericUpDown1.Value += 1;
      pointsUsed += 1;
   }
}

查看 MSDN NumericUpDown 了解更多信息。

最新更新