C# 在monogame的屏幕上使用鼠标单击为2D数组赋值

C# 在monogame的屏幕上使用鼠标单击为2D数组赋值,c#,xna,monogame,mouse-position,C#,Xna,Monogame,Mouse Position,我正在为我的游戏开发一个关卡编辑器,由C#开发,使用Monogame。 有一个2D矩阵表示贴图本身,每个单元格是一个64像素的单元。 加载背景时,坐标显示在屏幕顶部,请参见下面的示例。 首先,默认情况下,每个单元的2D矩阵设置为0。 我需要的是,用鼠标左键单击屏幕上的对象 按钮,为矩阵中与其对应的单元格设置不同的值。 这就是我在游戏中定义障碍物的方式,稍后将定义为玩家在击中障碍物时发生的碰撞。 例如,要定义天花板,我需要在同一行中逐个单击屏幕顶部的多个对象。 例如,对于640X192背景图像,它

我正在为我的游戏开发一个关卡编辑器,由C#开发,使用Monogame。 有一个2D矩阵表示贴图本身,每个单元格是一个64像素的单元。 加载背景时,坐标显示在屏幕顶部,请参见下面的示例。 首先,默认情况下,每个单元的2D矩阵设置为0。 我需要的是,用鼠标左键单击屏幕上的对象 按钮,为矩阵中与其对应的单元格设置不同的值。 这就是我在游戏中定义障碍物的方式,稍后将定义为玩家在击中障碍物时发生的碰撞。 例如,要定义天花板,我需要在同一行中逐个单击屏幕顶部的多个对象。 例如,对于640X192背景图像,它的矩阵如下所示 (因为width是width/64,height是height/64)

有关屏幕和坐标,请参见此处的示例: 非常感谢你的帮助

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

        }

    }
类游戏1:游戏
{
图形管理器图形;
SpriteBatch SpriteBatch;
纹理2D游戏背景;
SpriteFont字体;
int[,]图;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=1920;
graphics.PreferredBackBufferHeight=1080;
graphics.GraphicsProfile=GraphicsProfile.HiDef;
}
受保护的覆盖void LoadContent()
{
this.IsMouseVisible=true;
gameBackround=Content.Load(“level_01_A”);//这是背景
map=newint[gameBackround.Width/64,gameBackround.Height/64];
font=Content.Load(“字体/字体”);
spriteBatch=新spriteBatch(图形设备);
}          
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
spriteBatch.Draw(游戏背景,新矩形(0,-70038401984),彩色,白色);
DrawGrid(地图、spriteBatch、字体);
基础。抽签(游戏时间);
spriteBatch.End();
}
公共虚空DrawGrid(int[,]游戏地图,SpriteBatch SpriteBatch,SpriteFont f)
{
对于(intx=0;x
您需要执行以下操作:

  • 检测单击并获取鼠标光标的位置
  • 将位置的X和Y坐标转换为二维阵列的索引
  • 为数组编制索引,并根据需要修改值
首先,您需要使用获取鼠标的状态

然后,您可以检测鼠标点击,如下所示:

var mouseState = Mouse.GetState();

if (mouseState.LeftButton == ButtonState.Pressed)
{
    // do something here
}
mouseState
具有
X
Y
成员,可获取鼠标光标相对于游戏窗口左上角的位置

您需要将此位置转换为数组索引。为此,请将X和Y分量除以平铺的大小-64(在您的示例中):

var xIndex = mouseState.X / 64;
var yIndex = mouseState.Y / 64;
然后,您可以根据需要修改阵列:

map[xIndex, yIndex] = 1;
您还需要进行一些边界检查,以确保您计算的索引实际位于数组中

下限检查很简单-只要检查
xIndex
yIndex
是否大于零即可

对于上限检查,使用属性获取数组每个维度的长度。然后断言
xIndex
yIndex
小于这些值:

if (xIndex >= 0 && xIndex < map.GetLength(0) && yIndex >= 0 && yIndex < map.GetLength(1)) {
  map[xIndex][yIndex] = 1;
}
希望有帮助

if (xIndex >= 0 && xIndex < map.GetLength(0) && yIndex >= 0 && yIndex < map.GetLength(1)) {
  map[xIndex][yIndex] = 1;
}
var mouseState = Mouse.GetState();

if (mouseState.LeftButton == ButtonState.Pressed)
{
  var xIndex = mouseState.X / 64;
  var yIndex = mouseState.Y / 64;

  if (xIndex >= 0 && xIndex < map.GetLength(0) && yIndex >= 0 && yIndex < map.GetLength(1)) {
    map[xIndex][yIndex] = 1;
  }
}