0

我需要创建带有精灵列表的类,这些精灵列表是网格列表中的第 1 号特征。但我需要拆分窗口到 len(grid), len(grid[0]) 我在 WallTile 类中的表现如何。但是我需要在网格中为 1 的那个地方用瓷砖填充窗口。这将是一个迷宫般的瓷砖。我想使用精灵,因为我需要使用与瓷砖墙的碰撞检测。

width, height = 700, 700  #window size

grid = [[0, 1, 0, 0, 0, 0, 0],
       [0, 1, 0, 1, 1, 1, 0],
       [0, 1, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 1],
       [0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 0, 0],
       [0, 1, 0, 1, 1, 1, 1]]



class WallTile(pygame.sprite.Sprite):
    def __init__(self):
        super(WallTile, self).__init__()
        self.TILE_W = width / len(grid)     # spliting window by len(grid) and set size of one tile
        self.TILE_H = height / len(grid[0])
        self.tiles_x = width / self.TILE_W    # size of all tiles
        self.tiles_y = height / self.TILE_H
        self.imageMaster = pygame.image.load("images/wallBig.png")
        self.image = self.imageMaster
        self.pozX = 0.0
        self.pozY = 0.0
        self.image = pygame.transform.scale(self.imageMaster, (self.TILE_W, self.TILE_H))
        self.rect = self.image.get_rect()
        self.rect.center = (self.pozX, self.pozY)



        # here I need fill some sprite list with tiles with different coordinates.
            # I want to put tile on place where is number 1 in grid. But put it to screen.

        for y in range(self.tiles_y):
            for x in range(self.tiles_x):
                for i in range(len(grid[0])):
                    for j in range(len(grid)):
                        if grid[i][j] is 1:
                                # I need make new tile and put it to sprite list

                                self.pozX, self.pozY = (x * self.TILE_W), (y * self.TILE_H)

                                #
                                #--- here I dont know what to do...
                                #

class MainClass:
    def Main(self):

            # here is how Im drawing sprites on screen.

            screen = pygame.display.set_mode(size)
            screen.fill((grey))


            wallTile = WallTile()
            spriteGroup_1 = pygame.sprite.Group(wallTile)

            background = pygame.Surface(obrazovka.get_size())
            background.fill((grey))
            background.set_colorkey((60,0,0))

            running = True
            while running:
                for event in pygame.event.get():
                    if event.type == QUIT or \
                        event.type == KEYDOWN and (event.key in [K_ESCAPE, K_q]):  
                            running = False


                        spriteGroup_1.clear(screen, background)
                        spriteGroup_1.update()
                        updateScreen = spriteGroup_1.draw(screen)

                        pygame.display.update(updateScreen)
                        pygame.display.flip()




if __name__ == '__main__':
    mainWin = MainClass()
    sys.exit((mainWin.Main()))

真的很感谢您的建议。

4

1 回答 1

0

我首先将您的 WallTile 类重新定义为仅代表一个瓷砖。WallTile 的构造函数现在接受一个大小和位置参数,它们表示它在屏幕上的大小和位置。

class WallTile(pygame.sprite.Sprite):
    def __init__(self, size, location):
        pygame.sprite.Sprite.__init__(self)
        self.size = size
        self.location = location
        self.image = pygame.image.load("images/wallBig.png")
        self.image = pygame.transform.scale(self.image, size)
        self.rect = self.image.get_rect()

然后,在 WallTile 类的代码之外,您可以检查网格中是否存在“1”,并在找到时创建一个新图块。您可以查看网格上该元素的索引,并将这些值乘以每个图块的大小,以确定该图块在屏幕上的位置。然后,您可以获取该图块列表并使用它们创建一个 Sprite 组。

tile_size = (20, 20)
tiles = list()
for y in range(len(grid)):
    for x in range(len(grid[y])):
        if grid[x][y] == 1:            
            new_tile = WallTile(tile_size, (x * tile_size[0], y * tile_size[1]))
            tiles.append(new_tile)

tile_group = pygame.sprite.Group(tiles)

如果您想从组中拉出特定的图块,并且您还没有对该图块的引用,则可以遍历该组并查找某个位置

search_location = (40, 80)
for tile in tile_group:
    if tile.location = search_location:
        # we've found that tile, now do something with it
于 2013-04-23T21:14:05.053 回答