代码之家  ›  专栏  ›  技术社区  ›  Jxyjimmy

当使用pygame生成生物群落时,使用perlin噪声的最佳方式是什么。此外,我将如何在特定区域生成结构?

  •  0
  • Jxyjimmy  · 技术社区  · 1 年前

    我正试图制作一款生存游戏,但我对佩林的噪音有问题。我的程序提供了以下信息:

    enter image description here

    但我想要一些像岛屿或河流之类的东西。

    这是我的代码:

    #SetUp#
    import pygame, sys, random
    pygame.init()
    win = pygame.display.set_mode((800, 600))
    pygame.display.set_caption('Isom')
    x = 0
    y = 0
    s = 0
    tilel = list()
    random.seed(5843)
    MAP = [random.randint(0, 1) for _ in range(192)]
    
    #Tiles#
    class tile():
        grass = pygame.image.load('Sprites/Images/Grass.png')
        water = pygame.image.load('Sprites/Images/Water.png')
    
    #Loop#
    while True:
        for key in pygame.event.get():
            if key.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        #World#
        for a in range(12):
            for b in range(16):
                if MAP[s] == 0:
                    win.blit((tile.grass), (x, y))
                elif MAP[s] == 1:
                    win.blit((tile.water), (x, y))
                x += 50
                s += 1
            x = 0
            y += 50
        x = 0
        y = 0
        s = 0
        #Update#
        pygame.display.update()
    
    0 回复  |  直到 3 年前
        1
  •  2
  •   king conor    2 年前

    image of randomly genrated terrain pygame中的perlin噪声代码:

      from PIL import Image
      import numpy as np
      from perlin_noise import PerlinNoise
      import random
      import pygame
      pygame.init()
    
      noise = PerlinNoise(octaves=6, seed=random.randint(0, 100000))
      xpix, ypix = 500, 500
      pic = [[noise([i/xpix, j/ypix]) for j in range(xpix)] for i in range(ypix)]
    
    
    
      screen = pygame.display.set_mode ((500, 500),  pygame.RESIZABLE)
      while True:
            for event in pygame.event.get():
                  if event.type == pygame.QUIT:
                      pygame.quit()
                      run = False
            for i, row in enumerate(pic):
                  for j, column in enumerate(row):
                        if column>=0.6:
                              pygame.draw.rect(screen, (250, 250, 250), pygame.Rect(j, i, 1, 1))
                        elif column>=0.2:
                              pygame.draw.rect(screen, (80, 80, 80), pygame.Rect(j, i, 1, 1))                 
                        elif column>=0.09:
                              pygame.draw.rect(screen, (30, 90, 30), pygame.Rect(j, i, 1, 1))
                        elif column >=0.009:
                              pygame.draw.rect(screen, (10, 100, 10), pygame.Rect(j, i, 1, 1))
                        elif column >=0.002:
                              pygame.draw.rect(screen, (100, 150, 0), pygame.Rect(j, i, 1, 1))
                        elif column >=-0.06:
                              pygame.draw.rect(screen, (30, 190, 0), pygame.Rect(j, i, 1, 1))
                        elif column >=-0.02:
                              pygame.draw.rect(screen, (40, 200, 0), pygame.Rect(j, i, 1, 1))
                        elif column >=-0.1:
                              pygame.draw.rect(screen, (10, 210, 0), pygame.Rect(j, i, 1, 1))
                        elif column >=-0.8:
                              pygame.draw.rect(screen, (0, 0, 200), pygame.Rect(j, i, 1, 1))
    
            #------------
            #run the game class
    
            pygame.display.update()
      pygame.quit()
      quit()
    
    
     
    
        2
  •  0
  •   PoliszOwl    3 年前

    我建议安装 noise 包裹
    然后使用 noise.pnoise1(x) 对于一维Perlin噪声, noise.pnoise2(x, y) 对于二维Perlin噪声,以及 noise.pnoise3(x, y, z) 对于三维Perlin噪声。

        3
  •  0
  •   KdotJPG    3 年前

    首先,批判性思维:Perlin是一个流行的术语,但实际的“Perlin”噪声算法很旧,而且明显是平方对齐的。一般来说,最好使用Simplex类型的噪波。

    我建议PyFastNoiseLite: https://github.com/tizilogic/PyFastNoiseLite 按照安装说明进行操作,然后在此处镜像FastNoiseLite文档中的C++示例: https://github.com/Auburn/FastNoiseLite/tree/master/Cpp 一定要注意它的内部倍频,你可以改变它 SetFrequency(f)

    也可以将Python噪波库用于Simplex类型的噪波,带有噪波 snoise2(x, y) 不过如果你想使用 snoise3(x, y, z) 我首先考虑以下信息: https://www.reddit.com/r/proceduralgeneration/comments/qr6snt/countdown_timer_simplex_patent_expiration/