def update_aliens(infrompy_settings, aliens, screen, ship, alien_explosions,
                  alien_bullets, red_screen):

    for alien in aliens:
        if alien.health <= 0:
            if alien.death_ticks == 1:
                sounds.playsound('explosion2')
                new_explosion1 = Alien_Explosion(screen, alien)
                alien_explosions.add(new_explosion1)
                new_explosion2 = Alien_Explosion(screen, alien)
                alien_explosions.add(new_explosion2)
            if alien.death_ticks == 3:
                new_explosion1 = Alien_Explosion(screen, alien)
                alien_explosions.add(new_explosion1)
                new_explosion2 = Alien_Explosion(screen, alien)
                alien_explosions.add(new_explosion2)
            if alien.death_ticks == 5:
                new_explosion1 = Alien_Explosion(screen, alien)
                alien_explosions.add(new_explosion1)
            elif alien.death_ticks >= 6:
                infrompy_settings.add_score_alien_dead()
                aliens.remove(alien)
    # Upate position of all aliens and check if fleet is at edge
    check_fleet_edges(infrompy_settings, aliens)
    aliens.update()

    # Check alien ship collisions
    if pygame.sprite.spritecollideany(ship, aliens) and ship.invul == False:
        red_screen.turn_on()
        ship.health -= 1
        ship.invul = True
        ship.invul_tick = infrompy_settings.ship_invul_time
        infrompy_settings.ship_health = ship.health
def update_bullets(infrompy_settings, aliens, screen, ship, bullets,
                   bullet_explosions, alien_explosions, alien_bullets):
    # Update positions of bullets and remove old bullets
    # CHeck for bullets that have hit aliens
    # Get rid of bullet and alien

    for bullet in bullets:
        for alien in aliens:
            if pygame.sprite.collide_rect(alien, bullet):
                bullet.exploded = True
                new_bullet_explosion = Bullet_Explosion(screen, bullet)
                bullet_explosions.add(new_bullet_explosion)
                sounds.playsound('explosion1')
                bullets.remove(bullet)
                alien.health -= bullet.damage

    if len(aliens) == 0:
        bullets.empty()
        alien_bullets.empty()
        infrompy_settings.add_score_level_clear()
        infrompy_settings.alien_level += 1
        infrompy_settings.alien_firing_interval = max(
            50, infrompy_settings.alien_firing_interval - 50)
        create_fleet(infrompy_settings, screen, ship, aliens)

    #colisions = pygame.sprite.groupcollide(bullets, aliens, True, False)

    bullets.update()
    # Remove bullets
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)
def update_screen(infrompy_settings, screen, ship, ship_explosions, aliens,
                  bullets, bullet_explosions, alien_explosions, alien_bullets,
                  alien_bullet_explosions, info_board, play_button,
                  play_again_button, red_screen):

    # Redraw old bullets behind aliens and ship
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    for alien_bullet in alien_bullets:
        alien_bullet.draw_alien_bullet()

    ship.blitme()
    aliens.draw(screen)

    if not infrompy_settings.game_active and infrompy_settings.ship_dead == False:
        play_button.draw_button()
    elif infrompy_settings.ship_dead == True:
        play_again_button.draw_button()

    for bullet_explosion in bullet_explosions:
        bullet_explosion.draw_bullet_explosion()

    for alien_bullet_explosion in alien_bullet_explosions:
        alien_bullet_explosion.draw_alien_bullet_explosion()

    for alien_explosion in alien_explosions:
        alien_explosion.draw_alien_explosion()

    for ship_explosion in ship_explosions:
        ship_explosion.draw_ship_explosion()

    # Check redscreen on
    if red_screen.on:
        red_screen.blitme

    # Check whether to shoot:
    if ship.shooting_tick == 2:
        sounds.playsound('laser1')
        new_bullet = Bullet(infrompy_settings, screen, ship, bullet_explosions)
        bullets.add(new_bullet)
    # Check whether aliens shoot
    for alien in aliens:
        if alien.current_tick == alien.firing_tick:
            sounds.playsound('laser2')
            new_alien_bullet = Alien_Bullet(infrompy_settings, screen, ship,
                                            alien)
            alien_bullets.add(new_alien_bullet)
        elif alien.current_tick >= infrompy_settings.alien_start_firing_tick + infrompy_settings.alien_firing_interval:
            alien.current_tick = 0

    info_board.prep_info()
    info_board.display_info()

    #Make most recent screen visible
    pygame.display.flip()
