XNA中的开始和结束屏幕

时间:2013-01-20 12:38:34

标签: xna xna-4.0

我现在已经工作了好几个小时了。这是我在XNA的第一个程序,如果我错过了一些明显的东西,请道歉。我首先尝试通过案例逻辑实现加载和结束屏幕,当这不起作用时,我想知道我是否将它搞砸了,所以我切换到if / else语句。

到目前为止,我的结果是无论我改变什么,我的游戏加载了InGame逻辑。没有开始屏幕,没有结束屏幕,没有矢车菊蓝色与我的InGame Bisque。我不明白为什么。

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FeedMe
{
    public enum GameState
    {
        Start,
        InGame,
        GameOver
    }

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D kitty;
        Texture2D cheese;
        Vector2 kittyPosition;
        Vector2 cheesePosition = Vector2.Zero;
        float speed1 = 4f;
        float speed2 = 5f;
        float secs = 30000.0f;
        MouseState prevMouseState;
        bool wasDown;
        int clicks;
        int speedUp = 5;
        AudioEngine audioEngine;
        WaveBank waveBank;
        SoundBank soundBank;
        Cue trackCue;
        SpriteFont splashFont;
        GameState currentGameState = new GameState();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        public Random rnd { get; private set; }

        protected bool Collide()
        {
            Rectangle kittyRect = new Rectangle(
                (int)kittyPosition.X,
                (int)kittyPosition.Y, 
                kitty.Width, 
                kitty.Height
            );

            Rectangle cheeseRect = new Rectangle(
                (int)cheesePosition.X,
                (int)cheesePosition.Y, 
                cheese.Width, 
                cheese.Height
            );

            return kittyRect.Intersects(cheeseRect);
        }

        protected override void Initialize()
        {
            currentGameState = GameState.Start;
            kittyPosition = Vector2.Zero;
            wasDown = false;
            clicks = 0;
            this.IsMouseVisible = false;
            rnd = new Random();
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            kitty = Content.Load<Texture2D>(@"Images/kitty");
            cheese = Content.Load<Texture2D>(@"Images/cheese");
            audioEngine = new AudioEngine(@"Content\Audio\GameAudio.xgs");
            waveBank = new WaveBank(audioEngine, @"Content\Audio\Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, @"Content\Audio\Sound Bank.xsb");
            splashFont = Content.Load<SpriteFont>(@"Fonts\Splash");
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            if (currentGameState == GameState.Start)
                soundBank.PlayCue("gameStart");

            if (Keyboard.GetState().GetPressedKeys().Length > 0)
            {
                currentGameState = GameState.InGame;
            }
            else if (currentGameState == GameState.InGame)
            {
                kittyPosition.X += speed1;

                if (kittyPosition.X > Window.ClientBounds.Width - kitty.Width || kittyPosition.X < 0)
                    speed1 *= -1;

                kittyPosition.Y += speed2;

                if (kittyPosition.Y > Window.ClientBounds.Height - kitty.Height || kittyPosition.Y < 0)
                    speed2 *= -1;

                MouseState mouseState = Mouse.GetState();

                if (mouseState.X != prevMouseState.X || mouseState.Y != prevMouseState.Y)
                    cheesePosition = new Vector2(mouseState.X, mouseState.Y);

                if (cheesePosition.X < 0)
                    cheesePosition.X = 0;
                if (cheesePosition.Y < 0)
                    cheesePosition.Y = 0;
                if (cheesePosition.X > Window.ClientBounds.Width - cheese.Width)
                    cheesePosition.X = Window.ClientBounds.Width - cheese.Width;
                if (cheesePosition.Y > Window.ClientBounds.Height - cheese.Height)
                    cheesePosition.Y = Window.ClientBounds.Height - cheese.Height;

                secs -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;

                if (secs <= 0.0f)
                {
                    secs = 0;
                }

                ButtonState bs = mouseState.LeftButton;
                bool nowDown = bs == ButtonState.Pressed;

                if (Collide() && (!nowDown && wasDown))
                {
                    soundBank.PlayCue("chomp");
                    clicks++;
                    Window.Title = "Cheeseburgers fed = " + clicks + "     Time Remaining = " + (int)secs / 1000;
                    kittyPosition = new Vector2(
                        (rnd.Next(0, Window.ClientBounds.Width - kitty.Width)),
                        (rnd.Next(0, Window.ClientBounds.Height - kitty.Height))
                    );
                }

                wasDown = nowDown;

                if (clicks == speedUp)
                {
                    soundBank.PlayCue("turbo");
                    speedUp = speedUp + 5;
                    speed1 = speed1 * 1.5f;
                    speed2 = speed2 * 1.5f;
                }
            }
            else if (currentGameState == GameState.GameOver)
            {
                soundBank.PlayCue("gameOver");

                if (Keyboard.GetState().GetPressedKeys().Length > 0)
                {
                    currentGameState = GameState.InGame;
                }
            }

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            audioEngine.Update();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            if(currentGameState == GameState.Start)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
                spriteBatch.Begin();
                string text = "Testing!";
                spriteBatch.DrawString(
                    splashFont, 
                    text, 
                    new Vector2((Window.ClientBounds.Width / 2) - (splashFont.MeasureString(text).X / 2), 
                    (Window.ClientBounds.Height / 2) - (splashFont.MeasureString(text).Y / 2)), 
                    Color.DarkBlue
                );

                text = "Testing2!";

                spriteBatch.DrawString(
                    splashFont, 
                    text, 
                    new Vector2((Window.ClientBounds.Width/2) - (splashFont.MeasureString(text).X/2),        
                    (Window.ClientBounds.Height/2) - (splashFont.MeasureString(text).Y/2) + 30), 
                    Color.DarkBlue
                );

                spriteBatch.End();
            }
            else if(currentGameState == GameState.InGame)
            {
                GraphicsDevice.Clear(Color.Bisque);

                spriteBatch.Begin();
                spriteBatch.Draw(kitty, kittyPosition, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                spriteBatch.Draw(cheese, cheesePosition, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            else if (currentGameState == GameState.GameOver)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
                spriteBatch.Begin();
                string text = "Testing3";
                spriteBatch.DrawString(
                    splashFont, 
                    text, 
                    new Vector2((Window.ClientBounds.Width / 2) - (splashFont.MeasureString(text).X / 2), 
                    (Window.ClientBounds.Height / 2) - (splashFont.MeasureString(text).Y / 2)), 
                    Color.DarkBlue
                );

                text = "Testing4";

                spriteBatch.DrawString(
                    splashFont, 
                    text, 
                    new Vector2((Window.ClientBounds.Width/2) - (splashFont.MeasureString(text).X/2, 
                    (Window.ClientBounds.Height/2) - (splashFont.MeasureString(text).Y/2) + 30), 
                    Color.DarkBlue
                );

                spriteBatch.End();
            }

            base.Draw(gameTime);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

if (currentGameState == GameState.Start)
{
                soundBank.PlayCue("gameStart");
                if(Keyboard.GetState().GetPressedKeys().Length>0)
                {
                    currentGameState = GameState.InGame;
                } 
}

{ }之后没有if (currentGameState == GameState.Start)它只会为下一个语句提供if,因此它不会包含if(Keyboard.GetState().GetPressedKeys().Length>0)

如果你仔细看看触发它的是什么,

 if(Keyboard.GetState().GetPressedKeys().Length>0)
                {
                    currentGameState = GameState.InGame;
                }
            else if (currentGameState == GameState.InGame)

基本上,它表示如果没有按键,请转到游戏。只需在GameState.Start逻辑周围添加{}就可以了。

第234行也需要new Vector2((Window.ClientBounds.Width/2) - (splashFont.MeasureString(text).X/2), (Window.ClientBounds.Height/2) - (splashFont.MeasureString(text).Y/2) + 30), Color.DarkBlue);

编辑:您对else if (currentGameState == GameState.GameOver)做了同样的事情,添加了我上面所说的内容。