【发布时间】:2021-06-19 16:40:14
【问题描述】:
我的计划是让Perlin noise 生成二维地形, 我在互联网上四处寻找解决方案,但要么无法理解代码,要么尝试过但没有成功。
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)
这段代码提供了一个漂亮的数组,但我的其余代码不适用于它。我收到的错误消息是 Invalid 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])
【问题讨论】:
-
仅仅说“我的代码不能使用它”是不够的信息。请详细描述这是真实的情况,并包括正在发生的任何错误消息。
标签: python arrays numpy pygame perlin-noise