Beispiel #1
0
def begin_game():
    # setup
    sprite_groups.tower_icon_sprites.add(
        icon.create_tower_icon(icon.LINEAR_TOWER_ICON, (300, 100)),
        icon.create_tower_icon(icon.THREE_SIXTY_TOWER_ICON, (300, 150)),
        icon.create_tower_icon(icon.EXPLOSION_TOWER_ICON, (300, 200)),
        icon.create_tower_icon(icon.TELEPORTATION_TOWER_ICON, (300, 250)))

    basic_dashboard = pygame.draw.rect(DISPLAYSURF, colours.GRAY,
                                       (0, 300, 400, 100))

    # select font type
    bank_balance_font = game_utility.set_bank_balance_font()
    life_point_font = game_utility.set_life_point_font()

    levels = [level.Level1(), level.Level2(),
              level.Level3()]  # game_utility.create_game_levels()
    current_level = levels.pop(0)

    make_new_balloon_countdown = 10  # called every frame and dictates when to make new balloon

    # create left button click event handlers
    null_click_handler = NullClickHandler(None)
    create_new_tower_click_handler = CreateNewTowerClickHandler(
        null_click_handler)
    sell_tower_icon_click_handler = SellTowerIconClickHandler(
        create_new_tower_click_handler)
    upgrade_icon_click_handler = UpgradeIconClickHandler(
        sell_tower_icon_click_handler)
    tower_click_handler = TowerClickHandler(upgrade_icon_click_handler)
    tower_icon_click_handler = TowerIconClickHandler(tower_click_handler)

    while True:

        # if player finished this level and there are other levels remaining, start them, otherwise, proceed to "Win screen"
        if player_has_completed_level(current_level):
            if levels:
                current_level = levels.pop(0)
            else:
                return show_win_screen

        #check if the player still has life points. If not, player lost
        if life_point.life_balance <= 0:
            return show_lose_screen

        # if it's time to make a new balloon and the next balloon exists, then add that balloon to the balloon_sprites and restart countdown. If it doesn't exist, don't add it
        # if it isn't time to make a new balloon, decrement countdown
        if make_new_balloon_countdown == 0:
            if current_level.next_balloon_exists():
                sprite_groups.balloon_sprites.add(
                    current_level.get_next_balloon())
            make_new_balloon_countdown = 10
        else:
            make_new_balloon_countdown -= 1

        # handle events
        for event in pygame.event.get():
            '''Left mouse button clicked
            1. If clicked on tower icon: the mouse cursor will contain an identical tower icon, set a flag to indicate that a tower
            icon had been selected so that the next mouse click would make that tower
            2. If clicked on tower icon, show sell icon and upgrade icon
            3. If clicked on upgrade icon, notify the upgrade icon
            4. If clicked on sell tower icon, notify the sell tower icon
            5. Anywhere else
                a) if the flag to indicate to create a new tower had been set (user had clicked on a tower icon), create new tower there, and withdraw from bank
                b) nothing happens
            '''

            if event.type == pygame.locals.MOUSEBUTTONUP and event.button == 1:
                mouse_position = pygame.mouse.get_pos()
                handle_left_mouse_click(
                    tower_icon_click_handler,
                    mouse_position)  # start of chain of responsibility.

            # right mouse button is clicked. Remove the tower icon currently on the cursor (if it exists)
            elif event.type == pygame.locals.MOUSEBUTTONUP and event.button == 3:
                sprite_groups.selected_tower_icon_sprite.empty()
                sprite_groups.sell_tower_icon_sprite.empty()
                sprite_groups.upgrade_icon_sprites.empty()

            elif event.type == pygame.locals.QUIT:
                pygame.quit()
                sys.exit()

        DISPLAYSURF.fill(colours.BLACK)

        # draw dashboard and upgrade sprites
        pygame.draw.rect(DISPLAYSURF, colours.GRAY,
                         (0, 300, 400, 100))  # draw dashboard
        sprite_groups.tower_sprites.update(sprite_groups.balloon_sprites,
                                           sprite_groups.bullet_sprites)
        sprite_groups.balloon_sprites.update(sprite_groups.bullet_sprites)
        sprite_groups.bullet_sprites.update()
        sprite_groups.selected_tower_icon_sprite.update(pygame.mouse.get_pos())

        for sprite_group in sprite_groups.all_sprites:
            sprite_group.draw(DISPLAYSURF)

        # render text
        bank_balance_label = bank_balance_font.render(
            "Bank balance: {}".format(bank.balance), True, (255, 255, 0))
        DISPLAYSURF.blit(bank_balance_label, (300, 50))

        life_balance_label = life_point_font.render(
            "Life points: {}".format(life_point.life_balance), True,
            (255, 255, 0))
        DISPLAYSURF.blit(life_balance_label, (300, 30))

        fpsClock.tick(15)
        pygame.display.update()
Beispiel #2
0
    def test_tower_icon_type_with_TELEPORTATION_TOWER_TYPE(self):
        icon_type = icon.TELEPORTATION_TOWER_ICON
        position = (100, 100)
        return_value = icon.create_tower_icon(icon_type, position)

        self.assertIsInstance(return_value, icon.TeleportationTowerIcon)
Beispiel #3
0
    def test_tower_icon_type_with_EXPLOSION_TOWER_TYPE(self):
        icon_type = icon.EXPLOSION_TOWER_ICON
        position = (100, 100)
        return_value = icon.create_tower_icon(icon_type, position)

        self.assertIsInstance(return_value, icon.ExplosionTowerIcon)
