如何使Python中的Perlin噪声模块使用种子?

huangapple go评论65阅读模式
英文:

How do I make the perlin-noise module for python use a seed?

问题

我正在尝试在pygame中的一个游戏中添加无限生成功能以下是我遇到问题的代码部分

```python
from perlin_noise import PerlinNoise

global world, xpix, chunkSize # 设置全局变量
chunkSize = (12, 12)
xpix, ypix = chunkSize[0], chunkSize[1]
world = []
noise1 = PerlinNoise(octaves=octaves) # 生成噪声

for i in range(xpix): # 生成绘制器使用的列表
    row = []
    for j in range(ypix):
        noise_val = noise1([i / xpix + chunkCoordX * xpix, j / ypix + chunkCoordY * ypix])

        if noise_val <= .05:
            tiletoplace = tileclassdata.water
        elif noise_val <= .13:
            tiletoplace = tileclassdata.sand
        else:
            tiletoplace = tileclassdata.grass
        placed_tile = classes.tile(tiletoplace, i, j)
        row.append(placed_tile)
    world.append(row)

我的问题是,如果我去相同的坐标,我不能看到相同的区块超过两次,因为它每次生成一个区块时都会选择一个新的种子。有没有办法强制它对每个区块使用相同的种子?

我尝试查找此插件的文档,但未能找到,因此我查看了其他人提出的一些问题,但没有解决我的问题。我也尝试过不使用Perlin_Noise,而是使用Python的noise模块,但我已经尝试了几个小时,但还没有弄清楚如何操作。


<details>
<summary>英文:</summary>

I&#39;m trying to add infinite generation to a game I&#39;m working on in pygame. Here is the code I am having trouble with:


from perlin_noise import PerlinNoise

global world, xpix, chunkSize #set globals
chunkSize = (12, 12)
xpix, ypix = chunkSize[0], chunkSize[1]
world = []
noise1 = PerlinNoise(octaves=octaves) #make noise

for i in range(xpix): # make list for drawer to use
    row = []
    for j in range(ypix):
        noise_val = noise1([i / xpix + chunkCoordX * xpix, j / ypix + chunkCoordY * ypix])

        if noise_val &lt;= .05:
            tiletoplace = tileclassdata.water
        elif noise_val &lt;= .13:
            tiletoplace = tileclassdata.sand
        else:
            tiletoplace = tileclassdata.grass
        placed_tile = classes.tile(tiletoplace, i, j)
        row.append(placed_tile)
    world.append(row)

My problem is that I can&#39;t see the same chunk more twice if I go to the same coordinate because it picks a new seed every time it generates a chunk. Is there a way I can force it to use the same seed for every chunk?


I have tried finding documentation for this plugin but I am unable to so I have looked at other questions some people have asked and none of them solve my problem. I have not been able to find any kind of solution. I have also tried not use Perlin_Noise but using python&#39;s noise module which I have tried for hours but not figured out how to operate.

</details>


# 答案1
**得分**: 0

构造函数 `PerlinNoise` 接受一个名为 `seed` 的参数:

```python
PerlinNoise(octaves=3.5, seed=777)

请参考 README 中的第 3 行。

英文:

The constructor PerlinNoise takes a parameter called seed:

PerlinNoise(octaves=3.5, seed=777)

cf. line 3 in the README of the python module.

huangapple
  • 本文由 发表于 2023年7月10日 22:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定