我需要创建具有精灵列表的类,这些精灵列表在网格列表中表征为数字1。但我需要拆分窗口到len(网格),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()))
非常感谢您的建议。
答案 0 :(得分:0)
我首先要重新定义你的WallTile类,只代表一个tile。 WallTile的构造函数现在接受一个size和location参数,它表示它在屏幕上的大小和位置。
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”,并在找到新的tile时创建一个新的tile。您可以查看网格上该元素的索引,并将这些值乘以每个切片的大小,以确定该切片在屏幕上的位置。然后你可以拿出那个瓷砖列表并用它们创建一个Sprite Group。
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