def create_ufo(ai_settings, screen, boss):
    """Create an alien, and place it in the row."""
    ufo = UFO(ai_settings, screen)
    ufo_width = ufo.rect.width
    ufo.x = 530
    ufo.rect.x = ufo.x
    ufo.rect.y = 170
    ufo.add(boss)
示例#2
0
def run_game():
    # Initialize game
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invaders by Diana Joya")

    # Game Stats
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # Screens
    start_screen = Start(ai_settings, screen)
    score_board = HighScores(ai_settings, screen, stats)

    # Sprite Groups
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    bunkers = Group()
    random_ufo = UFO(ai_settings, screen)
    bullet_animation = BulletAnimation(ai_settings, screen, ship, bullets,
                                       aliens, stats, sb, random_ufo, bunkers)

    events = Update(ai_settings, screen, ship, bullets, aliens, stats, sb,
                    start_screen, score_board, random_ufo, bunkers)
    collision = Collisions(ai_settings, screen, ship, bullets, aliens, stats,
                           sb, start_screen, score_board, random_ufo, bunkers)

    # Game Loop
    while True:
        events.check_events()

        if stats.game_active:
            current_time = pygame.time.get_ticks()
            ship.update()
            bullet_animation.update_bullets()
            collision.update_aliens()
            bunkers.update()
            if ai_settings.ufo_allowed:
                random_ufo.update(current_time)

        events.update_screen()
    def makeEnemyShip(self, ship_type):
        new_enemy_ship = None

        if ship_type == "UFO":
            ship_parts_factory = UFOFactory()
            new_enemy_ship = UFO(ship_parts_factory)
            new_enemy_ship.setName("UFO Enemy Ship")
        elif ship_type == "Rocket":
            ship_parts_factory = RocketFactory()
            new_enemy_ship = Rocket(ship_parts_factory)
            new_enemy_ship.setName("Rocket Enemy Ship")
        else:
            pass

        if new_enemy_ship != None:
            new_enemy_ship.build()
            print(new_enemy_ship.getInfo())

        return new_enemy_ship
示例#4
0
def update_UFOs(ai_settings, screen, UFOs):
    curtime = pygame.time.get_ticks()
    if curtime - ai_settings.UFO_old_time > ai_settings.UFO_wait:
        UFOs.add(UFO(ai_settings, screen))
        ai_settings.UFO_old_time = curtime
        ai_settings.UFO_wait = random.randint(10000, 30000)
    for ufo in UFOs:
        ufo.update_pos()
        if ufo.rect.x > ai_settings.screen_width:
            UFOs.remove(ufo)
示例#5
0
    def __init__(self, ai_settings, screen):

        self.ai_settings = ai_settings
        self.screen = screen

        self.display_start = True
        self.display_high_score = False
        self.start_screen = StartScreen(ai_settings=ai_settings, screen=screen)
        self.stats = GameStats(ai_settings=self.ai_settings)
        self.sb = Scoreboard(ai_settings=self.ai_settings,
                             screen=self.screen,
                             stats=self.stats)
        self.ship = Ship(ai_settings=self.ai_settings, screen=self.screen)

        self.high_score = High_Score(self.ai_settings, self.screen)
        self.gameover = GameOver(self.ai_settings, self.screen)
        self.quit = False

        self.alien1 = Alien1(ai_settings=self.ai_settings, screen=self.screen)
        self.alien2 = Alien2(ai_settings=self.ai_settings, screen=self.screen)
        self.alien3 = Alien3(ai_settings=self.ai_settings, screen=self.screen)

        self.aliens = [self.alien1, self.alien2, self.alien3]

        self.UFO = UFO(ai_settings=self.ai_settings, screen=self.screen)

        self.faster = False

        self.bullets = Group()
        self.enemy_bullets = Group()
        self.alien_group = Group()

        self.play_music = 'sounds/play.mp3'
        self.play_music_faster = 'sounds/play-faster.mp3'
        self.missile_sound = pygame.mixer.Sound('sounds/missile.wav')

        self.bunker = Group()
        self.create_bunker()
