今天无聊突发奇想做个贪吃蛇,虽然网上很多这东西了,不过自己写的感觉还行吧
游戏规则:
1、蛇起始长度5,每吃一个食物增加1,最大15过关
2、蛇用蓝色表示,食物用绿色,障碍物用黑色
3、当蛇碰到自己、墙壁、障碍物则游戏失败
4、方向键控制蛇的移动方向,蛇不可反方向移动,如正在向上移动,不能马上向下,只能向左、右、上运动
5、每过关一次速度提升一次
大概思路:
1、地图用网格的形式表示,蛇由方格组成,保存在list中
2、1中提到了方格,方格保存的内容有,颜色,坐标,是否可以通过,是否是食物
3、向前移动一次,将前面方格添加进蛇列表中,将列表最后一个移除,若为前方格子为食物,则不移除最后一个
4、使用while死循环来做整个移动
5、空格键为加速键,通过修改while循环sleep时间来实现加速
包括了3个类一个主窗体,分别是Node(用来表示方格)、Map(用来表示地图)、Serpent(用来表示蛇),另外一个主窗体。下面依次把代码贴上,基本上每个方法都有注释
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 }
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)
168 {
169 brush = new SolidBrush(_node.HinderColor);
170 }
171 RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
172 rects[indexcount] = r;
173 indexcount++;
174 }
175 //添加底部方格进rects列表中
176 for (int i = 0; i < RowCount; i++)
177 {
178 Node _node = _nodes[i][ComsCount - 1];
179 _node.SetHinder(true);
180
181 RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
182 rects[indexcount] = r;
183 indexcount++;
184 }
185 //添加左侧方格进列表,因为左侧最上面以及最下面的两个方格已经添加进去,这里不需要重复添加
186 for (int i = 1; i < ComsCount - 1; i++)
187 {
188 Node _node = _nodes[0][i];
189 _node.SetHinder(true);
190 RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
191 rects[indexcount] = r;
192 indexcount++;
193 }
194 //添加右侧方格进列表,因为右侧最上面以及最下面两个方格已经添加进去,这里不需要重复添加
195 for (int i = 1; i < ComsCount - 1; i++)
196 {
197 Node _node = _nodes[RowCount - 1][i];
198 _node.SetHinder(true);
199 RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
200 rects[indexcount] = r;
201 indexcount++;
202 }
203 g.FillRectangles(brush, rects);
204 }
205 }
206 }
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 Serpent
12 {
13 private List<Node> serpentList = new List<Node>();
14 private Direction direction = Direction.Right;//运动方向
15 private int maxCount = 15;
16 private int minCount = 5;
17 private System.Windows.Forms.Control MapPanel;
18 Graphics g;
19 /// <summary>
20 /// 设置蛇长度数据
21 /// </summary>
22 /// <param name="maxLength">最大长度</param>
23 /// <param name="minLength">最小长度</param>
24 public Serpent(int maxLength, int minLength, System.Windows.Forms.Control c)
25 {
26 maxCount = maxLength;
27 minCount = minLength;
28 MapPanel = c;
29 g = MapPanel.CreateGraphics();
30 }
31
32 /// <summary>
33 /// 初始化蛇
34 /// </summary>
35 public void InitializeSerpent()
36 {
37 SolidBrush brush = null;
38 RectangleF[] rects = new RectangleF[minCount];
39 for (int i = 1; i < minCount; i++)
40 {
41 Node indexnode = new Node(i, 1);
42 indexnode.SetSerpent(true);//设置蛇颜色
43 serpentList.Insert(0, indexnode);
44 if (brush == null)
45 {
46 brush = new SolidBrush(indexnode.SerpentColor);
47 }
48 rects[i] = new RectangleF(indexnode.X * indexnode.Width, indexnode.Y * indexnode.Width, indexnode.Width, indexnode.Width);
49 }
50 g.FillRectangles(brush, rects);
51 }
52
53
54
55 /// <summary>
56 /// 插入一个
57 /// </summary>
58 /// <param name="node"></param>
59 public void InsertNode(Node node)
60 {
61 serpentList.Insert(0, node);
62 node.SetSerpent(true);
63 SolidBrush brush = new SolidBrush(node.SerpentColor);
64 RectangleF rect = new RectangleF(node.X * node.Width, node.Y * node.Width, node.Width, node.Width);
65 g.FillRectangle(brush, rect);
66 }
67
68 /// <summary>
69 /// 移除尾巴
70 /// </summary>
71 /// <param name="node"></param>
72 public void RemoveNode()
73 {
74 Node node = serpentList[serpentList.Count - 1];
75 serpentList.Remove(node);
76 node.SetSerpent(false);
77 SolidBrush brush = new SolidBrush(node.BgColor);
78 RectangleF rect = new RectangleF(node.X * node.Width, node.Y * node.Width, node.Width, node.Width);
79 g.FillRectangle(brush, rect);
80 }
81
82 /// <summary>
83 /// 获取蛇头
84 /// </summary>
85 /// <returns>蛇头方格</returns>
86 public Node GetSerpentHead()
87 {
88 return serpentList[0];
89 }
90
91 /// <summary>
92 /// 蛇是否最长
93 /// </summary>
94 /// <returns></returns>
95 public bool IsMax()
96 {
97 if (serpentList.Count == maxCount)
98 return true;
99 else
100 return false;
101 }
102
103 /// <summary>
104 /// 蛇运动方向
105 /// </summary>
106 public Direction Direction
107 {
108 get { return direction; }
109 set { direction = value; }
110 }
111
112 }
113 /// <summary>
114 /// 运动方向
115 /// </summary>
116 public enum Direction
117 {
118 Left,
119 Up,
120 Right,
121 Down
122 }
123 }
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Threading;
9
10 namespace EngorgeSerpent
11 {
12 public partial class MainForm : Form
13 {
14 public MainForm()
15 {
16 InitializeComponent();
17
18 }
19 List<List<Node>> maplist = new List<List<Node>>();
20 Map map;
21 Serpent serpent;
22 Graphics g;
23 int level = 1;
24 /// <summary>
25 /// 运行线程
26 /// </summary>
27 Thread Work_Thread = null;
28 /// <summary>
29 /// 运行线程监控值
30 /// </summary>
31 bool IsWork = false;
32 int sleepTime = 1000;
33 int thissleeptime;
34
35 private void MainForm_Load(object sender, EventArgs e)
36 {
37 g = this.panel1.CreateGraphics();
38 map = new Map(40, 30, this.panel1);//这里可以对画布进行下大小设置 此处偷懒省略
39 LoadMapList();//加载障碍物列表
40 }
41
42 /// <summary>
43 /// 默认将地图设置为30*40
44 /// </summary>
45 private void SetMap()
46 {
47 map.ResetMap();
48 g.Clear(map.BgColor);
49 map.SetBorder();//设置边界
50 List<Node> hiderList = GetHider();//获取障碍物列表
51 map.SetHinder(hiderList);//设置障碍物
52 SetSerpent();//初始化蛇
53 }
54
55 /// <summary>
56 /// 设置蛇
57 /// </summary>
58 private void SetSerpent()
59 {
60 serpent = new Serpent(15, 5, this.panel1);
61 serpent.InitializeSerpent();//初始化蛇
62 }
63
64 /// <summary>
65 /// 获取地图障碍物列表 以增加不同级别难度
66 /// </summary>
67 private void LoadMapList()
68 {
69 //目前分为5个级别
70 //第一级别
71 List<Node> hiderList1 = new List<Node>();
72 for (int i = 15; i < 25; i++)
73 {
74 hiderList1.Add(map.GetNode(i, 15));
75 hiderList1.Add(map.GetNode(15, i));
76 }
77 maplist.Add(hiderList1);
78
79 //第二级别
80 List<Node> hiderList2 = new List<Node>();
81 for (int i = 7; i < 25; i++)
82 {
83 hiderList2.Add(map.GetNode(i, 15));
84 hiderList2.Add(map.GetNode(15, i));
85 }
86 maplist.Add(hiderList2);
87
88 //第三级别
89 List<Node> hiderList3 = new List<Node>();
90 for (int i = 7; i < 25; i++)
91 {
92 hiderList3.Add(map.GetNode(i, 15));
93 hiderList3.Add(map.GetNode(15, i));
94 hiderList3.Add(map.GetNode(i, 25));
95 }
96 maplist.Add(hiderList3);
97
98 //第四级别
99 List<Node> hiderList4 = new List<Node>();
100 for (int i = 7; i < 25; i++)
101 {
102 hiderList4.Add(map.GetNode(i, 25));
103 hiderList4.Add(map.GetNode(i, 15));
104 hiderList4.Add(map.GetNode(15, i));
105 hiderList4.Add(map.GetNode(i, 7));
106 }
107 maplist.Add(hiderList4);
108
109 //第五级别
110 List<Node> hiderList5 = new List<Node>();
111 for (int i = 7; i < 25; i++)
112 {
113 hiderList5.Add(map.GetNode(i, 25));
114 hiderList5.Add(map.GetNode(i, 15));
115 hiderList5.Add(map.GetNode(15, i));
116 hiderList5.Add(map.GetNode(i, 7));
117 hiderList5.Add(map.GetNode(i, 35));
118 }
119 for (int i = 12; i < 20; i++)
120 {
121 hiderList5.Add(map.GetNode(7, i));
122 hiderList5.Add(map.GetNode(25, i));
123 }
124 maplist.Add(hiderList5);
125 }
126
127 /// <summary>
128 /// 获取障碍物列表
129 /// </summary>
130 /// <returns></returns>
131 private List<Node> GetHider()
132 {
133 //这里可以添加多个地图,当级别改变时需要重新加载
134 return maplist[level - 1];
135 }
136
137 /// <summary>
138 /// 重置地图
139 /// </summary>
140 /// <param name="sender"></param>
141 /// <param name="e"></param>
142 private void btnResetMap_Click(object sender, EventArgs e)
143 {
144 IsWork = false;
145 btnStop.Enabled = false;
146 button3.Enabled = false;
147 button2.Enabled = true;
148 //map.ResetMap();
149 SetMap();
150 }
151
152 /// <summary>
153 /// 运行
154 /// </summary>
155 private void Work()
156 {
157 map.SetFood();//设置食物
158 while (IsWork)
159 {
160 Node node_index;
161 Node serpentHead = serpent.GetSerpentHead();
162 switch (serpent.Direction)
163 {
164 case Direction.Left:
165 node_index = map.GetNode(serpentHead.X - 1, serpentHead.Y);
166 break;
167 case Direction.Right:
168 node_index = map.GetNode(serpentHead.X + 1, serpentHead.Y);
169 break;
170 case Direction.Up:
171 node_index = map.GetNode(serpentHead.X, serpentHead.Y - 1); break;
172 default:
173 node_index = map.GetNode(serpentHead.X, serpentHead.Y + 1);
174 break;
175 }
176 SerpentState index_move = SerpentMove(node_index);
177 if (index_move == SerpentState.Error)//游戏结束
178 {
179 IsWork = false;
180 //map.ResetMap();
181 MessageBox.Show("游戏结束!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
182 sleepTime = 1000;
183 level = 1;
184 thissleeptime = sleepTime;
185 lblLevel.BeginInvoke(new MethodInvoker(delegate()
186 {
187 btnStop.Enabled = false;
188 button3.Enabled = false;
189 button2.Enabled = true;
190 lblLevel.Text = "1";
191 lblCount.Text = "5";
192 }));
193 }
194 else if (index_move == SerpentState.NextLevel)
195 {
196 IsWork = false;
197 this.lblCount.BeginInvoke(new MethodInvoker(delegate()
198 {
199 level += 1;
200 lblLevel.Text = level.ToString();
201 lblCount.Text = "5";
202 }));
203 sleepTime = sleepTime / 2;
204 thissleeptime = sleepTime;
205 SetMap();//重置地图
206 }
207 else
208 {
209
210 Thread.Sleep(thissleeptime);
211 }
212 }
213 map.ResetMap();
214 }
215
216 /// <summary>
217 /// 开始
218 /// </summary>
219 /// <param name="sender"></param>
220 /// <param name="e"></param>
221 private void button2_Click(object sender, EventArgs e)
222 {
223 IsWork = false;
224 btnStop.Enabled = false;
225 button3.Enabled = false;
226 button2.Enabled = true;
227 //map.ResetMap();
228 SetMap();
229 thissleeptime = sleepTime;
230 this.panel1.Focus();
231 IsWork = true;
232 this.btnStop.Enabled = true;
233 this.button3.Enabled = true;
234 button2.Enabled = false;
235 Work_Thread = new Thread(new ThreadStart(Work));
236 Work_Thread.IsBackground = true;
237 Work_Thread.Start();
238 }
239
240 private void MainForm_KeyDown(object sender, KeyEventArgs e)
241 {
242
243 if (e.KeyCode == Keys.Right)
244 {
245 if (serpent.Direction != Direction.Left)
246 serpent.Direction = Direction.Right;
247 }
248 else if (e.KeyCode == Keys.Left)
249 {
250 if (serpent.Direction != Direction.Right)
251 serpent.Direction = Direction.Left;
252 }
253 else if (e.KeyCode == Keys.Up)
254 {
255 if (serpent.Direction != Direction.Down)
256 serpent.Direction = Direction.Up;
257 }
258 else if (e.KeyCode == Keys.Down)
259 {
260 if (serpent.Direction != Direction.Up)
261 serpent.Direction = Direction.Down;
262 }
263 else if (e.KeyCode == Keys.Space)
264 {
265 thissleeptime = sleepTime / 2;
266 }
267 else if (e.KeyCode == Keys.Escape)
268 {
269 if (IsWork)
270 {
271 this.button3.Text = "继续";
272 IsWork = false;
273 }
274
275 }
276 }
277
278 /// <summary>
279 /// 暂停
280 /// </summary>
281 /// <param name="sender"></param>
282 /// <param name="e"></param>
283 private void button3_Click(object sender, EventArgs e)
284 {
285 if (!IsWork)
286 {
287 this.button3.Text = "暂停";
288 IsWork = true;
289 Work_Thread = new Thread(new ThreadStart(Work));
290 Work_Thread.IsBackground = true;
291 Work_Thread.Start();
292 }
293 else
294 {
295 this.button3.Text = "继续";
296 IsWork = false;
297 }
298 }
299 /// <summary>
300 /// 退出
301 /// </summary>
302 /// <param name="sender"></param>
303 /// <param name="e"></param>
304 private void button4_Click(object sender, EventArgs e)
305 {
306 this.Close();
307 }
308
309 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
310 {
311 IsWork = false;
312 Application.Exit();
313 System.Diagnostics.Process.GetCurrentProcess().Kill();
314 }
315
316
317
318 private void btnStop_Click(object sender, EventArgs e)
319 {
320 // map.ResetMap();
321 btnStop.Enabled = false;
322 button3.Enabled = false;
323 button2.Enabled = true;
324 IsWork = false;
325 Work_Thread.Abort();
326 SetMap();
327 }
328
329 /// <summary>
330 /// 移动
331 /// </summary>
332 /// <param name="node">将要移动到的节点</param>
333 /// <returns>返回状态</returns>
334 private SerpentState SerpentMove(Node node)
335 {
336 if (!node.IsPass)
337 {
338 return SerpentState.Error;
339 }
340 serpent.InsertNode(node);
341 if (!node.IsFood)
342 {
343 //不是食物,则移除最后一个节点
344 serpent.RemoveNode();
345 }
346 else
347 {
348 lblCount.BeginInvoke(new MethodInvoker(delegate()
349 {
350 this.lblCount.Text = (Convert.ToInt32(this.lblCount.Text.Trim()) + 1).ToString();
351 }));
352 map.SetFood();//设置食物
353 }
354
355 if (serpent.IsMax())
356 {
357 return SerpentState.NextLevel;
358 }
359 return SerpentState.Moving;
360 }
361
362 private void MainForm_KeyUp(object sender, KeyEventArgs e)
363 {
364 if (e.KeyCode == Keys.Space)
365 {
366 thissleeptime = sleepTime;
367 }
368 }
369
370 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
371 {
372 int index = 1;
373 int index_count = Convert.ToInt32(comboBox1.Text);
374 for (int i = 1; i < index_count; i++)
375 {
376 index = index * 2;
377 }
378 level = index_count;
379 sleepTime = 1000 / index;
380 thissleeptime = sleepTime;
381 btnStop.Enabled = false;
382 button3.Enabled = false;
383 button2.Enabled = true;
384 IsWork = false;
385
386 SetMap();
387 lblCount.Text = "5";
388 lblLevel.Text = index_count.ToString();
389 serpent.Direction = Direction.Right;
390
391 }
392
393 private void checkBox1_Click(object sender, EventArgs e)
394 {
395 comboBox1.Enabled = this.checkBox1.Checked;
396 }
397
398 }
399 public enum SerpentState
400 {
401 Moving,
402 NextLevel,
403 Error
404 }
405 }
主界面
欢迎大家批评指正,代码写的比较乱
项目下载地址:http://pan.baidu.com/share/link?shareid=130990&uk=33979446
可执行文件地址:http://pan.baidu.com/share/link?shareid=130991&uk=33979446