Example #1
0
    def __init__(self, game_env): 
        Text.__init__(self, game_env, size=20)
        self.__game_env = game_env
        seperator = self.font.render(' ', 1, self.color)
        header = self.font.render('=== HELP ===', 1, self.color)
        footer = self.font.render('=== GOOD LUCK ===', 1, self.color)
        all_surfaces = []
        all_surfaces.append(seperator)
        all_surfaces.append(self.font.render('Your objective should you choose to accept is to navigate your jet without getting hit by', 1, self.color))
        all_surfaces.append(self.font.render('the incoming missiles. For self-defence you can shoot down the enemy missiles. You are', 1, self.color))
        all_surfaces.append(self.font.render('armed with 100 special missiles. Level-up awards you another 50 special missiles and a', 1, self.color))
        all_surfaces.append(self.font.render('power-up star which will instantly deactivate all the enemy missiles.', 1, self.color))
        all_surfaces.append(self.font.render('Your jet can carry maximum 999 special missiles.', 1, self.color))
        all_surfaces.append(seperator)
        all_surfaces.append(self.font.render('With keyboard input, you should use your keyboard arrow keys to navigate and spacebar', 1, self.color))
        all_surfaces.append(self.font.render('to shoot. With mouse as input, you should use your mouse to navigate your jet and', 1, self.color))
        all_surfaces.append(self.font.render('mouse click to shoot', 1, self.color))
        all_surfaces.append(self.font.render(' ', 1, self.color))
        all_surfaces.append(self.font.render('POINTS: Destroy Missle -> 10 pts. Power-up Star -> 100 pts. Level-up -> 10 pts.', 1, self.color))
        all_surfaces.append(seperator)

        self.surf = Surface((all_surfaces[1].get_width(), all_surfaces[0].get_height() * (len(all_surfaces) + 2)), self.__game_env.SRCALPHA)

        self.surf.blit(header, (self.surf.get_width()/2 - header.get_width()/2, 0))
        for index, temp_surf in enumerate(all_surfaces):
            self.surf.blit(temp_surf, (0, header.get_height() + index * temp_surf.get_height()))
        self.surf.blit(footer, (self.surf.get_width()/2 - footer.get_width()/2, self.surf.get_height() - footer.get_height()))

        self.rect = self.surf.get_rect(center=(self.__game_env.static.screen_width/2, self.__game_env.static.screen_height/2))
Example #2
0
    def __init__(self, game_env):
        Text.__init__(
            self, game_env,
            size=38)  # initilizing parent class with default text color as red
        self.__game_env = game_env
        self.__gameover = Text(self.__game_env, "GAME OVER", 60)

        self.__replaytext_surf = self.font.render(
            "Replay ", 1, self.color)  # creating surface with the Replay text

        self.__y_selected_surf = self.font.render(
            "Yes", 1, self.__game_env.static.text_selection_color
        )  # creating surface with Yes text when highlighted
        self.__n_surf = self.font.render(
            "/No", 1, self.color)  # creating surface with No text
        self.__y_surf = self.font.render(
            "Yes/", 1, self.color)  # creating surface with Yes text
        self.__n_selected_surf = self.font.render(
            "No", 1, self.__game_env.static.text_selection_color
        )  # creating surface with No text when highlighted

        self.__replaytext_pos_x = self.__gameover.surf.get_width() / 2 - (
            self.__replaytext_surf.get_width() +
            self.__y_selected_surf.get_width() + self.__n_surf.get_width()) / 2

        self.__highlight_yes(
        )  # calling method to highlight Yes (the default choice)
Example #3
0
 def __init__(self, game_env):
     Text.__init__(
         self, game_env,
         size=32)  # initilizing parent class with default text color as red
     self.__game_env = game_env
     self.__header = Text(self.__game_env, "=== ENTER YOUR NAME ===", 36)
     self.__footer = Text(self.__game_env,
                          "===============================", 36)
     self.__player_name = ''  # default player name
     self.render(self.__player_name)