示例#6
0
def run_game():
    pygame.init()

    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))

    pygame.display.set_caption("Alien Invasion")

    # Make the play button
    play_button = Button(ai_settings, screen, "Play!")

    # Create an instance to store game statistics and create a scoreboard
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # make a ship, a group of bullets, and a group of aliens, a group of barriers, and a UFO
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    # barrier = Group()
    ufo = UFO(ai_settings, screen)

    # create the fleet of aliens
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # create a way to keep track of time (for the ufo spawn)
    time_elapsed = 0
    clock = pygame.time.Clock()

    # Start the main loop for the game
    while True:

        gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
                        aliens, bullets)
        dt = clock.tick()
        time_elapsed += dt

        if stats.game_active:
            ship.update()
            gf.update_ufo(ai_settings, screen, ufo)
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
                              bullets)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
                             bullets)

        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
                         play_button, ufo)
示例#7
0
def run_game():
    pygame.init()

    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))

    pygame.display.set_caption("Alien Invasion!")

    # Make the play button and high scores button
    play_button = Button(ai_settings, screen, "Play!")
    high_scores_button = HighScoresButton(ai_settings, screen, "High Scores")

    # Create an instance to store game statistics and create a scoreboard
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # Make a ship, a group of bullets, and a group of aliens, a group of barriers, and a UFO
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    barrier = Barrier(ai_settings, screen)
    ufo = UFO(ai_settings, screen)
    hs_init = open("high_scores.txt", "r+")
    stats.high_score = int(hs_init.readline())
    stats.high_score_2 = int(hs_init.readline())
    stats.high_score_3 = int(hs_init.readline())
    hs_init.close()

    # Make the start screen
    start_screen = StartScreen(
        ai_settings, screen, aliens, "Alien Invasion!", "= 50", "= 100",
        "= 150", "= 300",
        "Gabriel Magallanes | For CPSC 386 (Video Game Design) | Cal State Fullerton | 2019"
    )

    # Make the high score window
    high_score_window = HighScoreWindow(ai_settings, screen, stats)

    # Create the fleet of aliens
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # Create a way to keep track of time (for the ufo spawn)
    time_elapsed = 0
    clock = pygame.time.Clock()

    # Start the main loop for the game
    while True:

        gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
                        aliens, bullets, high_scores_button)

        if stats.game_active:
            ship.update()
            gf.update_ufo(ai_settings, screen, ufo)
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
                              bullets, high_score_window)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
                             bullets, high_score_window)

        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
                         start_screen, play_button, ufo, barrier,
                         high_scores_button)
