python - 我使用 OpenSimplex 错了吗
问题描述
所以我尝试使用 OpenSimplex 噪声来生成 2D 地形,但我似乎遗漏了一些东西,因为地形看起来大多是随机的。我究竟做错了什么?这是代码:
import pygame
from opensimplex import OpenSimplex
tmp = OpenSimplex()
pygame.init()
display_width = 800
display_height = 600
black = (40,40,40)
gameDisplay = pygame.display.set_mode((display_width,display_height))
gameDisplay.fill(black)
gameDisplay.convert()
clock = pygame.time.Clock()
dimensions = [100,100]
size = 40
def mapping(x):
y = (x + 1) * 10 + 40
return y
class GroundCell:
def __init__(self,x,y,dim):
self.x = x
self.y = y
self.dim = dim
tempcells = []
allCells = []
for a in range(0,dimensions[0]):
tempcells = []
for b in range(0,dimensions[1]):
tempcells.append(GroundCell(a*size,b*size,mapping(tmp.noise2d(a,b))))
allCells.append(tempcells)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
for a in allCells:
for b in a:
pygame.draw.rect(gameDisplay,(b.dim,b.dim,b.dim),(b.x,b.y,size,size))
pygame.display.update()
clock.tick(120)
gameDisplay.fill(black)
这是产生噪音的图像
解决方案
你必须得到整数之间的值,这会“平滑”结果,否则你会得到普通的噪音。所以你将不得不改变这一行:
tempcells.append(GroundCell(a*size,b*size,mapping(tmp.noise2d(a,b))))
像这样:
tempcells.append(GroundCell(a*size,b*size,mapping(tmp.noise2d(a*0.1,b*0.1))))
推荐阅读
- mongodb - Strict (2048): MongodbSource::value() 的声明
- xml - 如何从返回字符串中提取特定字段
- python - scipy splrep 是否为 t 提供了错误的最后一个元素?
- python-3.x - 编辑文件中的特定字典
- javascript - ngb 下拉菜单 - 滚动到活动项目
- angular - 子应用程序延迟加载在单水疗中心的角度微前端内
- android - 不可点击的第二个列表视图。如何改变它
- ios - OSStatus 错误:[-25291] 没有可用的钥匙串。您可能需要重新启动计算机
- node.js - 如果提供了函数参数,则在 MongoDB 中应用过滤器
- python - 绝对导入包中的python ModuleNotFoundError错误?