def update_ship(infrompy_settings, screen, ship, ship_explosions):
    ship.update()
    if ship.dead == True:
        ship.moving_right = False
        ship.moving_left = False
        ship.moving_up = False
        ship.moving_down = False
        ship.shoting = False
        ship.death_tick += 1
        if ship.death_tick == 60:
            infrompy_settings.game_active = False
        else:
            sounds.playsound('explosion1')
            new_explosion = Ship_Explosion(screen, ship)
            ship_explosions.add(new_explosion)
def update_alien_bullets(infrompy_settings, aliens, screen, ship,
                         alien_bullets, alien_bullet_explosions, red_screen):

    alien_bullets.update()
    for alien_bullet in alien_bullets:
        if pygame.Rect.colliderect(ship.hitbox, alien_bullet):
            alien_bullet.exploded = True
            new_alien_bullet_explosion = Alien_Bullet_Explosion(
                screen, alien_bullet)
            alien_bullet_explosions.add(new_alien_bullet_explosion)
            sounds.playsound('explosion1')
            alien_bullets.remove(alien_bullet)
            if not ship.invul:
                ship.health -= alien_bullet.damage
                red_screen.turn_on()
                infrompy_settings.ship_health = ship.health
                ship.invul = True
                ship.invul_tick = infrompy_settings.ship_invul_time

    for alien_bullet in alien_bullets.copy():
        if alien_bullet.rect.top >= infrompy_settings.screen_height:
            alien_bullets.remove(alien_bullet)
Exemple #6
0
def run_game():
    #Initialize game and make a screen object
    pygame.init()
    sounds.playsound('theme1')

    infrompy_settings = Settings()
    screen = pygame.display.set_mode(
        (infrompy_settings.screen_width, infrompy_settings.screen_height))
    screenRect = screen.get_rect()
    pygame.display.set_caption("Invaders")
    background = Background(infrompy_settings, screen)
    red_screen = Red_Screen(infrompy_settings, screen)

    # Make play button and replay button
    play_button = Button(infrompy_settings, screen, "Play")
    play_again_button = Button(infrompy_settings, screen,
                               "Your ship ded lol. Play Again ?")
    # Make an instance to store game stats
    stats = GameStats(infrompy_settings)
    # Make a ship, group of bullets and group of aliens
    ship = Ship(infrompy_settings, screen)
    ship_explosions = Group()
    bullets = Group()
    bullet_explosions = Group()
    aliens = Group()
    alien_explosions = Group()
    alien_bullets = Group()
    alien_bullet_explosions = Group()
    background = Background(infrompy_settings, screen)

    # Level board
    info_board = Info_Board(infrompy_settings, screen)
    # Bullets group
    gf.create_fleet(infrompy_settings, screen, ship, aliens)

    while True:
        background.blitme()
        gf.check_events(infrompy_settings, screen, ship, bullets,
                        bullet_explosions, play_button)
        if infrompy_settings.game_active:

            gf.update_aliens(infrompy_settings, aliens, screen, ship,
                             alien_explosions, alien_bullet_explosions,
                             red_screen)
            gf.update_alien_explosions(aliens, screen, alien_explosions)

            gf.update_ship(infrompy_settings, screen, ship, ship_explosions)
            gf.update_ship_explosions(ship, screen, ship_explosions)

            gf.update_bullet_explosions(aliens, screen, bullets,
                                        bullet_explosions)
            gf.update_bullets(infrompy_settings, aliens, screen, ship, bullets,
                              bullet_explosions, alien_explosions,
                              alien_bullets)

            gf.update_alien_bullets(infrompy_settings, aliens, screen, ship,
                                    alien_bullets, alien_bullet_explosions,
                                    red_screen)
            gf.update_alien_bullet_explosions(aliens, screen,
                                              alien_bullet_explosions)
            gf.update_red_screen(infrompy_settings, red_screen)

        gf.update_screen(infrompy_settings, screen, ship, ship_explosions,
                         aliens, bullets, bullet_explosions, alien_explosions,
                         alien_bullets, alien_bullet_explosions, info_board,
                         play_button, play_again_button, red_screen)