2016-02-04 30 views
1

我想使点数购买系统使用数字上/下。这里的想法是: 有六个数字上/下,每一个特点(力量,敏捷,体质,智力,智慧和魅力)。每个特质从10点开始。你不能把一个特质低于7或高于18点购买系统的桌面RPG

我是一个总的小白,但我成功地做到这一点:

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.

有一个可爱的”框在我的表单中显示剩下多少分是在顶部的樱桃。

我尽我所能解释了这一点。请注意,英语不是我的母语,我有时会为此而苦恼。

我对如何实现这一目标毫无头绪,因为我几乎没有任何C#知识。代码会丢失什么?

+0

如果您调用'numericUpDown1.Maximum = 18; numericUpDown1.Minimum = 7;'onvalueChanged',它会在每个项目的每次更改时执行。你可以在设计器中设置这个值,或者用'formLoaded'代替它。 – mcy

+0

你可以显示你的aspx代码吗? – mikeyq6

+0

@ mikeyq6它的标签为WinForms。 – LarsTech

回答

0

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

首先,将这些事件功能之外:

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个stat箱重复使用相同的事件方法numericUpDown1_ValueChanged

1

它将更容易,如果你创建人物类

您可以定义默认的构造函数中的每个属性,和个人的方法来增加或减少其分。

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.DecreaseWisdom()...等

此外,在该选项,您可以永诺重用这个在游戏中的任何部分.. (例如:如果你的角色发现任何特殊药水..然后CharacterFoo.IncreaseStrength())

希望这有助于...

0

如果我是你,我会和一个班级一起去做chara因为它会大大简化您的未来工作,但如果您是新手,那么一开始可能会比较困难。 不过,你可以跟进的基本方法如下:

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(); 
    } 
} 
} 
0

下面是一些伪代码: 你想创建一个变量来保存当前的点。创建一些标签来保存该变量,并确保您进行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了解更多信息。