【发布时间】:2018-02-13 00:07:09
【问题描述】:
我正在创建一个项目,当我单击按钮时会显示随机图像,并且必须猜测图像(骰子)的总数。我已经记录了随机图像生成并跟踪人员的滚动数量。然而;我不知道如何使骰子(图像)具有一定的价值。就像骰子 5 显示的一样,它的值为 4。这个人将猜测放在guessBx 中,然后单击guessBtn,如果它们正确或不正确,它就会弹出。
这是我现在的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PetalsAroundTheRose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int a = 0;
int diceRoll;
int diceImage;
int diceValue;
private void btnRoll_Click_1(object sender, EventArgs e)
{
Random random = new Random();
picBx1.Image = imageList1.Images[random.Next(1, 6)];
picBx2.Image = imageList1.Images[random.Next(1, 6)];
picBx3.Image = imageList1.Images[random.Next(1, 6)];
picBx4.Image = imageList1.Images[random.Next(1, 6)];
a++;
txtBxRolls.Text = a.ToString();
//each dice seperate
//dice 1
diceRoll = 1;
diceImage = imageList1.Images[(1)];
diceValue = 0;
//dice 2
diceRoll = 1;
diceImage = imageList1.Images[(2)];
diceValue = 0;
//dice 3
diceRoll = 1;
diceImage = imageList1.Images[(3)];
diceValue = 2;
//dice 4
diceRoll = 1;
diceImage = imageList1.Images[(4)];
diceValue = 0;
//dice 5
diceRoll = 1;
diceImage = imageList1.Images[(5)];
diceValue = 4;
}
private void guessBx_TextChanged_1(object sender, EventArgs e)
{
}
}
}
在我的设计中,我使用 btnRoll 来掷骰子。 guessBx 用于输入猜测,btnGuess,txtBxCorrect 用于输入正确的数量,txtBxResult 用于说明它们是否正确
图像在 imageList 中
【问题讨论】:
-
你能解释一下吗。图像是骰子每一面的图像吗?并且您要检查输入的图像和数字是否相同?
-
是的,每个图像都是模具的一面。图像“滚动”后,可以检查显示的图像值是否正确。类似于花瓣的是玫瑰游戏。
-
从 (1-7] 中选择一个随机数。这将告诉您掷骰子的值。根据它们的列出方式将其用作 ImageList 的索引
-
...对整个应用程序也使用一个随机实例 - 不要在每次点击时创建一个新实例
-
不要使用 ImageList,而是使用 Image 和 Int32 作为属性创建一个自定义类,以将图像及其值放入其中,然后将它们的 List 作为随机选择的源。然后你就可以得到所选对象的图像和值。