C# 如何随机化位置
我有一个C# 如何随机化位置,c#,winforms,random,C#,Winforms,Random,我有一个winforms的应用程序,它具有记忆我游戏的逻辑。 我想要的是,每当我点击重置按钮或运行游戏时,我的按钮的位置都会不同(隆隆声),因此游戏会很有趣:) 这是我到目前为止的代码 public partial class Form1 : Form { int b1 = 1, b2 = 2, b3 = 3,b4 = 4,b5 = 5,b6 = 6; public Form1() { InitializeComponent(); } p
winforms
的应用程序,它具有记忆我游戏的逻辑。
我想要的是,每当我点击重置按钮或运行游戏时,我的按钮的位置都会不同(隆隆声),因此游戏会很有趣:)
这是我到目前为止的代码
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 = "";
}
}
好吧,如果你说不同,意味着位置是随机的,那么你就有了位置属性,你可以使用它
Random rand = new Random(5);
button1.Location = new Point(
rand.Next(Width - button1.Width),
rand.Next(Height - button1.Height))
如果你的意思是代码不同,你需要有一个随机标记,每个按钮在被点击时都会检查他是否得到了标记。如果你说的不同,意味着位置是随机的,那么你就有了可以使用的位置属性
Random rand = new Random(5);
button1.Location = new Point(
rand.Next(Width - button1.Width),
rand.Next(Height - button1.Height))
如果你的意思是代码不同,你需要有一个随机标记,每个按钮在被点击时都会检查他是否得到了标记我想我理解,你希望每个游戏的按钮位置都不同 创建一个类来存储位置:
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);
}
}
private void Form1\u加载(对象发送方,事件参数e)
{
//创建一个位置列表。
列表按钮位置=新建列表(){
新按钮位置{Left=100,Top=100},
新按钮位置{Left=100,Top=200},
新按钮位置{Left=100,Top=300},
新按钮位置{Left=300,Top=400},
新按钮位置{Left=300,Top=500},
新按钮位置{Left=300,Top=600}
};
//实例化随机
随机=新随机();
//环游不同的地点
while(buttonLocations.Count()>0)
{
//生成从0到-1的可能位置数的随机整数
int randomButtonLocationIndex=random.Next(0,buttonLocations.Count()-1);
//创建新按钮,请记住将事件添加到此新按钮!
按钮按钮=新按钮();
button.Left=按钮位置[randomButtonLocationIndex]。左侧;
button.Top=按钮位置[randomButtonLocationIndex].Top;
此.Controls.Add(按钮);
按钮定位。移除(随机按钮定位索引);
}
}
Martyn我想我明白了,你希望每个游戏的按钮位置都不同 创建一个类来存储位置:
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);
}
}
private void Form1\u加载(对象发送方,事件参数e)
{
//创建一个位置列表。
列表按钮位置=新建列表(){
新按钮位置{Left=100,Top=100},
新按钮位置{Left=100,Top=200},
新按钮位置{Left=100,Top=300},
新按钮位置{Left=300,Top=400},
新按钮位置{Left=300,Top=500},
新按钮位置{Left=300,Top=600}
};
//实例化随机
随机=新随机();
//环游不同的地点
while(buttonLocations.Count()>0)
{
//生成从0到-1的可能位置数的随机整数
int randomButtonLocationIndex=random.Next(0,buttonLocations.Count()-1);
//创建新按钮,请记住将事件添加到此新按钮!
按钮按钮=新按钮();
button.Left=按钮位置[randomButtonLocationIndex]。左侧;
button.Top=按钮位置[randomButtonLocationIndex].Top;
此.Controls.Add(按钮);
按钮定位。移除(随机按钮定位索引);
}
}
马汀