【发布时间】:2016-03-18 09:03:50
【问题描述】:
我是 OOP 和 C# 的初学者。
我正在使用 Windows 窗体开发一个问答游戏。 我的问题与两个类有关,form 和game logic。 我有一个带有经典 Froms 控件的基本 UI。看看吧。
我想要实现的是,当玩家按下任何答案按钮时,它会用红色或绿色突出显示按下的按钮,具体取决于答案是正确还是错误。更改颜色后,我希望程序等待一段时间,然后转到下一个问题。
问题是,我不知道如何正确实现这一点。我不知道如何使用线程以及 Form 应用程序如何与线程相关。我应该使用线程睡眠、定时器还是异步?
我会告诉你游戏逻辑类中应该处理这个的方法。
public static void Play(char answer) //Method gets a char representing a palyer answer
{
if (_rightAnswer == answer) //If the answer is true, the button should become green
{
Program.MainWindow.ChangeBtnColor(answer, System.Drawing.Color.LightGreen);
_score++;
}
else //Otherwise the button becomes Red
{
Program.MainWindow.ChangeBtnColor(answer, System.Drawing.Color.Red);
}
//SLEEP HERE
if (!(_currentIndex < _maxIndex)) //If it is the last question, show game over
{
Program.MainWindow.DisplayGameOver(_score);
}
else //If it is not the last question, load next question and dispaly it and finally change the button color to default
{
_currentIndex++;
_currentQuestion = Database.ListOfQuestions.ElementAt(_currentIndex);
_rightAnswer = _currentQuestion.RightAnswer;
Program.MainWindow.DisplayStats(_score, _currentIndex + 1, _maxIndex + 1);
Program.MainWindow.DisplayQuestion(_currentQuestion.Text);
Program.MainWindow.DisplayChoices(_currentQuestion.Choices);
}
Program.MainWindow.ChangeBtnColor(answer, System.Drawing.SystemColors.ControlLight);
}
我不想完全阻止 UI,但我也不希望用户在暂停期间通过按其他按钮来进行其他事件。因为这会导致应用程序运行不正常。
【问题讨论】:
标签: c# winforms delay thread-sleep pause