如何使用 pygame 将图像加载到网格,而不是仅使用填充颜色? [英] how to load an image to a grid using pygame, instead of just using a fill color?

查看:75
本文介绍了如何使用 pygame 将图像加载到网格,而不是仅使用填充颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a "map of a city" using pygame. I want to be able to put images of buildings in specific grid coords rather than just filling them in with a color.

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

This is how I am creating this map grid:

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();

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

Now how do I put images of buildings in those brown colored grids at those specific locations? I've tried some of the samples online but can't seem to get them to work. Any help is appreciated.

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

If anyone have a good source for free sprites that I can use for pygame, please let me know. Thanks!

推荐答案

这是我写的一个例子.它使用 numpy 来存储瓦片数据.代码加图像可在以下位置下载:http://code.google.com/p/ninmonkey/source/.../maptiles(如果失败,请使用此网址:https://storage.googleapis.com/google-code-archive-source/v2/code.google.com/ninmonkey/source-archive.zip )

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 )

  • S 切换鼠标滚动.
  • Map.tiles 是一个 2d numpy.array,用于存储 tile 的 id
  • Press S to toggle mouse scrolling.
  • Map.tiles is a 2d numpy.array that stores tile's id

如果您想添加位于瓷砖地图顶部的单位/精灵:(您的建筑物可能算作此)

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

  • 使用世界坐标的商店位置
  • 使用 world2screen 坐标函数绘制它们.
  • store location using world coordinates
  • draw them using a world2screen coordinate function.

该函数会将地图偏移量添加到它们的世界坐标中.因此,如果地图偏移量是 (-100,0) 并且您单位的世界坐标是 (0,0),那么您将在 (-100,0) 渲染,将它们放置在地图的左上角部分,正如预期的那样,即使您被滚动.

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.

这篇关于如何使用 pygame 将图像加载到网格,而不是仅使用填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