pygame世界生成算法

2024-06-26 13:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pygame为一个学校项目制作一个游戏,当时正在努力制作一个世界生成算法。我想知道是否有人能帮我。对于任何想知道这款游戏是Terreira游戏的翻版的人来说,如果有帮助的话


Tags: 项目算法游戏世界pygame学校terreira
1条回答
网友
1楼 · 发布于 2024-06-26 13:44:19

在pygame中制作地图的一种方法是使用瓷砖,但我发现这是一项巨大的工作,因为您首先必须学习如何使用它。另一种更常见的方法是创建一个二维列表,表示地图中每个点的xy,具体取决于您缩放地图的方式。首先,让我们谈谈地图创建:

# lets say 0 represents empty space
# 1 represents a block image u might have for example dirt image or a wall

    layout = [ [1, 0, 2],
               [0, 1, 1] ]

然后,要实际绘制地图,您可以在该列表上循环,首先是列表中的每个层,然后是该层中的每个元素,如下所示:

 y = 0
    for layer in layout:
        x = 0
        for element in layer:
           if element == 1:
               D.blit(your image, (x *  block_length, y * block_height))
           if element == 2:
               D.blit(your image, (x *  other_block_length, y * other_block_height)) 
           else: # else do nothing, which  leaves the area empty
               pass
       x += 1 # add 1 to x every inner loop
     y += 1 # add 1 to y value every outer loop

下面是我的一个游戏中的一个例子

class Game_map:
    def __init__(self):
        self.land = pygame.image.load(r"C:\Users\pro-gramar\OneDrive\Documents\A level python codes\final game\land.png").convert()
        self.height = 200
        self.width = 200
        self.land = pygame.transform.scale(self.land, (170 , 200))
        self.land.set_colorkey((0, 0, 0))

        self.map_width = 6000
        self.map_height = 2000

        # 0 = emepty
        # 1 = land

        self.layout = [[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0 ,0 ,0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,0 ,0 ,0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 ],
                              [ 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]]



    def draw(self):
        y = 0
        for layer in self.layout:
            x = 0
            for land in layer:
                if land == 1:
                    D.blit(self.land, (x * 160 ,  y * 200 ))
                if land == 0:
                    pass

                x += 1
            y += 1 

相关问题 更多 >