簡體   English   中英

在單機游戲中使用鼠標單擊在屏幕上分配值到2D數組

[英]Assigning values to a 2D array , using mouse clicks on a screen at monogame

我正在使用Monogame為C#開發的游戲開發關卡編輯器。 有一個2D矩陣代表地圖本身,每個像元是一個64像素單位。 加載background時,坐標顯示在屏幕頂部,請參見下面的示例。 首先,默認情況下,每個單元的2D矩陣設置為0。 我需要的是,使用鼠標左鍵單擊屏幕上的對象,以為矩陣中與之對應的單元格設置一個不同的值。 這就是我在游戲中定義障礙的方式,以后將其定義為玩家擊中障礙時的碰撞。 例如,要定義一個天花板,我需要在屏幕頂部的同一行中單擊另一個對象。 例如,對於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);
            }

        }

    }

您需要執行以下操作:

  • 檢測點擊並獲取鼠標光標的位置
  • 將位置的X和Y坐標轉換為2D數組的索引
  • 索引數組並根據需要修改值

首先,您需要使用Mouse.GetState獲取鼠標的狀態。

然后,您可以像這樣檢測鼠標單擊:

var mouseState = Mouse.GetState();

if (mouseState.LeftButton == ButtonState.Pressed)
{
    // do something here
}

mouseState具有XY成員,這使您可以將鼠標光標相對於游戲窗口左上角的位置。

您需要將此位置轉換為數組索引。 為此,將X和Y分量除以圖塊的大小-在您的情況下為64:

var xIndex = mouseState.X / 64;
var yIndex = mouseState.Y / 64;

然后,您可以根據需要修改數組:

map[xIndex, yIndex] = 1;

您還需要做一些邊界檢查,以確保您計算出的索引實際上在數組中。

下限檢查很簡單-只需檢查xIndexyIndex是否大於零即可。

對於上限檢查,請使用GetLength屬性獲取數組每個維度的長度。 然后斷言xIndexyIndex小於那些值:

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

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM