これは XNA での私の最初のプログラムです。最初はケースロジックでローディング画面とエンディング画面を実装しようと試みたのですが、それがうまくいかないとき、どこかで失敗したのではないかと思ったので、if/else ステートメントに切り替えました。
これまでの結果は、何を変更しても、ゲームは InGame ロジックをロードするということです。開始画面も終了画面も、コーンフラワー ブルーとインゲーム ビスクもありません。理由がわかりません。
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);
}
}
}