从零开发游戏需要学习的c#模块,第十九章(在游戏画面里显示文字 —— FontStashSharp)
本节课我们要学习的内容是安装字体渲染库加载系统字体文件在游戏画面里直接显示分数、金币数等信息第一步安装 NuGet 包在 Visual Studio 右侧“解决方案资源管理器”里右键你的项目名不是解决方案选择“管理 NuGet 程序包”点击“浏览”标签搜索FontStashSharp.MonoGame找到后点击安装第二步准备字体文件打开 Windows 的文件资源管理器地址栏输入C:\Windows\Fonts回车找一个你喜欢的.ttf字体比如consola.ttf、simhei.ttf黑体等复制这个文件粘贴到你项目的Content文件夹里在 VS 里右键这个.ttf文件 →属性把“复制到输出目录”改成“如果较新则复制”第三步完整代码把Game1.cs完整替换为using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using System;using System.Collections.Generic;using System.IO;using FontStashSharp;namespace MY_FIRST_GAME{public class Game1 : Game{private GraphicsDeviceManager _graphics;private SpriteBatch _spriteBatch;// 玩家private Texture2D playerTexture;private Vector2 playerPosition;private float playerSpeed 200f;// 金币private Texture2D coinTexture;private ListVector2 coins;private Random rng;private int score;// ★ 字体private SpriteFontBase font;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);rng new Random();coins new ListVector2();score 0;SpawnCoins(5);base.Initialize();}protected override void LoadContent(){_spriteBatch new SpriteBatch(GraphicsDevice);// 加载玩家图片using var stream File.OpenRead(Content/player.png);playerTexture Texture2D.FromStream(GraphicsDevice, stream);// 创建金币纹理coinTexture new Texture2D(GraphicsDevice, 32, 32);Color[] data new Color[32 * 32];for (int i 0; i data.Length; i)data[i] Color.Gold;coinTexture.SetData(data);// ★ 加载字体直接用 .ttf 文件不需要编译var fontSystem new FontSystem();fontSystem.AddFont(File.ReadAllBytes(Content/consola.ttf));font fontSystem.GetFont(18);}private void SpawnCoins(int count){for (int i 0; i count; i){float x rng.Next(50, 750);float y rng.Next(50, 550);coins.Add(new Vector2(x, y));}}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;playerPosition.X Math.Clamp(playerPosition.X, 32, 768);playerPosition.Y Math.Clamp(playerPosition.Y, 32, 568);CheckCoinCollision();if (coins.Count 0)SpawnCoins(5);if (keyboard.IsKeyDown(Keys.Escape))Exit();base.Update(gameTime);}private void CheckCoinCollision(){Rectangle playerRect new Rectangle((int)playerPosition.X - 32,(int)playerPosition.Y - 32,64, 64);for (int i coins.Count - 1; i 0; i--){Rectangle coinRect new Rectangle((int)coins[i].X, (int)coins[i].Y, 32, 32);if (playerRect.Intersects(coinRect)){coins.RemoveAt(i);score 10;}}}protected override void Draw(GameTime gameTime){GraphicsDevice.Clear(Color.CornflowerBlue);_spriteBatch.Begin();// 画金币foreach (Vector2 coinPos in coins){_spriteBatch.Draw(coinTexture, coinPos, Color.White);}// 画玩家_spriteBatch.Draw(playerTexture,playerPosition,null,Color.White,0f,new Vector2(playerTexture.Width / 2, playerTexture.Height / 2),1f,SpriteEffects.None,0f);// ★ 画文字_spriteBatch.DrawString(font, $分数{score}, new Vector2(10, 10), Color.White);_spriteBatch.DrawString(font, $金币{coins.Count}, new Vector2(10, 35), Color.Gold);_spriteBatch.DrawString(font, WASD/方向键 移动 | ESC 退出, new Vector2(10, 570), Color.LightGray);_spriteBatch.End();base.Draw(gameTime);}}}本节课新内容加载字体FontStashSharp 方式var fontSystem new FontSystem(); fontSystem.AddFont(File.ReadAllBytes(Content/consola.ttf)); font fontSystem.GetFont(18);只需要三行代码直接读.ttf文件不需要.spritefont文件不需要 MGCB Editor字号18可以随便改画文字_spriteBatch.DrawString(font, 内容, new Vector2(X, Y), Color.White);最后使用F5运行。好了这节课到此结束我叫魔法阵维护师关注我下期更精彩
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2633633.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!