我正在尝试生成一个大的平铺地图,然后将在屏幕上显示。地图应该是随机的,目前只包含两种类型的图块。
然而,现在发生的事情是Tile Map的每一行都被高度图的第一行覆盖,因此我的地图在显示时会显示类似图块的直线列。
我相信这是因为我写了我的for循环,它迭代整个高度图,甚至到达第二个tile列表。虽然由于我尝试包含的某些随意的磁贴选择元素,但应该至少有一些噪声引入到地图中,但似乎没有任何噪声。
以下是我运行代码时显示内容的屏幕截图的链接。
import pygame, random
class Map(object):
MAPWIDTH = 64
MAPHEIGHT = 48
HEIGHTS = [0, 1, 2, 3, 4, 5, 6, 7, 8]
def __init__(self):
self.heightmap = [[random.choice(self.HEIGHTS) for w in range(self.MAPWIDTH)] for h in range(self.MAPHEIGHT)]
self.Tiles = [['' for w in range(self.MAPWIDTH)] for h in range(self.MAPHEIGHT)]
for rows in self.Tiles:
for row in self.heightmap:
i = 0
for height in row:
# 100% water block
if height == 0:
rows[i] = 'WATER'
# 70% water block
if height == range(1, 3):
if random.randint(0, 9) == range(0, 6):
rows[i] = 'WATER'
else:
rows[i] = 'GRASS'
# 50% water block
if height == 4:
if random.random() == 0:
rows[i] = 'WATER'
else:
rows[i] = 'GRASS'
# 80% grass block
if height == range(5, 7):
if random.randint(0, 9) == range(0, 6):
rows[i] = 'GRASS'
else:
rows[i] = 'WATER'
# 100% grass block
if height == 8:
rows[i] = 'GRASS'
i += 1
答案 0 :(得分:0)
self.heightmap = [[random.choice(self.HEIGHTS) for w in range(self.MAPWIDTH)] for h in range(self.MAPHEIGHT)]
self.Tiles = [['' for w in range(self.MAPWIDTH)] for h in range(self.MAPHEIGHT)]
我不完全确定你要用它做什么,但试试这个:
self.heightmap=[]
self.Tiles=[]
q=[]
for w in range(self.MAPWIDTH):
for h in range(self.MAPHEIGHT): q.append(random.choice(self.HEIGHTS))
self.heightmap.append(q)
q=[]
for w in range(self.MAPWIDTH):
for h in range(self.MAPHEIGHT): q.append('')
self.Tiles.append(q)
q=[]
for rows in self.Tiles:
for row in self.heightmap:
i = 0
for height in row:
rows.pop(i)
# 100% water block
if height == 0:
rows.insert(i,'WATER')
# 70% water block
if height in range(1, 3):
if random.randint(0, 9) in range(0, 6):
rows.insert(i,'WATER')
else:
rows.insert(i,'GRASS')
# 50% water block
if height == 4:
if random.random() > 0.5:
rows.insert(i,'WATER')
else:
rows.insert(i,'GRASS')
# 80% grass block
if height in range(5, 7):
if random.randint(0, 9) in range(0, 6):
rows.insert(i,'GRASS')
else:
rows.insert(i,'WATER')
# 100% grass block
if height == 8:
rows.insert(i,'GRASS')
i += 1
答案 1 :(得分:0)
你写了很多行,如:
if height == range(1, 3):
但这并不符合你的想法。
range(1, 3)
返回列表[1, 2]
(如果您使用的是python3,则返回产生1
和2
的迭代),并且永远不会等于单个整数
您可能想要使用如下模式:
if height == 1 or height == 2
或
if 1 <= height <= 2:
或
if height in range(1, 2):
尽管如此,您可以通过创建一个辅助函数来简化代码,该函数返回给定高度的草图块的可能性:
def chance_of_grass(height):
if height == 0: return 0
elif height <= 3: return 30
elif height <= 4: return 50
elif height <= 7: return 80
return 100
并在你的循环中调用它:
def __init__(self):
self.heightmap = [[random.choice(self.HEIGHTS) for w in range(self.MAPWIDTH)] for h in range(self.MAPHEIGHT)]
self.Tiles = [['' for w in range(self.MAPWIDTH)] for h in range(self.MAPHEIGHT)]
r_i = 0 # index of row
for h_row in self.heightmap:
c_i = 0 # index of column
for height in row:
is_grass = change_of_grass(height) >= random.randint(1, 100)
self.Tiles[r_i][c_i] = 'GRASS' if is_grass else 'WATER'
c_i += 1
r_i += 1