从零开发游戏需要学习的c#模块,第十六章(安装 MonoGame 并创建第一个窗口)

从零开发游戏需要学习的c#模块,第十六章(安装 MonoGame 并创建第一个窗口) 关于c#的基本内容我们差不多学完了接下来我们来学习安装monogame并创建第一个窗口。第一步安装 MonoGame 模板打开Visual Studio Installer开始菜单搜一下就有找到你安装的 Visual Studio 版本点击修改在右侧找到“游戏开发”或直接搜“游戏”勾选“使用 C# 的游戏开发”(或者c的游戏开发这个工作负荷点击右下角修改等待安装完成安装完成后打开 Visual Studio。第二步创建第一个 MonoGame 项目在 Visual Studio 里点击创建新项目在搜索框里输入MonoGame选择MonoGame Cross-Platform Desktop Application不是 Android/iOS 的项目名称写MyFirst2DGame选好位置点创建如果无法搜索到请使用备用方案用命令行装模板备选方案如果VS里搜不到那个插件或者安装了还是没用可以打开命令行CMD或PowerShell输入下面这行代码然后回车它会直接把模板文件安装到你的电脑里dotnet new install MonoGame.Templates.CSharp第三步了解项目结构创建完后你会看到以下文件文件作用Game1.cs游戏主类核心逻辑都在这里Program.cs启动入口和之前一样Content文件夹存放图片、音效、字体等资源第四步看Game1.cs的结构打开Game1.cs你会看到这些你非常熟悉的东西protected override void Initialize(){// 初始化游戏数据你之前的 Start()base.Initialize();}protected override void LoadContent(){// 加载图片、音效你之前的 LoadGame()}protected override void Update(GameTime gameTime){// 每帧更新逻辑你之前的 Update()// gameTime 里有 deltaTime相当于你的 Thread.Sleep(30)}protected override void Draw(GameTime gameTime){// 每帧渲染画面你之前的 Render()}第五步画出第一个东西——红色方块在game里替换using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;namespace MyFirst2DGame{public class Game1 : Game{private GraphicsDeviceManager _graphics;private SpriteBatch _spriteBatch;// 玩家相关private Texture2D playerTexture; // 玩家的纹理图片private Vector2 playerPosition; // 玩家的位置private int playerSpeed 200; // 移动速度public Game1(){_graphics new GraphicsDeviceManager(this);Content.RootDirectory Content;IsMouseVisible true;}protected override void Initialize(){// 设置窗口大小_graphics.PreferredBackBufferWidth 800;_graphics.PreferredBackBufferHeight 600;_graphics.ApplyChanges();// 玩家初始位置屏幕中央playerPosition new Vector2(400, 300);base.Initialize();}protected override void LoadContent(){_spriteBatch new SpriteBatch(GraphicsDevice);// ★ 创建一个 1x1 的白色纹理然后染色成红色playerTexture new Texture2D(GraphicsDevice, 1, 1);playerTexture.SetData(new[] { Color.White });}protected override void Update(GameTime gameTime){// 获取键盘状态KeyboardState keyboard Keyboard.GetState();// 移动速度 * 帧时间 每秒移动固定距离float speed playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;if (keyboard.IsKeyDown(Keys.W) || keyboard.IsKeyDown(Keys.Up))playerPosition.Y - speed;if (keyboard.IsKeyDown(Keys.S) || keyboard.IsKeyDown(Keys.Down))playerPosition.Y speed;if (keyboard.IsKeyDown(Keys.A) || keyboard.IsKeyDown(Keys.Left))playerPosition.X - speed;if (keyboard.IsKeyDown(Keys.D) || keyboard.IsKeyDown(Keys.Right))playerPosition.X speed;// ESC 退出if (keyboard.IsKeyDown(Keys.Escape))Exit();base.Update(gameTime);}protected override void Draw(GameTime gameTime){// 清屏为深蓝色GraphicsDevice.Clear(Color.CornflowerBlue);// 开始画东西_spriteBatch.Begin();// ★ 画一个 50x50 的红色方块代表玩家Rectangle playerRect new Rectangle((int)playerPosition.X - 25,(int)playerPosition.Y - 25,50, 50);_spriteBatch.Draw(playerTexture, playerRect, Color.Red);_spriteBatch.End();base.Draw(gameTime);}}}第六步运行按F5你会看到一个 800x600 的深蓝色窗口一个 50x50 的红色方块在屏幕中央按WASD或方向键移动方块按ESC退出注意program改为using var myGame new MY_FIRST_GAME.Game1(); myGame.Run();保证程序能够正常运行好了今天的课程到此结束关注我下期更精彩