这个程序应该制作一个数组,并根据其中的值,它应该绘制不同颜色的点。
首先它创建一个空白地图(数组)。 之后,它会用障碍物填充地图。 最后,它应该在图像框上以图形方式表示障碍,但它不会那样做。
错误是什么?
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
continue;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
Brush aBrush = (Brush)Brushes.Black;
Graphics g = this.CreateGraphics();
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i,j] == 253)
{
continue;
}
//út
else if (map[i, j] == 0)
{
continue;
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, i, j);
}
}
}
}
}
修正了一些错误。
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public void PictureBuilder(byte[,] map)
{
Brush aBrush = (Brush)Brushes.Black;
Graphics g = e.Graphics();
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i, j] == 0)
{
}
//út
else if (map[i, j] == 253)
{
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, 1, 1);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte startX = 1;
byte startY = 1;
byte endX = 99;
byte endY = 99;
byte currentX = startX;
byte currentY = startY;
while (true)
{
}
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
PictureBuilder(map);
}
}
更多修复。
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public void PictureBuilder(byte[,] map, Graphics g)
{
Brush aBrush = (Brush)Brushes.Black;
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i, j] == 0)
{
}
//út
else if (map[i, j] == 253)
{
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, 1, 1);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte startX = 1;
byte startY = 1;
byte endX = 99;
byte endY = 99;
byte currentX = startX;
byte currentY = startY;
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
Graphics g = e.Graphics;
PictureBuilder(map, g);
}
}
...克洛斯
public partial class Form1 : Form
{
public byte[,] ObstacleGenerator(byte[,] map, byte leftTopX, byte leftTopY, byte rightBottomX, byte rightBottomY)
{
// akadályt építi fel
for (int i = leftTopX; i <= rightBottomX; i++)
{
for (int j = leftTopY; j <= rightBottomY; j--)
{
map[i, j] = 1;
}
}
return map;
}
public byte[,] MapGenerator()
{
// a térképet építi fel
byte[,] map = new byte[102-1, 102-1];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//a térkép grafikusan nem látható pereme
if (i == 0 || j == 0 || i == map.GetLength(0)-1 || j == map.GetLength(1)-1)
{
map[i, j] = 253;
}
else
//járható út
{
map[i, j] = 0;
}
}
}
return map;
}
public void PictureBuilder(byte[,] map, Graphics g)
{
Brush aBrush = (Brush)Brushes.Black;
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
//láthatatlan perem
if (map[i, j] == 0)
{
}
//út
else if (map[i, j] == 253)
{
}
//akadály
else if (map[i, j] == 1)
{
g.FillRectangle(aBrush, i, j, 1, 1);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
byte startX = 1;
byte startY = 1;
byte endX = 99;
byte endY = 99;
byte currentX = startX;
byte currentY = startY;
byte[,] map = MapGenerator();
map = ObstacleGenerator(map, 20, 80, 40, 20);
Graphics g = e.Graphics;
PictureBuilder(map, e.Graphics);
}