如何使用XNA绘制动态网格

时间:2011-11-06 15:24:55

标签: silverlight windows-phone-7 xna grid

我正在尝试使用XNA框架绘制网格,这个网格在XNA执行期间应该有一个固定的维度,但是应该给用户提供在启动游戏页面之前自定义它的机会(我是使用silverlight / xna模板构建我的应用程序。)

有没有人对如何实现这一目标提出建议?

谢谢

2 个答案:

答案 0 :(得分:1)

设置tileSize,然后在所需网格大小上绘制纹理。

这是一些重写的​​代码。这就是我开始使用二维数组生成一个tilemap的方法。

int tileSize = 32;
Vector2 position = Vector2.Zero;
Texture2D gridTexture;

int[,] map = new int[,]
{
    {1, 1, 0,},
    {0, 1, 1,},
    {1, 1, 0,},
};

然后在绘图功能中添加这样的内容:

for (int i = 0; i <= map.GetUpperBound(0); i++)
{
    for (int j = 0; j <= map.GetUpperBound(1); j++)
    {
        int textureId = map[i, j];
        if (textureId != 0)
        {
            Vector2 texturePosition = new Vector2(i * tileSize, j * tileSize) + position;

            //Here you would typically index to a Texture based on the textureId.
            spriteBatch.Draw(gridTexture, texturePosition, null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);             
        }
    }
}

答案 1 :(得分:1)

    ContentManager contentManager;
    GameTimer timer;
    SpriteBatch spriteBatch;
    LifeGrid life;


    int tileSize = 32;
    Vector2 position = Vector2.Zero;
    Texture2D gridTexture;
    int[,] map;

    public GamePage()
    {
        InitializeComponent();

        // Get the content manager from the application
        contentManager = (Application.Current as App).Content;

        // Create a timer for this page
        timer = new GameTimer();
        //timer.UpdateInterval = TimeSpan.FromTicks(333333);
        timer.UpdateInterval = TimeSpan.Zero;
        timer.Update += OnUpdate;
        timer.Draw += OnDraw;
        List<Position> p = new List<Position>();
        p.Add(new Position(1,1));
        p.Add(new Position(1,4));
        p.Add(new Position(1,5));
        p.Add(new Position(1,6));
        p.Add(new Position(1,7));
        this.life = new LifeGrid(10, 10, p);


        map = new int[,]{{1, 1, 0,},{0, 1, 1,},{1, 1, 0,},};

        // LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);
    }
    /// <summary>
    /// Allows the page to draw itself.
    /// </summary>
    private void OnDraw(object sender, GameTimerEventArgs e)
    {
        // SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
       // SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);
        // Draw the sprite
        spriteBatch.Begin();

        for (int i = 0; i <= map.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= map.GetUpperBound(1); j++)
            {
                int textureId = map[i, j];
                if (textureId != 0)
                {
                    Vector2 texturePosition = new Vector2(i * tileSize, j * tileSize) + position;

                    //Here you would typically index to a Texture based on the textureId.
                    spriteBatch.Draw(gridTexture, texturePosition, null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);

                }
            }
        }


        spriteBatch.End();
    }