python和libtcod:使用perlin nois生成地形

2024-09-30 08:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现了一篇关于世界一代的好文章,可以找到here.

它很好地描述了需要做什么,但是我很难弄清楚如何用python完成它。我相信是在as3里做的,但我不确定。无论如何,我还为perlin noise找到了libtcod函数:

noise2d=libtcod.noise_new(2)
value = libtcod.noise_turbulence_perlin(noise2d,[0.5,0.7],32.0)

我真的不知道如何实现这一点,然后根据高度分配字符。在

如果有人能帮我把文章中的步骤翻译成python,我会非常感激的。谢谢


Tags: 函数new高度herevalue文章世界步骤
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:07

这是我几年前写的一个类,它为我的rogulelike原型DomeriaRL创建一个世界地图。相关部分在^{} method。在

它将噪波应用于高度贴图,将其规格化,并基于高度贴图的值创建平铺。在

我使用颜色贴图来创建从一种瓷砖到另一种瓷砖的平滑过渡。在

from constants.constant import *
from world import World
from worldtile import WorldTile

class WorldGenerator(object):
  """Randomly generates a new world with terrain and objects"""

  def regular(self):
    """Randomly generate a new world with some water, swamps, hills, some objects etc"""

    idx = [ 0 , 15, 75, 90, 101 ] # indexes of the keys 
    col = [ T.Color(0,100,100),
            T.Color(0,75,0), 
            T.Color(50,150,0), 
            T.Color(150,120,80), 
            T.Color(180,180,180)]

    map=T.color_gen_map(col,idx)

    tiles = zip(idx, [[SWAMP, PLAINS],
                      [PLAINS, FOREST], 
                      [HILLS, FOREST],
                      [HILLS, HILLS, MOUNTAINS],
                      [HILLS, MOUNTAINS,MOUNTAINS]])

    world = self.__generate(map, tiles)
    return world

  def __generate(self, colormap, mtiles, noise_zoom=1, noise_octaves=10):
    hm = T.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
    hm1 = T.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
    hm2 = T.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)

    noise = T.noise_new(2)
    T.heightmap_add_fbm(hm1, noise, noise_zoom, noise_zoom, 0.0, 0.0, noise_octaves, 0.0, 1.0)
    T.heightmap_add_fbm(hm2, noise, noise_zoom*2, noise_zoom*2, 0.0, 0.0, noise_octaves/2, 0.0, 1.0)

    T.heightmap_multiply_hm(hm1, hm2, hm)
    T.heightmap_normalize(hm, mi=0.0, ma=1)

    tiles = {}

    # 0...100 -> value from noised heightmap
    for x in xrange(0, 101):
      lower = [c for c in mtiles if c[0] <= x][-1] # tile is between lower and upper color
      upper = [c for c in mtiles if c[0] > x][0]

      # calculating percentage
      lower_c = x - lower[0]
      upper_c = upper[0] - x
      count = lower_c + upper_c

      tiles[x] = colormap[x], lower[1], int(upper_c * 1.0 / count * 100), upper[1] 

    # generate world grid
    grid = []
    for x in xrange(WORLD_WIDTH):
      grid.append([])
      for y in xrange(WORLD_HEIGHT):
        hm_v = T.heightmap_get_value(hm, x, y)
        grid[x].append(WorldTile(*tiles[int(hm_v * 100)]))

    T.heightmap_delete(hm)
    T.heightmap_delete(hm1)
    T.heightmap_delete(hm2)

    objects = []
    while len(objects) < 5:
      for x in xrange(WORLD_WIDTH):
        for y in xrange(WORLD_HEIGHT):
          r = random.randrange(0, 10000)
          pos = [wo for wo in WORLDOBJECTS if wo.chance >= r and grid[x][y].tile in wo.tiles]
          if pos:
            o = random.choice(pos).create(x, y)
            objects.append(o)

    return World(grid, objects)

示例世界地图如下所示:

{1美元^

请注意,这个游戏使用libtcod1.5.1b1,一些函数名在较新版本中发生了变化。在

相关问题 更多 >

    热门问题