我希望在pygame中创建一个场景,但我似乎必须在每个子画面和障碍物中单独渲染,随着场景变大,它变得有点......重复。
self.tree01 = Obstacles(config.tree01, 0, 10, self.player.group)
self.tree02 = Obstacles(config.tree01, 1, 10, self.player.group)
self.tree03 = Obstacles(config.tree01, 2, 10, self.player.group)
self.tree04 = Obstacles(config.tree01, 3, 10, self.player.group)
self.tree05 = Obstacles(config.tree01, 4, 10, self.player.group)
self.tree06 = Obstacles(config.tree01, 5, 10, self.player.group)
self.tree07 = Obstacles(config.tree01, 6, 10, self.player.group)
self.tree08 = Obstacles(config.tree01, 7, 10, self.player.group)
self.tree09 = Obstacles(config.tree01, 8, 10, self.player.group)
self.tree10 = Obstacles(config.tree01, 9, 10, self.player.group)
self.tree11 = Obstacles(config.tree01, 10, 10, self.player.group)
self.tree12 = Obstacles(config.tree01, 11, 10, self.player.group)
self.tree13 = Obstacles(config.tree01, 12, 10, self.player.group)
self.tree14 = Obstacles(config.tree01, 13, 10, self.player.group)
self.tree15 = Obstacles(config.tree01, 14, 10, self.player.group)
self.tree16 = Obstacles(config.tree01, 15, 10, self.player.group)
self.tree17 = Obstacles(config.tree01, 16, 10, self.player.group)
这就是我当前创建对象的方式,并且...这不可能是正确的。我的班级在这里。有没有办法以简化的方式创建上述所有这些对象及其特定属性?我曾想过在 for 循环中创建它们,但这并不能为它们提供单独的属性。
class Obstacles(pygame.sprite.Sprite):
def __init__(self,image, x, y, player):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.player = player
self.rect.x = x * config.scale
self.rect.y = y * config.scale
self.check_collision = False
self.rect.topleft = self.rect.x, self.rect.y
self.groupObs = pygame.sprite.Group()
self.groupObs.add(self)
self.check_collide()
def draw(self, screen):
self.groupObs.draw(screen)
self.rect.topleft = self.rect.x, self.rect.y
炎炎设计
相关分类