如何将入侵者从左移动到右

发布于 2024-12-18 04:28:57 字数 3004 浏览 2 评论 0原文

我正在开发一款太空入侵者游戏,并且我对这个游戏编程很陌生。

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

我有 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;
                        }
                    }
                }
            }
        }
    }

i am developing a space invader game, and i am new to this game programming thing.

i need the invaders to move from left to right.

i have 4 rows of pictureboxes,10 invaders per row.

the problem i am having is that only 1 of the rows are moving.

please help!

Thank you

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;

    }

//moving invaders

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨巷深深 2024-12-25 04:28:57
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,但仅存储在入侵者列大小的数组中,因此外部 for 循环的每次迭代都会覆盖上一次迭代的结果。因此,您将没有以前的引用,只有新的引用,这就是为什么 move 方法只移动那些您有引用的引用。
您应该使用二维数组 (objsp.invader[r,c] (根据 kendfrey 的评论更正)) 或类似的东西。

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

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

Here you are creating invaderrow times invadercol count invaders, I assume 4 and 10 respectively, but storing only in an invadercol sized array, so each iteration of the outer for loop will override the result of the previous iteration. So you won't have the references for the previous ones, only the new ones, that's why the move method only moves those for which you have the reference.
You should use maybe a 2-dimension array (objsp.invader[r,c] (corrected according to kendfrey's comment)) or something similar.

Then in your move method make sure, to move all of the PictureBox instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文