C# C中简单控制台游戏中的巨大闪烁#

C# C中简单控制台游戏中的巨大闪烁#,c#,visual-studio-2013,console-application,C#,Visual Studio 2013,Console Application,我正在用C#制作我的第一个控制台游戏,这是一个简单的迷宫游戏,但由于某种原因,我的屏幕上闪烁的次数太多了。我试过使用Thread.Sleep和Console.CursorVisible=false;但是没有用。 如果您被卡住,请按1,然后在标题屏幕上输入,这将引导您进入迷宫,迷宫仍处于阿尔法前期阶段。如果有什么不同的话,我会使用VisualStudio2013作为IDE。我的问题是如何消除迷宫部分的过度闪烁 using System; using System.Collections.Gener

我正在用C#制作我的第一个控制台游戏,这是一个简单的迷宫游戏,但由于某种原因,我的屏幕上闪烁的次数太多了。我试过使用Thread.Sleep和Console.CursorVisible=false;但是没有用。 如果您被卡住,请按1,然后在标题屏幕上输入,这将引导您进入迷宫,迷宫仍处于阿尔法前期阶段。如果有什么不同的话,我会使用VisualStudio2013作为IDE。我的问题是如何消除迷宫部分的过度闪烁

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Threading;

class Game
{
    static void Main()
    {
        Console.WriteLine("Select Level (Available levels: 1,2):");
        Console.WriteLine("\n(\\_/)\n(o.o)\n(___)0\n");
        int gameLevel = int.Parse(Console.ReadLine()); // by pressing a number the user can select different labyrinths.
        // Console Height and Width
        Console.BufferHeight = Console.WindowHeight = 25;
        Console.BufferWidth = Console.WindowWidth = 80;
        Console.OutputEncoding = System.Text.Encoding.Unicode;  // Must have this + Change font to Lucida in CMD

        // Reads File:
        string map = File.ReadAllText(String.Format("level{0}.txt", gameLevel));
        string[] mapRows = Regex.Split(map, "\r\n");
        int mapSize = mapRows[0].Length;
        int mapHeight = mapRows.Count() - 1;
        char[,] charMap = new char[mapHeight, mapSize];

        // Creates Matrix:
        for (int row = 0; row < mapHeight; row++)
        {
            for (int col = 0; col < mapSize; col++)
            {
                charMap[row, col] = mapRows[row].ElementAt(col);
            }
        }
        // Rabbit init:
        string rabbitIcon = "\u0150";   //  \u0150   \u014E    \u00D2     \u00D3 --> alternatives
        int rabbitX = 1, rabbitY = 0;
        int carrotCounter = 0;
        // Game Loop:
        while (true)
        {
            DrawLabyrinth(mapHeight, mapSize, charMap);
            MoveRabbit(mapHeight, mapSize, ref rabbitX, ref rabbitY, charMap);
            EatCarrot(rabbitX, rabbitY, charMap,carrotCounter);
            Console.SetCursorPosition(rabbitX, rabbitY);
            Console.Write(rabbitIcon);
            Thread.Sleep(66);
            Console.CursorVisible = false;
            Console.Clear();

        }
    }
    static void EatCarrot(int rabbitX, int rabbitY, char[,] theMap,int carrotCount)
    {
        if (theMap[rabbitY, rabbitX] == '7' || theMap[rabbitY, rabbitX] == '8')
        {
            if (theMap[rabbitY, rabbitX] == '7')
            {
                theMap[rabbitY, rabbitX] = ' ';
                theMap[rabbitY - 1, rabbitX] = ' ';
                carrotCount++;

            }
            else if (theMap[rabbitY, rabbitX] == '8')
            {
                theMap[rabbitY, rabbitX] = ' ';
                theMap[rabbitY + 1, rabbitX] = ' ';
                carrotCount++;
            }
        }

    }

