如何编写适合我创建的地图模块的平台游戏生成器?

问题描述

我最近一直在忙于 pygame 并且遇到了一个问题。我正在编写一个平台游戏,并为地图创建提供了一个类。在地图模块上,我有一个函数可以根据运动创建块 - 特别是相机的“滚动”。我的问题是我的游戏地图被划分在一个 16x16 的网格上,而我的平台图块是 48x16 像素,这可能是我网格上的 3 个图块。我真的不知道如何让我的代码理解它是 3 个瓷砖,所以我可以随着玩家的上升随机生成平台。如果您知道如何实施,请告诉我。

import pygame,sys,random
from pygame.locals import *

class Map:
    def __init__(self):
        self.path_img = pygame.image.load("assets/imgs/path.png").convert()
        self.walls_img = pygame.image.load("assets/imgs/walls.png").convert()
        self.mossy_wall_img = pygame.image.load("assets/imgs/mossy_wall.png").convert()
        self.game_map = {}
        self.TILE_SIZE = self.walls_img.get_width()
        self.CHUNK_SIZE = 8
        self.tile_index = {1:self.path_img,2:self.walls_img,3:self.mossy_wall_img}

        self.platforms = []


    def draw_map(self,screen,scroll):
        self.tile_rects = []
        
        for y in range(3):
            for x in range(4):
                self.target_x = x - 1 + int(round(scroll[0]/(self.CHUNK_SIZE*16)))
                self.target_y = y - 1 + int(round(scroll[1]/(self.CHUNK_SIZE*16)))
                self.target_chunk = str(self.target_x) + ';' + str(self.target_y)
                if self.target_chunk not in self.game_map:
                    self.game_map[self.target_chunk] = self.generate_chunk(self.target_x,self.target_y)
                for tile in self.game_map[self.target_chunk]:
                    screen.blit(self.tile_index[tile[1]],(tile[0][0]*16-scroll[0],tile[0][1]*16-scroll[1]))
                    if tile[1] in [1,2,3,4]:
                        self.tile_rects.append(pygame.Rect(tile[0][0]*16,tile[0][1]*16,16,16))    
                
                

    def generate_chunk(self,x,y):
        self.chunk_data = []
        platform = False

        for y_pos in range(self.CHUNK_SIZE):
            for x_pos in range(self.CHUNK_SIZE):
                self.target_x = x * self.CHUNK_SIZE + x_pos 
                self.target_y = y * self.CHUNK_SIZE + y_pos
                tile_type = 0

                if self.target_x > 30:
                    if random.randint(1,5) == 1:
                        tile_type = 3
                    else:
                        tile_type = 2

                if self.target_x < 0:
                    if random.randint(1,5) == 1:
                        tile_type = 3
                    else:
                        tile_type = 2

                if self.target_y > 10:
                    if random.randint(1,5) == 1:
                        tile_type = 3
                    else:
                        tile_type = 2

                if self.target_y == 10:

                    if self.target_x > 30 or self.target_x < 0: 
                        if random.randint(1,5) == 1:
                            tile_type = 3
                        else:
                            tile_type = 2
                    else:
                        tile_type = 1

                if self.target_y < 10 and self.target_x < 30 and self.target_x > 0:

                    if random.randint(1,100) == 1:
                        platform = True


                if tile_type != 0:
                    self.chunk_data.append([[self.target_x,self.target_y],tile_type])

                if platform:
                    self.chunk_data.append([[self.target_x,1])
                    self.chunk_data.append([[self.target_x + 1,1])
                    self.chunk_data.append([[self.target_x + 2,1])

                    
        return self.chunk_data

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)