C#定时作物耕种游戏错误
本文关键字:游戏 错误 定时 | 更新日期: 2023-09-27 18:20:32
我有一个关于计时器的简短问题。在我的代码中,我想创建一个作物种植游戏,用一个计时器来显示植物是否完成
为什么:
public static string currentPlant;
public static Timer growTimer;
public static void InitGrowTimer( int time, string name )
{
growTimer = new Timer();
growTimer.Tick += new EventHandler(growTimer_Finished);
growTimer.Interval = time;
currentPlant = name;
}
public static void plantCrop(string crop)
{
if (plantActive == false)
{
if (plants.Contains(crop.ToLower()))
{
// growTimer.Interval = <plant>Time;
// proceed plants
switch (crop.ToLower())
{
case "wheat":
InitGrowTimer(wheatTime, wheatName);
growTimer.Start();
break;
default:
MessageBox.Show("FATAL ERROR'nThe plant is contained in the definition list but not in the plant menu!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
else
{
MessageBox.Show("This plant is not available!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("There is already a plant in progress! Current plant: {0}", currentPlant);
}
}
private static void growTimer_Finished (object sender, EventArgs e)
{
growTimer.Stop();
MessageBox.Show("Your " + currentPlant + " is finished!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
不启动计时器,或者最后不显示消息框。我在创建计时器或创建勾选事件时做错了什么?
编辑:这是我的主要方法:
static void Main(string[] args)
{
InitializeLists();
// game begin
Farm.plantCrop("wheat");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Please enter your desired name: ");
QC.resetColors();
name = Console.ReadLine();
Console.WriteLine(/*Introduction*/"Welcome to the world of Civilisation. In this world it is your choice what'n" +
"you are up to. You can be a farmer, miner or fighter, whatever you want, the'n" +
"world is yours to explore! Have fun!"
);
Console.ReadKey();
Console.Clear();
while (true) // run game
{
// menu
Console.Write(" What do you want to do?'n" +
"Farm Mine Explore Go to the city'n"
);
input = Console.ReadLine();
if (menuContent.Contains(input.ToLower()))
{
if (input.ToLower() == menuContent.ElementAt(0))
{
// farm
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("-- Farm --'nSelect a crop to plant:");
Console.ForegroundColor = ConsoleColor.DarkGreen;
int icount = 0;
for ( int i = 0; i < Farm.plants.Count; i++)
{
if (icount < 3)
{
Console.Write(Farm.plants.ElementAt(i));
Console.Write("'t't");
icount++;
}
else
{
Console.Write("'n");
icount = 0;
Console.Write(Farm.plants.ElementAt(i));
Console.Write("'t't");
icount++;
}
}
Console.WriteLine();
QC.resetColors();
string crop = Console.ReadLine();
Farm.plantCrop(crop);
}
if (input.ToLower() == menuContent.ElementAt(1))
{
// miner
}
if (input.ToLower() == menuContent.ElementAt(2))
{
// fight
}
}
}
}
System.Windows.Forms.Timer
计时器是为具有单个UI线程的Windows窗体应用程序制作的。
您需要为控制台应用程序使用System.Threading.Timer
计时器。
它的创建和回调的参数有点不同:
public static void InitGrowTimer(int time, string name)
{
growTimer = new System.Threading.Timer(GrowTimer_Finished, null, time, Timeout.Infinite);
currentPlant = name;
}
private static void GrowTimer_Finished(object sender)
{
MessageBox.Show("Your " + currentPlant + " is finished!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
您不需要任何Start
方法,它将在创建时自动启动。
你也不需要Stop
;由于CCD_ 5参数,它将仅运行一次。
如果需要,可以将null
替换为希望回调接收的对象(即EventArgs
中的对象)。
附带说明:我已经在PascalCase中重命名了您的回调方法。按照惯例,C#中的方法应该总是以大写字母开头。
因为你告诉自己,不要启动计时器。
growTimer = new Timer();
growTimer.Tick += new EventHandler(growTimer_Finished);
growTimer.Interval = time;
growTimer.Start();