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

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

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

问题

  1. 我正在尝试在pygame中的一个游戏中添加无限生成功能以下是我遇到问题的代码部分
  2. ```python
  3. from perlin_noise import PerlinNoise
  4. global world, xpix, chunkSize # 设置全局变量
  5. chunkSize = (12, 12)
  6. xpix, ypix = chunkSize[0], chunkSize[1]
  7. world = []
  8. noise1 = PerlinNoise(octaves=octaves) # 生成噪声
  9. for i in range(xpix): # 生成绘制器使用的列表
  10. row = []
  11. for j in range(ypix):
  12. noise_val = noise1([i / xpix + chunkCoordX * xpix, j / ypix + chunkCoordY * ypix])
  13. if noise_val <= .05:
  14. tiletoplace = tileclassdata.water
  15. elif noise_val <= .13:
  16. tiletoplace = tileclassdata.sand
  17. else:
  18. tiletoplace = tileclassdata.grass
  19. placed_tile = classes.tile(tiletoplace, i, j)
  20. row.append(placed_tile)
  21. world.append(row)

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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

  1. for i in range(xpix): # make list for drawer to use
  2. row = []
  3. for j in range(ypix):
  4. noise_val = noise1([i / xpix + chunkCoordX * xpix, j / ypix + chunkCoordY * ypix])
  5. if noise_val &lt;= .05:
  6. tiletoplace = tileclassdata.water
  7. elif noise_val &lt;= .13:
  8. tiletoplace = tileclassdata.sand
  9. else:
  10. tiletoplace = tileclassdata.grass
  11. placed_tile = classes.tile(tiletoplace, i, j)
  12. row.append(placed_tile)
  13. world.append(row)
  1. 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?
  2. 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.
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. 构造函数 `PerlinNoise` 接受一个名为 `seed` 的参数:
  7. ```python
  8. PerlinNoise(octaves=3.5, seed=777)

请参考 README 中的第 3 行。

英文:

The constructor PerlinNoise takes a parameter called seed:

  1. 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:

确定