1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
import numpy as np
import matplotlib.pyplot as plt
from noise import pnoise2
import matplotlib.colors
from PIL import Image
# 地图尺寸和每个格子的像素大小
size = 25
pixel_per_cell = 10
# 噪声缩放
scale = 25.0
# 生成噪声地图
noise_map = np.zeros((size, size))
for i in range(size):
for j in range(size):
noise_map[i][j] = pnoise2(i / scale, j / scale, octaves=6, persistence=0.5, lacunarity=2.0, repeatx=size, repeaty=size, base=42)
def get_terrain_type(value):
# 调整这些阈值以匹配实际所需的地形比例
if value < 0: # 示例阈值,需要根据实际调整
return '平原'
elif value < 0.15: # 示例阈值,需要根据实际调整
return '丘陵'
else:
return '高山'
def get_landform_type(terrain):
rand = np.random.rand()
if terrain == '平原':
if rand < 0.1875: # 7.5% 沙漠
return '沙漠'
elif rand < 0.1875 + 0.625: # 25% 草原
return '草原'
else: # 7.5% 沼泽,总和应为100%
return '沼泽'
elif terrain == '丘陵':
if rand < 0.175: # 15% 荒山
return '荒山'
elif rand < 0.575 + 0.175: # 15% 树林
return '树林'
else: # 10% 丛林,总和应为40%
return '丛林'
elif terrain == '高山':
if rand < 0.5: # 10% 土山
return '土山'
elif rand < 0.5 + 0.375: # 7.5% 石山
return '石山'
else: # 2.5% 雪山,总和应为20%
return '雪山'
# 创建地形地图和地貌地图
terrain_map = [[get_terrain_type(value) for value in row] for row in noise_map]
landform_map = [[get_landform_type(terrain) for terrain in row] for row in terrain_map]
# 颜色映射
color_map = {
'沙漠': '#F4E1A0',
'草原': '#A4DE02',
'沼泽': '#2F6666',
'荒山': '#B2A280',
'树林': '#228B22',
'丛林': '#006400',
'土山': '#8B4513',
'石山': '#808080',
'雪山': '#FFFFFF'
}
# 创建一个空白的图片
image = np.zeros((size * pixel_per_cell, size * pixel_per_cell, 3), dtype=np.uint8)
# 填充颜色
for i in range(size):
for j in range(size):
color = matplotlib.colors.to_rgb(color_map[landform_map[i][j]])
for k in range(pixel_per_cell):
for l in range(pixel_per_cell):
image[i * pixel_per_cell + k][j * pixel_per_cell + l] = [int(c * 255) for c in color]
# 保存图片
img = Image.fromarray(image)
img.save('/mnt/c/Users/blackhao/Desktop/landform_map.png')
|