c#蛇游戏按钮列表

时间:2015-04-26 04:54:31

标签: c# debugging overwrite

我对以下代码感到困惑:

public partial class Form1 : Form
{
    struct movedata
    {
        public Button s;
        public Point pos;
    };
    struct snake
    {
        public Point headpos;
        public List<movedata> snob;
        public Point posfood;
    };
    int limx;
    int limy;
    List<Point> food;
    List<snake> simpan;
    snake temps;
    movedata temd;
    Random rnd = new Random();
    public Form1()
    {
        InitializeComponent();
        limx = groupBox1.Width;
        limy = groupBox1.Height;
        simpan = new List<snake>();
        temps = new snake();
        temps.posfood = new Point();
        temps.headpos = new Point();
        temps.snob = new List<movedata>();
        temd = new movedata();
        temd.s = new Button();
        temd.s.Width = 30;
        temd.s.Height = 30;
        //  temd.s.Visible = false;
        temd.pos = new Point();
        createsnake(150, 150, 4);

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void refreshing()
    {
        foreach (snake a in simpan)
        {
            foreach (movedata i in a.snob)
            {
                this.groupBox1.Controls.Add(i.s);
                Console.WriteLine(i.s.Location.X.ToString() + " " + i.s.Location.Y.ToString());
                Console.WriteLine(i.s.Text);
            }
        }
    }
    private void createsnake(int x,int y, int length)
    {
        temps.headpos = new Point(x, y);
        for (int i = 0; i < length; i++)
        {
            temd.s.Location = new Point(x, y-i*30);
            Console.WriteLine(temd.s.Location.X.ToString() + " " +temd.s.Location.Y.ToString());
            temd.pos = new Point(0, 1);
            temd.s.Text = i.ToString();
            if (i == 0)
            {
                temps.headpos = new Point(x, y);
                temd.s.BackColor = Color.Black;
            }
            temps.snob.Add(temd);
            foreach (movedata a in temps.snob)
            {
                Console.WriteLine(a.s.Location.X.ToString() + " " + a.s.Location.Y.ToString());
                Console.WriteLine(a.s.Text);
            }
        }

        simpan.Add(temps);
        refreshing();
    }
}

我已经调试了我的程序并坚持使用过程createsnake(); 这个程序只打印一些按钮。

但是当我打印出按钮列表中的内容时:

    150 150 //1'st iteration
    150 150 // the position of the button
    0       // name of button
    150 120 //2 iteration
    150 120// begin content of list button
    1
    150 120
    1       // end
    150 90//3 iteration
    150 90 //begin position content list of button
    2      
    150 90
    2
    150 90
    2     // end
    150 60 // 4 iteration
    150 60 //begin content of position and name list of button
    3
    150 60
    3
    150 60
    3
    150 60
    3  //end

到底为什么它会覆盖之前的数据呢? 我怀疑它是从列表中添加方法但无法获得解决方案。 我之前的代码是不完整的,有些未使用的变量只是忽略它。

预期结果

    150 150
    0
    150 120
    1
    150 90
    2
    150 60
    3

1 个答案:

答案 0 :(得分:0)

createsnake()函数中,您有此代码

   foreach (movedata a in temps.snob)
    {
        Console.WriteLine(a.s.Location.X.ToString() + " " + a.s.Location.Y.ToString());
        Console.WriteLine(a.s.Text);
    }

写入位置一次然后调用refreshing()函数,它也执行以下操作

    foreach (movedata i in a.snob)
    {
        this.groupBox1.Controls.Add(i.s);
        Console.WriteLine(i.s.Location.X.ToString() + " " + i.s.Location.Y.ToString());
        Console.WriteLine(i.s.Text);
    }

您在两个不同的位置打印坐标两次,这就是他们复制的原因。