我的程序不断将整个数组的颜色更改为一种颜色,而不是每种颜色都不同
My program keeps changing the color of my entire array to one color instead of different colors for each one
我正在为我的 class 作业制作一个小板生成器,我在其中获取一个二维数组并用一个称为“tile”的对象填充它,该对象具有自己的符号、背景和前景色它(即空 =“#”,背景红色,前景蓝色,而房间 =“.”,背景绿色,前景黄色)。
我已经创建了图板并开始在上面绘制房间以及我的图块 class。我遇到的唯一问题是,每当我绘制我的板时,它只使用最后给出的瓷砖对象参数,所以如果我最后画一个房间,它就有房间参数。我尝试通过尝试更改我创建磁贴的位置并尝试创建一个磁贴来覆盖它来弄乱它。
这是我的开发板的代码 class 代码:
namespace BrandonPlayerGen
{
public class Board
{
public static object[,] board;
public static int height;
public static int width;
public static Array MakeBoard(int bwidth, int bheight)
{
Tile empty = new Tile("#", ConsoleColor.Red, ConsoleColor.Yellow,false);
board = new object[bheight, bwidth];
for (int hcheck = 0; hcheck < (bheight-1); hcheck++)
{
for(int wcheck = 0; wcheck < (bwidth-1); wcheck++)
{
board[hcheck, wcheck] = empty;
}
}
return board;
}
public static void DrawBoard(int dheight, int dwidth)
{
dwidth = width;
dheight = height;
for (int hcheck = 0; hcheck<dheight; hcheck++)
{
for (int wcheck = 0; wcheck < dwidth; wcheck++)
{
Tile.DrawTile();
}
Console.Write("\n");
}
}
public static Array MakeRoom()
{
Tile room = new Tile(".", ConsoleColor.Blue, ConsoleColor.Red,true);
int bheight = (height-1);
int bwidth = (width-1);
int hstart = Random.randInt(1, bheight);
int wstart = Random.randInt(1, bwidth);
int rheight = Random.randInt(1, 5);
int rwidth = Random.randInt(1, 5);
while (hstart > height || hstart < 1 || wstart < 1 || wstart > width||
(hstart+rheight)>bheight|| (wstart+rwidth)>bwidth)
{
hstart = Random.randInt(1, bheight);
wstart = Random.randInt(1, bwidth);
rheight = Random.randInt(1, 5);
rwidth = Random.randInt(1, 5);
}
for (; hstart<rheight; hstart++)
{
for (; wstart < rwidth; wstart++)
{
board[hstart, wstart] = room;
}
}
return board;
}
这是我的图块的代码 class:
namespace BrandonPlayerGen
{
public class Tile
{
public static string symbol;
public static ConsoleColor foreColor;
public static ConsoleColor backColor;
public static string stairisHere;
public Tile(string boardSymbol, ConsoleColor
back, ConsoleColor fore, bool stairs)
{
backColor = back;
foreColor = fore;
symbol = boardSymbol;
if (stairs == true)
{
symbol = "<";
backColor = ConsoleColor.Green;
foreColor = ConsoleColor.White;
}
}
public static void DrawTile()
{
Console.BackgroundColor = backColor;
Console.ForegroundColor = foreColor;
Console.Write(symbol);
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
}
}
这里是在 main 中调用板的地方:
int height, width;
Console.Write("Please Enter Board height: ");
height = Convert.ToInt32(Console.ReadLine());
Console.Write("Please Enter Board height: ");
width = Convert.ToInt32(Console.ReadLine());
Board.height = height;
Board.width = width;
Board.MakeBoard(height, width);
Board.DrawBoard(Board.height, Board.width);
Console.ReadKey();
Board.MakeRoom();
Board.DrawBoard( Board.width, Board.height);
Console.ReadKey();
任何帮助将不胜感激,如果我的问题格式不正确,我很抱歉这是我的第一个问题。
编辑:
修复了生成图块的方式,以便每次都创建一个新图块,但是当我显示板时,它在整个板上都是相同的颜色和符号的问题。
您只创建了 Tile
的一个实例。您需要为面板中的每个单元格创建一个新单元格。
另外,除非你打算在你的看板中包含其他东西,否则我建议它的类型应该是 Tile[,]
而不是 object[,]
。
board = new Tile[bheight, bwidth];
for (int hcheck = 0; hcheck < (bheight-1); hcheck++)
{
for(int wcheck = 0; wcheck < (bwidth-1); wcheck++)
{
Tile empty = new Tile("#", ConsoleColor.Red, ConsoleColor.Yellow,false);
board[hcheck, wcheck] = empty;
}
}
我正在为我的 class 作业制作一个小板生成器,我在其中获取一个二维数组并用一个称为“tile”的对象填充它,该对象具有自己的符号、背景和前景色它(即空 =“#”,背景红色,前景蓝色,而房间 =“.”,背景绿色,前景黄色)。
我已经创建了图板并开始在上面绘制房间以及我的图块 class。我遇到的唯一问题是,每当我绘制我的板时,它只使用最后给出的瓷砖对象参数,所以如果我最后画一个房间,它就有房间参数。我尝试通过尝试更改我创建磁贴的位置并尝试创建一个磁贴来覆盖它来弄乱它。
这是我的开发板的代码 class 代码:
namespace BrandonPlayerGen
{
public class Board
{
public static object[,] board;
public static int height;
public static int width;
public static Array MakeBoard(int bwidth, int bheight)
{
Tile empty = new Tile("#", ConsoleColor.Red, ConsoleColor.Yellow,false);
board = new object[bheight, bwidth];
for (int hcheck = 0; hcheck < (bheight-1); hcheck++)
{
for(int wcheck = 0; wcheck < (bwidth-1); wcheck++)
{
board[hcheck, wcheck] = empty;
}
}
return board;
}
public static void DrawBoard(int dheight, int dwidth)
{
dwidth = width;
dheight = height;
for (int hcheck = 0; hcheck<dheight; hcheck++)
{
for (int wcheck = 0; wcheck < dwidth; wcheck++)
{
Tile.DrawTile();
}
Console.Write("\n");
}
}
public static Array MakeRoom()
{
Tile room = new Tile(".", ConsoleColor.Blue, ConsoleColor.Red,true);
int bheight = (height-1);
int bwidth = (width-1);
int hstart = Random.randInt(1, bheight);
int wstart = Random.randInt(1, bwidth);
int rheight = Random.randInt(1, 5);
int rwidth = Random.randInt(1, 5);
while (hstart > height || hstart < 1 || wstart < 1 || wstart > width||
(hstart+rheight)>bheight|| (wstart+rwidth)>bwidth)
{
hstart = Random.randInt(1, bheight);
wstart = Random.randInt(1, bwidth);
rheight = Random.randInt(1, 5);
rwidth = Random.randInt(1, 5);
}
for (; hstart<rheight; hstart++)
{
for (; wstart < rwidth; wstart++)
{
board[hstart, wstart] = room;
}
}
return board;
}
这是我的图块的代码 class:
namespace BrandonPlayerGen
{
public class Tile
{
public static string symbol;
public static ConsoleColor foreColor;
public static ConsoleColor backColor;
public static string stairisHere;
public Tile(string boardSymbol, ConsoleColor
back, ConsoleColor fore, bool stairs)
{
backColor = back;
foreColor = fore;
symbol = boardSymbol;
if (stairs == true)
{
symbol = "<";
backColor = ConsoleColor.Green;
foreColor = ConsoleColor.White;
}
}
public static void DrawTile()
{
Console.BackgroundColor = backColor;
Console.ForegroundColor = foreColor;
Console.Write(symbol);
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
}
}
这里是在 main 中调用板的地方:
int height, width;
Console.Write("Please Enter Board height: ");
height = Convert.ToInt32(Console.ReadLine());
Console.Write("Please Enter Board height: ");
width = Convert.ToInt32(Console.ReadLine());
Board.height = height;
Board.width = width;
Board.MakeBoard(height, width);
Board.DrawBoard(Board.height, Board.width);
Console.ReadKey();
Board.MakeRoom();
Board.DrawBoard( Board.width, Board.height);
Console.ReadKey();
任何帮助将不胜感激,如果我的问题格式不正确,我很抱歉这是我的第一个问题。
编辑: 修复了生成图块的方式,以便每次都创建一个新图块,但是当我显示板时,它在整个板上都是相同的颜色和符号的问题。
您只创建了 Tile
的一个实例。您需要为面板中的每个单元格创建一个新单元格。
另外,除非你打算在你的看板中包含其他东西,否则我建议它的类型应该是 Tile[,]
而不是 object[,]
。
board = new Tile[bheight, bwidth];
for (int hcheck = 0; hcheck < (bheight-1); hcheck++)
{
for(int wcheck = 0; wcheck < (bwidth-1); wcheck++)
{
Tile empty = new Tile("#", ConsoleColor.Red, ConsoleColor.Yellow,false);
board[hcheck, wcheck] = empty;
}
}