【发布时间】:2015-04-10 23:40:00
【问题描述】:
我正在用 C# 制作我的第一个控制台游戏,这是一个简单的迷宫游戏,但由于某种原因,我在屏幕上出现了可笑的闪烁量。我试过使用 Thread.Sleep 和 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();
}
}
}
【问题讨论】:
-
在整个画布上绘画时通常不会清除背景...尝试删除
.Clear调用,而只是定位光标。 -
控制台输出很慢。您必须摆脱 Console.Clear() 并编写仅更新已更改字符位置的代码,并尽可能将它们分组。
标签: c# visual-studio-2013 console-application