Ejemplo n.º 1
0
class InvadersGame:

    def __init__(self):
        self.game = Game()
        floorframe = Frame(self.game.screen, Rect(0, 542, MAX_X + 36, 596))
        self.floor = TiledMap(self.game, floorframe)
        self.floor.fill_map('#', (25, 2))
        self.player = Player(self.game)
        self.aliens = Group()
        self.create_aliens()
        self.shots = Group()
        self.alien_shots = Group()

    def create_aliens(self):
        for i in range(4):
            for j in range(20):
                direction = [LEFT, RIGHT][i % 2]
                alien = Alien(self.game, Vector(j * 32 + 32, i * 64), direction)
                self.aliens.add(alien)

    def shoot(self):
        shot = self.player.get_shot()
        self.shots.add(shot)

    def draw(self):
        self.game.screen.clear()
        self.player.draw()
        self.shots.draw(self.game.screen.display)
        self.alien_shots.draw(self.game.screen.display)
        self.aliens.draw(self.game.screen.display)
        self.floor.draw()

    def update(self):
        self.player.move()
        self.shots.update()
        self.alien_shots.update()
        self.aliens.update()
        for a in self.aliens:
            shot = a.get_shot()
            if shot:
                self.alien_shots.add(shot)
        self.draw()
        groupcollide(self.shots, self.aliens, True, True, collided=collide_mask)
        if spritecollideany(self.player, self.alien_shots, collided=collide_mask):
            self.game.exit()
        if not self.aliens:
            self.game.exit()

    def run(self):
        self.game.event_loop(figure_moves=self.player.set_direction,
            draw_func=self.update, keymap={K_SPACE: self.shoot}, delay=30)
Ejemplo n.º 2
0
class MiniGame:

    def __init__(self):
        self.game = Game()

        self.map = TiledMap(self.game)
        self.map.fill_map('#', (10, 10))
        self.map.set_map(MAZE)
        self.map.set_tile((4,4), 'a')
        self.game.frame.print_text("Hello World", (32, 330))
        self.sprite = Sprite(self.game, 'b.pac_right', (1, 1), speed=4)
        self.game.event_loop(figure_moves=self.move, draw_func=self.draw)

    def draw(self):
        self.sprite.move()
        self.map.draw()
        self.sprite.draw()

    def move(self, direction):
        self.sprite.add_move(direction)