我一直在研究python中的terrin生成器,该生成器应该生成高度图。我希望它看起来像this。
上面所说的代码如何产生类似于this的结果。 我想知道是否有人能告诉我代码为什么会产生奇数对角线,因为我一生无法弄清楚它...
import random as rand
from PIL import Image
class QuickMap(object):
def __init__(self, x, y):
self.size = (x, y)
self.randomNoise = [[0 for cl in range(self.size[1])] for rw in range(self.size[0])]
self.cNoise = [[0 for cl in range(self.size[1])] for rw in range(self.size[0])]
def makeStart(self, init_num, scale):
for i in range(init_num):
randPos = (rand.randint(1, self.size[0]-1), rand.randint(1, self.size[1]-1))
self.randomNoise[randPos[0]][randPos[1]] = rand.randrange(0, scale)
self.cNoise[randPos[0]][randPos[1]] = self.randomNoise[randPos[0]][randPos[1]]
def gen_map(self, scale, pascle):
for rw in range(1, self.size[0]-1):
for cl in range(1, self.size[1]-1):
for x in range(-1, 2):
for y in range(-1, 2):
if self.randomNoise[rw+x][cl+y] < self.randomNoise[rw][cl]:
if float(self.randomNoise[rw+y][cl+x]).is_integer():
randVar = rand.randint(1, 10)
if randVar % 2 == 1:
self.randomNoise[rw+y][cl+x] = (self.randomNoise[rw][cl] - 3)
#print('\n\n\n\n\n')
for rw in range(0, self.size[0]):
for cl in range(0, self.size[1]):
self.cNoise[rw][cl] = self.randomNoise[rw][cl]
def smooth(self, smoothing_itterations, scale):
pscale = scale
self.gen_map(scale, pscale)
for i in range(smoothing_itterations):
for rw in range(self.size[0]):
for cl in range(self.size[1]):
self.randomNoise[rw][cl] = self.cNoise[rw][cl]
self.cNoise = [[0 for cl in range(self.size[1])] for rw in range(self.size[0])]
scale -= 3
self.gen_map(scale, pscale)
print(i/smoothing_itterations)
def make_picture(self):
img = Image.new('L', (self.size[0], self.size[1]))
dataToWrite = [[0 for cl in range(self.size[1])] for rw in range(self.size[0])]
for rw in range(self.size[0]):
for cl in range(self.size[1]):
dataToWrite[rw][cl] = self.cNoise[rw][cl] % 256
for rw in range(self.size[0]):
for cl in range(self.size[1]):
img.putpixel((rw, cl), int(dataToWrite[rw][cl]))
img.save('Output.png')
qm = QuickMap(100, 100)
qm.makeStart(16, 256)
qm.smooth(0, 256)
qm.make_picture()
谢谢!