如何解决暂停使用c#主机游戏
我正在做一个关于跑来跑去刺人的小游戏。 我想制作一种方法,当调用该方法时,它会停止计时器,计分并显示菜单(基本上暂停游戏),直到您按enter取消暂停为止。
我制作了一个暂停布尔控件,该暂停脚本旨在停止显示并刷新时间,级别和得分的更新功能,但我无法触发它(与检查按键是否具有不同的功能)。 基本上,我不会想出在按下某个键时如何停止undate功能。
我拥有的代码:
static void Update(int LVL,int scr,bool pause)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread thread2 = new Thread(() => Keypresses(pause,stopWatch));
thread2.Start();
TimeSpan time = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",time.Hours,time.Minutes,time.Seconds,time.Milliseconds / 10);
do
{
time = stopWatch.Elapsed;
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",time.Milliseconds / 10);
System.Threading.Thread.Sleep(450);
switch (true)
{
case true:
scr = scr + 100;
break;
}
Console.SetCursorPosition(8,64);
Console.Write(elapsedTime);
Console.SetCursorPosition(116,64);
Console.Write(LVL);
Console.SetCursorPosition(218,64);
Console.Write(scr);
} while (pause == false);
Console.Clear();
}
//check for keypresses and perform actions
static void Keypresses(bool pause,Stopwatch stopWatch)
{
while (true)
{
ConsoleKey key;
do //stops keys spamming
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
key = keyInfo.Key;
} while (Console.KeyAvailable);
switch (key)
{
case ConsoleKey.W:
//move up and turn
//if (map.jeprehoden(player.x,player.y-1))
break;
case ConsoleKey.D:
break;
case ConsoleKey.S:
break;
case ConsoleKey.A:
break;
case ConsoleKey.Enter:
//stab
break;
case ConsoleKey.Backspace:
pause = true;
stopWatch.Stop();
System.Threading.Thread.Sleep(400);
Screens.Drawpause();
while (pause == true)
{
ConsoleKey keyP;
do
{
ConsoleKeyInfo keyInfoP = Console.ReadKey(true);
keyP = keyInfoP.Key;
} while (Console.KeyAvailable);
switch (keyP)
{
case ConsoleKey.Enter:
stopWatch.Start();
Init();
System.Threading.Thread.Sleep(400);
pause = false;
break;
case ConsoleKey.Escape:
Environment.Exit(1);
break;
}
}
break;
}
}
}
编辑: 我必须将bool声明为类级别的变量(而不是通过所有方法进行传递)。示例:
static bool pause = false;
static void Update(int LVL,int scr)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread thread2 = new Thread(() => Keypresses(stopWatch));
thread2.Start();
TimeSpan time = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",time.Milliseconds / 10);
while(true)
{
while (pause == false)
{
time = stopWatch.Elapsed;
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",time.Milliseconds / 10);
System.Threading.Thread.Sleep(450);
switch (true)
{
case true:
scr = scr + 100;
break;
}
Console.SetCursorPosition(8,64);
Console.Write(elapsedTime);
Console.SetCursorPosition(116,64);
Console.Write(LVL);
Console.SetCursorPosition(218,64);
Console.Write(scr);
}
}
}
//check for keypresses and perform actions
static void Keypresses(Stopwatch stopWatch)
{
while (true)
{
ConsoleKey key;
do
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
key = keyInfo.Key;
} while (Console.KeyAvailable);
switch (key)
{
case ConsoleKey.W:
//move up and turn
//if (map.jeprehoden(player.x,player.y-1))
break;
case ConsoleKey.D:
break;
case ConsoleKey.S:
break;
case ConsoleKey.A:
break;
case ConsoleKey.Enter:
//stab
break;
case ConsoleKey.Backspace:
pause = true;
stopWatch.Stop();
System.Threading.Thread.Sleep(400);
Screens.Drawpause();
while (pause == true)
{
ConsoleKey keyP;
do
{
ConsoleKeyInfo keyInfoP = Console.ReadKey(true);
keyP = keyInfoP.Key;
} while (Console.KeyAvailable);
switch (keyP)
{
case ConsoleKey.Enter:
stopWatch.Start();
Init();
System.Threading.Thread.Sleep(400);
pause = false;
break;
case ConsoleKey.Escape:
Environment.Exit(1);
break;
}
}
break;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。