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



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

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

我的程序.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 = "-";
}
}
}
}
}

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

通常UI游戏是由用户与控件的交互驱动的。因此,你只需要一个写着"滚动"的按钮,而不是一个有"WaitForRoll"的无限循环,当他们点击它时,你就可以为特定事件(掷骰子)运行代码。

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

最新更新