[Python] 纯文本查看 复制代码 from PIL import Image
import random
def create_mining_map(width=100, height=200):
# 定义颜色
colors = {
'地面': (139, 69, 19), # 棕色(地面)
'煤矿': (0, 0, 0), # 黑色(煤矿)
'铁矿': (192, 192, 192), # 银色(铁矿)
'金矿': (255, 215, 0), # 金色(金矿)
'钻石': (0, 255, 255), # 青色(钻石)
'石头': (169, 169, 169), # 灰色(石头)
'障碍': (128, 128, 128) # 深灰色(障碍)
}
# 创建一个新的 RGB 图像
img = Image.new('RGB', (width, height), 'white')
pixels = img.load() # 访问像素值
# 填充图像
for y in range(height):
for x in range(width):
if y == 0: # 地面
pixels[x, y] = colors['地面']
elif y % 20 == 1: # 每20个像素有一个煤矿
pixels[x, y] = colors['煤矿']
elif y % 20 == 2: # 每20个像素有一个铁矿
pixels[x, y] = colors['铁矿']
elif y % 20 == 3: # 每20个像素有一个金矿
pixels[x, y] = colors['金矿']
elif y % 20 == 4: # 每20个像素有一个钻石
pixels[x, y] = colors['钻石']
elif y % 20 == 5: # 每20个像素有一个石头
pixels[x, y] = colors['石头']
elif y % 20 == 6: # 每20个像素有一个障碍
pixels[x, y] = colors['障碍']
else: # 默认填充
pixels[x, y] = colors['石头'] # 主要是石头
# 生成随机生成的资源
for y in range(1, height):
if random.random() < 0.05: # 5% 的概率放置随机资源
x = random.randint(0, width - 1)
resource = random.choice(['煤矿', '铁矿', '金矿', '钻石'])
pixels[x, y] = colors[resource]
# 保存图像
img.save("mining_map.png")
if __name__ == "__main__":
create_mining_map()
print("挖矿地图已生成:mining_map.png")
|