【发布时间】:2019-07-23 14:18:23
【问题描述】:
我正在为我的游戏开发一个关卡编辑器,由 C# 开发,使用 Monogame。 有一个代表地图本身的二维矩阵,每个单元格是一个 64 像素单元。 加载背景时,坐标显示在屏幕顶部,请参见下面的示例。 首先,默认情况下,每个单元格的 2D 矩阵设置为 0。 我需要的是,通过使用鼠标左键单击屏幕上的对象 button ,为矩阵中对应的单元格设置不同的值。 这就是我在游戏中定义障碍物的方式,稍后将定义为玩家在击中它们时的碰撞。 例如,要定义天花板,我需要在屏幕顶部的同一行中一个一个地单击多个对象。 例如,对于 640X192 背景图像,矩阵看起来像下面的示例 (因为 width 是 width/64 ,而 height 是 height/64 )
{
{1,1,1,1,1,1,1,1,1,1}
{0,0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0,0}
}
有关屏幕和坐标,请参见此处的示例: https://imgur.com/a/BcwfwpH 非常感谢您的帮助!
class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D gameBackround;
SpriteFont font;
int[,] map ;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 1920;
graphics.PreferredBackBufferHeight = 1080;
graphics.GraphicsProfile = GraphicsProfile.HiDef;
}
protected override void LoadContent()
{
this.IsMouseVisible = true;
gameBackround = Content.Load<Texture2D>("level_01_A");//this is the background
map = new int[gameBackround.Width / 64, gameBackround.Height / 64];
font = Content.Load<SpriteFont>("Fonts/Font");
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(gameBackround, new Rectangle(0, -700, 3840, 1984), Color.White);
DrawGrid(map, spriteBatch, font);
base.Draw(gameTime);
spriteBatch.End();
}
public void DrawGrid(int[,] gameMap, SpriteBatch spriteBatch, SpriteFont f)
{
for (int x = 0; x < gameMap.GetLength(1); x++)
{
for (int y = 0; y < gameMap.GetLength(0); y++)
{
spriteBatch.DrawString(f, x + " / " + y, new Vector2(x * 64, y * 64), Color.White);
}
}
}
【问题讨论】:
标签: c# xna monogame mouse-position