Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在控制台中显示某些unicode字符_C#_Console - Fatal编程技术网

C# 在控制台中显示某些unicode字符

C# 在控制台中显示某些unicode字符,c#,console,C#,Console,所以我正在制作一个控制台游戏,但是我在显示一些unicode字符时遇到了问题,这些字符显示为“?”。它显示/U2580上半块,但不适用于/U2586下四分之三块。我试着改变编码,改变控制台的字体,但没有任何效果。有没有简单的方法来解决这个问题 编辑:这是代码,这是未完成的Arkanoid游戏 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Thre

所以我正在制作一个控制台游戏,但是我在显示一些unicode字符时遇到了问题,这些字符显示为“?”。它显示/U2580上半块,但不适用于/U2586下四分之三块。我试着改变编码,改变控制台的字体,但没有任何效果。有没有简单的方法来解决这个问题

编辑:这是代码,这是未完成的Arkanoid游戏

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

namespace ConsoleApplication1
{
    class Program
    {

        static string ballSymbol = "\u25CF";
        static string brickSymbol = "\u2580";

        static int ballRow;
        static int ballCol;

        static int right = 0;
        static int left = 1;

        static int ballDirection = right;
        static bool goingUp = true;

        static int fieldHeight;
        static int fieldWidth;


        static int[,] bricks;

        static int movesCounter = 0;

        static bool finished;
        static void Main(string[] args)
        {
            SetFieldSize();
            fieldHeight = Console.WindowHeight;
            fieldWidth = Console.WindowWidth;

            Console.OutputEncoding = Encoding.Unicode;

            bricks = new int[fieldHeight, fieldWidth];
            CreateBricks();

            //start position
            ballRow = 17;
            ballCol = Console.WindowHeight / 2;

            PrintBricks();

            int previousRow = ballRow;
            int previousCol = ballCol;

            while (true)
            {
                movesCounter++;

                PrintBall(previousRow, previousCol);
                previousRow = ballRow;
                previousCol = ballCol;

                BounceWalls();
                BreakBricks();

                if (goingUp == true)
                {
                    GoUp();
                }

                else
                {
                    GoDown();
                }

                Thread.Sleep(45);

            }
        }

        static void PrintBall(int previousRow, int previousCol)
        {
            DeleteAtPosition(previousRow, previousCol);
            WriteOnPosition(ballRow, ballCol, ballSymbol, ConsoleColor.White);
            WriteOnPosition(0, 0, movesCounter.ToString(), ConsoleColor.White);

        }
        static void CreateBricks()
        {
            for (int i = 4; i < 9; i++)
            {
                for (int j = 16; j < 46; j++)
                {
                    bricks[i, j] = 1;
                }
            }
        }
        static void PrintBricks()
        {
            for (int i = 0; i < fieldHeight; i++)
            {
                for (int j = 0; j < fieldWidth; j++)
                {
                    if (bricks[i, j] != 0)
                    {
                        WriteOnPosition(i, j, brickSymbol, ConsoleColor.Green);
                    }
                }
            }

        }
        static void BreakBricks()
        {
            if (goingUp == true && bricks[ballRow - 1, ballCol] != 0)
            {
                bricks[ballRow - 1, ballCol] = 0;
                DeleteAtPosition(ballRow - 1, ballCol);
                goingUp = false;
            }
            else if (goingUp == true && ballDirection == right && bricks[ballRow - 1, ballCol +1] != 0)
            {
                bricks[ballRow - 1, ballCol+1] = 0;
                DeleteAtPosition(ballRow - 1, ballCol+1);
                goingUp = false;
            }
            else if (goingUp == true && ballDirection == left && bricks[ballRow - 1, ballCol - 1] != 0)
            {
                bricks[ballRow - 1, ballCol - 1] = 0;
                DeleteAtPosition(ballRow - 1, ballCol - 1);
                goingUp = false;
            }

            if (goingUp == false && bricks[ballRow + 1, ballCol] != 0)
            {
                bricks[ballRow + 1, ballCol] = 0;
                DeleteAtPosition(ballRow + 1, ballCol);
                goingUp = true;
            }

            else if (goingUp == false && ballDirection == right && bricks[ballRow + 1, ballCol + 1] != 0)
            {
                bricks[ballRow + 1, ballCol + 1] = 0;
                DeleteAtPosition(ballRow + 1, ballCol + 1);
                goingUp = true;
            }
            else if (goingUp == false && ballDirection == right && bricks[ballRow + 1, ballCol - 1] != 0)
            {
                bricks[ballRow + 1, ballCol - 1] = 0;
                DeleteAtPosition(ballRow + 1, ballCol - 1);
                goingUp = true;
            }

            if (ballDirection == right && bricks[ballRow, ballCol + 1] != 0)
            {
                bricks[ballRow, ballCol + 1] = 0;
                DeleteAtPosition(ballRow, ballCol + 1);
                ChangeBallDirection();
            }
            else if (ballDirection == right && goingUp == true && bricks[ballRow - 1, ballCol + 1] != 0)
            {
                bricks[ballRow - 1, ballCol + 1] = 0;
                DeleteAtPosition(ballRow - 1, ballCol + 1);
                goingUp = false;
            }
            else if (ballDirection == right && goingUp == false && bricks[ballRow + 1, ballCol + 1] != 0)
            {
                bricks[ballRow + 1, ballCol + 1] = 0;
                DeleteAtPosition(ballRow + 1, ballCol + 1);
                goingUp = true;
            }
            if (ballDirection == left && bricks[ballRow, ballCol - 1] != 0)
            {
                bricks[ballRow, ballCol - 1] = 0;
                DeleteAtPosition(ballRow, ballCol - 1);
                ChangeBallDirection();
            }
            else if (ballDirection == left && goingUp == true && bricks[ballRow - 1, ballCol - 1] != 0)
            {
                bricks[ballRow - 1, ballCol - 1] = 0;
                DeleteAtPosition(ballRow - 1, ballCol - 1);
                goingUp = false;
            }
            else if (ballDirection == left && goingUp == false && bricks[ballRow + 1, ballCol - 1] != 0)
            {
                bricks[ballRow + 1, ballCol + 1] = 0;
                DeleteAtPosition(ballRow + 1, ballCol + 1);
                goingUp = true;
            }
        }

