예제 #1
0
def main():
    os.environ['SDL_VIDEO_WINDOW_POS'] = '400,200'
    SCREEN_WIDTH = 640
    SCREEN_HEIGHT = 480
    SCREEN_BACKGROUND = 232, 232, 232

    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
                                  pygame.DOUBLEBUF)
    pygame.display.set_caption('Pywander - save the world in less than 9 times')

    clock = pygame.time.Clock()
    board = StartupBoard()
    sound = SoundObject()
    sound.play('background', -1)

    while board is not None:
        surface.fill(SCREEN_BACKGROUND)

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.locals.KEYDOWN and \
               event.key == pygame.locals.K_ESCAPE:
                sys.exit(0)

        board = board.update(surface, events)

        fps = clock.get_fps()
        fps_label = LabelObject('FPS: %d' % fps, 14)
        fps_label.change_realign('bottom-left', left=15, bottom=25)
        fps_label.draw_on_surface(surface)

        pygame.display.flip()
        clock.tick(200)
예제 #2
0
def main():
    os.environ['SDL_VIDEO_WINDOW_POS'] = '400,200'
    SCREEN_WIDTH = 640
    SCREEN_HEIGHT = 480
    SCREEN_BACKGROUND = 232, 232, 232

    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
                                      pygame.DOUBLEBUF)
    pygame.display.set_caption(
        'Pywander - save the world in less than 9 times')

    clock = pygame.time.Clock()
    board = StartupBoard()
    sound = SoundObject()
    sound.play('background', -1)

    while board is not None:
        surface.fill(SCREEN_BACKGROUND)

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.locals.KEYDOWN and \
               event.key == pygame.locals.K_ESCAPE:
                sys.exit(0)

        board = board.update(surface, events)

        fps = clock.get_fps()
        fps_label = LabelObject('FPS: %d' % fps, 14)
        fps_label.change_realign('bottom-left', left=15, bottom=25)
        fps_label.draw_on_surface(surface)

        pygame.display.flip()
        clock.tick(200)
예제 #3
0
    def process_draw_on_surface(self, surface):
        time_before = self.background_time
        if self.background_time != 0:
            self.background_time = pygame.time.get_ticks()
            elapsed = self.background_time - time_before
        else:
            self.background_time = pygame.time.get_ticks()
            elapsed = 0
        self.background_x -= int(elapsed * self.background_speed)
        if self.background_x < -self.background_width:
            self.background_x += self.background_width
        for i in range(3):
            self.background.rect.left = int(self.background_x) + (self.background_width * (i - 1))
            self.background.draw_on_surface(surface, self)

        score_label = LabelObject('Score: %d' % self.score, 14)
        score_label.change_realign('top-right', right=15, top=15)
        score_label.draw_on_surface(surface, self)

        life_label = LabelObject('Life: %d' % self.life, 14)
        life_label.change_realign('top-left', left=15, top=15)
        life_label.draw_on_surface(surface, self)

        life_label = LabelObject('Attempts left: %d' % self.attempts_left, 14)
        life_label.change_realign('top-center', top=15)
        life_label.draw_on_surface(surface, self)

        for bullet in self.bullets_group.sprites():
            if bullet.image.rect.left >= 640:
                self.bullets_group.remove(bullet)
            else:
                bullet.draw_on_surface(surface, self)

        self.ship.draw_on_surface(surface, self)

        for enemy in self.enemy_group.sprites():
            if isinstance(enemy, EnemySprite):
                if enemy.is_completed():
                    self.enemy_group.remove(enemy)
            enemy.draw_on_surface(surface, self)

        for inactive in self.inactive_group.sprites():
            if isinstance(inactive, EnemySprite):
                if inactive.is_completed():
                    self.inactive_group.remove(inactive)
                    continue
            inactive.draw_on_surface(surface, self)

        for hit in pygame.sprite.groupcollide(self.enemy_group, self.bullets_group, 0, 1):
            if isinstance(hit, AsteroidSprite):
                self.score += 9

            if isinstance(hit, BulletSprite):
                hit.kill()

            if isinstance(hit, BossSprite):
                self.score += 13
                hit.hit()
                self.boss_life = hit.life
                if hit.life <= 0:
                    self.status = PLAYER_WON
                    return False

            if isinstance(hit, EnemySprite):
                self.score += 7
                hit.kill()
                hit.show_explosion()
                sound = SoundObject()
                sound.play('boom', 0)
                self.inactive_group.add(hit)

        hit = pygame.sprite.spritecollideany(self.ship, self.enemy_group)
        if hit:
            fatal = isinstance(hit, AsteroidSprite) or isinstance(hit, BossSprite)
            if not fatal:
                hit.kill()

                if isinstance(hit, EnemySprite):
                    hit.show_explosion()
                    sound = SoundObject()
                    sound.play('boom', 0)

                self.life -= 25
            else:
                self.status = PLAYER_LOSE
        else:
            self.read_level_info()

        if self.attempts_left == 0 or self.life == 0:
            self.status = PLAYER_LOSE

        buffer = pygame.image.tostring(surface, 'RGB')
        self.buffer = buffer
        self.buffer_size = surface.get_size()
