C#开发跨平台游戏——游戏的全屏窗口及退出
在上一篇,我们介绍了在游戏中使用中文,接下去我们介绍如何在游戏中使用键盘修改游戏的显示模式(窗口和全屏)以及游戏的退出。
退出游戏
打开Game1.cs文件,找到Update(GameTime gameTime)方法。在方法体中有一段【 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)】,这一段代码的含义是当按下游戏手柄上的返回键,则退出游戏,由于我们现在创建的游戏是Windows游戏,所以并没有游戏手柄GamePad。所以将它修改为【if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))】。也可以拆分这段代码,让思路更加清晰:
KeyboardState keyboardState = Keyboard.GetState(PlayerIndex.One);//获取键盘状态 if(keyboardState.IsKeyDown(Keys.Escape))//判断是否按下了Escape键盘 { this.Exit();//退出游戏 }
可以注意到Keyboard的GetState方法带一个PlayerIndex枚举参数(存在四个值One、Two、Three、Four),当游戏是多人游戏的时候(支持四个)就可以判断哪个人按下来什么键盘。当前游戏可以省略这个参数!
点击【启动】
按下键盘上的ESC键盘,游戏便退出了!
游戏全屏显示
还是打开Game1.cs文件,找到Update(GameTime gameTime)方法。在退出游戏的代码下面输入:
if(keyboardState.IsKeyDown(Keys.F10)) { graphics.IsFullScreen = true;//设为全屏 graphics.ApplyChanges();//全屏立即生效 } if(keyboardState.IsKeyDown(Keys.F11)) { graphics.IsFullScreen = false;//退出全屏 graphics.ApplyChanges();//立即生效 }
将graphics的IsFullScreen属性设置为true或false,就可以改变游戏的显示状态(全屏和窗口),设置以后调用graphics的ApplyChanges方法执行!前面提到graphics可以看作是显卡,很多显示相关的操作都可以在里面找到相应的属性和方法。以后将会做更多的介绍
完整代码如下:
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 HelloWorld.XNA { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont defaultFont; 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); // TODO: use this.Content to load your game content here defaultFont = Content.Load<SpriteFont>("DefaultFont"); } /// <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) { // Allows the game to exit KeyboardState keyboardState = Keyboard.GetState(PlayerIndex.One); if(keyboardState.IsKeyDown(Keys.Escape)) { this.Exit(); } if(keyboardState.IsKeyDown(Keys.F10)) { graphics.IsFullScreen = true; graphics.ApplyChanges(); } if(keyboardState.IsKeyDown(Keys.F11)) { graphics.IsFullScreen = false; graphics.ApplyChanges(); } // TODO: Add your update logic here base.Update(gameTime); } /// <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); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.DrawString(defaultFont, "这是我的第一个游戏", Vector2.Zero, Color.White); spriteBatch.End(); base.Draw(gameTime); } } }
点击【启动】
按下键盘F10,游戏全屏。按下F11退出全屏变为窗口模式。按下Esc退出游戏