C# 暂停菜单按钮不绘制
目前正在为一款太空入侵者游戏制作暂停菜单,但我遇到的问题是,当按下回车键时,游戏会显示暂停菜单,但C# 暂停菜单按钮不绘制,c#,class,xna,C#,Class,Xna,目前正在为一款太空入侵者游戏制作暂停菜单,但我遇到的问题是,当按下回车键时,游戏会显示暂停菜单,但 它不显示暂停菜单中的按钮:btnResume&btnMainMenu & 我在游戏中的精灵仍然可以操作,例如通过左箭头键旋转,通过空格键发射子弹 最好是我想让按钮显示出来,但我也希望看到我的精灵被冻结在原地,包括子弹,而不是它们在游戏暂停时在屏幕上飞过 代码如下: namespace Rotationgame { /// <summary> /// This is the main
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();
名称空间旋转游戏
{
///
///这是游戏的主要类型
///
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧;
//不同的窗口
枚举配子状态
{
主菜单,
玩,
}
GameState CurrentGameState=GameState.main菜单;
//尖叫声调整
int屏幕宽度=1250,屏幕高度=930;
//主菜单按钮
按钮btnPlay;
按钮b取消;
//暂停菜单和按钮
布尔=假;
按钮btnResume;
按钮BTN主菜单;
矢量2速度;
恒浮切向速度=0f;
浮子摩擦力=1f;
质感2维织物;
长方形斜六角形;
//图像的中心
矢量2精神起源;
矢量2精神定位;
浮动旋转;
//背景
纹理2D背景纹理;
矩形背景矩形;
//护盾
纹理2D屏蔽纹理;
矩形屏蔽;
//子弹
列表项目符号=新列表();
键盘状态pastKey;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
///
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
///
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
///
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
///
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
shieldTexture=Content.Load(“屏蔽”);
shieldRectangle=新矩形(517345250220);
spriteTexture=Content.Load(“PlayerShipright”);
spritePosition=newvector2(640450);
backgroundTexture=Content.Load(“背景”);
背景矩形=新矩形(0,01250930);
//屏幕调整
graphics.PreferredBackBufferWidth=屏幕宽度;
graphics.PreferredBackBufferHeight=屏幕高度;
graphics.ApplyChanges();
IsMouseVisible=true;
//主菜单按钮和位置
btnPlay=新按钮(Content.Load(“Playbutton”)、graphics.GraphicsDevice);
BTN显示设置位置(新矢量2(550310));
btnQuit=新按钮(Content.Load(“Quitbutton”)、graphics.GraphicsDevice);
b取消设置位置(新矢量2(550580));
//暂停菜单按钮和位置
btnResume=新按钮(Content.Load(“Playbutton”)、graphics.GraphicsDevice);
BTN消耗设置位置(新矢量2(550310));
btnMainMenu=新建按钮(Content.Load(“Quitbutton”)、graphics.GraphicsDevice);
设置位置(新矢量2(550580));
//TODO:使用此.Content在此处加载游戏内容
}
///
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
///
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
///
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
///
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
MouseState mouse=mouse.GetState();
//允许游戏退出
if(Keyboard.GetState().IsKeyDown(Keys.Escape))
这是Exit();
开关(当前游戏状态)
{
case GameState.main菜单:
如果(btnPlay.isClicked==true)CurrentGameState=GameState.Playing;
btnPlay.Update(鼠标);
if(btnQuit.isClicked==true)
这是Exit();
更新(鼠标);
打破
案例游戏状态。游戏:
如果(!暂停)
{
if(Keyboard.GetState().IsKeyDown(Keys.Enter))
{
暂停=真;
btnResume.isClicked=false;
}
}
否则,如果(暂停)
{
if(Keyboard.GetState().IsKeyDown(Keys.Enter))
如果(btnResume.isClicked)
暂停=错误;
如果(btnMainMenu.isClicked)CurrentGameState=GameState.MainMenu;
}
打破
}
//TODO:在此处添加更新逻辑
if(Keyboard.GetState().IsKeyDown(Keys.Space)和&pastKey.iskeydup(Keys.Space))
射击();
pastKey=Keyboard.GetState();
spritePosition=spriteVelocity+spritePosition;
spriteRectangle=新矩形((int)spritePosition.X,(int)spritePosition.Y,
spriteTexture.宽度、spriteTexture.高度);
spriteOrigin=新矢量2(spriteRectangle.Width/2,spriteRectangle.Height
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))
if (btnResume.isClicked)
paused = false;
if (btnMainMenu.isClicked) CurrentGameState = GameState.MainMenu;
}
else if (!paused)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
paused = true;
btnResume.isClicked = false;
}
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();
break;
}
}
}