    static void MoveRabbit(int height, int width, ref int rabbitX, ref int rabbitY, char[,] theMap)
    {
        if (Console.KeyAvailable == true)
        {
            ConsoleKeyInfo pressedKey = Console.ReadKey(true);
            while (Console.KeyAvailable) Console.ReadKey(true);
            if (pressedKey.Key == ConsoleKey.LeftArrow || pressedKey.Key == ConsoleKey.A)
            {
                if (theMap[rabbitY, rabbitX - 1] == ' ' || theMap[rabbitY,rabbitX - 1 ] == '7' || theMap[rabbitY,rabbitX - 1 ] == '8')
                {
                    rabbitX -= 1;
                }
            }
            else if (pressedKey.Key == ConsoleKey.RightArrow || pressedKey.Key == ConsoleKey.D)
            {
                if (theMap[rabbitY, rabbitX + 1] == ' ' || theMap[rabbitY,rabbitX + 1 ] == '7' || theMap[rabbitY,rabbitX + 1 ] == '8')
                {
                    rabbitX += 1; 
                }
            }
            else if (pressedKey.Key == ConsoleKey.UpArrow || pressedKey.Key == ConsoleKey.W)
            {
                if (theMap[rabbitY - 1, rabbitX] == ' ' || theMap[rabbitY - 1,rabbitX ] == '7' || theMap[rabbitY - 1,rabbitX ] == '8')
                {
                    rabbitY -= 1;
                }
            }
            else if (pressedKey.Key == ConsoleKey.DownArrow || pressedKey.Key == ConsoleKey.S)
            {
                if (theMap[rabbitY + 1, rabbitX] == ' ' || theMap[rabbitY + 1, rabbitX] == '7' || theMap[rabbitY + 1, rabbitX] == '8')
                {
                    rabbitY += 1;

                }
            }
        }
    }
    static void DrawLabyrinth(int height, int width, char[,] array)
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                if (array[i, j] == '1')
                    Console.Write("─");
                else if (array[i, j] == '2')
                    Console.Write("│");
                else if (array[i, j] == '3')
                    Console.Write("┌");
                else if (array[i, j] == '4')
                    Console.Write("┐");
                else if (array[i, j] == '5')
                    Console.Write("└");
                else if (array[i, j] == '6')
                    Console.Write("┘");
                else if (array[i, j] == '7')
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("▼");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else if (array[i, j] == '8')
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("\u00B8");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else if (array[i, j] == '9')
                {
                    Console.Write("┬");
                }
                else if (array[i, j] == '0')
                {
                    Console.Write("┴");
                }
                else if (array[i, j] == 'a')
                {
                    Console.Write('├');
                }
                else if (array[i, j] == 'b')
                {
                    Console.Write('┤');
                }
                else if (array[i, j] == 'c')
                {
                    Console.Write('┼');
                }
                else
                {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Text.RegularExpressions;
使用系统线程;
班级游戏
{
静态void Main()
{
Console.WriteLine(“选择级别(可用级别:1,2):”;
Console.WriteLine(“\n(\\\\\ u/)\ n(o.o)\n(\\\ uuu0\n”);
int gameLevel=int.Parse(Console.ReadLine());//按一个数字,用户可以选择不同的迷宫。
//控制台高度和宽度
Console.BufferHeight=Console.WindowHeight=25;
Console.BufferWidth=Console.WindowWidth=80;
Console.outpunecoding=System.Text.Encoding.Unicode;//必须在CMD中将此+字体更改为Lucida
//读取文件:
string map=File.ReadAllText(string.Format(“level{0}.txt”,gameLevel));
字符串[]mapRows=Regex.Split(映射,“\r\n”);
int mapSize=mapRows[0]。长度;
int mapHeight=mapRows.Count()-1;
char[,]charMap=新字符[mapHeight,mapSize];
//创建矩阵:
对于(int row=0;row可选项
int-rabbitX=1,rabbitY=0;
int=0;
//游戏循环:
while(true)
{
Draw迷宫(mapHeight、mapSize、charMap);
MoveRabbit(映射高度、映射大小、参考rabbitX、参考rabbitY、charMap);
EatCarrot(兔子、兔子、charMap、胡萝卜计数器);
控制台。设置光标位置(兔子,兔子);
Console.Write(rabbitIcon);
睡眠(66);
Console.CursorVisible=false;
Console.Clear();
}
}
静态无效EatCarrot(int-rabbitX,int-rabbitY,char[,]theMap,int-carrotCount)
{
如果(地图[兔子,兔子]='7'| |地图[兔子,兔子]='8')
{
如果(theMap[rabbitY,rabbitX]=“7”)
{
theMap[兔子,兔子]='';
theMap[rabbitY-1,rabbitX]='';
胡萝卜计数++;
}
else if(映射[rabbitY,rabbitX]=“8”)
{
theMap[兔子,兔子]='';
theMap[rabbitY+1,rabbitX]='';
胡萝卜计数++;
}
}
}
静态void-MoveRabbit(int-height、int-width、ref-int-rabbitX、ref-int-rabbitY、char[,]theMap)
{
if(Console.KeyAvailable==true)
{
ConsoleKeyInfo pressedKey=Console.ReadKey(真);
while(Console.KeyAvailable)Console.ReadKey(true);
如果(按Key.Key==ConsoleKey.LeftArrow | |按Key.Key==ConsoleKey.A)
{
如果(图[rabbitY,rabbitX-1]=''| |图[rabbitY,rabbitX-1]='7'| |图[rabbitY,rabbitX-1]='8')
{
rabbitX-=1;
}
}
else if(按Key.Key==ConsoleKey.RightArrow | |按Key.Key==ConsoleKey.D)
{
如果(图[rabbitY,rabbitX+1]=''| |图[rabbitY,rabbitX+1]='7'| |图[rabbitY,rabbitX+1]='8')
{
rabbitX+=1;
}
}
else if(按Key.Key==ConsoleKey.UpArrow | |按Key.Key==ConsoleKey.W)
{
如果(地图[兔子-1,兔子]=''| |地图[兔子-1,兔子]='7'| | |地图[兔子-1,兔子]='8')
{
兔子-=1;
}
}
else if(按Key.Key==ConsoleKey.DownArrow | |按Key.Key==ConsoleKey.S)
{
如果(地图[兔子+1,兔子]=''| |地图[兔子+1,兔子]=''7'| | |地图[兔子+1,兔子]=''8')
{
兔子+=1;
}
}
}
}
静态void draw迷宫(整数高度、整数宽度、字符[,]数组)
{
对于(int i=0;i// One time actions
var maze = ReadMaze(level);
DrawMaze(maze);
DrawRabbit(rabbitX, rabbitY);

// Game loop
while ((var input = GetInput()) != Input.Quit) {
    oldRabbitX = rabbitX, oldRabbitY = rabbitY;
    if (MoveRabbit(input, rabbitX, rabbitY, maze)) {
        EraseRabbit(oldX, oldY);
        DrawRabbit(rabbitX, rabbitY);
        if (IsPositionWithCarrot(rabbitX, rabbitY, maze))
            // This only the erases the carrot on screen.
            EatCarrot(rabbitX, rabbitY, maze);
    }
}
public class Game
{
    const string RabbitIcon = "\u0150";   //  \u0150   \u014E    \u00D2     \u00D3 --> alternatives
    static readonly char[] MazeChars = { '─', '│', '┌', '┐', '└', '┘', '▼', '\u00B8', '┬', '┴', '├', '┤', '┼' };
    static readonly ConsoleColor MazeFgColor = ConsoleColor.DarkGray;

    enum Input
    {
        MoveLeft,
        MoveRight,
        MoveUp,
        MoveDown,
        Quit
    };

    public static void Run()
    {
        Console.WriteLine("Select Level (Available levels: 1,2):");
        Console.WriteLine("\n(\\_/)\n(o.o)\n(___)0\n");
        int carrotCounter = 0;
        int gameLevel = int.Parse(Console.ReadLine()); // by pressing a number the user can select different labyrinths.

        // Console Height and Width
        Console.WindowHeight = 25;
        Console.BufferHeight = Console.WindowHeight + 1; // +1 to allow writing last character in the screen corner
        Console.BufferWidth = Console.WindowWidth = 80;
        Console.OutputEncoding = System.Text.Encoding.Unicode;  // Must have this + Change font to Lucida in CMD

        // Reads maze map
        string[] mapRows = File.ReadAllLines(String.Format("game.level{0}.txt", gameLevel));
        if (!mapRows.All(r => r.Length == mapRows[0].Length))
            throw new InvalidDataException("Invalid map");
        var charMap = mapRows.Select(r => r.ToCharArray()).ToArray();

        // Draw maze & rabbit once 
        Console.CursorVisible = false;
        DrawLabyrinth(charMap);
        int rabbitX = 1, rabbitY = 1;
        DrawRabbit(rabbitX, rabbitY, RabbitIcon);

        // Game Loop:
        Input input;
        while ((input = GetInput()) != Input.Quit)
        {
            if (MoveRabbit(input, ref rabbitX, ref rabbitY, charMap) &&
                IsPositionWithCarrot(rabbitX, rabbitY, charMap))
                EatCarrot(rabbitX, rabbitY, charMap, ref carrotCounter);
        }
    }

    static void EatCarrot(int rabbitX, int rabbitY, char[][] theMap, ref int carrotCounter)
    {
        // determine carrot top position.
        var carrotTopY = theMap[rabbitY][rabbitX] == '7' ? rabbitY - 1 : rabbitY;
        // "eat it" from the map.
        theMap[carrotTopY][rabbitX] = ' ';
        theMap[carrotTopY + 1][rabbitX] = ' ';
        // and erase it on screen;
        Console.SetCursorPosition(rabbitX, carrotTopY);
        Console.Write(' ');
        Console.SetCursorPosition(rabbitX, carrotTopY + 1);
        Console.Write(' ');
        // redraw the rabbit
        carrotCounter++;
        DrawRabbit(rabbitX, rabbitY, RabbitIcon);
    }

    static Input GetInput()
    {
        while (true)
        {
            var key = Console.ReadKey(true);
            switch (key.Key)
            {
                case ConsoleKey.LeftArrow:
                case ConsoleKey.A:
                    return Input.MoveLeft;
                case ConsoleKey.RightArrow:
                case ConsoleKey.D:
                    return Input.MoveRight;
                case ConsoleKey.UpArrow:
                case ConsoleKey.W:
                    return Input.MoveUp;
                case ConsoleKey.DownArrow: 
                case ConsoleKey.S:
                    return Input.MoveDown;
                case ConsoleKey.Q:
                    return Input.Quit;
                default:
                    break;
            }
        }
    }

    static bool IsValidRabbitPosition(int x, int y, char[][] theMap)
    {
        return x >= 0 && x < theMap[0].Length && y >= 0 && y < theMap.Length &&
               (theMap[y][x] == ' ' || IsPositionWithCarrot(x, y, theMap));
    }

    static bool IsPositionWithCarrot(int x, int y, char[][] theMap)
    {
        return theMap[y][x] == '7' || theMap[y][x] == '8';
    }

    static void DrawRabbit(int x, int y, string rabbitIcon)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = ConsoleColor.DarkYellow;
        Console.Write(rabbitIcon);
        Console.ResetColor();
    }

    static bool MoveRabbit(Input direction, ref int rabbitX, ref int rabbitY, char[][] theMap)
    {
        int newX = rabbitX, newY = rabbitY;
        switch (direction)
        {
            case Input.MoveLeft: newX--; break;
            case Input.MoveRight: newX++; break;
            case Input.MoveUp: newY--; break;
            case Input.MoveDown: newY++; break;
            default: return false;
        }
        if (IsValidRabbitPosition(newX, newY, theMap))
        {
            DrawRabbit(rabbitX, rabbitY, " "); // erase
            rabbitX = newX;
            rabbitY = newY;
            DrawRabbit(rabbitX, rabbitY, RabbitIcon); // draw
            return true;
        }
        return false;
    }

    static void DrawLabyrinth(char[][] theMap)
    {
        Console.Clear();
        for (int y = 0; y < theMap.Length; y++)
        {
            Console.SetCursorPosition(0, y);
            for (int x = 0; x < theMap[0].Length; x++)
            {
                var ndx = theMap[y][x] - '1';
                var c = ndx >= 0 && ndx < MazeChars.Length 
                    ? MazeChars[ndx] 
                    : ' ';

                Console.ForegroundColor = IsPositionWithCarrot(x, y, theMap)
                    ? ndx == 6 ? ConsoleColor.Red : ConsoleColor.Green
                    : MazeFgColor;

                Console.Write(c);
                Console.ResetColor();
            }
        }
        Console.WindowTop = 0; // scroll back up.
    }
}