我有一个winforms
的应用程序与REMEMBER me game的逻辑。
我想要的是,当我点击重置按钮或运行游戏时,我的按钮的位置将是不同的(隆隆声),以便游戏将是有趣的:)
到目前为止这是我的代码
public partial class Form1 : Form
{
int b1 = 1, b2 = 2, b3 = 3,b4 = 4,b5 = 5,b6 = 6;
public Form1()
{
InitializeComponent();
}
private void Wrong_Tick(object sender, EventArgs e)
{
progressBar1.Increment(10);
if (progressBar1.Value == 100)
{
Wrong.Stop();
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
b1 = 1;
b2 = 2;
b3 = 3;
b4 = 4;
b5 = 5;
b6 = 6;
progressBar1.Value = 0;
}
}
private void Tb1b2_Tick(object sender, EventArgs e)
{
progressBar1.Increment(10);
if (progressBar1.Value == 100)
{
Tb1b2.Stop();
button1.Visible = false;
button2.Visible = false;
progressBar1.Value = 0;
}
}
private void Tb3b4_Tick(object sender, EventArgs e)
{
progressBar1.Increment(10);
if (progressBar1.Value == 100)
{
Tb3b4.Stop();
button3.Visible = false;
button4.Visible = false;
progressBar1.Value = 0;
}
}
private void Tb5b6_Tick(object sender, EventArgs e)
{
progressBar1.Increment(10);
if (progressBar1.Value == 100)
{
Tb5b6.Stop();
button5.Visible = false;
button6.Visible = false;
progressBar1.Value = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
if(b1 == 1)
{
b1 = 0;
button1.Text = "kevin";
if (b1 == b2)
{
Tb1b2.Start();
}
else if (b1 == b3)
{
Wrong.Start();
}
else if (b1 == b4)
{
Wrong.Start();
}
else if (b1 == b5)
{
Wrong.Start();
}
else if (b1 == b6)
{
Wrong.Start();
}
}
else if (b1 == 0)
{
b1 = 1;
button1.Text = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (b2 == 2)
{
b2 = 0;
button2.Text = "kevin";
if (b2 == b1)
{
Tb1b2.Start();
}
else if (b2 == b3)
{
Wrong.Start();
}
else if (b2 == b4)
{
Wrong.Start();
}
else if (b2 == b5)
{
Wrong.Start();
}
else if (b2 == b6)
{
Wrong.Start();
}
}
else if (b1 == 0)
{
b2 = 2;
button2.Text = "";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (b3 == 3)
{
b3 = 0;
button3.Text = "kath";
if (b3 == b4)
{
Tb3b4.Start();
}
else if (b3 == b1)
{
Wrong.Start();
}
else if (b3 == b2)
{
Wrong.Start();
}
else if (b3 == b5)
{
Wrong.Start();
}
else if (b3 == b6)
{
Wrong.Start();
}
}
else if (b3 == 0)
{
b3 = 3;
button1.Text = "";
}
}
private void button4_Click(object sender, EventArgs e)
{
if (b4 == 4)
{
b4 = 0;
button4.Text = "kath";
if (b4 == b3)
{
Tb3b4.Start();
}
else if (b4 == b1)
{
Wrong.Start();
}
else if (b4 == b2)
{
Wrong.Start();
}
else if (b4 == b5)
{
Wrong.Start();
}
else if (b4 == b6)
{
Wrong.Start();
}
}
else if (b4 == 0)
{
b4 = 3;
button4.Text = "";
}
}
private void button5_Click(object sender, EventArgs e)
{
if (b5 == 5)
{
b5 = 0;
button5.Text = "eka";
if (b5 == b6)
{
Tb5b6.Start();
}
else if (b5 == b1)
{
Wrong.Start();
}
else if (b5 == b2)
{
Wrong.Start();
}
else if (b5 == b3)
{
Wrong.Start();
}
else if (b5 == b4)
{
Wrong.Start();
}
}
else if (b5 == 0)
{
b5 = 5;
button5.Text = "";
}
}
private void button6_Click(object sender, EventArgs e)
{
if (b6 == 6)
{
b6 = 0;
button6.Text = "eka";
if (b6 == b5)
{
Tb5b6.Start();
}
else if (b6 == b1)
{
Wrong.Start();
}
else if (b6 == b2)
{
Wrong.Start();
}
else if (b6 == b3)
{
Wrong.Start();
}
else if (b6 == b4)
{
Wrong.Start();
}
}
else if (b6 == 0)
{
b6 = 6;
button6.Text = "";
}
}
private void Reset_Click(object sender, EventArgs e)
{
b1 = 1;
b2 = 2;
b3 = 3;
b4 = 4;
b5 = 5 ;
b6 = 6;
button1.Visible = true;
button2.Visible = true;
button3.Visible = true;
button4.Visible = true;
button5.Visible = true;
button6.Visible = true;
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
}
}
答案 0 :(得分:1)
我想我明白,你希望每个游戏都有不同按钮的位置。
创建一个用于存储位置的类:
public class ButtonLocation
{
public int Left {get;set;}
public int Top {get;set;}
}
然后,使用每个可能的位置填充列表,然后使用Random
选择一个位置,并在那里放一个按钮,这样做取决于您,因为您可能想要创建如果您希望将来有更多按钮,可以使用更具可扩展性的东西。
用于随机选择位置的继承人代码:
private void Form1_Load(object sender, EventArgs e)
{
//Create a list of locations.
List<ButtonLocation> buttonLocations = new List<ButtonLocation>() {
new ButtonLocation {Left = 100, Top = 100},
new ButtonLocation {Left = 100, Top = 200},
new ButtonLocation {Left = 100, Top = 300},
new ButtonLocation {Left = 300, Top = 400},
new ButtonLocation {Left = 300, Top = 500},
new ButtonLocation {Left = 300, Top = 600}
};
//Instantiate Random
Random random = new Random();
//Loop through the different Locations
while (buttonLocations.Count() > 0)
{
//Generate a random int from 0 to -1 of the number of possible locations
int randomButtonLocationIndex = random.Next(0, buttonLocations.Count() - 1);
//Create New button, Rememeber to add the event to this new button!
Button button = new Button();
button.Left = buttonLocations[randomButtonLocationIndex].Left;
button.Top = buttonLocations[randomButtonLocationIndex].Top;
this.Controls.Add(button);
buttonLocations.RemoveAt(randomButtonLocationIndex);
}
}
马丁
答案 1 :(得分:0)
好吧,如果你说不同并且意味着该位置是随机的,那么你就拥有了你可以玩的位置属性。
Random rand = new Random(5);
button1.Location = new Point(
rand.Next(Width - button1.Width),
rand.Next(Height - button1.Height))
如果你的意思是代码不同,你需要有一个随机令牌,每个按钮在被点击时会检查他是否有令牌