c# - C# 中的简单控制台游戏出现巨大闪烁

标签 c# visual-studio-2013 console-application

我正在用 C# 制作我的第一个控制台游戏,这是一个简单的迷宫游戏,但由于某种原因,我的屏幕上有大量的闪烁。我尝试过使用 Thread.Sleep AND Console.CursorVisible=false;但无济于事。 如果你遇到困难,请按 1,然后进入标题屏幕,这将引导你进入仍处于 pre-alpha 阶段的迷宫。如果有影响的话,我会使用 Visual Studio 2013 作为 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();
        }
    }
}

最佳答案

您的代码最大的问题是您不断刷新屏幕,即使不需要重绘任何内容,因为用户没有移动兔子。

正如所指出的,你想要做的是最小的重画量,即只有在有东西需要重画的时候才重画,然后尝试做尽可能少的量。对于您的示例游戏,在伪代码中应该如下所示:

// 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);
    }
}

可以找到一篇博客文章,其中包含有关构建 C# 控制台游戏的大量有用信息 here

因为我发现这是一个有趣的问题,所以我对你的代码进行了一些重新调整以匹配上面的伪代码。这会消除游戏中的所有闪烁。您可以在下面找到此尝试:

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.
    }
}

关于c# - C# 中的简单控制台游戏出现巨大闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572274/

相关文章:

c# - 字符串或二进制数据将被截断。该语句已终止。上传个人资料时

c++ - 编辑字符串时接收内存泄漏

c# - 使用 AddSignInManager 时出现 ASP.NET Core MVC 异常

c# - foreach-if-statement with/L​​inq 条件给出意想不到的结果

c++ - Visual C++ 2013、TFS 和属性表的问题

c++ - DCMTK 字符集 Unicode 错误

c# - AllowAnonymous 不工作 MVC5

c# - 将 PictureBox 的图像更改为来 self 的资源的图像?

c# - C# 控制台应用程序中基于用户输入运行函数

c# - 如何每隔几秒截屏一次C#控制台