1

我正在尝试为 Pong 编程以完成编程课程中的作业,但是当我尝试使用案例制作主菜单时弹出错误。代码位于 XNA Window 游戏的更新区域。游戏案例中的代码自行运行良好。当我尝试制作主菜单并将我的 pong 代码移动到游戏箱时出现问题

switch (CurrentGameState)
{
    case GameState.MainMenu:
        if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
        btnPlay.Update(mouse);
        break;

    case GameState.Playing:
        if (Keyboard.GetState().IsKeyDown(Keys.Down))
        {
            if (POPBox.Y >= 373)
            {
                POPBox.Y += 0;
            }
            else
            {
                POPBox.Y += PlayersSpeed;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                if (POPBox.Y <= 0)
                {
                    POPBox.Y += 0;
                }
                else
                {
                    POPBox.Y += -PlayersSpeed;
                }
            }

            // Ball Limets
            if (BallBox.Y <= 0)
                VelocityY *= -1;
            if (BallBox.Y >= 463)
                VelocityY *= -1;
            if (BallBox.X <= 0)
                VelocityX *= -1;

            //Collision Detection (Runs this code if it hits the player one's paddle)
            if (BallBox.Intersects(POPBox))
            {
                //Used to deflect in different directions for some veriety
                if (PlayersSpeed > 0)
                    VelocityY += 3;
                if (PlayersSpeed < 0)
                    VelocityY -= 3;

                VelocityX *= -1;
                HitCount++;
                ShockerGeneratorPlayerOne();

                //Stopping the no slope bug. If it wants to bounce perfectly straight, it is slightly shifty to fix that error.
                if (VelocityY == 0)
                    VelocityY = VelocityY += 3;
                if (VelocityX == 0)
                    VelocityX = VelocityX += 3;

                //speed control
                if (VelocityX > 10)
                    VelocityX = 10;
                if (VelocityY > 10)
                    VelocityY = 10;
            }

            // Runs this code if the ball hits player two's paddle
            if (BallBox.Intersects(PTPBox))
            {
                VelocityX *= -1;
                ShockerGeneratorPlayerTwo();

                if (VelocityY == 0)
                     VelocityY = VelocityY += 3;
                if (VelocityX == 0)
                    VelocityX = VelocityX += 3;
            }

            //Object a collision
            if (BallBox.Intersects(ShocObjectARectangle))
            {
                VelocityY *= -1;
            }

            if (BallBox.Intersects(ShocObjectBRectangle))
            {
                VelocityX *= -1;
            }

            // If Player One Loses
            if (BallBox.X >= 790)
            {
                PlayerOneLoses();
            }

            //Player Two's "AI" and limets
            if (PTPBox.Y >= 173)
                PTPBox.Y += 0;
            else
                PTPBox.Y = BallBox.Y;

            if (PTPBox.Y <= 0)
                PTPBox.Y += 0;
            else
                PTPBox.Y = (BallBox.Y -30);

            //Object A movement code
            ShocObjectARectangle.X += ObjectASpeed;
            if (ShocObjectARectangle.X <= 80)
                ObjectASpeed *= -1;
            else if (ShocObjectARectangle.X >= 600)
                ObjectASpeed *= -1;

            //Object B movement code
            ShocObjectBRectangle.Y += ObjectBSpeed;

            if (ShocObjectBRectangle.Y <= 0)
                ObjectBSpeed *= -1;
            else if (ShocObjectBRectangle.Y >= 415)
                ObjectBSpeed *= -1;

            // Ball Velocity
            BallBox.Y += -VelocityY;
            BallBox.X += VelocityX;
            PlayersSpeed = 10;
        }
    }
}


//Called Every Hit
public void ShockerGeneratorPlayerOne()
{
    Random rnd = new Random();
    RanShocMatch = rnd.Next(10);
    if (RanShocMatch == 1)
    {
    //Speed Boost!
        VelocityX = (VelocityX - 1);
        VelocityY = (VelocityY - 1);
    }
    else if (RanShocMatch == 2)
        {
            if (ObjectBCalled == false)
            {
                ShocObjectBRectangle = new Rectangle(362, 200, 10, 100);
                ObjectBCalled = true;
            }
        }
        else if (RanShocMatch == 3)
            {
                if (ObjectACalled == false)
                {
                    ShocObjectARectangle = new Rectangle(80, 200, 100, 10);
                    ObjectACalled = true;
                }
            }
}

//Called Every Hit
public void ShockerGeneratorPlayerTwo()
{
    Random rnd = new Random();
    RanShocMatch = rnd.Next(5);

    if (RanShocMatch == 1)
    {
        //Speed Boost!
        VelocityX = (VelocityX + 3);
        VelocityY = (VelocityY + 3);
    }
}

//Called When Player One Loses
public void PlayerOneLoses()
{
    // MediaPlayer.Play(LosingBeep);
    VelocityX = -BasicVelocity;
    VelocityY = BasicVelocity;
    BallBox.X += -360;

    if (HitCount > highScore)
        highScore = HitCount;
        HitCount = 0;
        break;  
}

}                
}                     

    base.Update(gameTime);
}

错误:

  1. 命名空间不能直接包含字段或方法等成员
  2. 类型或命名空间定义,或预期的文件结尾
  3. 控制不能从一个案例标签中消失
  4. 没有可以中断或继续的封闭循环

