【问题标题】:c# snake game list of buttonc#蛇游戏按钮列表
【发布时间】:2015-07-04 14:50:00
【问题描述】:

我对下面的代码感到困惑:

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

【问题讨论】:

    标签: c# debugging overwrite


    【解决方案1】:

    在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);
        }
    

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

    【讨论】:

    • 很抱歉,但无论 4 按钮在 1 个位置重叠,我都已经尝试过了。所以我不知道是什么原因导致 writeline 控制台只是为了确定变量的内容。事实上,当我从列表中调用方法 add() 时。正在改变之前的数据。这是这个程序问题的核心
    猜你喜欢
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多