2

我正在尝试使用 pygame 创建“城市地图”。我希望能够将建筑物的图像放在特定的网格坐标中,而不仅仅是用颜色填充它们。

这就是我创建此地图网格的方式:

def clear():
    for r in range(rows):
        for c in range(rows):
            if r%3 == 1 and c%3 == 1:
                color = brown;
                grid[r][c] = 1;
            else:
                color = white;
                grid[r][c] = 0; 
            pygame.draw.rect(screen, color, [(margin+width)*c+margin, (margin+height)*r+margin, width, height])         
    pygame.display.flip();

现在我如何将建筑物的图像放在那些特定位置的棕色网格中?我已经在网上尝试了一些示例,但似乎无法让它们工作。任何帮助表示赞赏。

如果有人有我可以用于 pygame 的免费精灵的良好来源,请告诉我。谢谢!

这是我创建的网格图

4

1 回答 1

6

Here's an example I wrote. It uses a numpy to store tile data. The code plus image is downloadable at: http://code.google.com/p/ninmonkey/source/.../maptiles (If that fails, use this url: https://storage.googleapis.com/google-code-archive-source/v2/code.google.com/ninmonkey/source-archive.zip )

  • Press S to toggle mouse scrolling.
  • Map.tiles is a 2d numpy.array that stores tile's id

tileset demo

If you want to add units/sprites that are on top of the tilemap: (Your buildings might count as this)

  • store location using world coordinates
  • draw them using a world2screen coordinate function.

The function will add the map offset to their world coordinates. So if the map offset is (-100,0) and your unit's world coordinate is (0,0), you render at (-100,0) placing them a the top left part of the map, as expected, even though you are scrolled.

于 2012-09-30T21:29:59.457 回答