【问题标题】:Monogame Breakout: having trouble with adding abilities like longer paddle or more ballsMonogame Breakout:在添加更长的桨或更多球等能力时遇到问题
【发布时间】:2017-05-02 11:27:29
【问题描述】:

我要为随机方块添加一些能力,你知道当你碰到一个随机方块时它会下降。就像你得到 2 个球或更大的球拍或更慢的球速。

我在编写 for 循环和 if 语句方面需要帮助,这可以为我的块级别添加一些功能。就像 1 级的 1 个随机能力等等。因此,当您击中 1 个 20 块的随机块时,这就是我的能力。它会像原来的突围游戏一样落到桨上。

我正在考虑一个开关,如果一个具有能力的随机方块被击中并使用该开关并将其随机化。

    void PowerUp()
    {
        powerups.Add(abilityballs_rect);
        powerups.Add(abilitylong_rect);
        powerups.Add(abilityslow_rect);
    }

-

    List<Rectangle> block = new List<Rectangle>();
    List<Rectangle> block2 = new List<Rectangle>();
    List<Rectangle> block3 = new List<Rectangle>();
    List<Rectangle> block4 = new List<Rectangle>();
    List<Rectangle> block5 = new List<Rectangle>();
    List<Rectangle> block6 = new List<Rectangle>();

    List<Rectangle> powerups = new List<Rectangle>();

-

            if (level == 1)
            {
                if (block.Count == 14 && block2.Count == 14)
                {
                    spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(252, 400), Color.White);
                }

                foreach (Rectangle g in block)
                {
                    spriteBatch.Draw(block_texture, g, Color.LimeGreen);
                }
                foreach (Rectangle r in block2)
                {
                    spriteBatch.Draw(block_texture, r, Color.IndianRed);
                }
            }

            else if (level == 2)
            {
                if (block3.Count == 18 && block4.Count == 27)
                {
                    spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
                }

                foreach (Rectangle b in block3)
                {
                    spriteBatch.Draw(block_texture, b, Color.CornflowerBlue);
                }
                foreach (Rectangle y in block4)
                {
                    spriteBatch.Draw(block_texture, y, Color.Yellow);
                }
            }

            else if (level == 3)
            {
                if (block5.Count == 36 && block6.Count == 18)
                {
                    spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
                }

                foreach (Rectangle o in block5)
                {
                    spriteBatch.Draw(block_texture, o, Color.Orange);
                }
                foreach (Rectangle p in block6)
                {
                    spriteBatch.Draw(block_texture, p, Color.HotPink);
                }
            }

-

    void AddBlocks()
    {
        //LEVEL 1
        for (int i = 1; i < 3; i++)
        {
            for (int f = 1; f < 8; f++)
            {
                block.Add(new Rectangle((f * 63) + 94, (i * 40) + 60, block_texture.Width, block_texture.Height));
            }
        }
        for (int i = 1; i < 3; i++)
        {
            for (int g = 1; g < 8; g++)
            {
                block2.Add(new Rectangle((g * 63) + 94, (i * 40) + 40, block_texture.Width, block_texture.Height));
            }
        }

        //LEVEL 2
        for (int i = 1; i < 3; i++)
        {
            for (int j = 1; j < 10; j++)
            {
                block3.Add(new Rectangle((j * 63) + 34, (i * 200) - 60, block_texture.Width, block_texture.Height));
            }
        }
        for (int i = 1; i < 10; i++)
        {
            for (int k = 1; k < 4; k++)
            {
                block4.Add(new Rectangle((k * 103) + 143, (i * 20) + 140, block_texture.Width, block_texture.Height));
            }
        }

        //LEVEL 3
        for (int i = 1; i < 7; i++)
        {
            for (int j = 1; j < 7; j++)
            {
                block5.Add(new Rectangle((j * 63) + 127, (i * 20) + 190, block_texture.Width, block_texture.Height));
            }
        }
        for (int i = 1; i < 10; i++)
        {
            for (int k = 1; k < 3; k++)
            {
                block6.Add(new Rectangle((k * 443) - 317, (i * 20) + 160, block_texture.Width, block_texture.Height));
            }
        }
    }

-

    void DeleteBlocks()
    {
        if (level == 1)
        {
            for (int j = 0; j < block.Count; j++)
            {
                if (ball_rect.Intersects(block[j]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block.RemoveAt(j);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }

            for (int k = 0; k < block2.Count; k++)
            {
                if (ball_rect.Intersects(block2[k]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block2.RemoveAt(k);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }
            if (block.Count == 0 && block2.Count == 0)
            {
                level++;
                StartValueBallPaddle();
                Start = false;
            }
        }

        else if (level == 2)
        {
            for (int l = 0; l < block3.Count; l++)
            {
                if (ball_rect.Intersects(block3[l]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block3.RemoveAt(l);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }
            for (int m = 0; m < block4.Count; m++)
            {
                if (ball_rect.Intersects(block4[m]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block4.RemoveAt(m);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }
            if (block3.Count == 0 && block4.Count == 0)
            {
                level++;
                StartValueBallPaddle();
                Start = false;
            }
        }

        else if (level == 3)
        {
            for (int n = 0; n < block5.Count; n++)
            {
                if (ball_rect.Intersects(block5[n]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block5.RemoveAt(n);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }
            for (int o = 0; o < block6.Count; o++)
            {
                if (ball_rect.Intersects(block6[o]))
                {
                    ball_speed.Y *= -1;
                    points += 1;
                    block6.RemoveAt(o);
                    if (points > highscore)
                    {
                        highscore = points;
                    }
                }
            }
        }
    }

【问题讨论】:

  • 不清楚您想通过帖子询问什么。你在让你的代码做某事时遇到问题吗?你想让别人检查你的代码吗?您需要将帖子的标题改写为更清晰的问题。
  • 是的,你是对的,我认为现在更清楚了。对不起我的英语:P

标签: if-statement random switch-statement monogame breakout


【解决方案1】:

您可能希望将块数据封装到一个类中,其中包含块的颜色、位置和电源。
您可以在里面有一个称为“AddPowerup()”或其他方法的方法,该方法将用于向块添加随机(或特定)的加电。您可能需要一个吸气剂来进行通电。

现在在您的游戏中,您可以将关卡的所有块保存在列表、二维数组或任何适合您的集合中 然后,要对关卡中的随机块应用加电,您可以执行以下操作(主要是伪代码,因此未经测试):

List<Block> blockList = CreateBlockLevel(1);
int randomIndex = new Random().Next(blockList.Length-1);
blockList[randomIndex].AddPowerup();

如果你想对多个方块应用一个加电,你可以把它放在一个for循环中,但是你可能想检查这个方块是否已经加电了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2020-11-10
    • 2017-08-25
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多