我试图只使用gameover bool显示一个消息框来控制情况,但它不起作用,它显示无限的消息框。
bool player2turn = false;
bool gameover = false;
private void timer1_Tick(object sender, EventArgs e)
{
if ((pb00.BackColor == pb10.BackColor) && (gameover == false))
{
winner();
}
}
private void winner()
{
if (player2turn == false)
{
MessageBox.Show("Player 2 wins!", "Retry?", MessageBoxButtons.YesNo);
gameover = true;
}
else
{
MessageBox.Show("Player 1 wins!", "Retry?", MessageBoxButtons.YesNo);
gameover = true;
}
}
答案 0 :(得分:0)
MessageBox.Show
阻止执行代码,直到您关闭消息框,因此您需要在调用MessageBox.Show
之前移动gameover分配。
private void winner()
{
if (player2turn == false)
{
gameover = true;
MessageBox.Show("Player 2 wins!", "Retry?", MessageBoxButtons.YesNo);
}
else
{
gameover = true;
MessageBox.Show("Player 1 wins!", "Retry?", MessageBoxButtons.YesNo);
}
}
答案 1 :(得分:0)
@HansPassant谢谢
private void winner()
{
if (player2turn == false)
{
timer1.Stop();
MessageBox.Show("Player 2 wins!", "Retry?", MessageBoxButtons.YesNo);
gameover = true;
}
else
{
timer1.Stop();
MessageBox.Show("Player 1 wins!", "Retry?", MessageBoxButtons.YesNo);
gameover = true;
}
}