代码效率,分阶段

本文关键字:分阶段 效率 代码 | 更新日期: 2023-09-27 18:25:50

好的,所以我正在重新审视我制作的一款从未完成的原型游戏,当原型制作时,你并不总能想出最好的主意哈哈。这对我来说很难解释,但基本上屏幕上有一些游戏对象可以是"Eaten",然后我用新的点值和纹理重新生成它。

当游戏对象是"Eaten"时,当纹理与屏幕上的嘴相交时,调用下一阶段方法

隐藏扰流板

代码:

void NextPhase(int food)
    {
        switch (foodNumber)
        {
                //Detects what food object has been eaten
            case 1:
                {
                    food1Phase++;
                    break;
                }
            case 2:
                {
                    food2Phase++
                    break;
                }
            case 3:
                {
                    food3Phase++;
                    break;
                }
                //
        }
        switch (foodNumber)
        {
            case 1:
                {
                    switch (food1Phase)
                    {
                    //Each phase is a new set of Textures, Points Values and specific spawn delay.
                    //Multiplier sotrage tells another method
                    //what food has been eaten and decides whether to increase the score mulitplier
                    //the position properties reset it to a location off screen and then it flies on screen
                    //for the player to collect
                    //delayCompleted false activates another method that starts timing the spawn
                    //delay
                case 1:
                    {

                        multiplierStorage("Gay Bacon");
                        food1Position.X = -200;
                        food1Position.Y = 60;
                        food1 = contentManager.Load<Texture2D>("Food2Img");
                                food1Height = food1.Bounds.Height + 20;
                                food1Width = food1.Bounds.Width + 30;
                                food1Speed.X = 0;
                                food1Speed.Y = 0;
                        delayCompleted = false;
                        if (gameOverallTimer < 30.0)
                        {
                            timeWanted = gameOverallTimer + 4.0;
                        }
                        break;
                    }
                case 2:
                    {
                        food1 = contentManager.Load<Texture2D>("Food6");
                        food1Height = food1.Bounds.Height + 20;
                        food1Width = food1.Bounds.Width + 30;
                        food1Position.X = -100;
                        food1Position.Y = 60;
                        food1Speed.X = 0;
                        food1Speed.Y = 0;
                        multiplierStorage("Bacon");


                        break;
                    }
                case 3:
                    {
                        food1 = contentManager.Load<Texture2D>("Food1Img");
                        food1Height = food1.Bounds.Height + 20;
                        food1Width = food1.Bounds.Width + 30;
                        food1Position.X = -100;
                        food1Position.Y = 60;
                        food1Speed.X = 0;
                        food1Speed.Y = 0;
                        vegetableEatenMultiplier();
                        lives = lives - 1;
                        UpdateLives();
                        food1Phase = 0;
                        break;
                    }
                }
            }
        }
            case 2:
                {
                    switch (food2Phase)
                    {
                        food2 = contentManager.Load<Texture2D>("Food4");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;
                        multiplierStorage("Bacon");
                        break;
                    }
                case 2:
                    {

                        food2 = contentManager.Load<Texture2D>("Food5");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;
                        vegetableEatenMultiplier();
                        lives = lives - 1;
                        UpdateLives();
                        break;
                    }
                case 3:
                    {
                        food2 = contentManager.Load<Texture2D>("Food2Img");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;
                        food2Phase = 0;
                        multiplierStorage("Steak");
                        break;
                    }
                }
            }

            case 3:
                {
                    switch (food3Phase)
                    {
                        food3Position.X = 600;
                        food3Position.Y = -100;
                        food3Speed.X = 0;
                        food3Speed.Y = 0;
                        food3Phase = 0;
                        multiplierStorage("Gay Bacon");
                        break;
                    }
                }
            }
        }
    }      

问题是,这对我来说似乎不是很有效,现在是的,我已经想过创建一个具有纹理和位置等属性的新类,然后我只创建该类的新对象,并以这种方式使其更好,但我仍然必须在该类中使用这个阶段代码。我只想要一个代码的替代方案。

代码效率,分阶段

您可能可以组合其中一些开关。

switch (foodNumber)
{
    case 1:
        switch (food1Phase)
        {
            case 1:

可能变成:

switch (foodNumber << 8 | food1Phase)
{
    case (1 << 8 | 1):