Example #4
0
    def __init__(self, game_env):
        Text.__init__(self, game_env, size=20)
        self.__game_env = game_env
        name_length = self.__game_env.static.name_length * 2
        leaders = LeaderBoardHandler().load()
        seperator = self.font.render(
            '===================================================================================================',
            1, self.color)
        header = self.font.render('=== HALL OF FAME ===', 1, self.color)
        all_surfaces = []
        all_surfaces.append(seperator)
        all_surfaces.append(
            self.font.render(
                '{} {} {} {} {} {}'.format('RANK'.ljust(5),
                                           'NAME'.ljust(name_length),
                                           'SCORE'.ljust(10), 'LEVEL'.ljust(5),
                                           'ACCURACY'.ljust(8),
                                           'TIME'.rjust(21)), 1, self.color))
        all_surfaces.append(seperator)
        try:
            if len(leaders) == 0:
                all_surfaces.append(
                    self.font.render(
                        'No records, make sure you have working internet connectivity',
                        1, self.color))

            for index, score in enumerate(leaders['scores']):
                all_surfaces.append(
                    self.font.render(
                        '{} {} {} {} {} {}'.format(
                            str(index + 1).ljust(5),
                            score['name'][:name_length].ljust(name_length),
                            str(score['score']).ljust(10),
                            str(score['level']).ljust(5),
                            str(score['accuracy'] + '%').ljust(8),
                            str(time.ctime(int(score['epoch']))).rjust(25)), 1,
                        self.color))
        except:
            pass
        all_surfaces.append(seperator)

        self.surf = Surface(
            (all_surfaces[2].get_width(), all_surfaces[0].get_height() *
             (len(all_surfaces) + 1)), self.__game_env.SRCALPHA)

        self.surf.blit(header,
                       (self.surf.get_width() / 2 - header.get_width() / 2, 0))
        for index, temp_surf in enumerate(all_surfaces):
            self.surf.blit(
                temp_surf,
                (0, header.get_height() + index * temp_surf.get_height()))

        self.rect = self.surf.get_rect(
            center=(self.__game_env.static.screen_width / 2,
                    self.__game_env.static.screen_height / 2))
Example #5
0
    def __init__(self, game_env):
        Text.__init__(
            self, game_env,
            size=36)  # initilizing parent class with default text color as red
        self.__game_env = game_env
        self.__jet = Jet(game_env)  # creating a of jet
        self.__prefix_surf = self.font.render(
            "What's your input {}?".format(
                self.__game_env.dynamic.player_name), 1,
            self.color)  # creating surface with the prefix text
        self.__mouse = self.font.render(
            ' Mouse', 1, self.color)  # creating surface with mouse text
        self.__keybrd = self.font.render(
            ' Keyboard', 1, self.color)  # creating surface with mouse text

        keybrd_surf = self.font.render(
            ' Keyboard', 1, game_env.static.text_selection_color
        )  # creating surface with Keyboard text when highlighted
        self.__keybrd_selected = Surface(
            (self.__jet.surf.get_width() + keybrd_surf.get_width(),
             keybrd_surf.get_height()), game_env.SRCALPHA
        )  # creating surface for jet and highlighted keyboard text
        self.__keybrd_selected.blit(self.__jet.surf, (0, 0))  # drawing the jet
        self.__keybrd_selected.blit(
            keybrd_surf,
            (self.__jet.surf.get_width(),
             0))  # drawing the highligted keyboard text after the jet image

        mouse_surf = self.font.render(
            ' Mouse', 1, game_env.static.text_selection_color
        )  # creating surface with mouse text when highlighted
        self.__mouse_selected = Surface(
            (self.__jet.surf.get_width() + mouse_surf.get_width(),
             mouse_surf.get_height()), game_env.SRCALPHA
        )  # creating surface for jet and highlighted mouse text
        self.__mouse_selected.blit(self.__jet.surf, (0, 0))  # drawing the jet
        self.__mouse_selected.blit(
            mouse_surf,
            (self.__jet.surf.get_width(),
             0))  # drawing the highligted mouse text after the jet image
        self.__left_padding = self.__prefix_surf.get_width(
        ) / 2 - self.__keybrd_selected.get_width() / 2
        self.__highlight_keyboard(
        )  # calling method to highlight keyboard (the default choice)
