2
我正在开发一个太空入侵者游戏,并且我是这个游戏编程的新手。如何从左向右移动入侵者
我需要侵略者从左向右移动。
我有4行pictureboxes,每行10个入侵者。
我遇到的问题是只有1行正在移动。
请帮忙!
谢谢
private void Form1_Load(object sender, EventArgs e)
{
Cursor.Dispose();
objsp.gsImage = Image.FromFile(@"C:\Users\kuven\Desktop\SpaceInvader\SpaceInvader\Space Shooter.png");
objsp.gsImage = Resized(objsp.gsImage, 2);
objsp.gsPos = new Point(660, 650);
Cursor.Hide();
Cursor.Position = new Point(660 + objsp.gsImage.Width/2, 650 + objsp.gsImage.Height/2);
Cursor.Clip = new Rectangle(this.Location, this.Size);
objsp.invader = new PictureBox[invaderrow,invadercol];
for(int r = 0; r <= invaderrow-1; r++)
{
for(int c = 0; c<=invadercol-1; c++)
{
objsp.invader[r,c] = new PictureBox();
objsp.invader[r,c].Image= pictureBox2.Image;
objsp.invader[r,c].Image = Resized(pictureBox2.Image, 2);
objsp.invader[r,c].BackColor = Color.Transparent;
objsp.invader[r,c].Location= new Point((c * 100) + 10, 10 + r * 50);
this.Controls.Add(objsp.invader[r,c]);
}
}
invadermove.Enabled = true;
}
//移动侵略者
private void invadermove_Tick(object sender, EventArgs e)
{
for (int r = 0; r <= invaderrow-1 ; r++)
{
for (int c = 0; c <= invadercol - 1; c++)
{
if (level == 0)
dir = "r";
if (objsp.invader[r,9].Left >= ClientSize.Width)
dir = "l";
if (dir == "r")
{
if (c < 9)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left + 10, level * 5 + 25);
}
if (c > 8)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left + 10, level * 5 + 61);
if (objsp.invader[r,9].Left >= ClientSize.Width)
{
level += 1;
}
}
}
if (dir == "l")
{
if (c < 9)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left - 10, level * 5 + 25);
}
if (c > 8)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left - 10, level * 5 + 61);
if (objsp.invader[r,0].Left <=0)
{
dir = "r";
level += 1;
}
}
}
}
}
}
一个二维数组是这样的:'objsp.invader [r,c]' –
@kendfrey感谢您指出这一点,实际上我之前从未在C#中使用过多维数组,所以对语法不太确定。我会在我的回答中纠正。 – akoso
我已经按照您的建议编辑了代码,但它的确做了同样的事情。只有现在它删除了所有其他侵入者行,并且只移动最后一行。 –