namespace Snake
{
public partial class Form1 : Form
{
Label[] Snake_Body = new Label[500];//巨蟒身体数组,由label实例(标签)构成
Label food = new Label(); //食物
string Keyname = "stop"; //按键状态
int x, y,b=0;
int Snake_Long = 0, Snake_Tail_x, Snake_Tail_y;
Random R = new Random();//随机数
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Makefood();//创建食物
for (int i = 0; i < 5; i++)
{
Label Snaker = new Label();//创造五个初始巨蟒身体实例
Snaker.Height = 10;
Snaker.Width = 10;
Snaker.Top = 300;
Snaker.Left = 150 - i * 10;
Snaker.BackColor = Color.DeepPink;
Snaker.Text = " ";
Snaker.Tag = i;
Snake_Body[i] = Snaker;
this.Controls.Add(Snaker);
}
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void START_Click(object sender, EventArgs e)
{
Keyname = "start";//开始游戏
}
private void STOP_Click(object sender, EventArgs e)
{
Keyname = "stop";//暂停游戏
}
private void button1_Click(object sender, EventArgs e)//上
{
Keyname = "up";//上
}
private void LEFT_Click(object sender, EventArgs e)
{
Keyname = "left";//左
}
private void RIGHT_Click(object sender, EventArgs e)
{
Keyname = "right";//右
}
private void DOWN_Click(object sender, EventArgs e)
{
Keyname = "down";//下
}
private void timer1_Tick(object sender, EventArgs e)
{
b++;//目前不知道为啥定时器每到时间会进入两次,此处b使每到时间执执行一次
if (b == 10)
b = 0;
if (b % 2 == 1)
{
;
}
else
{
x = Snake_Body[0].Left;
y = Snake_Body[0].Top;
if (Keyname == "start")
{
Snake_Body[0].Left += 10;
}
if (Keyname == "up")
{
Snake_Body[0].Top -= 10;
}
if (Keyname == "down")
{
Snake_Body[0].Top += 10;
}
if (Keyname == "left")
{
Snake_Body[0].Left -= 10;
}
if (Keyname == "right")
{
Snake_Body[0].Left += 10;
}
if (Snake_Body[0].Left >= 450)
{
Snake_Body[0].Left = 0;
}
if (Snake_Body[0].Left < 0)
{
Snake_Body[0].Left = 450;
}
if (Snake_Body[0].Top >= 480)
{
Snake_Body[0].Top = 0;
}
if (Snake_Body[0].Top < 0)
{
Snake_Body[0].Top = 480;
}
if (Keyname != "stop")
{
Snake_Move();
}
if (Snake_Body[0].Left == food.Left && Snake_Body[0].Top == food.Top)//巨蟒吃到食物
{
food.Top = R.Next(0, 47) * 10;
food.Left = R.Next(0, 44) * 10;
Label Snaker = new Label();
Snaker.Height = 10;
Snaker.Width = 10;
Snaker.Top = Snake_Tail_y;
Snaker.Left = Snake_Tail_x;
Snaker.BackColor = Color.DeepPink;
Snaker.Text = " ";
Snaker.Tag = Snake_Long + 1;
Snake_Body[Snake_Long + 1] = Snaker;
this.Controls.Add(Snaker);
}
}
}
public void Snake_Move()//巨蟒移动
{
int i;
Snake_Long = 0;
for (i = 1; Snake_Body[i]!= null; i++)
{
Snake_Long++;
}
textBox2.Text = Convert.ToString(Snake_Long-4);//分数显示
timer1.Interval = (int)(200 - Snake_Long * 10)+1;//随巨蟒长度增加,移动速度加快
Snake_Tail_x = Snake_Body[Snake_Long].Left;
Snake_Tail_y = Snake_Body[Snake_Long].Top;
for (i = Snake_Long; i > 1; i--)
{
Snake_Body[i].Left = Snake_Body[i - 1].Left;
Snake_Body[i].Top = Snake_Body[i - 1].Top;
}
Snake_Body[1].Left = x;
Snake_Body[1].Top = y;
}
public void Makefood()//创造食物实例
{
food.Width = 10;
food.Height = 10;
food.Top = R.Next(0,47) * 10;//食物y位置0-470
food.Left = R.Next(0,44) * 10;//食物x位置0-470
food.Text = " ";
food.BackColor = Color.DarkSlateBlue;
this.Controls.Add(food);
}
}
}