繁体   English   中英

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

[英]Huge flickering in simple console game in C#

我正在用C#制作第一个控制台游戏,这是一个简单的迷宫游戏,但是由于某种原因,我在屏幕上出现了很多可笑的闪烁。 我已经尝试使用Thread.Sleep和Console.CursorVisible = false; 但无济于事。 万一您卡住了,请按1,然后在标题屏幕上输入,这将带您进入仍处于预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);
    }
}

此处可以找到有关构建ac#控制台游戏的大量有用信息的博客文章。

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

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

这是控制台应用程序不是实时游戏(或实际上是任何其他动画)的不错选择的众多原因之一。 正如您所显示的,您绝对可以做到,但是不断清除和重绘整个窗口将会闪烁。

因此,真正的解决方案是选择一种对动画效果更好的技术,例如Windows Forms甚至更好的WPF。 两者都可以在屏幕上移动元素,并且只能重绘“脏”区域,这在减少闪烁方面具有巨大的功能。

如果您打算在控制台应用程序中执行此操作,则可以通过移动控制台光标,擦除旧位置并在新位置重新绘制字符来进行“脏”检查。 它仍然不会是一个真正的图形库一样高效,而你的性格可能仍然闪烁如果它有显著的大小,但是这将是一个更好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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