【问题标题】:How can I get a 2D tile output from perlin-noise?如何从 perlin-noise 获得 2D 瓦片输出?
【发布时间】: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


【解决方案1】:

Surface.blit 想要一个矩形作为area 参数。你传递一个字符串。

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

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 转换为字符串。

【讨论】:

    猜你喜欢
    • 2012-08-20
    • 2015-03-20
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多