Python 如何从柏林噪波中获得2D瓷砖输出?

Python 如何从柏林噪波中获得2D瓷砖输出?,python,arrays,numpy,pygame,perlin-noise,Python,Arrays,Numpy,Pygame,Perlin Noise,我的计划是生成2D地形, 我在互联网上四处寻找解决方案,但要么是看不懂代码,要么是尝试过,但没有成功 import pygame import numpy as np from pygame import * # source images to the game display # Tiling thing from https://markbadiola.com/2012/01/22/rendering-background-image-using-pygame-in-python-2-7

我的计划是生成2D地形, 我在互联网上四处寻找解决方案,但要么是看不懂代码,要么是尝试过,但没有成功

import pygame
import numpy as np
from pygame import *

# source images to the game display
# Tiling thing from https://markbadiola.com/2012/01/22/rendering-background-image-using-pygame-in-python-2-7/

# assigns source image
SOURCE = pygame.image.load('tiles.png')

p = pygame.Rect(288, 0, 32, 32) # pavement
g = pygame.Rect(416, 0, 32, 32) # grass
s = pygame.Rect(288, 160, 32, 32) # sand/dirt
b = pygame.Rect(288, 320, 32, 32) # bush

# matrix containing the pattern of tiles to be rendered

import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)
from scipy.ndimage.interpolation import zoom
arr = np.random.uniform(size=(4,4))
arr = zoom(arr, 8)
arr = arr > 0.5
arr = np.where(arr, '-', '#')
arr = np.array_str(arr, max_line_width=500)
print(arr)
这段代码提供了一个好看的数组,但我的其余代码无法使用它。我收到的错误消息是无效的rectstyle参数。如果我能把“-”和“#”改成g和s,我相信这是可行的,但那也不行

for y in range(len(map1.arr)):
  for x in range(len(map1.arr[y])):
    location = (x * 32, y * 32)
    screen.blit(map1.SOURCE, location, map1.arr[y][x])
需要一个rect作为
区域
参数。你传一串

您可以创建一个映射,将字符串转换为已定义的右矩形,如下所示:

mapping = { '#': s, '-': g }

tile = map1.arr[y][x]
area = mapping[tile]
screen.blit(map1.SOURCE, location, area)
您还希望删除

arr = np.array_str(arr, max_line_width=500)

行,因为这会将
arr
转换为字符串。

仅仅说“我的代码不能使用它”是不够的信息。请详细描述这是怎么回事,并包括正在发生的任何错误消息。非常感谢