debugging - c# snake game list of button -
i confused below code:
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 in simpan) { foreach (movedata 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); (int = 0; < 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 in temps.snob) { console.writeline(a.s.location.x.tostring() + " " + a.s.location.y.tostring()); console.writeline(a.s.text); } } simpan.add(temps); refreshing(); } }
i've debugged program , stuck procedure createsnake();
procedure print button.
but when printout in list of button :
150 150 //1'st iteration 150 150 // position of 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 , name list of button 3 150 60 3 150 60 3 150 60 3 //end
in end why overwrite data before it? suspect add method list can't solution for. code before incomplete there unused variable ignore it.
expected result:
150 150 0 150 120 1 150 90 2 150 60 3
inside createsnake()
function have code
foreach (movedata in temps.snob) { console.writeline(a.s.location.x.tostring() + " " + a.s.location.y.tostring()); console.writeline(a.s.text); }
which writes position once , calling refreshing()
function, following
foreach (movedata 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); }
you printing co-ordinates twice in 2 different locations , that's why duplicating.
Comments
Post a Comment