[Python] How to make more diversity generation with wave function collapse?

Discussão em 'Python' iniciado por Stack, Junho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a procedural generation project using the Wave Function Collapse algorithm in Python with Pygame. While the current implementation generates patterns, I am looking to enhance the diversity and complexity of the generated output. I would appreciate insights on how to improve the algorithm to achieve this goal.

    import pygame
    import sys
    import random

    def sprite_sheet_cutter(path,width,height,size):
    imgs = []
    sprite_sheet = pygame.transform.scale_by(pygame.image.load(path),size)
    sheet_width,sheet_height = sprite_sheet.get_size()
    for x in range(0,sheet_width,width):
    imgs.append(sprite_sheet.subsurface((x,0,width,height)))
    return tuple(imgs)

    Neighbor_Cell = ((-1,0),(0,-1),(0,1),(1,0))

    def Rule_exceuss(list1,Rule):
    return_list = []
    for i in list1:
    if i in Rule:
    return_list.append(i)
    return return_list


    class Game():
    def __init__(self):
    pygame.mixer.init()
    pygame.init()
    self.screen = pygame.display.set_mode((832, 672))
    self.clock = pygame.time.Clock()
    self.running = True

    Title1 = sprite_sheet_cutter("Image/Grass.png",16,16,1)
    Title2 = sprite_sheet_cutter("Image/Shore.png",16,16,1)

    self.assets = {
    "High_Grass":(Title1[2],("High_Grass","Low_Grass")),
    "Low_Grass":(Title1[1],("Low_Grass","High_Grass","Sand")),
    "Sand":(Title2[0],("Low_Grass","Sand","Shore_Water")),
    "Shore_Water":(Title2[1],("Sand","Shore_Water","Sea_Water")),
    "Sea_Water":(Title2[2],("Sea_Water,Shore_Water,Low_Water")),
    "Low_Water":(Title2[3],("Sea_Water","Low_Water","Deep_Water")),
    "Deep_Water":(Title2[4],("Low_Water","Deep_Water")),
    }

    self.Cellist = {}
    self.Node_list = {(800,16):WFC_Node((800,16),self,["High_Grass"])
    }

    def Run(self):
    while self.running:
    self.screen.fill((255,255,255))
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    self.running = False

    try:
    self.WFC(min(self.Node_list.values(),key=lambda x: len(x.option)))
    except:
    print("Done")

    for Cell in self.Cellist.values():
    Cell.Render(self.screen)


    pygame.display.flip()
    self.clock.tick(60)

    pygame.mixer.quit()
    pygame.quit()
    sys.exit()


    def WFC(self,cell):
    if len(cell.option) > 1:
    print(cell.option)
    cell.option = [random.choice(cell.option)]
    self.Cellist[cell.pos] = cell.Create_Cell()

    for i in Neighbor_Cell:
    pos = (cell.pos[0] +i[0]*16,cell.pos[1] +i[1]*16)
    if 0 <= pos[0] < 832 and 0 <= pos[1] < 672 and pos not in self.Cellist:
    if pos not in self.Node_list.keys():
    self.Node_list[pos] = WFC_Node(pos,self)
    self.Node_list[pos].option = Rule_exceuss(self.Node_list[pos].option,self.assets[cell.option[0]][1])
    elif len(self.Node_list[pos].option) > 1:
    self.Node_list[pos].option = Rule_exceuss(self.Node_list[pos].option,self.assets[cell.option[0]][1])

    self.Node_list.pop(cell.pos)


    class WFC_Node:
    def __init__(self,pos,game,option = ["High_Grass","Low_Grass","Sand","Shore_Water","Sea_Water","Low_Water","Deep_Water"]):
    self.option = option
    self.game = game
    self.pos = pos

    def Create_Cell(self):
    return Cell(self.pos,self.game.assets[self.option[0]][0])


    class Cell:
    def __init__(self,pos,sprite):
    self.pos = pos
    self.sprite = sprite

    def Render(self,surf):
    surf.blit(self.sprite,self.pos)

    Game().Run()


    I have implemented a basic WFC algorithm that generates patterns based on a set of predefined rules and options. However, the generated patterns lack the desired diversity and not optimized

    Continue reading...

Compartilhe esta Página