【问题标题】:Pig game - Roll the dice猪游戏 - 掷骰子
【发布时间】:2014-05-05 10:27:42
【问题描述】:

目前,当我单击“单击以掷骰子”按钮后,骰子图像会发生变化。
当我点击“点击掷骰子”底部时,我还想更新标签“玩家总数”。

我如何做到这一点?

private void rollDieBotton_Click(object sender, EventArgs e) 
{
    RollDice();
}       

private void RollDice() 
{
    for (int i = 0; i < dice.Length; i++)
        dice[i] = roll.Next(1, 6);

    dicePictureBox.Image = diceImages[dice[0]];
}

private void PigForm_Load(object sender, EventArgs e) 
{
    diceImages = new Image[6];
    diceImages[0] = Properties.Resources.Alea_1;
    diceImages[1] = Properties.Resources.Alea_2;
    diceImages[2] = Properties.Resources.Alea_3;
    diceImages[3] = Properties.Resources.Alea_4;
    diceImages[4] = Properties.Resources.Alea_5;
    diceImages[5] = Properties.Resources.Alea_6;

    dice = new int[1] { 0 };

    roll = new Random();
} 

【问题讨论】:

  • 您想汇总骰子结果并呈现给用户?
  • 嘿chrfin !是的,我想在单击“rolldiebotton”并将它们呈现给用户后总结骰子结果
  • 请注意 - 对您正在使用的 Random 实例 roll.Next(1, 6) 的调用将仅返回 1 到 5 之间的数字。第二个参数是“独占”上限.如果您想要从 1 到 6 的数字,则无需将其称为 roll.Next(1, 7)
  • @Enigmativity 知道了!!干杯

标签: c# arrays winforms random dice


【解决方案1】:

您可以使用dice 值对卷进行求和,而不仅仅是存储当前卷并执行以下操作:

private void RollDice() 
{
    for (int i = 0; i < dice.Length; i++)
    {
        var currenRoll = roll.Next(1, 6);
        dice[i] += currentRoll;
        dicePictureBox.Image = diceImages[currentRoll];

        playerTotalLabel.Text = String.Format("Total: {0}", dice[i]);
    }
}

【讨论】:

    猜你喜欢
    • 2015-12-24
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多