在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
今天无聊突发奇想做个贪吃蛇,虽然网上很多这东西了,不过自己写的感觉还行吧 游戏规则: 1、蛇起始长度5,每吃一个食物增加1,最大15过关 2、蛇用蓝色表示,食物用绿色,障碍物用黑色 3、当蛇碰到自己、墙壁、障碍物则游戏失败 4、方向键控制蛇的移动方向,蛇不可反方向移动,如正在向上移动,不能马上向下,只能向左、右、上运动 5、每过关一次速度提升一次
大概思路: 1、地图用网格的形式表示,蛇由方格组成,保存在list中 2、1中提到了方格,方格保存的内容有,颜色,坐标,是否可以通过,是否是食物 3、向前移动一次,将前面方格添加进蛇列表中,将列表最后一个移除,若为前方格子为食物,则不移除最后一个 4、使用while死循环来做整个移动 5、空格键为加速键,通过修改while循环sleep时间来实现加速
包括了3个类一个主窗体,分别是Node(用来表示方格)、Map(用来表示地图)、Serpent(用来表示蛇),另外一个主窗体。下面依次把代码贴上,基本上每个方法都有注释
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Drawing; 5 6 namespace EngorgeSerpent 7 { 8 /// <summary> 9 /// 节点 10 /// </summary> 11 class Node 12 { 13 #region 字段 14 private int x; 15 private int y; 16 private int width = 10; 17 private bool isFood = false; 18 private bool isPass = true;//是否可通过 19 private Color bgColor = Color.FromArgb(224, 224, 224); 20 private Color foodColor = Color.Green; 21 private Color hinderColor = Color.Black; 22 private Color thisColor; 23 private Color serpentColor = Color.Chocolate; 24 25 #endregion 26 /// <summary> 27 /// 设置食物参数 28 /// </summary> 29 /// <param name="_isFood"></param> 30 public void SetFood(bool _isFood) 31 { 32 IsFood = _isFood; 33 if (_isFood) 34 { 35 ThisColor = FoodColor; 36 37 } 38 else 39 { 40 ThisColor = BgColor; 41 } 42 } 43 44 /// <summary> 45 /// 设置障碍物参数 46 /// </summary> 47 /// <param name="_isHinder">是否为障碍物</param> 48 public void SetHinder(bool _isHinder) 49 { 50 IsPass =! _isHinder; 51 if (_isHinder) 52 { 53 ThisColor = HinderColor; 54 } 55 else 56 { 57 ThisColor = BgColor; 58 } 59 } 60 61 /// <summary> 62 /// 设置蛇颜色 63 /// </summary> 64 /// <param name="_isSerpent"></param> 65 public void SetSerpent(bool _isSerpent) 66 { 67 IsPass = !_isSerpent; 68 if (_isSerpent) 69 { 70 ThisColor = SerpentColor; 71 } 72 else 73 { 74 ThisColor = BgColor; 75 } 76 } 77 #region 构造函数 78 public Node() 79 { 80 thisColor = bgColor; 81 } 82 83 /// <summary> 84 /// 有参构造方法 85 /// </summary> 86 /// <param name="_x">相对x坐标</param> 87 /// <param name="_y">相对y坐标</param> 88 /// <param name="_width">边长</param> 89 /// <param name="_isFood">是否是食物</param> 90 /// <param name="_isPass">是否可通过</param> 91 public Node(int _x, int _y, int _width, bool _isFood, bool _isPass) 92 { 93 thisColor = bgColor; 94 X = _x; 95 Y = _y; 96 Width = _width; 97 IsFood = _isFood; 98 IsPass = _isPass; 99 } 100 101 /// <summary> 102 /// 有参构造方法 103 /// </summary> 104 /// <param name="_x">相对x坐标</param> 105 /// <param name="_y">相对y坐标</param> 106 /// <param name="_width">边长</param> 107 public Node(int _x, int _y, int _width) 108 { 109 X = _x; 110 Y = _y; 111 Width = _width; 112 } 113 114 /// <summary> 115 /// 有参构造方法 116 /// </summary> 117 /// <param name="_x">相对x坐标</param> 118 /// <param name="_y">相对y坐标</param> 119 public Node(int _x, int _y) 120 { 121 X = _x; 122 Y = _y; 123 } 124 #endregion 125 126 #region 属性 127 /// <summary> 128 /// 蛇颜色 129 /// </summary> 130 public Color SerpentColor 131 { 132 get { return serpentColor; } 133 } 134 135 /// <summary> 136 /// 背景色 137 /// </summary> 138 public Color BgColor 139 { 140 get { return bgColor; } 141 } 142 143 /// <summary> 144 /// 食物颜色 145 /// </summary> 146 public Color FoodColor 147 { 148 get { return foodColor; } 149 } 150 151 /// <summary> 152 /// 障碍物颜色 153 /// </summary> 154 public Color HinderColor 155 { 156 get { return hinderColor; } 157 } 158 159 /// <summary> 160 /// 当前颜色 161 /// </summary> 162 public Color ThisColor 163 { 164 get { return thisColor; } 165 set { thisColor = value; } 166 } 167 168 /// <summary> 169 /// 获取或设置相对横坐标 170 /// </summary> 171 public int X 172 { 173 get { return x; } 174 set { x = value; } 175 } 176 177 /// <summary> 178 /// 获取或设置相对纵坐标 179 /// </summary> 180 public int Y 181 { 182 get { return y; } 183 set { y = value; } 184 } 185 186 /// <summary> 187 /// 获取或设置节点边长 188 /// </summary> 189 public int Width 190 { 191 get { return width; } 192 set { width = value; } 193 } 194 195 /// <summary> 196 /// 获取或设置是否为食物 197 /// </summary> 198 public bool IsFood 199 { 200 get { return isFood; } 201 set { isFood = value; } 202 } 203 204 /// <summary> 205 /// 获取或设置是否可以通过 206 /// </summary> 207 public bool IsPass 208 { 209 get { return isPass; } 210 set { isPass = value; } 211 } 212 #endregion 213 } 214 }
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Drawing; 5 6 namespace EngorgeSerpent 7 { 8 /// <summary> 9 /// 地图 10 /// </summary> 11 class Map 12 { 13 /// <summary> 14 /// 节点数组 15 /// </summary> 16 private List<List<Node>> _nodes; 17 private int RowCount; 18 private int ComsCount; 19 private Color bgColor = Color.FromArgb(224, 224, 224); 20 private System.Windows.Forms.Control MapPanel; 21 Graphics g; 22 /// <summary> 23 /// 地图背景色 和node中背景色一致 24 /// </summary> 25 public Color BgColor 26 { 27 get { return bgColor; } 28 } 29 /// <summary> 30 /// 构造方法 31 /// </summary> 32 /// <param name="rows">行数</param> 33 /// <param name="coms">列数</param> 34 public Map(int rows, int coms, System.Windows.Forms.Control c) 35 { 36 RowCount = rows; 37 ComsCount = coms; 38 MapPanel = c; 39 g = c.CreateGraphics(); 40 _nodes = new List<List<Node>>(); 41 for (int i = 0; i < rows; i++)//行 42 { 43 List<Node> index = new List<Node>(); 44 for (int j = 0; j < coms; j++) 45 { 46 Node node = new Node(j, i); 47 index.Add(node); 48 } 49 _nodes.Add(index); 50 } 51 } 52 53 /// <summary> 54 /// 构造方法 55 /// </summary> 56 /// <param name="rows">行数</param> 57 /// <param name="coms">列数</param> 58 /// <param name="width">节点宽度</param> 59 public Map(int rows, int coms, int width, System.Windows.Forms.Control c) 60 { 61 RowCount = rows; 62 ComsCount = coms; 63 MapPanel = c; 64 g = c.CreateGraphics(); 65 _nodes = new List<List<Node>>(); 66 for (int i = 0; i < coms; i++)//行 67 { 68 List<Node> index = new List<Node>(); 69 for (int j = 0; j < rows; j++) 70 { 71 Node node = new Node(j, i, width); 72 index.Add(node); 73 } 74 _nodes.Add(index); 75 } 76 } 77 78 /// <summary> 79 /// 重新加载地图 80 /// </summary> 81 public void ResetMap() 82 { 83 for (int i = 0; i < ComsCount; i++)//行 84 { 85 for (int j = 0; j < RowCount; j++) 86 { 87 Node node = GetNode(i, j); 88 node.IsPass = true; 89 node.IsFood = false; 90 } 91 } 92 } 93 /// <summary> 94 /// 获得节点 95 /// </summary> 96 /// <param name="x"></param> 97 /// <param name="y"></param> 98 /// <returns></returns> 99 public Node GetNode(int x, int y) 100 { 101 return _nodes[y][x]; 102 } 103 104 /// <summary> 105 /// 设置食物 106 /// </summary> 107 public void SetFood() 108 { 109 SolidBrush brush = null; 110 int _x, _y; 111 Random r = new Random(); 112 while (true) 113 { 114 _x = r.Next(0, RowCount); 115 _y = r.Next(0, ComsCount); 116 if (_nodes[_x][_y].IsPass) 117 { 118 break; 119 } 120 } 121 Node nodeindex = _nodes[_x][_y]; 122 nodeindex.SetFood(true); 123 brush = new SolidBrush(nodeindex.FoodColor); 124 RectangleF[] rects = { new RectangleF(nodeindex.X * nodeindex.Width, nodeindex.Y * nodeindex.Width, nodeindex.Width, nodeindex.Width) }; 125 g.FillRectangles(brush, rects); 126 } 127 128 /// <summary> 129 /// 设置障碍物 130 /// </summary> 131 /// <param name="list"></param> 132 public void SetHinder(List<Node> list) 133 { 134 SolidBrush brush = null; 135 RectangleF[] rects = new RectangleF[list.Count]; 136 for (int i = 0; i < list.Count; i++) 137 { 138 Node _node = list[i]; 139 _node.SetHinder(true); 140 _node.IsPass = false; 141 if (brush == null) 142 { 143 brush = new SolidBrush(_node.HinderColor); 144 } 145 RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width); 146 rects[i] = r; 147 } 148 g.FillRectangles(brush, rects); 149 } 150 151 /// <summary> 152 /// 设置边界 153 /// </summary> 154 public void SetBorder() 155 { 156 //通过计算得出边界的个数是2(x+y-2)个方格 157 158 SolidBrush brush = null; 159 int borders = 2 * (ComsCount + RowCount - 2); 160 RectangleF[] rects = new RectangleF[borders]; 161 int indexcount = 0; 162 //添加顶部方格进rects列表中 163 for (int i = 0; i < RowCount; i++) 164 { 165 Node _node = _nodes[i][0]; 166 _node.SetHinder(true); 167 if (brush == null |
请发表评论