代码是网上找的,我没找到设置初始长度的代码,能帮我看下吗
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 //蛇身初始状态,吃的算法
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public class 蛇
{
public static int D;
public static int s=2;
}
void Add_food() //添加食物
{
Label label = new Label();
label.Name = "Lab" + 蛇.s;
label.BackColor = System.Drawing.Color.Red;
label.Size = new System.Drawing.Size(10, 10);
label.Margin = new System.Windows.Forms.Padding(0);
label.ForeColor = System.Drawing.Color.Red;
label.AutoSize = false;
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Random rd = new Random();
tag1:
label.Location = new System.Drawing.Point(rd.Next(0, 30) * 10, rd.Next(0, 30) * 10);
for (int i = 1; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (label.Location == lo.Location) goto tag1;
}
this.Controls.Add(label);
label.SendToBack();
panel1.SendToBack();
}
private void button1_Click(object sender, EventArgs e)
{
Add_food();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (蛇.D == 1) { System.Windows.Forms.SendKeys.Send("{S}"); return; }
if (蛇.D == 2) { System.Windows.Forms.SendKeys.Send("{W}"); return; }
if (蛇.D == 3) { System.Windows.Forms.SendKeys.Send("{A}"); return; }
if (蛇.D == 4) { System.Windows.Forms.SendKeys.Send("{D}"); return; };
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.S && 蛇.D != 2)
{
timer1.Enabled = false;
蛇.D = 1;
Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
Point[,] Lo = new Point[101, 101];
for (int i = 1; i <= 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.S && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.Y != 290)
{
for (int i = 2; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
}
}
if (lb.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y + 10))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
lab1.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y + 10);
lb.Location = le.Location;
this.Controls.Remove(le);
蛇.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y + 10)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;
}
if (e.KeyCode == Keys.S && lab1.Location.Y == 290)
{
timer1.Enabled = false;
MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
Application.Exit();
};
lab1.Top = lab1.Top + 10;
timer1.Enabled = true;
}
if (e.KeyCode == Keys.W && 蛇.D != 1)
{
timer1.Enabled = false;
蛇.D = 2;
Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
Point[,] Lo = new Point[101, 101];
for (int i = 1; i <= 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.W && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.Y != 0)
{
for (int i = 2; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
}
}
if (lb.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y - 10))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
lab1.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y - 10);
lb.Location = le.Location;
this.Controls.Remove(le);
蛇.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0];
if (lo.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y - 10))
{
timer1.Enabled = false;
MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
Application.Exit();
};
}
if (e.KeyCode == Keys.W && lab1.Location.Y == 0)
{
timer1.Enabled = false;
MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
Application.Exit();
};
lab1.Top = lab1.Top - 10;
timer1.Enabled = true;
}
if (e.KeyCode == Keys.A && 蛇.D != 4)
{
timer1.Enabled = false;
蛇.D = 3;
Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
Point[,] Lo = new Point[101, 101];
for (int i = 1; i <= 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0];
Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.A && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.X != 0)
{
for (int i = 2; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
}
}
if (lb.Location == new System.Drawing.Point(lab1.Location.X - 10, lab1.Location.Y))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
lab1.Location = new System.Drawing.Point(lab1.Location.X - 10, lab1.Location.Y);
lb.Location = le.Location;
this.Controls.Remove(le);
蛇.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0];
if (lo.Location == new System.Drawing.Point(lab1.Location.X - 10, lab1.Location.Y))
{
timer1.Enabled = false;
MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
Application.Exit();
};
}
if (e.KeyCode == Keys.A && lab1.Location.X == 0)
{
timer1.Enabled = false; MessageBox.Show("游戏结束,本次得分为:"+Convert.ToString(蛇.s-1), "提示");
Application.Exit();
};
lab1.Left = lab1.Left - 10;
timer1.Enabled = true;
}
if (e.KeyCode == Keys.D && 蛇.D != 3)
{
timer1.Enabled = false;
蛇.D = 4;
Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
Point[,] Lo = new Point[101, 101];
for (int i = 1; i <= 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
}
Label le = new Label();
le.Visible = false;
if (e.KeyCode == Keys.D && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.X != 290)
{
for (int i = 2; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
}
}
if (lb.Location == new System.Drawing.Point(lab1.Location.X + 10, lab1.Location.Y))
{
lb.BackColor = System.Drawing.Color.Black;
le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
lab1.Location = new System.Drawing.Point(lab1.Location.X + 10, lab1.Location.Y);
lb.Location = le.Location;
this.Controls.Remove(le);
蛇.s++;
Add_food();
timer1.Enabled = true;
return;
}
for (int i = 1; i < 蛇.s; i++)
{
Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(lab1.Location.X + 10, lab1.Location.Y)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;
}
if (e.KeyCode == Keys.D && lab1.Location.X == 290)
{
timer1.Enabled = false;
MessageBox.Show("游戏结束,本次得分为:"+Convert.ToString(蛇.s-1), "提示");
Application.Exit();
};
lab1.Left = lab1.Left + 10;
timer1.Enabled = true;
}
}
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
Add_food();
}
private void 简单ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Interval = 150;
}
private void 困难ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Interval = 20;
}
private void 一般ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Interval = 80;
}
private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
MessageBox.Show("继续?", "");
}
private void 结束ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}