我试图制作一个非常简单的迷宫游戏,这是C#控制台应用程序中预定义的2维度数组。玩家使用箭头键移动。玩家以[0,0]协调产生红色,其余为白色。 每当玩家使用箭头键移动时,新协调的颜色会变为红色。我想知道如何更改在2维数组的特定协调中设置的字符串的颜色(数组定义为字符串)。 如果你帮忙,我会很高兴。
static void Main(string[] args)
{
Stack MyStack = new Stack();
string[,] arr = new string[5, 5] { { "1", "0", "0", "0", "0" }, { "1", "0", "0", "0", "0" }, { "1", "0", "0", "0", "0" }, { "1", "1", "0", "0", "0" }, {"1", "1", "1", "1", "1" }};
var rowCount = arr.GetLength(0);
var colCount = arr.GetLength(1);
for (int row = 0; row < rowCount; row++)
{
for (int col = 0; col < colCount; col++)
Console.Write(String.Format("{0}\t", arr[row, col]));
Console.WriteLine();
}
Console.ReadLine();
}
答案 0 :(得分:0)
这是一个简单的解决方案。您可以随意修改它。
using System;
struct Index
{
public int i;
public int j;
}
public class Node
{
public int Value { get; set; }
public ConsoleColor Color { get; set; }
public Node(int value, ConsoleColor color = ConsoleColor.White)
{
Value = value;
Color = color;
}
}
public class Maze
{
private readonly int _size;
private Node[,] _maze;
private Index _currentIndex;
public Maze(int size)
{
_size = size;
_maze = new Node[_size, _size];
}
private void Initialize()
{
int k = 1;
for (int i = 0; i < _size; i++)
for (int j = 0; j < _size; j++)
_maze[i, j] = new Node(k++);
_maze[0, 0].Color = ConsoleColor.Red;
}
private void Print()
{
Console.WriteLine("Use the direction keys to move starting at (0,0). I'll not move until you enter a valid direction.\nYou can only move within the maze.\nYou cannot move to a red spot.\n\nPress ESC to quit.\n\n");
for (int i = 0; i < _size; i++)
{
for (int j = 0; j < _size; j++)
{
Node node = _maze[i, j];
Console.ForegroundColor = node.Color;
Console.Write(string.Format("{0:00}",node.Value));
Console.ResetColor();
Console.Write(" ");
}
Console.WriteLine();
}
}
public void Run()
{
Initialize();
Print();
ConsoleKey key;
do
{
key = Console.ReadKey().Key;
ValidateMove(key);
Console.Clear();
Print();
} while (key != ConsoleKey.Escape);
}
private void ValidateMove(ConsoleKey key)
{
Index tmp = _currentIndex;
switch (key)
{
case ConsoleKey.UpArrow:
--tmp.i;
if(Validate(tmp))
{
_maze[tmp.i, tmp.j].Color = ConsoleColor.Red;
_currentIndex = tmp;
}
break;
case ConsoleKey.DownArrow:
++tmp.i;
if (Validate(tmp))
{
_maze[tmp.i, tmp.j].Color = ConsoleColor.Red;
_currentIndex = tmp;
}
break;
case ConsoleKey.LeftArrow:
--tmp.j;
if (Validate(tmp))
{
_maze[tmp.i, tmp.j].Color = ConsoleColor.Red;
_currentIndex = tmp;
}
break;
case ConsoleKey.RightArrow:
++tmp.j;
if (Validate(tmp))
{
_maze[tmp.i, tmp.j].Color = ConsoleColor.Red;
_currentIndex = tmp;
}
break;
default:
break;
}
}
private bool Validate(Index index) => (index.i < _size && index.i >= 0 && index.j < _size && index.j >= 0 && _maze[index.i, index.j].Color != ConsoleColor.Red);
public static void Main()
{
Maze maze = new Maze(5);
maze.Run();
}
}