【问题标题】:Assigning values to a 2D array , using mouse clicks on a screen at monogame在 monogame 的屏幕上使用鼠标单击将值分配给 2D 数组
【发布时间】: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


    【解决方案1】:

    您需要执行以下操作:

    • 检测点击并获取鼠标光标的位置
    • 将位置的 X 和 Y 坐标转换为二维数组的索引
    • 索引数组并根据需要修改值

    首先您需要使用Mouse.GetState 获取鼠标的状态。

    然后您可以像这样检测鼠标点击:

    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 是否大于零。

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

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

    在你的Update 方法中这样的东西应该可以工作:

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

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 2015-04-07
      • 2011-09-12
      • 1970-01-01
      • 2021-06-25
      • 2017-11-15
      • 2011-05-27
      相关资源
      最近更新 更多