从零开发游戏需要学习的c#模块,第十七章(显示真正的图片——精灵绘制)

第一步:准备图片

先用一张简单的来做测试。把你准备好的图片命名为 player.png

第二步:把图片加入项目

  1. 在 Visual Studio 右侧"解决方案资源管理器"里,找到 Content 文件夹

  2. 右键 Content添加现有项

  3. 选择你的 player.png 文件

  4. 选中刚添加的 player.png,在下方属性窗口里,确认:

    • 生成操作 = Content

    • 复制到输出目录 = 如果较新则复制

第三步:完整代码

打开 Game1.cs完整替换为:

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

namespace MY_FIRST_GAME

{

public class Game1 : Game

{

private GraphicsDeviceManager _graphics;

private SpriteBatch _spriteBatch;

private Texture2D playerTexture;

private Vector2 playerPosition;

private float playerSpeed = 200f;

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);

// ★ 加载图片:用 Content.Load<Texture2D>("文件名不带后缀")

playerTexture = Content.Load<Texture2D>("player");

}

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;

if (keyboard.IsKeyDown(Keys.Escape))

Exit();

base.Update(gameTime);

}

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

_spriteBatch.Begin();

// ★ 画图片:纹理, 位置, 颜色(Color.White = 原色)

_spriteBatch.Draw(playerTexture, playerPosition, Color.White);

_spriteBatch.End();

base.Draw(gameTime);

}

}

}

其中新知识

cs 复制代码
playerTexture = Content.Load<Texture2D>("player");

其中

  • Content.Load<T>() 是 MonoGame 的资源加载器

  • "player" 是文件名不带后缀

  • 图片必须放在 Content 文件夹里

cs 复制代码
_spriteBatch.Draw(playerTexture, playerPosition, Color.White);
  • 第一个参数:纹理(你的图片)

  • 第二个参数:位置(Vector2

  • 第三个参数:颜色滤镜(Color.White = 原样显示)

注意一下本节课要准备的东西

准备一张你喜欢的32*32的图片,可以用piskel制作

好了,本节课的内容到此结束,关注我,下期更精彩。

相关推荐
金銀銅鐵3 天前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏
金銀銅鐵4 天前
借助 Pygame 探索最大公约数的规律
python·数学·游戏
nujnewnehc8 天前
不会 py, 用 ai 写了个游戏辅助的感受
人工智能·游戏
jump_jump9 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
XIAOHEZIcode10 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
Aloys_Code11 天前
逆向一个被遗忘的DVD游戏格式:从DES加密到Rust模拟器
游戏·模拟器·retroarch·复古游戏·native32·sunplus·赤刃·钢铁风暴
金銀銅鐵11 天前
用 Python 实现 Take-Away 游戏
python·游戏
金銀銅鐵12 天前
用 Pygame 实现 15 puzzle
python·数学·游戏
通信小呆呆14 天前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
H__Rick14 天前
自动对焦学习-3
人工智能·学习·计算机视觉