示例#8
0
class PlayScreen:
    def __init__(self, ai_settings, screen):

        self.ai_settings = ai_settings
        self.screen = screen

        self.display_start = True
        self.display_high_score = False
        self.start_screen = StartScreen(ai_settings=ai_settings, screen=screen)
        self.stats = GameStats(ai_settings=self.ai_settings)
        self.sb = Scoreboard(ai_settings=self.ai_settings,
                             screen=self.screen,
                             stats=self.stats)
        self.ship = Ship(ai_settings=self.ai_settings, screen=self.screen)

        self.high_score = High_Score(self.ai_settings, self.screen)
        self.gameover = GameOver(self.ai_settings, self.screen)
        self.quit = False

        self.alien1 = Alien1(ai_settings=self.ai_settings, screen=self.screen)
        self.alien2 = Alien2(ai_settings=self.ai_settings, screen=self.screen)
        self.alien3 = Alien3(ai_settings=self.ai_settings, screen=self.screen)

        self.aliens = [self.alien1, self.alien2, self.alien3]

        self.UFO = UFO(ai_settings=self.ai_settings, screen=self.screen)

        self.faster = False

        self.bullets = Group()
        self.enemy_bullets = Group()
        self.alien_group = Group()

        self.play_music = 'sounds/play.mp3'
        self.play_music_faster = 'sounds/play-faster.mp3'
        self.missile_sound = pygame.mixer.Sound('sounds/missile.wav')

        self.bunker = Group()
        self.create_bunker()

    def create_bunker(self):
        for x in range(3):
            bunk = Bunker(ai_settings=self.ai_settings,
                          screen=self.screen,
                          centerx=x * 400 + 200,
                          centery=self.ai_settings.screen_height - 100)
            self.bunker.add(bunk)

    def check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self.check_keydown_events(event)

            elif event.type == pygame.KEYUP:
                self.check_keyup_events(event)

    def check_keyup_events(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False

    def check_keydown_events(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_SPACE:
            self.fire_bullet()
        elif event.key == pygame.K_q:
            sys.exit()

    def new_game(self):
        self.ai_settings.initialize_dynamic_settings()

        pygame.mouse.set_visible(False)

        self.stats.reset_stats()
        self.stats.game_active = True

        self.sb.prep_score()
        self.sb.prep_ships()

        self.alien_group.empty()
        self.bullets.empty()
        self.enemy_bullets.empty()

        # create fleet
        self.create_fleet()
        self.create_bunker()

        self.ship.center_ship()

    def fire_bullet(self):
        if len(self.bullets) < self.ai_settings.bullets_allowed:
            new_bullet = Bullet(ai_settings=self.ai_settings,
                                screen=self.screen,
                                ship=self.ship)
            self.bullets.add(new_bullet)
            self.missile_sound.play()

    def fire_enemy_bullet(self):
        if random.random() < self.ai_settings.probability_to_fire:
            if len(self.enemy_bullets
                   ) < self.ai_settings.enemy_bullets_allowed:
                alien = random.choice(list(self.alien_group.spritedict.keys()))
                new_bullet = Bullet(ai_settings=self.ai_settings,
                                    screen=self.screen,
                                    ship=alien,
                                    enemy=True)
                self.enemy_bullets.add(new_bullet)
                self.missile_sound.play()

    def get_number_aliens_x(self):
        alien_width = self.aliens[0].rect.width
        available_space_x = self.ai_settings.screen_width - (2 * alien_width)
        number_aliens_x = int(available_space_x / (2 * alien_width))
        return number_aliens_x

    def get_number_rows(self):
        alien_height = self.aliens[0].rect.height
        available_space_y = (self.ai_settings.screen_height -
                             (3 * alien_height) - self.ship.rect.height)
        number_rows = int(available_space_y / (2 * alien_height))
        return number_rows

    def create_alien(self, which_alien, row_number, alien_number):
        one_alien = None
        if which_alien == 0:
            one_alien = Alien1(ai_settings=self.ai_settings,
                               screen=self.screen)
        elif which_alien == 1:
            one_alien = Alien2(ai_settings=self.ai_settings,
                               screen=self.screen)
        elif which_alien == 2:
            one_alien = Alien3(ai_settings=self.ai_settings,
                               screen=self.screen)
        alien_width = one_alien.rect.width
        one_alien.x = alien_width + 2 * alien_width * alien_number
        one_alien.rect.x = one_alien.x
        one_alien.rect.y = one_alien.rect.height + 2 * one_alien.rect.height * row_number
        self.alien_group.add(one_alien)

    def create_fleet(self):
        rows = self.get_number_rows()
        aliens_per_row = self.get_number_aliens_x()
        for row_number in range(rows):
            which_alien = int(row_number / 2)
            for alien_number in range(aliens_per_row):
                self.create_alien(which_alien=which_alien,
                                  row_number=row_number,
                                  alien_number=alien_number)

    def check_bullet_alien_collisions(self):
        collisions = pygame.sprite.groupcollide(self.bullets, self.alien_group,
                                                True, True)

        pygame.sprite.groupcollide(self.alien_group, self.bunker, False, True)

        if self.UFO.alive:
            if pygame.sprite.spritecollideany(self.UFO, self.bullets):
                self.UFO.explode()
                self.UFO.dead()
                self.stats.score += self.ai_settings.ufo_points
                self.sb.prep_score()

        if collisions:
            for aliens in collisions.values():
                for alien in aliens:
                    for x in range(len(self.aliens)):
                        if type(alien) == type(self.aliens[x]):
                            self.stats.score += self.ai_settings.alien_points[
                                x]
                            self.sb.prep_score()
                    alien.explode()
                    sleep(0.05)
                    alien.explosion_timer.reset()
                # needs to update settings for alien points

        if len(self.alien_group) < 11 and not self.faster:
            self.play_game_music(faster=True)

        if len(self.alien_group) == 0 and not self.UFO.alive:
            self.bullets.empty()
            self.ai_settings.increase_speed()

            self.stats.level += 1
            self.sb.prep_level()

            self.create_fleet()
            self.UFO.reset()

            self.play_game_music(faster=False)

        bunker_collision = pygame.sprite.groupcollide(
            groupa=self.bunker,
            groupb=self.enemy_bullets,
            dokilla=False,
            dokillb=True,
            collided=pygame.sprite.collide_rect_ratio(2))

    def ship_hit(self):
        self.stats.ships_left -= 1
        print(self.stats.ships_left)
        if self.stats.ships_left > 0:
            # Update the scoreboard
            self.sb.prep_ships()

            # Empty list of aliens and bullets
            self.alien_group.empty()
            self.bullets.empty()
            self.enemy_bullets.empty()

            # create_fleet
            self.create_fleet()
            self.ship.center_ship()
            self.create_bunker()

            # Pause
            sleep(0.5)

        else:
            for i in range(6):
                self.ship.explode()
                sleep(0.3)
            self.ship.explosion_timer.reset()
            self.stats.game_active = False
            pygame.mouse.set_visible(True)
            self.high_score.update_score(self.stats.score)

    def update_screen(self):
        self.screen.fill(self.ai_settings.bg_color)
        self.bullets.draw(self.screen)
        self.enemy_bullets.draw(self.screen)

        self.ship.blitme()
        for alien in self.alien_group:
            alien.blitme()

        if self.UFO.alive:
            self.UFO.blitme()

        for bunker in self.bunker:
            bunker.draw()

        self.sb.show_score()

        pygame.display.flip()

    def start_screen_play(self):
        self.start_screen.play_music()
        while self.display_start:
            for e in pygame.event.get():
                if e.type == pygame.QUIT:
                    sys.exit()
                elif e.type == pygame.MOUSEBUTTONDOWN:
                    _mouse_x, _mouse_y = pygame.mouse.get_pos()
                    if self.start_screen.check_play_button(_mouse_x, _mouse_y):
                        self.display_start = False
                        continue
                    if self.start_screen.check_high_score_button(
                            _mouse_x, _mouse_y):
                        self.display_high_score = True

                elif e.type == pygame.KEYDOWN:
                    if e.key == pygame.K_SPACE:
                        self.display_high_score = False
            if self.display_high_score:
                self.high_score.draw()
            else:
                self.start_screen.draw()

            pygame.display.flip()

        pygame.mixer.music.stop()

    def gameOver_play(self):
        self.gameover.draw()
        for e in pygame.event.get():
            if e.type == pygame.KEYDOWN:
                if e.type == pygame.QUIT:
                    sys.exit()
            if e.type == pygame.MOUSEBUTTONDOWN:
                _mouse_x, _mouse_y = pygame.mouse.get_pos()
                if self.gameover.check_play_button(_mouse_x=_mouse_x,
                                                   _mouse_y=_mouse_y):
                    self.new_game()
                    self.stats.game_active = True
                elif self.gameover.check_quit_button(_mouse_x=_mouse_x,
                                                     _mouse_y=_mouse_y):
                    self.quit = True

    def play_game_music(self, faster):
        pygame.mixer.music.stop()
        if faster:
            pygame.mixer.music.load(self.play_music_faster)
            self.faster = True
        else:
            pygame.mixer.music.load(self.play_music)
            self.faster = False
        pygame.mixer.music.play(-1, 0.0)

    def update_bullets(self):
        self.bullets.update()
        self.enemy_bullets.update()

        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)

        for bullet in self.enemy_bullets.copy():
            if bullet.rect.top >= self.ai_settings.screen_height:
                self.enemy_bullets.remove(bullet)

        self.fire_enemy_bullet()

        if pygame.sprite.spritecollideany(self.ship, self.enemy_bullets):
            self.ship_hit()
        self.check_bullet_alien_collisions()

    def change_fleet_direction(self):
        for alien in self.alien_group:
            alien.rect.y += self.ai_settings.fleet_drop_speed
        self.ai_settings.fleet_direction *= -1

    def check_fleet_edges(self):
        for alien in self.alien_group:
            if alien.check_edges():
                self.change_fleet_direction()
                break

    def check_aliens_bottom(self):
        screen_rect = self.screen.get_rect()
        for alien in self.alien_group:
            if alien.rect.bottom >= screen_rect.bottom:
                self.ship_hit()
                break

    def update_aliens(self):
        self.check_fleet_edges()
        self.alien_group.update()
        if pygame.sprite.spritecollideany(self.ship, self.alien_group):
            self.ship_hit()
        if self.UFO.alive:
            self.update_UFO()
        self.check_aliens_bottom()

    def update_UFO(self):
        if self.UFO.check_edges():
            self.ai_settings.UFO_direction *= -1
        self.UFO.update()

    def play(self):
        self.start_screen_play()

        self.new_game()
        self.play_game_music(faster=False)
        while not self.quit:
            if self.stats.game_active:
                self.check_events()
                self.ship.update()
                self.update_bullets()
                self.update_aliens()
                self.update_screen()

            else:
                self.gameOver_play()
                pygame.display.flip()
示例#9
0
    def play(self):
        ai_settings = Settings()
        screen = pg.display.set_mode(
            (ai_settings.screen_width, ai_settings.screen_height))
        pg.display.set_caption(ai_settings.title)

        play_button = Button(screen, "Play")
        score_button = ScoreButton(ai_settings, screen, "High Score")

        stats = GameStats(ai_settings=ai_settings)
        sb = Scoreboard(ai_settings=ai_settings, screen=screen, stats=stats)

        sound = Sound()
        sound.play()
        sound.pause_bg()

        ship = Ship(ai_settings=ai_settings, screen=screen)
        ships = Group()
        ships.add(ship)
        barriers = Group()

        aliens = Group()

        bullets = Group()

        alien_bullets = Group()
        aliens_list = []
        timer = [pg.time.get_ticks()]

        UFO_object = UFO(ai_settings=ai_settings, screen=screen)
        UFOs = Group()
        UFOs.add(UFO_object)
        timer_score_display = [pg.time.get_ticks()]

        for ship in ships:
            gf.create_fleet(ai_settings=ai_settings,
                            screen=screen,
                            ship=ship,
                            aliens=aliens)
        gf.create_barriers(ai_settings=ai_settings,
                           screen=screen,
                           barriers=barriers)
        screen.fill(ai_settings.bg_color)
        while True:
            gf.check_events(ai_settings=ai_settings,
                            screen=screen,
                            play_button=play_button,
                            scores_button=score_button,
                            stats=stats,
                            sb=sb,
                            sound=sound,
                            ships=ships,
                            aliens=aliens,
                            bullets=bullets)
            if stats.game_active:
                gf.update_ships(ai_settings=ai_settings,
                                screen=screen,
                                stats=stats,
                                sb=sb,
                                sound=sound,
                                ships=ships,
                                aliens=aliens,
                                bullets=bullets,
                                alien_bullets=alien_bullets,
                                UFOs=UFOs)
                gf.update_aliens(ai_settings=ai_settings,
                                 screen=screen,
                                 stats=stats,
                                 sb=sb,
                                 sound=sound,
                                 ships=ships,
                                 aliens=aliens,
                                 bullets=bullets,
                                 alien_bullets=alien_bullets,
                                 timer=timer,
                                 aliens_list=aliens_list,
                                 UFOs=UFOs)
                gf.update_bullets(ai_settings=ai_settings,
                                  screen=screen,
                                  stats=stats,
                                  sb=sb,
                                  sound=sound,
                                  ships=ships,
                                  aliens=aliens,
                                  bullets=bullets,
                                  barriers=barriers)
                gf.update_alien_bullets(ai_settings=ai_settings,
                                        ships=ships,
                                        alien_bullets=alien_bullets,
                                        barriers=barriers)
                gf.update_UFO(ai_settings=ai_settings,
                              screen=screen,
                              stats=stats,
                              sb=sb,
                              sound=sound,
                              bullets=bullets,
                              UFOs=UFOs,
                              timer_value_display=timer_score_display)
            else:
                screen.blit(self.game_title, (300, 100))
                screen.blit(self.alien_one, (200, 250))
                screen.blit(self.alien_one_label, (235, 380))
                screen.blit(self.alien_two, (400, 250))
                screen.blit(self.alien_two_label, (435, 380))
                screen.blit(self.alien_three, (600, 250))
                screen.blit(self.alien_three_label, (635, 380))
                screen.blit(self.ufo, (800, 220))
                screen.blit(self.ufo_label, (852, 380))

            gf.update_screen(ai_settings=ai_settings,
                             screen=screen,
                             play_button=play_button,
                             score_button=score_button,
                             stats=stats,
                             sb=sb,
                             sound=sound,
                             ships=ships,
                             aliens=aliens,
                             bullets=bullets,
                             alien_bullets=alien_bullets,
                             UFOs=UFOs,
                             timer_score_display=timer_score_display,
                             barriers=barriers)