C#开发跨平台游戏——设置游戏的窗口大小及分辨率
上一篇我们提到游戏的全屏窗口及退出,接下去我们来探讨如何设置游戏窗口的大小(即游戏分辨率)
首先打开Game1.cs文件,找到Game1类的构造函数,public Game1(),在【Content.RootDirectory = “Content”;】下面输入来设置游戏界面的大小
graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 600;
点击【启动】
各位可以注意到800×600只是游戏部分,不包括标题栏的高度。
加入【graphics.IsFullScreen = true;】将游戏设置为全屏,点击【启动】
graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 600; graphics.IsFullScreen = true;
这时候看到的游戏是全屏状态,但上面的【这是我的第一个游戏】明显有锯齿,不够清晰。因为游戏的分辨率只有800×600。在大于该分辨率的显示器下会出现拉伸的模糊状态!
接下去,我们使用点击键盘上的F12键,来修改游戏的分辨率。找到Update(GameTime gameTime)。在上一篇全屏代码的下面加入
if (keyboardState.IsKeyDown(Keys.F12)) { graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; graphics.ApplyChanges(); }
点击【启动】
游戏运行以后,按下键盘上的F12,这时候你会发现,全屏游戏上的【这是我的第一个游戏】变清晰了!这是因为虽然还是全屏,但分辨率从800×600变为1920×1080,刚好是标准1080P显示器的大小。
要注意一点。在修改分辨率后,要使用graphics的ApplyChanges()方法,才会生效!
完整代码如下:
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"; graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 600; graphics.IsFullScreen = true; } /// <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(); } if (keyboardState.IsKeyDown(Keys.F12)) { graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; 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); } } }