这是我在XNA中的第一个程序,如果我错过了明显的内容,深表歉意。我首先尝试通过大小写逻辑实现加载和结束屏幕,当该操作不起作用时,我想知道是否将其固定在某个地方,所以我切换到了if / else语句。
到目前为止,我的结果是,无论我进行什么更改,我的游戏都会加载InGame逻辑。没有开始屏幕,没有结束屏幕,没有矢车菊蓝色vs我的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);
}
}
}
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)
做了同样的事情,也加上了我上面说的内容。