暂停使用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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...