예제 #4
0
    def process_draw_on_surface(self, surface):
        time_before = self.background_time
        if self.background_time != 0:
            self.background_time = pygame.time.get_ticks()
            elapsed = self.background_time - time_before
        else:
            self.background_time = pygame.time.get_ticks()
            elapsed = 0
        self.background_x -= int(elapsed * self.background_speed)
        if self.background_x < -self.background_width:
            self.background_x += self.background_width
        for i in range(3):
            self.background.rect.left = int(
                self.background_x) + (self.background_width * (i - 1))
            self.background.draw_on_surface(surface, self)

        score_label = LabelObject('Score: %d' % self.score, 14)
        score_label.change_realign('top-right', right=15, top=15)
        score_label.draw_on_surface(surface, self)

        life_label = LabelObject('Life: %d' % self.life, 14)
        life_label.change_realign('top-left', left=15, top=15)
        life_label.draw_on_surface(surface, self)

        life_label = LabelObject('Attempts left: %d' % self.attempts_left, 14)
        life_label.change_realign('top-center', top=15)
        life_label.draw_on_surface(surface, self)

        for bullet in self.bullets_group.sprites():
            if bullet.image.rect.left >= 640:
                self.bullets_group.remove(bullet)
            else:
                bullet.draw_on_surface(surface, self)

        self.ship.draw_on_surface(surface, self)

        for enemy in self.enemy_group.sprites():
            if isinstance(enemy, EnemySprite):
                if enemy.is_completed():
                    self.enemy_group.remove(enemy)
            enemy.draw_on_surface(surface, self)

        for inactive in self.inactive_group.sprites():
            if isinstance(inactive, EnemySprite):
                if inactive.is_completed():
                    self.inactive_group.remove(inactive)
                    continue
            inactive.draw_on_surface(surface, self)

        for hit in pygame.sprite.groupcollide(self.enemy_group,
                                              self.bullets_group, 0, 1):
            if isinstance(hit, AsteroidSprite):
                self.score += 9

            if isinstance(hit, BulletSprite):
                hit.kill()

            if isinstance(hit, BossSprite):
                self.score += 13
                hit.hit()
                self.boss_life = hit.life
                if hit.life <= 0:
                    self.status = PLAYER_WON
                    return False

            if isinstance(hit, EnemySprite):
                self.score += 7
                hit.kill()
                hit.show_explosion()
                sound = SoundObject()
                sound.play('boom', 0)
                self.inactive_group.add(hit)

        hit = pygame.sprite.spritecollideany(self.ship, self.enemy_group)
        if hit:
            fatal = isinstance(hit, AsteroidSprite) or isinstance(
                hit, BossSprite)
            if not fatal:
                hit.kill()

                if isinstance(hit, EnemySprite):
                    hit.show_explosion()
                    sound = SoundObject()
                    sound.play('boom', 0)

                self.life -= 25
            else:
                self.status = PLAYER_LOSE
        else:
            self.read_level_info()

        if self.attempts_left == 0 or self.life == 0:
            self.status = PLAYER_LOSE

        buffer = pygame.image.tostring(surface, 'RGB')
        self.buffer = buffer
        self.buffer_size = surface.get_size()