gpt4 book ai didi

c# - 如何为 2 个不同的变量设置计时器?

转载 作者:行者123 更新时间:2023-12-04 10:51:55 25 4
gpt4 key购买 nike

当您有两个不同的间隔时,如何在控制台应用程序中模拟战斗?制作第二个计时器?

private static Timer aTimer;

我的间隔值将反射(reflect)在 2 个 int 变量中,即每秒攻击次数。
int myAttackSpeed = 95; //Multiplied by 10 converts to milliseconds intervals
int yourAttackSpeed = 92; //as more than one attack per second
//You should make more attacks at the same time I do. Fewer milliseconds to attack = more attacks.

如何使两个间隔同时工作并在屏幕上输入一些内容?只有将文本显示在屏幕上才会满足,然后尝试计时器的输出以反射(reflect)发生的真实变化。

当它们有不同的间隔时如何运行或重复不同的方法?或者我应该研究多线程以获得这样的想法?

最佳答案

您可能会发现利用计时器更容易 Elapsed 事件:

public class Example { 
private static Timer t1;
private static Timer t2;
int myAttackSpeed = 95;
yourAttackSpeed = 92;

public static void Main()
{
t1 = new System.Timers.Timer {
Interval = myAttackSpeed*10,
Enabled = true,
Elapsed += OnT1Elapsed
};

t2 = new System.Timers.Timer {
Interval = yourAttackSpeed*10,
Enabled = true,
Elapsed += OnT2Elapsed
};

Console.WriteLine("Press the Enter key to exit the program at any time... ");
Console.ReadLine();
}

private static void OnT1Elapsed(Object source, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("My attack");
}

private static void OnT2Elapsed(Object source, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Your attack");
}
}

在实际项目中,您可能应该删除重复的代码并进行整理,但我希望这能说明我的观点,让您继续前进

关于c# - 如何为 2 个不同的变量设置计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59440253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com