示例#1
0
        /// <summary>
        /// Loads all of the content to play the game, loads all the scenes and the music for the scenes
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Adding our scenes to our components aswell as to the services
            // to be able to directly access the components list from other classes

            menuScene = new MenuScene(this, spriteBatch);
            this.Components.Add(menuScene);
            this.Services.AddService <MenuScene>(menuScene);
            menuScene.Show();

            actionScene = new ActionScene(this, spriteBatch);
            this.Components.Add(actionScene);
            this.Services.AddService <ActionScene>(actionScene);
            actionScene.Hide();

            helpScene = new HelpScene(this, spriteBatch);
            this.Components.Add(helpScene);
            this.Services.AddService <HelpScene>(helpScene);
            helpScene.Hide();

            creditScene = new CreditScene(this, spriteBatch);
            this.Components.Add(creditScene);
            this.Services.AddService <CreditScene>(creditScene);
            creditScene.Hide();

            gameOverScene = new GameOverScene(this, spriteBatch);
            this.Components.Add(gameOverScene);
            this.Services.AddService <GameOverScene>(gameOverScene);
            gameOverScene.Hide();

            // Getting the music for the scenes
            menuSong   = this.Content.Load <Song>("Music/Menu");
            actionSong = this.Content.Load <Song>("Music/Action");
            MediaPlayer.Play(menuSong);
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Volume      = 0.1f;
        }
示例#2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            int           selectedIdx = 0;
            KeyboardState ks          = Keyboard.GetState();

            // manage menus & scenes
            if (startScene.Enabled)
            {
                if (oldState.IsKeyDown(Keys.Enter) && ks.IsKeyUp(Keys.Enter))
                {
                    selectedIdx = startScene.Menu.SelectedIdx;
                    startScene.Hide();
                    if (selectedIdx == (int)MainMenu.Start)
                    {
                        actionScene.Show();
                    }
                    else if (selectedIdx == (int)MainMenu.Help)
                    {
                        helpScene.Show();
                    }
                    else if (selectedIdx == (int)MainMenu.HighScore)
                    {
                        RebootHighScoreScene();
                        highScoreScene.Show();
                    }
                    else if (selectedIdx == (int)MainMenu.About)
                    {
                        aboutScene.Show();
                    }
                    else if (selectedIdx == (int)MainMenu.Quit)
                    {
                        Exit();
                    }
                }
            }
            if (actionScene.Enabled)
            {
                if (actionScene.ShowGameOver)
                {
                    actionScene.Enabled = false;
                    gameOverScene.Score = (int)actionScene.Score;
                    highScorePlayers    = ReadScores(); // read scores

                    // if there are players saved and the current score is lower compared to the highest one,
                    // set highscore to this max one;
                    // otherwise set to the current score
                    if (highScorePlayers.Any() && gameOverScene.Score < highScorePlayers[0].Score)
                    {
                        gameOverScene.HighScore = highScorePlayers[0].Score;
                    }
                    else
                    {
                        gameOverScene.HighScore = (int)actionScene.Score;
                    }
                    gameOverScene.Show();
                }
            }
            if (helpScene.Enabled)
            {
                if (ks.IsKeyDown(Keys.Escape))
                {
                    helpScene.Hide();
                    startScene.Show();
                }
            }
            if (highScoreScene.Enabled)
            {
                if (ks.IsKeyDown(Keys.Escape))
                {
                    highScoreScene.Hide();
                    startScene.Show();
                }
            }
            if (aboutScene.Enabled)
            {
                if (ks.IsKeyDown(Keys.Escape))
                {
                    aboutScene.Hide();
                    startScene.Show();
                }
            }
            if (gameOverScene.Enabled && !gameOverScene.IsEditNameMode)
            {
                if (oldState.IsKeyDown(Keys.Enter) && ks.IsKeyUp(Keys.Enter))
                {
                    selectedIdx = gameOverScene.Menu.SelectedIdx;

                    gameOverScene.Hide();

                    Player player = new Player(gameOverScene.Name, gameOverScene.Score);
                    UpdateScores(player); // update the 'scores' file

                    if (selectedIdx == (int)GameOverMenu.PlayAgain)
                    {
                        actionScene.Hide();
                        RebootActionScene();
                        actionScene.Show();
                    }
                    else if (selectedIdx == (int)GameOverMenu.MainMenu)
                    {
                        gameOverScene.Menu.SelectedIdx = 0;
                        actionScene.Hide();
                        //RebootGameOverScene();
                        RebootActionScene();
                        startScene.Show();
                    }
                }
            }
            oldState = ks;

            base.Update(gameTime);
        }
示例#3
0
        /// <summary>
        /// Controls the various scenes for the game, waiting for user input and responding with the correct follow up action
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState ks = Keyboard.GetState();
            //the index of which menu item is currently hovered/selected
            int selectedIndex = 0;

            //check to see if were on menu screen
            if (menuScene.Enabled)
            {
                selectedIndex = menuScene.Menu.SelectedIndex;       //sets the selected index
                if (selectedIndex == 0 && ks.IsKeyDown(Keys.Enter)) //if Play game is selected, hide the menu and menu music and play action scene with music
                {
                    menuScene.Hide();
                    MediaPlayer.Stop();
                    actionScene.ResetGame();
                    actionScene.Show();
                    MediaPlayer.Play(actionSong);
                    MediaPlayer.IsRepeating = true;
                    MediaPlayer.Volume      = 0.1f;
                }
                else if (selectedIndex == 1 && ks.IsKeyDown(Keys.Enter)) //if Help scene is selected, hide menu and show help scene
                {
                    menuScene.Hide();
                    helpScene.Show();
                }
                else if (selectedIndex == 2 && ks.IsKeyDown(Keys.Enter)) //if Credit scene is selected, hide menu and show credit scene
                {
                    menuScene.Hide();
                    creditScene.Show();
                }
                else if (selectedIndex == 3 && ks.IsKeyDown(Keys.Enter)) //if Quit is selected, close the application
                {
                    Exit();
                }
            }
            else if (actionScene.Enabled) //check to see if the user is on the action scene
            {
                if (ks.IsKeyDown(Keys.Escape))
                {
                    MediaPlayer.Stop();
                    actionScene.Hide();
                    menuScene.Show();
                    MediaPlayer.Play(menuSong);
                    MediaPlayer.IsRepeating = true;
                }
            }
            else if (helpScene.Enabled) //check to see if user is on the help scene
            {
                if (ks.IsKeyDown(Keys.Escape))
                {
                    helpScene.Hide();
                    menuScene.Show();
                }
            }
            else if (creditScene.Enabled) //check to see if the user is on the credit scene
            {
                if (ks.IsKeyDown(Keys.Escape))
                {
                    creditScene.Hide();
                    menuScene.Show();
                }
            }
            else if (gameOverScene.Enabled) //check to see if the user is on the game over scene
            {
                if (ks.IsKeyDown(Keys.Escape))
                {
                    gameOverScene.Hide();
                    menuScene.Show();
                    MediaPlayer.Play(menuSong);
                    MediaPlayer.IsRepeating = true;
                }
            }
            base.Update(gameTime);
        }