Winfrom 极简版贪吃蛇源码
该源码是我在百度知识库借助前辈的的经验,加上自己的一点小改动写的一个非常简陋的贪吃蛇小程序。如果你们有更好的改动方案,欢迎评论。
进入主题吧!
1.创建一个桌面应运程序,拖一个定时器控件。这样,程序界面完成。下面是源码!
1 /// <summary> 2 /// 键盘状态,初始为 start 3 /// </summary> 4 string Key_Name = "start"; 5 6 /// <summary> 7 /// 蛇身数组 8 /// </summary> 9 Label[] Snake_Body = new Label[3000]; 10 11 /// <summary> 12 /// 随机数,用于food 13 /// </summary> 14 Random R = new Random(); 15 16 /// <summary> 17 /// 记录位置 18 /// </summary> 19 int Snake_Body_content_x = 0, Snake_Body_content_y = 0; 20 21 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 this.Top = 120; 25 this.Left = 120; 26 this.Width = 800; 27 this.Height = 800; 28 this.BackColor = Color.White; 29 30 //初始化一个Label蛇体,长度为5个Label 一个Label height = weight = 10 31 32 for (int i = 0; i < 8; i++) 33 { 34 //蛇段 35 Label Snake_Boby_content = new Label(); 36 Snake_Boby_content.Height = 20; 37 Snake_Boby_content.Width = 20; 38 39 //蛇段的位置 40 Snake_Boby_content.Top = 400; 41 Snake_Boby_content.Left = 400 - 2 * 10; 42 43 //背景色 44 this.BackColor = Color.White; 45 //Snake_Boby_content.BackColor = Color.Black; 46 //Snake_Boby_content.Text = "▉"; 47 Snake_Boby_content.Image = Image.FromFile(Application.StartupPath + @"\3.png"); 48 49 //获取或设置包含有关控件的数据的对象 50 Snake_Boby_content.Tag = i; 51 52 //加入蛇体 53 Snake_Body[i] = Snake_Boby_content; 54 this.Controls.Add(Snake_Boby_content); 55 } 56 57 //每隔一段时间发生一次右移 58 tmTimer.Tick += new EventHandler(tmTimer_Tick); 59 60 //按键时发生的事件监控 61 this.KeyDown += new KeyEventHandler(Form1_KeyDown); 62 63 Snake_food(); //food 生成 64 //tmTimer.Start(); //计时器开始 65 tmTimer.Enabled = true; 66 tmTimer.Interval = 300; 67 } 68 69 /// <summary> 70 /// snake的自动移动事件 71 /// </summary> 72 /// <param name="sender"></param> 73 /// <param name="e"></param> 74 private void tmTimer_Tick(object sender, EventArgs e) 75 { 76 //用来记录snake的head的xy坐标 77 int x, y; 78 x = Snake_Body[0].Left; 79 y = Snake_Body[0].Top; 80 //获取键盘代码 81 82 if (Key_Name == "start") //键盘状态处于初始状态 83 { 84 Snake_Body[0].Left = x + 20; //Snake_Body[0]右移10 85 Snake_move(x, y); //调用 86 } 87 if (Key_Name == "Right") //键盘状态处于向右状态 88 { 89 Snake_Body[0].Left = x + 20; 90 Snake_move(x,y); 91 } 92 if (Key_Name == "Up") //键盘状态处于向上状态 93 { 94 Snake_Body[0].Top = y - 20; 95 Snake_move(x,y); 96 } 97 if (Key_Name == "Down")//键盘状态处于向下状态 98 { 99 Snake_Body[0].Top = y + 20; 100 Snake_move(x, y); 101 } 102 if (Key_Name == "Left")//键盘状态处于向左状态 103 { 104 Snake_Body[0].Left = x - 20; 105 Snake_move(x, y); 106 } 107 108 //穿墙设置 109 if (x > 800) 110 { 111 Snake_Body[0].Left = 0; 112 } 113 if (x < 0) 114 { 115 Snake_Body[0].Left = 800; 116 } 117 if (y > 600) 118 { 119 Snake_Body[0].Top = 0; 120 } 121 if (y < 0) 122 { 123 Snake_Body[0].Top = 600; 124 } 125 126 //每动一次,判断是否与食物重合 127 Eat_time(); 128 } 129 130 /// <summary> 131 /// 对键盘按键输入的响应 132 /// </summary> 133 /// <param name="sender"></param> 134 /// <param name="e"></param> 135 136 private void Form1_KeyDown(object sender, KeyEventArgs e) 137 { 138 int x, y; 139 x = Snake_Body[0].Left; 140 y = Snake_Body[0].Top; 141 142 //获取键盘代码 143 Key_Name = e.KeyCode.ToString(); 144 145 if (Key_Name == "Right") //向右 146 { 147 Snake_Body[0].Left = x + 20; 148 Snake_move(x,y); 149 } 150 if (Key_Name == "Up") //向上 151 { 152 Snake_Body[0].Top = y - 20; 153 Snake_move(x, y); 154 } 155 if (Key_Name == "Down") //向下 156 { 157 Snake_Body[0].Top = y + 20; 158 Snake_move(x, y); 159 } 160 if (Key_Name == "Left") //向左 161 { 162 Snake_Body[0].Left = x - 20; 163 Snake_move(x, y); 164 } 165 166 //每按一次,判断是否与食物重合 167 Eat_time(); 168 } 169 170 /// <summary> 171 /// 蛇的自动移动事件 172 /// </summary> 173 /// <param name="x"></param> 174 /// <param name="y"></param> 175 public void Snake_move(int x, int y) 176 { 177 //记录x,y的中间变量 跟冒泡排序相同用法,临时存放变量 178 int temp_x = 0, temp_y = 0; 179 180 //遍历蛇身进行移动 181 for (int i = 1; Snake_Body[i] != null; i++) 182 { 183 if (i >= 3) 184 { 185 //将记录前一个蛇段位置赋给中间变量 186 temp_x = Snake_Body_content_x; 187 temp_y = Snake_Body_content_y; 188 } 189 if (i == 1) 190 { 191 //将记录蛇头的改变前的位置给x y 赋给第一个蛇段,并记录蛇段的位置 192 temp_x = Snake_Body[i].Left; 193 temp_y = Snake_Body[i].Top; 194 Snake_Body[i].Left = x; 195 Snake_Body[i].Top = y; 196 } 197 else 198 { 199 //将记录前一个个蛇段的改变前的位置temp_赋给第二个蛇段,并记录改前位置 200 Snake_Body_content_x = Snake_Body[i].Left; 201 Snake_Body_content_y = Snake_Body[i].Top; 202 Snake_Body[i].Left = temp_x; 203 Snake_Body[i].Top = temp_y; 204 } 205 } 206 } 207 208 /// <summary> 209 /// food的生成事件 210 /// </summary> 211 public void Snake_food() 212 { 213 //创建食物 214 Label Food = new Label(); 215 Food.Width = 20; 216 Food.Height = 20; 217 218 //生成一个随机位置的food 219 Food.Top = R.Next(1, 20) * 20; 220 Food.Left = R.Next(1, 20) * 20; 221 // Food.Text = ""; 222 Food.Tag = "food"; 223 Food.Image = Image.FromFile(Application.StartupPath + @"\3.png"); //Application.StartupPath + @"\3.png" 获取程序的根目录中的Debug中的图片 224 this.Controls.Add(Food); 225 } 226 227 /// <summary> 228 /// 蛇吃食物事件 229 /// </summary> 230 public void Eat_time() 231 { 232 double x1 = 20, y1 = 20, x2 = 20, y2 = 20; 233 //遍历Controls中所有Lable 234 foreach (Label lb in this.Controls) 235 { 236 //如果lb为food,将lable的位置记录 237 if (lb.Tag.ToString() == "food".ToString()) 238 { 239 x2 = lb.Left; 240 y2 = lb.Top; 241 } 242 //如果lable为snake,将lable的位置记录 243 if (lb.Tag.ToString() == "0".ToString()) 244 { 245 x1 = lb.Left; 246 y1 = lb.Top; 247 } 248 } 249 250 if (x2 == x1 && y2 == y1) //判断蛇头和食物是否重合, 251 { 252 Snake_eat(); 253 254 //重新随机生成食物 255 foreach (Label lb in this.Controls) 256 { 257 if (lb.Tag.ToString() == "food".ToString()) 258 { 259 lb.Top = R.Next(1, 20) * 20; 260 lb.Left = R.Next(1, 20) * 20; 261 } 262 } 263 } 264 } 265 266 /// <summary> 267 /// 蛇触碰到食物的事件 268 /// </summary> 269 public void Snake_eat() 270 { 271 int i = 0; 272 //遍历到蛇尾 将食物添加到最后一个位置 273 for (; Snake_Body[i] != null; i++) ; 274 //蛇触碰到food蛇段加1,定义蛇段 275 Label Snake_Boby_content = new Label(); 276 Snake_Boby_content.Width = 20; 277 Snake_Boby_content.Height = 20; 278 Snake_Boby_content.Top = Snake_Body_content_y; //记录最后一段的位置 279 Snake_Boby_content.Left = Snake_Body_content_x; 280 Snake_Boby_content.BackColor = Color.White; 281 Snake_Boby_content.Image = Image.FromFile(Application.StartupPath + @"\3.png"); 282 Snake_Boby_content.Tag = i; 283 Snake_Body[i] = Snake_Boby_content; 284 Snake_Boby_content.BackColor = Color.Black; 285 this.Controls.Add(Snake_Boby_content); 286 }