【问题标题】:Python random seed for Perlin noisePerlin 噪声的 Python 随机种子
【发布时间】:2016-12-03 21:13:43
【问题描述】:

我最近一直在寻找为游戏制作程序生成的地形。我看到 Perlin 噪声对此很有用,所以我试了一下。至此,地形生成得很漂亮。但是,每当我多次运行该程序时,地形都是完全相同的。有没有办法随机化生成的 Perlin 噪声?

代码:

from opensimplex import OpenSimplex
import random
from time import time

height = 40
width = height
scale = height / 10

value = [[0 for x in range(width)] for y in range(height)]

gen = OpenSimplex()
def noise(nx, ny):
    # Rescale from -1.0:+1.0 to 0.0:1.0
    return gen.noise2d(nx, ny) / 2.0 + 0.5

def printBiome(y, x):
  if value[y][x] <= 2:
    print('O', end = " ")
  elif value[y][x] >= 8:
    print('M', end = " ")
  else:
    print('L', end = " ")

for y in range(height):
    for x in range(width):
        nx = x/width - 0.5
        ny = y/height - 0.5
        value[y][x] = 10 * noise(1 * scale * nx, 1 * scale * ny) +  0.5 * noise(2 * scale * nx, 2 * scale* ny) + 0.25 * noise(4 * scale * nx, 4 * scale * ny)

for y in range(height):
    for x in range(width):
        printBiome(y, x)
    print()

【问题讨论】:

    标签: python terrain perlin-noise procedural-generation


    【解决方案1】:

    OpenSimplex 类defaults to using seed=0。要生成不同的地形,请输入不同的种子值:

    import uuid
    # http://stackoverflow.com/a/3530326/190597
    seed = uuid.uuid1().int>>64
    gen = OpenSimplex(seed=seed)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2012-09-10
      • 2014-02-15
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2021-06-30
      相关资源
      最近更新 更多