我是这些论坛的新手,我的格式很糟糕,如果您有任何改进它的提示,我会进行编辑。

4

1 回答 1

1

看起来您突出显示了代码并将其向下移动,试试这个

更新方法

 switch (CurrentGameState)
            {
            case GameState.MainMenu:
                if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
                btnPlay.Update(mouse);
                break;
            case GameState.Playing:
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                {
                    if (POPBox.Y >= 373)
                    {
                        POPBox.Y += 0;
                    }
                    else
                    {
                        POPBox.Y += PlayersSpeed;
                    }
                }

                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                {
                    if (POPBox.Y <= 0)
                    {
                        POPBox.Y += 0;
                    }
                    else
                    {
                        POPBox.Y += -PlayersSpeed;
                    }
                }
                // Ball Limets
                if (BallBox.Y <= 0)
                    VelocityY *= -1;
                if (BallBox.Y >= 463)
                    VelocityY *= -1;
                if (BallBox.X <= 0)
                    VelocityX *= -1;
                //Collision Detection (Runs this code if it hits the player one's paddle)
                if (BallBox.Intersects(POPBox))
                {
                    //Used to deflect in different directions for some veriety
                    if (PlayersSpeed > 0)
                        VelocityY += 3;
                    if (PlayersSpeed < 0)
                        VelocityY -= 3;
                    VelocityX *= -1;
                    HitCount++;
                    ShockerGeneratorPlayerOne();
                    //Stopping the no slope bug. If it wants to bounce perfectly straight,                it is slightly shifty to fix that error.
                    if (VelocityY == 0)
                        VelocityY = VelocityY += 3;
                    if (VelocityX == 0)
                        VelocityX = VelocityX += 3;
                    //speed control
                    if (VelocityX > 10)
                        VelocityX = 10;
                    if (VelocityY > 10)
                        VelocityY = 10;
                 }
                // Runs this code if the ball hits player two's paddle
                if (BallBox.Intersects(PTPBox))
                {
                    VelocityX *= -1;
                    ShockerGeneratorPlayerTwo();
                    if (VelocityY == 0)
                        VelocityY = VelocityY += 3;
                    if (VelocityX == 0)
                        VelocityX = VelocityX += 3;
                }

                //Object a collision
                if (BallBox.Intersects(ShocObjectARectangle))
                {
                    VelocityY *= -1;
                }
                if (BallBox.Intersects(ShocObjectBRectangle))
                {
                    VelocityX *= -1;
                }
                // If Player One Loses
                if (BallBox.X >= 790)
                {
                    PlayerOneLoses();
                }
                //Player Two's "AI" and limets
                if (PTPBox.Y >= 173)
                    PTPBox.Y += 0;
                else
                    PTPBox.Y = BallBox.Y;

                if (PTPBox.Y <= 0)
                    PTPBox.Y += 0;
                else
                    PTPBox.Y = (BallBox.Y -30);
                //Object A movement code
                ShocObjectARectangle.X += ObjectASpeed;
                if (ShocObjectARectangle.X <= 80)
                    ObjectASpeed *= -1;
                else if (ShocObjectARectangle.X >= 600)
                    ObjectASpeed *= -1;
                //Object B movement code
                ShocObjectBRectangle.Y += ObjectBSpeed;
                if (ShocObjectBRectangle.Y <= 0)
                    ObjectBSpeed *= -1;
                else if (ShocObjectBRectangle.Y >= 415)
                    ObjectBSpeed *= -1;
                // Ball Velocity
                BallBox.Y += -VelocityY;
                BallBox.X += VelocityX;
                PlayersSpeed = 10;
                break;
            }
        base.Update(gameTime);

和你的其他 3 种方法

                //Called Every Hit
                public void ShockerGeneratorPlayerOne()
                {
                    Random rnd = new Random();
                    RanShocMatch = rnd.Next(10);
                    if (RanShocMatch == 1)
                    {
                    //Speed Boost!
                        VelocityX = (VelocityX - 1);
                        VelocityY = (VelocityY - 1);
                    }
                    else if (RanShocMatch == 2)
                    {
                        if (ObjectBCalled == false)
                        {
                            ShocObjectBRectangle = new Rectangle(362, 200, 10, 100);
                            ObjectBCalled = true;
                        }
                    }
                    else if (RanShocMatch == 3)
                    {
                        if (ObjectACalled == false)
                        {
                            ShocObjectARectangle = new Rectangle(80, 200, 100, 10);
                            ObjectACalled = true;
                        }
                    }
                }

                //Called Every Hit
                public void ShockerGeneratorPlayerTwo()
                {
                    Random rnd = new Random();
                    RanShocMatch = rnd.Next(5);
                    if (RanShocMatch == 1)
                    {
                    //Speed Boost!
                        VelocityX = (VelocityX + 3);
                        VelocityY = (VelocityY + 3);
                    }
                }

                //Called When Player One Loses
                public void PlayerOneLoses()
                {
                // MediaPlayer.Play(LosingBeep);
                    VelocityX = -BasicVelocity;
                    VelocityY = BasicVelocity;
                    BallBox.X += -360;
                    if (HitCount > highScore)
                        highScore = HitCount;
                    HitCount = 0;
                } 
于 2013-05-30T19:18:55.960 回答