在游戏中循环之前加载表单

时间:2015-04-21 04:03:28

标签: c# winforms

我正在使用C#开发游戏。但它有一些无限循环阻止表单被加载。我在互联网上尝试了很多解决方案,但这似乎不起作用。

我的问题是:“如何在执行while循环之前加载表单。”?

我的Program.cs来源

namespace SumSwamp
{
static class Program
{

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var window = new Form1();
        window.Show();

    }
}
}

Form1.cs:

namespace SumSwamp
{
public partial class Form1 : Form
{
    public static int DieLargeNum = 0;
    public static int DieSmallNum = 0;
    public static int DieOperator = 0;
    public static int DieTotal = 0;
    public static int TotalSpaces = 42;
    public static int CompSum = 0;
    public static int PlayerSum = 0;
    public static Boolean PlayersRoll = true;
    public static Boolean WaitForRoll = true;
    public static int Turn = 0;

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(this.Form1_Load);
        while(Turn == 0) //INFINITE LOOP
        {
            if (WaitForRoll==false)
            {
                DieTotal=DieLargeNum;
                Random rnd1 = new Random();
                DieLargeNum = rnd1.Next(1, 7);
                if (DieTotal>DieLargeNum)
                {
                    Turn = 1;
                    labelStatus.Text = "Player 1's Turn";
                    WaitForRoll=true;
                }
                else
                {
                    Turn = 2;
                    labelStatus.Text = "Player 2's Turn";
                    WaitForRoll = false;
                }
            }
        }

        while ((CompSum < TotalSpaces) & (PlayerSum < TotalSpaces))//INFINITE LOOP
        {
            while (Turn == 1)
            {
                if (WaitForRoll == false)
                {
                    if (DieOperator == 1)
                    {
                        DieTotal = DieLargeNum + DieSmallNum;
                    }
                    else
                    {
                        if (DieLargeNum > DieSmallNum)
                        {
                            DieTotal = DieLargeNum - DieSmallNum;
                        }
                        else
                        {
                            DieTotal = DieSmallNum - DieLargeNum;
                        }

                    }
                    PlayerSum = PlayerSum + DieTotal;
                    Turn = 2;
                    PlayersRoll = false;
                    labelStatus.Text = "Player 2's Turn";
                }
            }

            while (Turn == 2)
            {
                Random rnd1 = new Random();
                DieLargeNum = rnd1.Next(1, 7);
                Random rnd2 = new Random();
                DieSmallNum = rnd2.Next(1, 7);
                Random rnd3 = new Random();
                DieOperator = rnd3.Next(1, 3);
                labelDieLargeNum.Text = DieLargeNum.ToString();
                labelDieSmallNum.Text = DieSmallNum.ToString();
                if (DieOperator == 1)
                {
                    labelDieOperator.Text = "+";
                }
                else
                {
                    labelDieOperator.Text = "-";

                }

                if (DieOperator == 1)
                {
                    DieTotal = DieLargeNum + DieSmallNum;
                }
                else
                {
                    if (DieLargeNum > DieSmallNum)
                    {
                        DieTotal = DieLargeNum - DieSmallNum;
                    }
                    else
                    {
                        DieTotal = DieSmallNum - DieLargeNum;
                    }
                }
                CompSum = CompSum + DieTotal; 
                Turn = 1;
                PlayersRoll = true;
                labelStatus.Text = "Player 1's Turn";                    
            }

        }
        if (CompSum>=TotalSpaces)
        {
            labelResult.Text = "CPU Player has won!";
        }
        else
        {
            labelResult.Text = "You win!";
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void buttonRoll_Click(object sender, EventArgs e)
    {
        if (PlayersRoll == true)
        {
            Random rnd1 = new Random();
            DieLargeNum = rnd1.Next(1, 7);
            Random rnd2 = new Random();
            DieSmallNum = rnd2.Next(1, 7);
            Random rnd3 = new Random();
            DieOperator = rnd3.Next(1, 3);
            WaitForRoll = false;
            labelDieLargeNum.Text = DieLargeNum.ToString();
            labelDieSmallNum.Text = DieSmallNum.ToString();
            if(DieOperator == 1)
            {
                labelDieOperator.Text = "+";
            }
            else
            {
                labelDieOperator.Text = "-";

            }

        }
    }

}
}

1 个答案:

答案 0 :(得分:1)

如果将无限循环放在Form.Enter()事件中,则首先加载表单然后运行循环。但如果您真的处于无限循环中,UI将无法响应。

通常,UI游戏由用户与控件的交互驱动。所以,不是一个无限循环,你有#34; WaitForRoll&#34;,你只需要一个按钮,说出&#34;滚动&#34;,当他们点击它时,你运行你的特定事件的代码(滚动骰子)。

如果在用户不与控件交互时必须运行循环,则应在单独的线程中完成,例如BackgroundWorker或Task。