我正在开发一款太空入侵者游戏,而我对这种游戏编程还是陌生的。

我需要入侵者从左到右移动。

我有4行图片框,每行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;
                        }
                    }
                }
            }
        }
    }

最佳答案

for(int r = 0; r <= invaderrow-1; r++)
    {
        for( int c = 0; c<=invadercol-1; c++)
        {
            objsp.invader[c] = new PictureBox();
            objsp.invader[c].Image= pictureBox2.Image;
            objsp.invader[c].Image = Resized(pictureBox2.Image, 2);
            objsp.invader[c].BackColor = Color.Transparent;
            objsp.invader[c].Location= new Point((c * 100) + 10, 10 + r * 50);
            this.Controls.Add(objsp.invader[c]);
        }
    }

在这里,您要创建入侵者次数,分别假设入侵者数和入侵者数分别为4和10,但仅存储在大小为invadercol的数组中,因此外部for循环的每次迭代都将覆盖前一次迭代的结果。因此,您将没有以前的引用,只有新的引用,这就是为什么move方法仅移动那些您具有引用的引用的原因。
您应该使用2-dimension array(objsp.invader[r,c](根据kendfrey的评论更正))或类似的东西。

然后,在您的move方法中确保移动所有PictureBox实例。

关于c# - 如何从左向右移动入侵者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8279165/

10-11 06:16