简体   繁体   中英

Clearing the keyboard buffer or blocking input (C# Console)

"Apologies in advance for the terrible coding" XD

The problem is that when I switch to a new map, the transition animation starts (darkening and revealing the new map) (in the code, this is NextMap ()) During this animation, I will have time to make a few more moves (let's say to the left), and when the animation ends, my character instantly walks the same number of moves, and I need him to stand near the transition between the maps. I would like to somehow clear the keyboard buffer when the animation finishes. Is it possible to somehow solve this problem?


(to better understand my problem, run this code and check my actions)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace The_Legend_Of_Game
{
    class Program
    {
        public static ConsoleKey key;
        public static string[] map = new string[9];
        public static int[] playerPos = new int[2];
        public static int[] _playerPosTEMP = new int[2];
        public static string mapNow = "10000";

        static void Main(string[] args)
        {
            playerPos[0] /*y*/ = 4;
            playerPos[1] /*x*/ = 7;

            while (true)
            {
                Console.Clear();
                ShowMap(mapNow);

                key = Console.ReadKey(false).Key;
                ControlButtons(key);

            
            }
        }
    
        static void ShowMap(string mapIndex)
        {
            Console.Clear();

            if (mapIndex == "10000")
            {
                map[0] = "███████████████";
                map[1] = "██████████    █";
                map[2] = "██▒██         █";
                map[3] = "█             █";
                map[4] = "              █";
                map[5] = "█             █";
                map[6] = "█             █";
                map[7] = "█             █";
                map[8] = "███████████████";

                ShowMap_();
            }

            if (mapIndex == "11000")
            {
                map[0] = "███████████████";
                map[1] = "█             █";
                map[2] = "█             █";
                map[3] = "█             █";
                map[4] = "               ";
                map[5] = "█             █";
                map[6] = "█             █";
                map[7] = "█             █";
                map[8] = "███████████████";

                ShowMap_();
            }

            if (mapIndex == "12000")
            {
                map[0] = "███████████████";
                map[1] = "█             █";
                map[2] = "█      *      █";
                map[3] = "█             █";
                map[4] = "               ";
                map[5] = "█             █";
                map[6] = "█             █";
                map[7] = "█             █";
                map[8] = "███████████████";

                ShowMap_();
            }
        }

        static void ShowMap_()
        {
            for (int i = 0; i < 9; i++)
            {
                if (playerPos[0] == i)
                {
                    char[] chars = map[i].ToCharArray();
                    chars[playerPos[1]] = '☺';
                    map[i] = new string(chars);
                }
                Console.WriteLine(map[i]);
            }

            Console.Write("\n");
            Console.WriteLine(mapNow);
        }




    


















        static void ControlButtons(ConsoleKey key)
        {
            if (key == ConsoleKey.UpArrow)
            {
                if (!IsExit(key))
                {
                    if (map[playerPos[0] - 1][playerPos[1]] == '█')
                    {
                        // Nothing
                    }
                    else
                    {
                        playerPos[0]--;
                    }
                }
                else
                {
                    NextRoom1();



                    NextRoom2();
                }
            }
            if (key == ConsoleKey.DownArrow)
            {
                if (!IsExit(key))
                {
                    if (map[playerPos[0] + 1][playerPos[1]] == '█')
                    {
                        // Nothing
                    }
                    else
                    {
                        playerPos[0]++;
                    }
                }
                else
                {
                    NextRoom1();



                    NextRoom2();
                }
            }
            if (key == ConsoleKey.LeftArrow)
            {
                if (!IsExit(key))
                {
                    if (map[playerPos[0]][playerPos[1] - 1] == '█')
                    {
                        // Nothing
                    }
                    else
                    {
                        playerPos[1]--;
                    }
                }
                else
                {
                    NextRoom1();

                    char[] chars = mapNow.ToCharArray();
                    chars[1] = Convert.ToChar(Convert.ToInt32(chars[1]) + 1);
                    mapNow = new string(chars);

                    playerPos[1] = 14;
                    _playerPosTEMP[1] = 14;
                    playerPos[0] = 4;
                    _playerPosTEMP[0] = 4;

                    NextRoom2();
                }
            }
            if (key == ConsoleKey.RightArrow)
            {
                if (!IsExit(key))
                {
                    if (map[playerPos[0]][playerPos[1] + 1] == '█')
                    {
                        // Nothing
                    }
                    else
                    {
                        playerPos[1]++;
                    }
                }
                else
                {
                    NextRoom1();

                    char[] chars = mapNow.ToCharArray();
                    chars[1] = Convert.ToChar(Convert.ToInt32(chars[1]) - 1);
                    mapNow = new string(chars);

                    playerPos[1] = 0;
                    _playerPosTEMP[1] = 0;
                    playerPos[0] = 4;
                    _playerPosTEMP[0] = 4;

                    NextRoom2();
                }
            }
        }










        static bool IsExit(ConsoleKey key)
        {
            if (key == ConsoleKey.UpArrow && playerPos[0] - 1 < 0 ||
                key == ConsoleKey.DownArrow && playerPos[0] + 1 > 8 ||
                key == ConsoleKey.LeftArrow && playerPos[1] - 1 < 0 ||
                key == ConsoleKey.RightArrow && playerPos[1] + 1 > 14)
            {
            
                return true;
            }

            else
            {
                return false;
            }
        }







        static void NextRoom1()
        {
            Thread.Sleep(200);
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            ShowMap(mapNow);

            Thread.Sleep(200);
            Console.ForegroundColor = ConsoleColor.Black;
            ShowMap(mapNow);
        }

        static void NextRoom2()
        {
            Thread.Sleep(200);
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            ShowMap(mapNow);

            Thread.Sleep(200);
            Console.ForegroundColor = ConsoleColor.Green;
            ShowMap(mapNow);
        }
    }
}

At the bottom of your NextRoomX() methods, call ClearBuffer() :

static void ClearBuffer()
{
    while (Console.KeyAvailable) {
        Console.ReadKey(false);
    }            
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM