該源碼是我在百度知識庫藉助前輩的的經驗,加上本身的一點小改動寫的一個很是簡陋的貪吃蛇小程序。若是大家有更好的改動方案,歡迎評論。小程序
進入主題吧!數組
1.建立一個桌面應運程序,拖一個定時器控件。這樣,程序界面完成。下面是源碼!dom
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 }