代码2:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace EngorgeSerpent
{
/// <summary>
/// 地图
/// </summary>
class Map
{
/// <summary>
/// 节点数组
/// </summary>
private List<List<Node>> _nodes;
private int RowCount;
private int ComsCount;
private Color bgColor = Color.FromArgb(224, 224, 224);
private System.Windows.Forms.Control MapPanel;
Graphics g;
/// <summary>
/// 地图背景色 和node中背景色一致
/// </summary>
public Color BgColor
{
get { return bgColor; }
}
/// <summary>
/// 构造方法
/// </summary>
/// <param name="rows">行数</param>
/// <param name="coms">列数</param>
public Map(int rows, int coms, System.Windows.Forms.Control c)
{
RowCount = rows;
ComsCount = coms;
MapPanel = c;
g = c.CreateGraphics();
_nodes = new List<List<Node>>();
for (int i = 0; i < rows; i++)//行
{
List<Node> index = new List<Node>();
for (int j = 0; j < coms; j++)
{
Node node = new Node(j, i);
index.Add(node);
}
_nodes.Add(index);
}
}
/// <summary>
/// 构造方法
/// </summary>
/// <param name="rows">行数</param>
/// <param name="coms">列数</param>
/// <param name="width">节点宽度</param>
public Map(int rows, int coms, int width, System.Windows.Forms.Control c)
{
RowCount = rows;
ComsCount = coms;
MapPanel = c;
g = c.CreateGraphics();
_nodes = new List<List<Node>>();
for (int i = 0; i < coms; i++)//行
{
List<Node> index = new List<Node>();
for (int j = 0; j < rows; j++)
{
Node node = new Node(j, i, width);
index.Add(node);
}
_nodes.Add(index);
}
}
/// <summary>
/// 重新加载地图
/// </summary>
public void ResetMap()
{
for (int i = 0; i < ComsCount; i++)//行
{
for (int j = 0; j < RowCount; j++)
{
Node node = GetNode(i, j);
node.IsPass = true;
node.IsFood = false;
}
}
}
/// <summary>
/// 获得节点
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public Node GetNode(int x, int y)
{
return _nodes[y][x];
}
/// <summary>
/// 设置食物
/// </summary>
public void SetFood()
{
SolidBrush brush = null;
int _x, _y;
Random r = new Random();
while (true)
{
_x = r.Next(0, RowCount);
_y = r.Next(0, ComsCount);
if (_nodes[_x][_y].IsPass)
{
break;
}
}
Node nodeindex = _nodes[_x][_y];
nodeindex.SetFood(true);
brush = new SolidBrush(nodeindex.FoodColor);
RectangleF[] rects = { new RectangleF(nodeindex.X * nodeindex.Width, nodeindex.Y * nodeindex.Width, nodeindex.Width, nodeindex.Width) };
g.FillRectangles(brush, rects);
}
/// <summary>
/// 设置障碍物
/// </summary>
/// <param name="list"></param>
public void SetHinder(List<Node> list)
{
SolidBrush brush = null;
RectangleF[] rects = new RectangleF[list.Count];
for (int i = 0; i < list.Count; i++)
{
Node _node = list[i];
_node.SetHinder(true);
_node.IsPass = false;
if (brush == null)
{
brush = new SolidBrush(_node.HinderColor);
}
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[i] = r;
}
g.FillRectangles(brush, rects);
}
/// <summary>
/// 设置边界
/// </summary>
public void SetBorder()
{
//通过计算得出边界的个数是2(x+y-2)个方格
SolidBrush brush = null;
int borders = 2 * (ComsCount + RowCount - 2);
RectangleF[] rects = new RectangleF[borders];
int indexcount = 0;
//添加顶部方格进rects列表中
for (int i = 0; i < RowCount; i++)
{
Node _node = _nodes[i][0];
_node.SetHinder(true);
if (brush == null)
{
brush = new SolidBrush(_node.HinderColor);
}
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] = r;
indexcount++;
}
//添加底部方格进rects列表中
for (int i = 0; i < RowCount; i++)
{
Node _node = _nodes[i][ComsCount - 1];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] = r;
indexcount++;
}
//添加左侧方格进列表,因为左侧最上面以及最下面的两个方格已经添加进去,这里不需要重复添加
for (int i = 1; i < ComsCount - 1; i++)
{
Node _node = _nodes[0][i];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] = r;
indexcount++;
}
//添加右侧方格进列表,因为右侧最上面以及最下面两个方格已经添加进去,这里不需要重复添加
for (int i = 1; i < ComsCount - 1; i++)
{
Node _node = _nodes[RowCount - 1][i];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] = r;
indexcount++;
}
g.FillRectangles(brush, rects);
}
}
}










