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 snake
{
public partial class Form1 : Form
{
//1:上 2:下 3.左 4.右
private int direction = 1; //新建picturebox数组
private PictureBox[] pb = new PictureBox[50];
//蛇的结点数
private int i=0;
//蛇的身体结点
private string picPath = Application.StartupPath + "\\snake.jpg";
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//timer1控制蛇的运动,timer2控制食物出现间隔
timer1.Enabled = true;
timer2.Enabled = true;
pictureBox2.Visible = false;
//初始将蛇头放入picturebox数组中
pb[0] = pictureBox1;
} private void timer1_Tick(object sender, EventArgs e)
{
//根据不同方向控制蛇运动
if (this.direction == 1)
{
pb[0].Top = pb[0].Top - 5;
}
if (this.direction == 2)
{
pb[0].Top = pb[0].Top + 5;
}
if (this.direction == 3)
{
pb[0].Left = pb[0].Left - 5;
}
if (this.direction == 4)
{
pb[0].Left = pb[0].Left + 5;
}
//循环设置蛇的身体位置
for (int j = 1; j <= i; j++)
{
if (this.direction == 1)
{
pb[j].Top = pb[j - 1].Top + 17;
pb[j].Left = pb[j - 1].Left; }
if (this.direction == 2)
{
pb[j].Top = pb[j - 1].Top - 17;
pb[j].Left = pb[j - 1].Left;
}
if (this.direction == 3)
{
pb[j].Top = pb[j - 1].Top;
pb[j].Left = pb[j - 1].Left + 17;
}
if (this.direction == 4)
{
pb[j].Top = pb[j - 1].Top;
pb[j].Left = pb[j - 1].Left - 17;
}
}
//如果蛇头与食物距离坐标在范围5之内,算吃到食物
if (Math.Abs(pictureBox1.Top - pictureBox2.Top) <= 5 && Math.Abs(pictureBox1.Left - pictureBox2.Left) <= 5)
{
//蛇的结点数+1
this.i++;
//随机生成食物
rand_loc();
pictureBox2.Visible = false;
timer2.Enabled = true;
//新生成一结点
pb[i] = new PictureBox();
pb[i].Image = Image.FromFile(this.picPath);
pb[i].Size = new Size(16, 16);
//增加结点到窗体
this.Controls.Add(pb[i]);
}
//是否撞墙
if (pb[0].Top < 0 || pb[0].Top > this.Height-50 || pb[0].Left < 0 || pb[0].Left > this.Width-25)
{
timer1.Enabled = false;
timer2.Enabled = false;
if (MessageBox.Show("你输了!是否重玩?", "game over!", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Hide();
Form1 f1 = new Form1();
f1.Show();
}
else
{
Application.Exit();
}
}
} private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
this.direction = 1;
}
if (e.KeyCode == Keys.Down)
{
this.direction = 2;
}
if (e.KeyCode == Keys.Left)
{
this.direction = 3;
}
if (e.KeyCode == Keys.Right)
{
this.direction = 4;
}
}
//随机生成食物位置
public void rand_loc()
{
Random r = new Random();
int x = r.Next(30, this.Width - 30);
int y = r.Next(30, this.Height - 50);
pictureBox2.Location = new Point(x, y);
}
private void timer2_Tick(object sender, EventArgs e)
{
rand_loc();
pictureBox2.Visible = true;
timer2.Enabled = false;
}
}
}
现在效果是蛇只能一直线,如何让蛇扭动起来,如果在timer1.tick()中设置循环pb[j].location=pb[j-1].location,这样,蛇的身体都会与蛇头重合,应该怎么改呢...
for (int j = 1; j < pb.length; j++)
pb[j] = pb[j - 1];
pb[0]=
if (this.direction == 1)
{
pb[0].Top = pb[0].Top - 5;
}
else if (this.direction == 2)
{
pb[0].Top = pb[0].Top + 5;
}
else if (this.direction == 3)
{
pb[0].Left = pb[0].Left - 5;
}
else if (this.direction == 4)
{
pb[0].Left = pb[0].Left + 5;
}
[/code]
for (int j = 1; j < pb.length; j++)
{
pb[j].Top = pb[j - 1].Top;
pb[j].Left = pb[j - 1].Left;
}
if (this.direction == 1)
{
pb[0].Top = pb[0].Top - 5;
}
else if (this.direction == 2)
{
pb[0].Top = pb[0].Top + 5;
}
else if (this.direction == 3)
{
pb[0].Left = pb[0].Left - 5;
}
else if (this.direction == 4)
{
pb[0].Left = pb[0].Left + 5;
}
{
pb[j].Top = pb[j - 1].Top;
pb[j].Left = pb[j - 1].Left;
}
if (this.direction == 1)
{
pb[0].Top = pb[0].Top - 5;
}
else if (this.direction == 2)
{
pb[0].Top = pb[0].Top + 5;
}
else if (this.direction == 3)
{
pb[0].Left = pb[0].Left - 5;
}
else if (this.direction == 4)
{
pb[0].Left = pb[0].Left + 5;
}
......
然后再处理pb[0]
一定要从最后一个开始向前修改起,而不能倒过来。