        static void DeleteAtPosition(int row, int col)
        {
            WriteOnPosition(row, col, "\u2588", ConsoleColor.Black);

        }
        static void BounceWalls()
        {
            if (ballRow == 0 || ballRow == fieldHeight-1)
            {
                goingUp = !goingUp;
            }

            if (ballCol == 0 || ballCol == fieldWidth-1)
            {
                ChangeBallDirection();
            }

        }

        static void ChangeBallDirection()
        {
            if (ballDirection == left)
            {
                ballDirection = right;
            }
            else if (ballDirection == right)
            {
                ballDirection = left;
            }
        }
        static void GoUp()
        {
            if (ballDirection == right)
            {
                ballRow--;
                ballCol++;
            }
            if (ballDirection == left)
            {
                ballRow--;
                ballCol--;
            }
            if (ballCol == 0 || ballCol == fieldWidth)
            {
                finished = true;
            }
            if (finished)
            {
                return;
            }
        }

        static void GoDown()
        {
            if (ballDirection == right)
            {
                ballRow++;
                ballCol++;
            }
            if (ballDirection == left)
            {
                ballRow++;
                ballCol--;
            }

            if (ballCol == 0 || ballCol == fieldWidth)
            {
                finished = true;
            }
            if (finished)
            {
                return;
            }
        }

        static void SetFieldSize()
        {
            Console.WindowHeight = 20;
            Console.BufferHeight = 20;

            Console.WindowWidth = 60;
            Console.BufferWidth = 60;

        }
        static void WriteOnPosition(int row, int col, string symbol, ConsoleColor color)
        {
            Console.ForegroundColor = color;
            Console.SetCursorPosition(col, row);
            Console.Write(symbol);
        }
    }


}

同时显示您的作品。这取决于cmd窗口中使用的字体。Arial Unicode MS就是这样一种具有u2586字符的字体。问题是,cmd字体设置与C/.NET无关。如果更改cmd.exe的默认字体不适合您,很遗憾,我不知道如何通过编程或在命令行上更改特定cmd.exe窗口的字体。我想,你可以创建一种程序启动程序,启动cmd.exe,自动进入cmd窗口的属性对话框,更改字体,然后运行你的程序。如果不工作,它必须是固定间距字体,如Consolas或Lucida Console。都没有U+2586。汉斯,你说得对。啊!虽然可以尝试使用这些块字符获得第三方单间距字体,但仍需要在注册表中进行修改,以使该字体在CMD窗口中可用,这似乎是一种相当不切实际的方法。。。