C#开发跨平台游戏——设置游戏的窗口大小及分辨率
上一篇我们提到游戏的全屏窗口及退出,接下去我们来探讨如何设置游戏窗口的大小(即游戏分辨率)
首先打开Game1.cs文件,找到Game1类的构造函数,public Game1(),在【Content.RootDirectory = “Content”;】下面输入来设置游戏界面的大小
1 2 |
graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 600; |
点击【启动】
各位可以注意到800×600只是游戏部分,不包括标题栏的高度。
加入【graphics.IsFullScreen = true;】将游戏设置为全屏,点击【启动】
1 2 3 |
graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 600; graphics.IsFullScreen = true; |
这时候看到的游戏是全屏状态,但上面的【这是我的第一个游戏】明显有锯齿,不够清晰。因为游戏的分辨率只有800×600。在大于该分辨率的显示器下会出现拉伸的模糊状态!
接下去,我们使用点击键盘上的F12键,来修改游戏的分辨率。找到Update(GameTime gameTime)。在上一篇全屏代码的下面加入
1 2 3 4 5 6 |
if (keyboardState.IsKeyDown(Keys.F12)) { graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; graphics.ApplyChanges(); } |
点击【启动】
游戏运行以后,按下键盘上的F12,这时候你会发现,全屏游戏上的【这是我的第一个游戏】变清晰了!这是因为虽然还是全屏,但分辨率从800×600变为1920×1080,刚好是标准1080P显示器的大小。
要注意一点。在修改分辨率后,要使用graphics的ApplyChanges()方法,才会生效!
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
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); } } } |
如果您对C#游戏开发感兴趣,可以扫下面二维码加入我们的QQ群来一起学习交流
原创文章,转载请注明本文链接地址(违者必究):C#开发跨平台游戏——设置游戏的窗口大小及分辨率