Exemple #1
0
                player.die()
                net.theChosenNet = True
                control = False
                player.x = net.x + (net.actualWidth / 2)
                player.y = net.y + (net.actualHeight / 2)

    for net in nets:
        net.drawBack()

    player.draw()
    # player.lookRay()

    if (player.actualX() > boatX and player.actualX() < boatX + 300
            and player.actualY() > (-player.depth - 140) and player.actualY() <
        (-player.depth - 140 + 150)):
        explosion = Explosion.explosion(screen, boatX, -player.depth - 140)
        explosion.explodeP1(boatX, -player.depth - 140)
        boatX = 100000
        explosion.explodeP2(boatX, -player.depth - 140)
        actualWon = True

    for net in nets:
        net.drawFront()

    pygame.draw.rect(
        screen, black,
        pygame.Rect(width / 20, (height * 2 / 5) - 50, width / 10,
                    (height / 5) + 50))
    pygame.draw.rect(
        screen, (0, 0, 255),
        pygame.Rect((width / 20) + (width / 100),
Exemple #2
0
def game_loop():

    new()

    # Create game loop conditions and score
    running = True
    score = 0
    count = 0
    shadow_score = 0
    rocks_condition = True
    bullets_exist = False
    dialogue = True
    music_play = True
    stage_1_end = False
    explosion_counter = 0

    # Play background music
    def music_1(condition):
        if condition:
            pygame.mixer.music.load(
                os.path.join(sound_folder, 'Vegas_Loop.wav'))
            pygame.mixer.music.set_volume(.5)
            pygame.mixer.music.play(-1)
        else:
            pygame.mixer.music.load(os.path.join(sound_folder,
                                                 'Serotonin.wav'))
            pygame.mixer.music.set_volume(1)
            pygame.mixer.music.play(-1)

    # Game Loop
    while running:
        # Set FPS
        dt = clock.tick(FPS) / 1000
        mainSurface.blit(background, background_rect)
        if music_play and rocks_condition:
            music_1(True)
            music_play = False

        if not music_play and not rocks_condition:
            music_1(False)
            music_play = True
        if score > 1500:
            rocks_condition = False

        # Events loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if dialogue:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN:
                        dialogue = False
            elif event.type == pygame.KEYDOWN and not dialogue:
                if event.key == pygame.K_ESCAPE:
                    # pygame.mixer.music.pause()
                    menu_resume()
                if event.key == pygame.K_SPACE:
                    shoot_sound.play()
                    shoot_sound.set_volume(.3)
                    shoot_player(player)
                if event.key == pygame.K_RETURN and dialogue:
                    dialogue = False

        # Control rules for all units movement
        for player in units_player:
            if not dialogue and not stage_1_end:
                Player.player_1.manual_control(player)
        for unit in units_rocks:
            if not dialogue:
                SpaceRock.space_rock1.AI_control(unit)
        if not rocks_condition:
            for unit in units_mobships:
                if not dialogue:
                    Mobship.mob_ship.AI_control_outside(unit)
        for planet in units_planets:
            Planet.planet_1.AI_control(planet, shadow_score, count)
            count = 0
            shadow_score = 0

        # Checking if mobs entered the screen and assign new controls and assign bullet
        for unit in units_mobships:
            if unit.rect.right <= SCREEN_WIDTH or unit.rect.top <= 0:
                Mobship.mob_ship.AI_control_inside(unit)
                if (unit.rect.left <
                        SCREEN_WIDTH / 2 + 200) and not bullets_exist:
                    super_bullet = Bullets.bullet_2(mainSurface,
                                                    unit.rect.left,
                                                    unit.rect.centery)
                    shoot_sound_2.play()
                    shoot_sound_2.set_volume(.2)
                    units_bullets_mob.add(super_bullet)
                    units_all.add(super_bullet)
                    bullets_exist = True
                if bullets_exist:
                    expl_bullet = Explosion.explosion(
                        mainSurface, super_bullet.rect.left,
                        super_bullet.rect.centery, 'small')
                    units_all.add(expl_bullet)
                if len(units_bullets_mob) == 0:
                    bullets_exist = False

        # Checking if any rock passed the screen to apply self destruct
        for unit in units_rocks:
            if unit.rect.right <= 0:
                unit.kill()
                score += 10
                if rocks_condition:
                    rock = new_rock()
                count += 1
            if unit.rect.bottom > SCREEN_HEIGHT:
                print('check3!')
                unit.kill()
                if rocks_condition:
                    rock = new_rock()

        # Checking collision between rocks and rocks and initiating speedy rocks
        for unit in units_rocks:
            units_rocks.remove(unit)
            if (collision_check(unit, units_rocks, False,
                                False)) and unit.rect.x <= SCREEN_WIDTH:
                unit.speedx -= 5
                explosion_sound.set_volume(.2)
                explosion_sound.play()
                expl_rock = Explosion.explosion(mainSurface, unit.rect.centerx,
                                                unit.rect.centery, 'large')
                units_all.add(expl_rock)
            units_rocks.add(unit)

        # Checking collision between rocks and bullets
        hits_2 = pygame.sprite.groupcollide(units_rocks, units_bullets, True,
                                            True, pygame.sprite.collide_circle)
        for hit in hits_2:
            if rocks_condition:
                new_rock()
            score += 10
            shadow_score += 10
            explosion_sound.set_volume(.2)
            explosion_sound.play()
            expl_rock = Explosion.explosion(mainSurface, hit.rect.centerx,
                                            hit.rect.centery, 'large')
            units_all.add(expl_rock)

        # Checking collision between player and rocks
        hits_1 = pygame.sprite.groupcollide(units_player, units_rocks, False,
                                            True, pygame.sprite.collide_circle)
        for hit in hits_1:
            rock = new_rock()
            for unit in units_rocks:
                damage = unit.hit_damage
            player.hp -= damage
            explosion_player_sound.play()
            expl_player = Explosion.explosion(mainSurface, player.rect.centerx,
                                              player.rect.centery, 'large')
            units_all.add(expl_player)
            if player.hp <= 0:
                player.hide()
                player.lives += -1
                player.hp = player.full_hp
                if player.lives == 0:
                    running = False

        # Check collision between player ship and enemy bullets
        if collision_check_groups(units_player, units_bullets_mob, False,
                                  True):
            player.hp -= 100
            explosion_player_sound.play()
            expl_player = Explosion.explosion(mainSurface, player.rect.centerx,
                                              player.rect.centery, 'large')
            units_all.add(expl_player)
            if player.hp <= 0:
                player.hide()
                player.lives += -1
                player.hp = player.full_hp
                if player.lives == 0:
                    running = False

        # Check collision between player bullets and enemy bullets
        if collision_check_groups(units_bullets, units_bullets_mob, True,
                                  False):
            pass

        # Check collision between player bullets and enemy ship
        if collision_check_groups(units_mobships, units_bullets, False, True):
            for unit in units_mobships:
                unit.hp -= 100
                expl_mob_ship = Explosion.explosion(mainSurface,
                                                    unit.rect.centerx,
                                                    unit.rect.centery, 'large')
                explosion_sound_2.play()
                units_all.add(expl_mob_ship)
                print(unit.hp)
                if unit.hp <= 0:
                    stage_1_end = True

        # Explosion of boss when dead
        if stage_1_end:
            for unit in units_bullets_mob:
                unit.kill()
            for unit in units_bullets:
                unit.kill()
        if explosion_counter < 5 and stage_1_end:
            clock.tick(3)
            for unit in units_mobships:
                if explosion_counter == 0:
                    expl_mob_ship = Explosion.explosion(
                        mainSurface, unit.rect.right, unit.rect.centery,
                        'small')
                    units_all.add(expl_mob_ship)
                    explosion_sound_3.play()
                    explosion_counter += 1
                    print('explosion of boss 0')
                elif explosion_counter == 1:
                    expl_mob_ship = Explosion.explosion(
                        mainSurface, unit.rect.left, unit.rect.centery,
                        'large')
                    units_all.add(expl_mob_ship)
                    explosion_sound_3.play()
                    explosion_counter += 1
                    print('explosion of boss 1')
                elif explosion_counter == 2:
                    expl_mob_ship = Explosion.explosion(
                        mainSurface, unit.rect.right, unit.rect.centery,
                        'small')
                    units_all.add(expl_mob_ship)
                    explosion_sound_3.play()
                    explosion_counter += 1
                elif explosion_counter == 3:
                    expl_mob_ship = Explosion.explosion(
                        mainSurface, unit.rect.left, unit.rect.centery,
                        'large')
                    units_all.add(expl_mob_ship)
                    explosion_sound_3.play()
                    explosion_counter += 1
                elif explosion_counter == 4:
                    expl_mob_ship = Explosion.explosion(
                        mainSurface, unit.rect.right, unit.rect.centery,
                        'small')
                    explosion_sound_3.play()
                    units_all.add(expl_mob_ship)
                    explosion_counter += 1
                    unit.kill()

        # Background and game units on screen
        units_all.update()
        units_all.draw(mainSurface)
        if dialogue:
            dialogue_1 = draw.dialogue(
                mainSurface,
                "Welcome this is my first game. Press Enter to continue! ", 0,
                400, SCREEN_WIDTH, 300, blue, white, True)
        # Screen platform, e.g. (Score, Hp bar)
        text_1 = draw.text(mainSurface, 'Score ' + str(score), 18,
                           SCREEN_WIDTH / 2, 20, white)
        hp_bar = draw.draw_hp_bar(mainSurface, 0, 20, player.full_hp,
                                  player.hp)
        lives_icons = draw.draw_icons_lives(mainSurface, 0, player.lives)
        if explosion_counter == 5:
            text_2 = draw.text(mainSurface, 'You Win', 40, SCREEN_WIDTH / 2,
                               SCREEN_HEIGHT / 2, blue_bright)
        pygame.display.update()