我需要创建带有精灵列表的类,这些精灵列表是网格列表中的第 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()))
真的很感谢您的建议。