【问题标题】:Monogame - Generating a tile mapMonogame - 生成平铺地图
【发布时间】:2016-05-03 20:36:19
【问题描述】:

我正在创建一个 2D 游戏,我希望能够绘制图块来创建我的游戏世界。我创建了一个List,如下所示:

static private List<Rectangle> tiles = new List<Rectangle>();

public const int TileWidth = 50;
public const int TileHeight = 50;

public static void initialise(Texture2D tileTexture)
{
        texture = tileTexture;
        tiles.Clear();

        tiles.Add(new Rectangle(0, 267, TileWidth, TileHeight));
        tiles.Add(new Rectangle(1575,795, TileWidth, TileHeight));
        tiles.Add(new Rectangle(1315,267, TileWidth, TileHeight));
        tiles.Add(new Rectangle(1051,797, TileWidth, TileHeight));
    }

我正在尝试以一种允许我使用数组手动选择放置每个图块的位置的方式将图块生成到我的游戏世界中,但是我在这方面遇到了一些困难。

【问题讨论】:

  • 更明确一点,描述你的问题并展示你的尝试。

标签: c# .net xna monogame


【解决方案1】:

我猜你想要一个双数组的瓷砖?我会继续猜测你在这里想要实现的目标。

可能是这样的:

private static Rectangle[][] tiles;
private const int worldWidth = 50;
private const int worldDepth = 50;

public static void Initialize(Texture2D tileTexture)
{
    // Initialize game table
    tiles = new Rectangle[worldWidth][];
    for (int l = 0; l < worldWidth; l++)
    {
        tiles[l] = new Rectangle[worldDepth];
    }

    // Fill rectangles in for every tile
    for(int i = 0; i < worldWidth; i++)
    {
        for (int k = 0; k < worldDepth; k++)
        {
            // Tile (0,0): x = 0, y = 0
            // Tile (0,1): x = 0, y = 50
            // and so on...
            tiles[i][k] = new Rectangle(i * TileWidth, k * TileHeight, TileWidth, TileHeight);
        }
    }
}

可能有更好的方法可以做到这一点,但这是一个简单的解决方案,应该很容易理解。

【讨论】:

  • 是的,我创建了一个名为“tiles”的矩形列表,从我拥有的精灵表中保存了我的瓷砖的位置。我想用二维数组绘制瓷砖,但只在地图的某些部分,因为我的目标是地牢爬行者/迷宫风格。非常感谢任何帮助。
【解决方案2】:

你可以像这样绘制矩形列表

for(int i = 0; i < tiles.Count(); i++)
    spriteBatch.Draw(tileImage, tiles[i], Color.White);

请提供有关您的错误/挣扎的更多信息,我会提供更好的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多