C#窗口形式的蛇

C#窗口形式的蛇,c#,visual-studio,C#,Visual Studio,我有一个简单的蛇游戏,我的问题是当它达到三条尾巴时,尾巴不会加起来 namespace Snake { public partial class Form1 : Form { bool left = false, right = false; bool top = false, down = false; PictureBox pic = new PictureBox(); List<PictureBox>

我有一个简单的蛇游戏,我的问题是当它达到三条尾巴时,尾巴不会加起来

namespace Snake
{
    public partial class Form1 : Form
    {
        bool left = false, right = false;
        bool top = false, down = false;
        PictureBox pic = new PictureBox();
        List<PictureBox> tails = new List<PictureBox>();
        int score = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (((e.KeyChar.ToString() == "a") || (e.KeyChar.ToString() == "A"))&&(right == false))
            {
                right = false;
                top = false;
                down = false;
                left = true;

            }
            else if (((e.KeyChar.ToString() == "d") || (e.KeyChar.ToString() == "D"))&& (left == false))
            {
                top = false;
                down = false;
                left = false;
                right = true;
            }
            else if (((e.KeyChar.ToString() == "w") || (e.KeyChar.ToString() == "W"))&& (down == false))
            {
                down = false;
                left = false;
                right = false;
                top = true;
            }
            else if (((e.KeyChar.ToString() == "s") || (e.KeyChar.ToString() == "S"))&& (top == false))
            {
                top = false;
                left = false;
                right = false;
                down = true;
            }

        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            //ticks every 1 sec
            if (pic.Location == head.Location)
            {
                score++;
                spawnFood();
                tails.Add(addTails());
            }
            sortLocation();
            if (right == true)
            {
                int r = head.Location.X + head.Height;
                head.Location = new Point(r, head.Location.Y);
            }
            else if(left == true)
            {
                int l = head.Location.X - head.Height;
                head.Location = new Point(l, head.Location.Y);
            }
            else if (top == true)
            {
                int t = head.Location.Y - head.Height;
                head.Location = new Point(head.Location.X, t);
            }
            else if (down == true)
            {
                int d = head.Location.Y + head.Height;
                head.Location = new Point(head.Location.X,d);
            }
            txtScore.Text = score.ToString();
        }

        private void sortLocation()
        {
            if (tails.Count == 0)
            {

            }
            else
            {
                for (int i = 1; i < tails.Count; i++)
                {
                    tails[i].Location = tails[i-1].Location;
                }
                tails[0].Location = head.Location;
            }
        }

        private PictureBox addTails()
        {
            PictureBox tail = new PictureBox();
            tail.Name = "tail" + score.ToString();
            tail.BackColor =  Color.Black;
            tail.Width = 10;
            tail.Height = 10;
            this.Controls.Add(tail);
            return tail;
        }

        private void spawnFood()
        {
            Random rnd = new Random();
            int rndLocationX = rnd.Next(10, 50);
            int rndLocationY = rnd.Next(10, 50);
            pic.BackColor = Color.Red;
            pic.Height = 10;
            pic.Width = 10;
            this.Controls.Add(pic);
            if (rndLocationX >= 500)
            {
                rndLocationX -= 10;
            }
            if (rndLocationY >= 500)
            {
                rndLocationY -= 10;
            }
            pic.Location = new Point(rndLocationX*10,rndLocationY*10);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            spawnFood();
        }
    }
}
名称空间蛇
{
公共部分类Form1:Form
{
布尔左=假,右=假;
bool top=false,down=false;
PictureBox pic=新PictureBox();
列表尾部=新列表();
智力得分=0;
公共表格1()
{
初始化组件();
}
私有void Form1\u按键(对象发送方,按键事件参数e)
{
if((e.KeyChar.ToString()=“a”)||(e.KeyChar.ToString()=“a”)&&(right==false))
{
右=假;
顶部=假;
向下=假;
左=真;
}
else如果((e.KeyChar.ToString()=“d”)| |(e.KeyChar.ToString()=“d”)&&(left==false))
{
顶部=假;
向下=假;
左=假;
右=真;
}
else如果((e.KeyChar.ToString()=“w”)| |(e.KeyChar.ToString()=“w”)&&(down==false))
{
向下=假;
左=假;
右=假;
top=正确;
}
else如果((e.KeyChar.ToString()=“s”)||(e.KeyChar.ToString()=“s”)&&&(top==false))
{
顶部=假;
左=假;
右=假;
向下=真;
}
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
//每1秒滴答一次
if(pic.Location==头部位置)
{
分数++;
产卵食物();
tails.Add(addTails());
}
排序位置();
if(right==true)
{
INTR=头部位置X+头部高度;
头部位置=新点(r,头部位置Y);
}
else if(左==真)
{
int l=头部位置X-头部高度;
头部位置=新点(l,头部位置Y);
}
else if(top==true)
{
int t=头部位置Y-头部高度;
头部位置=新点(头部位置X,t);
}
否则如果(向下==真)
{
INTD=头部位置Y+头部高度;
头部位置=新点(头部位置X,d);
}
txtScore.Text=score.ToString();
}
私有void排序位置()
{
如果(tails.Count==0)
{
}
其他的
{
对于(int i=1;i=500)
{
rndLocationX-=10;
}
如果(rndLocationY>=500)
{
rndLocationY-=10;
}
图位置=新点(rndLocationX*10,rndLocationY*10);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
timer1.Start();
产卵食物();
}
}
}
for(int i=1;i

可以这么说,你的尾巴是叠在那里的吗?这是我可能在想的另一个想法。我将尾部[I-1]更改为尾部[I+1]。看看这是否奏效

你试过使用调试器吗?你的addtails方法需要在某种循环上。看起来它只运行了一次,然后代码就在下一次迭代中简单地传递了它。也许可以添加一些注释,以便我们更容易理解您的代码在做什么。谢谢,先生,我会尝试,我认为问题出在sortLocation();函数is做的是跟随snakeI的头部。我有System.ArgumentOutOfRangeException,带尾部[i+1]哦,你试过尾部[i]
        for (int i = 1; i < tails.Count; i++)
        {
            tails[i].Location = tails[i+1].Location; 
        }
        tails[0].Location = head.Location;