1

我有一个应用程序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 = "";

    }
}
4

2 回答 2

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);
        }





    }

马丁

于 2013-07-28T07:31:42.150 回答
0

好吧,如果您说不同并且意味着该位置是随机的,那么您就有了可以使用的位置属性。

Random rand = new Random(5);
button1.Location = new Point(
       rand.Next(Width - button1.Width), 
       rand.Next(Height - button1.Height))

如果您的意思是代码不同,您需要有一个随机令牌,每个按钮在被点击时都会检查他是否得到了令牌

于 2013-07-28T07:09:22.667 回答