如何使button2更改其BackColor,如果它与button1中的颜色相同?
我的计划如何运作?好吧,我必须按开始(按钮5)混合我的颜色,之后我按检查按钮看到答案。
正如你在图片上看到的,我有两个绿色按钮和两个橙色按钮,但我不希望任何button.backcolors相等。他们都应该是不同的。
换句话说,如果button2.backcolor 与button1.backcolor相同,它应该混合其背景颜色直到它变得不同。我允许使用的唯一颜色是绿色,红色,蓝色,紫色,橙色,黄色
任何想法我如何实现这一目标?
Random random = new Random();
List<Color> possibleColors = new List<Color>()
{
Color.Red,
Color.Green,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Yellow,
};
private Color GetRandomColorOfLoist()
{
return possibleColors[random.Next(0, possibleColors.Count)];
}
private void button5_Click(object sender, EventArgs e)
{
button1.BackColor = GetRandomColorOfLoist();
button2.BackColor = GetRandomColorOfLoist();
button3.BackColor = GetRandomColorOfLoist();
button4.BackColor = GetRandomColorOfLoist();
button1.Visible = false;
button2.Visible = false;
button3.Visible = false;
button4.Visible = false;
}
private void button6_Click(object sender, EventArgs e)
{
button1.Visible = true;
button2.Visible = true;
button3.Visible = true;
button4.Visible = true;
}
编辑1
Arul Manivannans的想法很有效但在按下开始和检查后,如果我再次按下开始,游戏就会崩溃。我的最后一个问题是,如何在点击检查按钮(按钮6)后隐藏开始按钮(按钮5)?
我的代码:
Random random = new Random();
List<Color> possibleColors = new List<Color>()
{
Color.Red,
Color.Green,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Yellow,
};
private Color GetRandomColorOfLoist()
{
int index = random.Next(0, possibleColors.Count);
Color ColorToReturn = possibleColors[index];
possibleColors.Remove(possibleColors[index]);
return ColorToReturn;
}
private void button5_Click(object sender, EventArgs e)
{
button1.BackColor = GetRandomColorOfLoist();
button2.BackColor = GetRandomColorOfLoist();
button3.BackColor = GetRandomColorOfLoist();
button4.BackColor = GetRandomColorOfLoist();
button1.Visible = false;
button2.Visible = false;
button3.Visible = false;
button4.Visible = false;
List<Color> possibleColors = new List<Color>()
{
Color.Red,
Color.Green,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Yellow,
};
}
private void button6_Click(object sender, EventArgs e)
{
button1.Visible = true;
button2.Visible = true;
button3.Visible = true;
button4.Visible = true;
}
编辑2
好的,我明白了。谢谢你的帮助
答案 0 :(得分:0)
从mightColors列表中删除GetRandomColorOfLoist返回的Color。在button5_Click开始时,您必须再次重新填充可能的颜色列表。然后它应该工作。
答案 1 :(得分:0)
为了不重复颜色,如何从列表中删除颜色项?
private Color GetRandomColorOfLoist()
{
int index = random.Next(0, possibleColors.Count);
Color ColorToReturn = possibleColors[index];
possibleColors.Remove(possibleColors[index]);
return ColorToReturn;
}