Example #6
0
    def __init__(self, game_env):
        Text.__init__(self, game_env, size=38)
        self.__game_env = game_env
        self.__title = Text(self.__game_env, "Do you want to quit?", 48)

        self.__y_selected_surf = self.font.render(
            "Yes", 1, self.__game_env.static.text_selection_color
        )  # creating surface with Yes text when highlighted
        self.__n_surf = self.font.render(
            "/No", 1, self.color)  # creating surface with No text
        self.__y_surf = self.font.render(
            "Yes/", 1, self.color)  # creating surface with Yes text
        self.__n_selected_surf = self.font.render(
            "No", 1, self.__game_env.static.text_selection_color
        )  # creating surface with No text when highlighted

        self.__choices = self.__title.surf.get_width() / 2 - (
            self.__y_selected_surf.get_width() + self.__n_surf.get_width()) / 2
        self.__highlight_no()
Example #7
0
def play():
    pygame.mixer.init()  # initializing same audio mixer with default settings
    pygame.init()  # initializing pygame
    game_env = GameEnvironment()  # initializing game environment

    game_env.dynamic.collision_sound.set_volume(1.5)
    game_env.dynamic.levelup_sound.set_volume(1.5)
    game_env.dynamic.shoot_sound.set_volume(1.5)
    game_env.dynamic.hit_sound.set_volume(3)
    game_env.dynamic.powerup_sound.set_volume(10)
    game_env.dynamic.samfire_sound.set_volume(5)

    pygame.mixer.music.load(game_env.static.game_sound.get(
        'music'))  # setting main game background musicm
    pygame.mixer.music.play(loops=-1)  # lopping the main game music
    pygame.mixer.music.set_volume(.3)

    # screen = pygame.display.set_mode((game_env.static.screen_width, game_env.static.screen_height))         # creating game screen with custom width and height
    screen = pygame.display.set_mode(
        (game_env.static.screen_width, game_env.static.screen_height),
        game_env.FULLSCREEN
    )  # creating game screen with custom width and height
    pygame.display.set_caption('{} version. {}'.format(
        game_env.static.name,
        game_env.static.version))  # setting name of game window
    pygame.display.set_icon(pygame.image.load(
        game_env.static.game_icon))  # updating game icon to the jet image
    pygame.mouse.set_visible(
        False)  # hiding the mouse pointer from the game screen

    gameclock = pygame.time.Clock(
    )  # setting up game clock to maintain constant fps
    check_update(game_env)

    ADD_MISSILE = pygame.USEREVENT + 1  # creating custom event to automatically add missiles in the screen
    pygame.time.set_timer(
        ADD_MISSILE, int(1000 / game_env.static.missile_per_sec)
    )  # setting event to auto-trigger every 500ms; 2 missiles will be created every second

    ADD_CLOUD = pygame.USEREVENT + 2  # creating custom event to automatically add cloud in the screen
    pygame.time.set_timer(
        ADD_CLOUD, int(1000 / game_env.static.cloud_per_sec)
    )  # setting event to auto-trigger every 1s; 1 cloud will be created every second

    ADD_SAM_LAUNCHER = pygame.USEREVENT + 3
    pygame.time.set_timer(
        ADD_SAM_LAUNCHER, 5000
    )  # setting event to auto-trigger every 5s; 1 level can have 4 sam launcher

    running = True  # game running variable
    gameover = False  # no gameover by default
    game_started = False  # game is not started by default
    game_pause = False
    star_shown = False
    mouse_pos = (
        game_env.static.screen_width, game_env.static.screen_height / 2
    )  # default mouse position, let the jet move forward on a straight line
    screen_color = game_env.static.background_default if game_started else game_env.static.background_special

    backgrounds = pygame.sprite.Group(
    )  # creating seperate group for background sprites
    stars = pygame.sprite.GroupSingle()  # group of stars with max 1 sprite
    vegetations = pygame.sprite.Group(
    )  # creating cloud group for storing all the clouds in the game
    clouds = pygame.sprite.Group(
    )  # creating cloud group for storing all the clouds in the game
    missiles = pygame.sprite.Group(
    )  # creating missile group for storing all the missiles in the game
    deactivated_missile = pygame.sprite.Group(
    )  # creating missile group for storing all the deactivated missiles in the game
    samlaunchers = pygame.sprite.GroupSingle(
    )  # creating missile group for storing all the samlaunchers in the game
    title_sprites = pygame.sprite.Group()

    general_hint_text = "Press M->Game menu, H->Help, L->LeaderBoard, ESC->Quit"
    active_sprite = NameInputText(game_env)
    hint_sprite = Text(
        game_env,
        "Enter your name and press [ENTER] or press [ESC] to quit",
        22,
        pos_x=game_env.static.screen_width / 2,
        pos_y=145)  # creating game help
    title_banner_sprite = Text(
        game_env,
        "{} {}".format(game_env.static.name, game_env.static.version),
        100,
        pos_x=game_env.static.screen_width / 2,
        pos_y=100)  # creating title_banner_sprite text sprite with game name
    title_author_sprite = Text(game_env,
                               "By Lakhya Jyoti Nath (www.ljnath.com)",
                               26,
                               pos_x=game_env.static.screen_width / 2,
                               pos_y=game_env.static.screen_height -
                               20)  # creating game author

    if game_env.dynamic.player_name:
        hint_sprite = Text(game_env,
                           general_hint_text,
                           22,
                           pos_x=game_env.static.screen_width / 2,
                           pos_y=145)  # creating game help
        active_sprite = GameMenuText(game_env)

    game_env.dynamic.all_sprites.add(hint_sprite)
    [
        title_sprites.add(sprite)
        for sprite in (active_sprite, title_banner_sprite, title_author_sprite)
    ]  # adding all the necessary sprites to title_sprites
    [game_env.dynamic.all_sprites.add(sprite) for sprite in title_sprites
     ]  # adding all title_sprites sprite to all_sprites

    jet = Jet(game_env)  # creating jet sprite
    scoretext_sprite = ScoreText(game_env)  # creating scoreboard sprite
    game_env.dynamic.noammo_sprite = Text(game_env, "NO AMMO !",
                                          24)  # creating noammo-sprite

    create_vegetation(game_env, vegetations)
    menu_screens = {Screen.REPLAY_MENU, Screen.GAME_MENU, Screen.EXIT_MENU}
    last_active_sprite = (game_env.dynamic.active_screen, active_sprite)

    def hide_exit_menu():
        nonlocal game_pause, game_started, active_sprite
        pygame.mixer.music.unpause()
        game_started, game_pause = game_pause, game_started
        game_env.dynamic.all_sprites.remove(active_sprite)
        game_env.dynamic.active_screen, active_sprite = last_active_sprite
        if game_env.dynamic.active_screen != Screen.GAME_SCREEN:
            [
                game_env.dynamic.all_sprites.add(sprite)
                for sprite in (active_sprite, hint_sprite)
            ]

    def start_gameplay():
        nonlocal gameover, jet, star_shown, screen_color, game_started
        pygame.mouse.set_visible(
            True if game_env.dynamic.game_input == InputMode.MOUSE else
            False)  # displaying mouse cursor based on user input mode
        screen_color = game_env.static.background_default  # restoring  screen color
        [sprite.kill() for sprite in title_sprites
         ]  # kill all the title_sprites sprite sprite
        jet = Jet(game_env)  # re-creating the jet
        missiles.empty()  # empting the missle group
        game_env.dynamic.all_sprites = pygame.sprite.Group(
        )  # re-creating group of sprites
        [
            game_env.dynamic.all_sprites.remove(sprite)
            for sprite in (active_sprite, hint_sprite)
        ]  # removing active sprite and hint sprite
        [
            game_env.dynamic.all_sprites.add(sprite)
            for sprite in (jet, scoretext_sprite)
        ]  # adding the jet and scoreboard to all_sprites
        game_env.reset()  # reseting game data
        pygame.time.set_timer(ADD_MISSILE,
                              int(1000 / game_env.static.missile_per_sec)
                              )  # resetting missile creation event timer
        create_vegetation(game_env, vegetations)  # creating vegetation
        [backgrounds.add(sprite) for sprite in vegetations.sprites()
         ]  # adding vegetation to background
        game_env.dynamic.active_screen = Screen.GAME_SCREEN  # setting gamescreen as the active sprite
        game_started = True  # game has started
        gameover = False  # game is not over yet
        star_shown = False  # no star is displayed

    # Main game loop
    while running:
        for event in pygame.event.get():  # Look at every event in the queue
            # stopping game when ESC key is pressed or when the game window is closed
            if (event.type == game_env.KEYDOWN and event.key
                    == game_env.K_ESCAPE or event.type == game_env.QUIT
                ) and game_env.dynamic.active_screen != Screen.EXIT_MENU:
                pygame.mixer.music.pause()
                last_active_sprite = (game_env.dynamic.active_screen,
                                      active_sprite)
                game_started, game_pause = game_pause, game_started
                [
                    game_env.dynamic.all_sprites.remove(sprite)
                    for sprite in (active_sprite, hint_sprite)
                ]
                active_sprite = ExitMenuText(game_env)
                game_env.dynamic.all_sprites.add(active_sprite)
                game_env.dynamic.active_screen = Screen.EXIT_MENU

            # showing the exit menu when [ESC] key is pressed
            elif game_env.dynamic.active_screen == Screen.EXIT_MENU and (
                    event.type == game_env.KEYDOWN
                    and event.key == game_env.K_ESCAPE):
                hide_exit_menu()

            # start the game
            elif game_started and not gameover:
                if game_env.dynamic.game_input == InputMode.MOUSE and event.type == game_env.MOUSEMOTION:  # moving jet based on mouse movement
                    mouse_pos = pygame.mouse.get_pos(
                    )  # saving the mouse co-ordinate for smooth movement later
                elif (game_env.dynamic.game_input == InputMode.KEYBOARD
                      and event.type == game_env.KEYDOWN
                      and event.key == game_env.K_SPACE) or (
                          game_env.dynamic.game_input == InputMode.MOUSE
                          and event.type == game_env.MOUSEBUTTONDOWN):
                    jet.shoot()
                if event.type == ADD_MISSILE:  # is event to add missile is triggered; missles are not added during gameover
                    new_missile = Missile(game_env)  # create a new missile
                    missiles.add(
                        new_missile)  # adding the missile to missle group
                    game_env.dynamic.all_sprites.add(
                        new_missile
                    )  # adding the missile to all_sprites group as well
                if event.type == ADD_SAM_LAUNCHER and not samlaunchers.sprites(
                ) and game_env.dynamic.game_level > 5:
                    samlauncher = SamLauncher(game_env)
                    samlaunchers.add(samlauncher)
                    game_env.dynamic.all_sprites.add(samlauncher)

            # all keyboard key interaction
            elif event.type == game_env.KEYDOWN:  # handling all the VALID key press, action varies based on current active screen
                if not game_started and game_env.dynamic.active_screen == Screen.NAME_INPUT:
                    active_sprite.render(event.unicode)
                    if game_env.dynamic.player_name:  # if user has entered the name, then gamemenu is shown
                        [
                            game_env.dynamic.all_sprites.remove(sprite)
                            for sprite in (active_sprite, hint_sprite)
                        ]
                        active_sprite = GameMenuText(game_env)
                        hint_sprite = Text(game_env,
                                           general_hint_text,
                                           22,
                                           pos_x=game_env.static.screen_width /
                                           2,
                                           pos_y=145)
                        [
                            game_env.dynamic.all_sprites.add(sprite)
                            for sprite in (active_sprite, hint_sprite)
                        ]
                        game_env.dynamic.active_screen = Screen.GAME_MENU
                elif event.key == game_env.K_h and game_env.dynamic.active_screen not in {
                        Screen.EXIT_MENU, Screen.HELP
                }:  # displyaing the help menu
                    game_env.dynamic.all_sprites.remove(active_sprite)
                    active_sprite = HelpText(game_env)
                    game_env.dynamic.all_sprites.add(active_sprite)
                    game_env.dynamic.active_screen = Screen.HELP
                elif event.key == game_env.K_l and game_env.dynamic.active_screen not in {
                        Screen.EXIT_MENU, Screen.LEADERBOARD
                }:  # displyaing the leaderboard
                    game_env.dynamic.all_sprites.remove(active_sprite)
                    active_sprite = LeaderBoardText(game_env)
                    game_env.dynamic.all_sprites.add(active_sprite)
                    game_env.dynamic.active_screen = Screen.LEADERBOARD
                elif event.key == game_env.K_m:
                    game_env.dynamic.all_sprites.remove(active_sprite)
                    if not gameover and game_env.dynamic.active_screen not in {
                            Screen.EXIT_MENU, Screen.GAME_MENU
                    }:  # displyaing the game menu
                        active_sprite = GameMenuText(game_env)
                        game_env.dynamic.active_screen = Screen.GAME_MENU
                    elif gameover and game_env.dynamic.active_screen not in {
                            Screen.EXIT_MENU, Screen.REPLAY_MENU
                    }:  # displaying the replay menu
                        active_sprite = ReplayMenuText(game_env)
                        game_env.dynamic.active_screen = Screen.REPLAY_MENU
                    game_env.dynamic.all_sprites.add(active_sprite)
                elif event.key == game_env.K_RETURN:  # handling all [ENTER] key press activity
                    if game_env.dynamic.active_screen == Screen.GAME_MENU:  # selecting input mode in gamemenu screen
                        start_gameplay()  # starting game  on 1st start
                    elif game_env.dynamic.active_screen == Screen.REPLAY_MENU:  # selecting reply option in replaymenu screen
                        if game_env.dynamic.replay:
                            start_gameplay()  # starting game on replay
                        else:
                            running = False  # stopping game as user as opted not to replay
                    elif game_env.dynamic.active_screen == Screen.EXIT_MENU:
                        if not game_env.dynamic.exit:
                            hide_exit_menu(
                            )  # hide exitmenu if user opts to no exit the game
                        else:
                            running = False

            if event.type == ADD_CLOUD:
                if game_pause:
                    continue

                last_sprite = vegetations.sprites(
                )[-1]  # storing the last available vegetation for computation
                if last_sprite.rect.x + last_sprite.rect.width / 2 - game_env.static.screen_width < 0:  # checking if the last vegetation has appeared in the screen, if yes a new vegetation will be created and appended
                    vegetation = Vegetation(
                        game_env,
                        x_pos=last_sprite.rect.x + last_sprite.rect.width +
                        last_sprite.rect.width / 2
                    )  # position of the new sprite is after the last sprite
                    vegetations.add(
                        vegetation
                    )  # adding sprite to groups for update and display
                    backgrounds.add(vegetation)

                new_cloud = Cloud(
                    game_env)  # is event to add cloud is triggered
                clouds.add(new_cloud)  # create a new cloud
                backgrounds.add(
                    new_cloud)  # adding the cloud to all_sprites group
                if not gameover and game_started:
                    game_env.dynamic.game_playtime += 1  # increasing playtime by 1s as this event is triggered every second; just reusing existing event instead of recreating a new event
                    if not star_shown and random.randint(
                            0,
                            30) % 3 == 0:  # probabity of getting a star is 30%
                        star = Star(game_env)
                        stars.add(star)
                        game_env.dynamic.all_sprites.add(star)
                        star_shown = True
                    if game_env.dynamic.game_playtime % 20 == 0:  # changing game level very 20s
                        star_shown = False
                        game_env.dynamic.levelup_sound.play(
                        )  # playing level up sound
                        game_env.dynamic.game_level += 1  # increasing the game level
                        pygame.time.set_timer(
                            ADD_MISSILE,
                            int(1000 / (game_env.static.missile_per_sec +
                                        int(game_env.dynamic.game_level / 2)))
                        )  # updating timer of ADD_MISSLE for more missiles to be added
                        game_env.dynamic.ammo += 50  # adding 50 ammo on each level up
                        game_env.dynamic.game_score += 10  # increasing game score by 10 after each level
                        game_env.dynamic.all_sprites.remove(
                            game_env.dynamic.noammo_sprite
                        )  # removing no ammo sprite when ammo is refilled

        screen.fill(screen_color)  # Filling screen with sky blue color
        [screen.blit(sprite.surf, sprite.rect)
         for sprite in backgrounds]  # drawing all backgrounds sprites
        [
            screen.blit(sprite.surf, sprite.rect)
            for sprite in game_env.dynamic.all_sprites
        ]  # drawing all sprites in the screen

        if not gameover:
            # missile hit
            if pygame.sprite.spritecollideany(
                    jet, missiles
            ) or pygame.sprite.spritecollideany(
                    jet, game_env.dynamic.sam_missiles
            ):  # Check if any missiles have collided with the player; if so
                gameover = True  # setting gameover to true to prevent new missiles from spawning
                active_sprite = ReplayMenuText(game_env)
                game_env.dynamic.active_screen = Screen.REPLAY_MENU
                jet.kill()  # killing the jet
                [
                    sam_missile.kill()
                    for sam_missile in game_env.dynamic.sam_missiles
                ]  # killing the SAM missile
                game_env.dynamic.collision_sound.play()
                [
                    game_env.dynamic.all_sprites.add(sprite)
                    for sprite in (active_sprite, hint_sprite)
                ]  # adding the gameover and the hint sprite
                game_env.dynamic.all_sprites.remove(
                    game_env.dynamic.noammo_sprite)
                submit_result(game_env)

            # bullet hit
            collision = pygame.sprite.groupcollide(
                missiles, game_env.dynamic.bullets, True, True
            )  # checking for collision between bullets and missiles, killing each one of them on collision
            if len(collision) > 0:
                game_env.dynamic.hit_sound.play(
                )  # play missile destroyed sound
                game_env.dynamic.game_score += len(
                    collision) * 10  # 1 missle destroyed = 10 pts.
                game_env.dynamic.missiles_destroyed += len(
                    collision)  # to calulate player accuracy

            # powerup hit
            if pygame.sprite.spritecollideany(
                    jet, stars):  # collition between jet and star (powerup)
                game_env.dynamic.powerup_sound.play()
                [
                    game_env.dynamic.all_sprites.remove(s)
                    for s in stars.sprites()
                ]  # removing the star from all_sprites to hide from screen
                game_env.dynamic.game_score += 100 * game_env.dynamic.game_level  # increasing game score by 100
                stars.empty()  # removing star from stars group
                for missile in missiles.sprites():
                    missile.deactivate()  # making missile as deactivated
                    deactivated_missile.add(
                        missile)  # adding missile to deactivated_missile group
                    missiles.remove(
                        missile
                    )  # remove missiles from missles group to avoid collision with jet

        pygame.display.flip()  # updating display to the screen
        gameclock.tick(
            game_env.static.fps)  # ticking game clock at 30 to maintain 30fps

        pressed_keys = pygame.key.get_pressed()  # getting all the pressed keys
        if not game_pause and game_started and not gameover and game_env.dynamic.game_input == InputMode.KEYBOARD:
            jet.update(pressed_keys)
        elif not game_pause and game_started and not gameover and game_env.dynamic.game_input == InputMode.MOUSE:  # performing the jet movement here for smooth movement till mouse cursor
            jet.auto_move(mouse_pos)
        elif game_env.dynamic.active_screen in menu_screens:
            active_sprite.update(
                pressed_keys
            )  # handling menu interactions for all the possible interactive screens

        if not game_started:
            title_author_sprite.moveOnXaxis(
                2)  # moving the game author sprite across the X axis

        if game_pause:
            continue
        if game_started:
            vegetations.update(
            )  # vegetations will move only after the game starts

        game_env.dynamic.bullets.update()
        game_env.dynamic.sam_missiles.update()
        missiles.update()  # update the position of the missiles
        deactivated_missile.update()
        clouds.update()  # update the postition of the clouds
        stars.update()
        samlaunchers.update(
            (jet.rect.x + jet.rect.width / 2, jet.rect.y + jet.rect.height))
        scoretext_sprite.update()  # update the game score

    pygame.mixer.music.stop()  # stopping game music
    pygame.mixer.quit()  # stopping game sound mixer
    notify_user_of_update(game_env)
Example #8
0
 def __init__(self, game_env):
     Text.__init__(self, game_env, text="LEVEL 00 TIME 0 AMMO 0 SCORE 0", size=28, color=(103,103,103))                             # initializing parent class with defautl text and color
     self.__game_env = game_env
     self.rect = self.surf.get_rect(topright=(self.__game_env.static.screen_width-self.surf.get_width()/2 + 30, 2))       # creating rectangle from text surface