Beispiel #4
0
    def test_tower_icon_type_with_THREE_SIXTY_TOWER_ICON(self):
        icon_type = icon.THREE_SIXTY_TOWER_ICON
        position = (100, 100)
        return_value = icon.create_tower_icon(icon_type, position)

        self.assertIsInstance(return_value, icon.ThreeSixtyTowerIcon)
Beispiel #5
0
    def test_tower_icon_type_with_LINEAR_TOWER_ICON(self):
        icon_type = icon.LINEAR_TOWER_ICON
        position = (100, 100)
        return_value = icon.create_tower_icon(icon_type, position)

        self.assertIsInstance(return_value, icon.LinearTowerIcon)
Beispiel #6
0
def begin_game():
    # setup
    sprite_groups.tower_icon_sprites.add(icon.create_tower_icon(icon.LINEAR_TOWER_ICON, (300, 100)),
                                         icon.create_tower_icon(icon.THREE_SIXTY_TOWER_ICON, (300, 150)),
                                         icon.create_tower_icon(icon.EXPLOSION_TOWER_ICON, (300, 200)),
                                         icon.create_tower_icon(icon.TELEPORTATION_TOWER_ICON, (300, 250)))

    basic_dashboard = pygame.draw.rect(DISPLAYSURF, colours.GRAY, (0, 300, 400, 100))

    # select font type
    bank_balance_font = game_utility.set_bank_balance_font()
    life_point_font = game_utility.set_life_point_font()

    levels = [level.Level1(),
              level.Level2(),
              level.Level3()]  # game_utility.create_game_levels()
    current_level = levels.pop(0)

    make_new_balloon_countdown = 10  # called every frame and dictates when to make new balloon

    # create left button click event handlers
    null_click_handler = NullClickHandler(None)
    create_new_tower_click_handler = CreateNewTowerClickHandler(null_click_handler)
    sell_tower_icon_click_handler = SellTowerIconClickHandler(create_new_tower_click_handler)
    upgrade_icon_click_handler = UpgradeIconClickHandler(sell_tower_icon_click_handler)
    tower_click_handler = TowerClickHandler(upgrade_icon_click_handler)
    tower_icon_click_handler = TowerIconClickHandler(tower_click_handler)

    while True:

        # if player finished this level and there are other levels remaining, start them, otherwise, proceed to "Win screen"
        if player_has_completed_level(current_level):
            if levels:
                current_level = levels.pop(0)
            else:
                return show_win_screen

        #check if the player still has life points. If not, player lost
        if life_point.life_balance <= 0:
            return show_lose_screen

        # if it's time to make a new balloon and the next balloon exists, then add that balloon to the balloon_sprites and restart countdown. If it doesn't exist, don't add it
        # if it isn't time to make a new balloon, decrement countdown
        if make_new_balloon_countdown == 0:
            if current_level.next_balloon_exists():
                sprite_groups.balloon_sprites.add(current_level.get_next_balloon())
            make_new_balloon_countdown = 10
        else:
            make_new_balloon_countdown -= 1

        # handle events
        for event in pygame.event.get():

            '''Left mouse button clicked
            1. If clicked on tower icon: the mouse cursor will contain an identical tower icon, set a flag to indicate that a tower
            icon had been selected so that the next mouse click would make that tower
            2. If clicked on tower icon, show sell icon and upgrade icon
            3. If clicked on upgrade icon, notify the upgrade icon
            4. If clicked on sell tower icon, notify the sell tower icon
            5. Anywhere else
                a) if the flag to indicate to create a new tower had been set (user had clicked on a tower icon), create new tower there, and withdraw from bank
                b) nothing happens
            '''

            if event.type == pygame.locals.MOUSEBUTTONUP and event.button == 1:
                mouse_position = pygame.mouse.get_pos()
                handle_left_mouse_click(tower_icon_click_handler, mouse_position)  # start of chain of responsibility.

            # right mouse button is clicked. Remove the tower icon currently on the cursor (if it exists)
            elif event.type == pygame.locals.MOUSEBUTTONUP and event.button == 3:
                sprite_groups.selected_tower_icon_sprite.empty()
                sprite_groups.sell_tower_icon_sprite.empty()
                sprite_groups.upgrade_icon_sprites.empty()

            elif event.type == pygame.locals.QUIT:
                pygame.quit()
                sys.exit()

        DISPLAYSURF.fill(colours.BLACK)

        # draw dashboard and upgrade sprites
        pygame.draw.rect(DISPLAYSURF, colours.GRAY, (0, 300, 400, 100))  # draw dashboard
        sprite_groups.tower_sprites.update(sprite_groups.balloon_sprites, sprite_groups.bullet_sprites)
        sprite_groups.balloon_sprites.update(sprite_groups.bullet_sprites)
        sprite_groups.bullet_sprites.update()
        sprite_groups.selected_tower_icon_sprite.update(pygame.mouse.get_pos())

        for sprite_group in sprite_groups.all_sprites:
            sprite_group.draw(DISPLAYSURF)



        # render text
        bank_balance_label = bank_balance_font.render("Bank balance: {}".format(bank.balance), True, (255, 255, 0))
        DISPLAYSURF.blit(bank_balance_label, (300, 50))

        life_balance_label = life_point_font.render("Life points: {}".format(life_point.life_balance), True, (255, 255, 0))
        DISPLAYSURF.blit(life_balance_label, (300, 30))

        fpsClock.tick(15)
        pygame.display.update()