Я путать с ниже код:C# игра змея список кнопки
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
sory, но я уже пробовал, независимо от того, что кнопка 4 перекрывается в 1 месте. поэтому я не знаю, для чего предназначена консольная консоль, чтобы убедиться в том, что такое содержимое переменной. тот факт, что когда я вызываю метод add() из списка. изменяет предыдущие данные. Это ядро этой проблемы программы – ricardo