目前正在为太空入侵者游戏制作暂停菜单,但我遇到的问题是,当按下回车键时,游戏会显示暂停菜单,但是
- 它不显示按钮:暂停菜单中的 btnResume & btnMainMenu
&
- 我在游戏中的精灵仍然可以操作,例如通过左键和箭头键旋转并通过空格键射击子弹。
最好我想让按钮显示,但我也希望看到我的精灵被冻结在适当的位置,包括子弹,而不是在游戏暂停时它们飞过屏幕。
代码如下:
namespace Rotationgame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Different Windows
enum GameState
{
MainMenu,
Playing,
}
GameState CurrentGameState = GameState.MainMenu;
// Screeb Adjustments
int screenWidth = 1250, screenHeight = 930;
// Main Menu Buttons
button btnPlay;
button btnQuit;
// Pause Menu & buttons
bool paused = false;
button btnResume;
button btnMainMenu;
Vector2 spriteVelocity;
const float tangentialVelocity = 0f;
float friction = 1f;
Texture2D spriteTexture;
Rectangle spriteRectangle;
// The centre of the image
Vector2 spriteOrigin;
Vector2 spritePosition;
float rotation;
// Background
Texture2D backgroundTexture;
Rectangle backgroundRectangle;
// Shield
Texture2D shieldTexture;
Rectangle shieldRectangle;
// Bullets
List<Bullets> bullets = new List<Bullets>();
KeyboardState pastKey;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
shieldTexture = Content.Load<Texture2D>("Shield");
shieldRectangle = new Rectangle(517, 345, 250, 220);
spriteTexture = Content.Load<Texture2D>("PlayerShipright");
spritePosition = new Vector2(640, 450);
backgroundTexture = Content.Load<Texture2D>("Background");
backgroundRectangle = new Rectangle(0, 0, 1250, 930);
// Screen Adjustments
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
graphics.ApplyChanges();
IsMouseVisible = true;
// Main menu Buttons & locations
btnPlay = new button(Content.Load<Texture2D>("Playbutton"), graphics.GraphicsDevice);
btnPlay.setPosition(new Vector2(550, 310));
btnQuit = new button(Content.Load<Texture2D>("Quitbutton"), graphics.GraphicsDevice);
btnQuit.setPosition(new Vector2(550, 580));
// Pause menu buttons & locations
btnResume = new button(Content.Load<Texture2D>("Playbutton"), graphics.GraphicsDevice);
btnResume.setPosition(new Vector2(550, 310));
btnMainMenu = new button(Content.Load<Texture2D>("Quitbutton"), graphics.GraphicsDevice);
btnMainMenu.setPosition(new Vector2(550, 580));
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
MouseState mouse = Mouse.GetState();
// Allows the game to exit
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
switch (CurrentGameState)
{
case GameState.MainMenu:
if(btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
if (btnQuit.isClicked == true)
this.Exit();
btnQuit.Update(mouse);
break;
case GameState.Playing:
if (!paused)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
paused = true;
btnResume.isClicked = false;
}
}
else if (paused)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
if (btnResume.isClicked)
paused = false;
if (btnMainMenu.isClicked) CurrentGameState = GameState.MainMenu;
}
break;
}
// TODO: Add your update logic here
if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastKey.IsKeyUp(Keys.Space))
Shoot();
pastKey = Keyboard.GetState();
spritePosition = spriteVelocity + spritePosition;
spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y,
spriteTexture.Width, spriteTexture.Height);
spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);
if (Keyboard.GetState().IsKeyDown(Keys.Right)) rotation += 0.025f;
if (Keyboard.GetState().IsKeyDown(Keys.Left)) rotation -= 0.025f;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
spriteVelocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
spriteVelocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
}
else if (Vector2.Zero != spriteVelocity)
{
float i = spriteVelocity.X;
float j = spriteVelocity.Y;
spriteVelocity.X = i -= friction * i;
spriteVelocity.Y = j -= friction * j;
base.Update(gameTime);
}
UpdateBullets();
}
public void UpdateBullets()
{
foreach (Bullets bullet in bullets)
{
bullet.position += bullet.velocity;
if (Vector2.Distance(bullet.position, spritePosition) > 760)
bullet.isVisible = false;
}
for (int i = 0; i < bullets.Count; i++)
{
if(!bullets[i].isVisible)
{
bullets.RemoveAt(i);
i--;
}
}
}
public void Shoot()
{
Bullets newBullet = new Bullets(Content.Load<Texture2D>("bullet"));
newBullet.velocity = new Vector2((float)Math.Cos(rotation),(float)Math.Sin(rotation)) * 3f + spriteVelocity;
newBullet.position = spritePosition + newBullet.velocity * 5;
newBullet.isVisible = true;
if(bullets.Count() < 25)
bullets.Add(newBullet);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Draw(Content.Load<Texture2D>("MainMenu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
btnPlay.Draw(spriteBatch);
btnQuit.Draw(spriteBatch);
break;
case GameState.Playing:
// Drawing Background
spriteBatch.Draw(backgroundTexture, backgroundRectangle, Color.White);
// Drawing Shield
spriteBatch.Draw(shieldTexture, shieldRectangle, Color.White);
// Drawing Bullets
foreach (Bullets bullet in bullets)
bullet.Draw(spriteBatch);
// Drawing Player's Character
spriteBatch.Draw(spriteTexture, spritePosition, null, Color.White, rotation, spriteOrigin, 1f, SpriteEffects.None, 0);
if (paused)
{
spriteBatch.Draw(Content.Load<Texture2D>("PauseMenu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
btnResume.Draw(spriteBatch);
btnMainMenu.Draw(spriteBatch);
}
break;
}
spriteBatch.End();
有什么想法我哪里出错了吗?