Пример #1
0
    def mouse_input(self, mouse=None):
        ##============================================================================
        if not mouse:
            mouse = libtcod.mouse_get_status()
        mx = mouse.cx - (self.x_pos + self.dest_x)
        my = mouse.cy - (self.y_pos + self.dest_y)

        if mx >= 0 and mx <= self.width and my >= 0 and my <= self.height:
            self.label = color_text(self.label_o, libtcod.red)
            if mouse.lbutton:
                self.label = color_text(self.label_o, libtcod.green)
                if mouse.lbutton_pressed:
                    if self.type is True:
                        return 1
                    else:
                        return 0
            if mouse.lbutton_pressed:
                if self.type is True:
                    return 1
                else:
                    return 0
        else:
            self.label = color_text(self.label_o, libtcod.white)

        return -1
Пример #2
0
    def mouse_input(self, mouse=None):
##============================================================================
        if not mouse:
            mouse = libtcod.mouse_get_status()
        mx = mouse.cx - (self.x_pos + self.dest_x)
        my = mouse.cy - (self.y_pos + self.dest_y)

        if mx >= 0 and mx <= self.width and my >= 0 and my <= self.height:
            self.label = color_text(self.label_o,libtcod.red)
            if mouse.lbutton:
                self.label = color_text(self.label_o,libtcod.green)
                if mouse.lbutton_pressed:
                    if self.type is True:
                        return 1
                    else:
                        return 0
            if mouse.lbutton_pressed:
                if self.type is True:
                    return 1
                else:
                    return 0
        else:
            self.label = color_text(self.label_o, libtcod.white)

        return -1
Пример #3
0
    def update(self, mouse, key, game):
        """
        Handles keyboard and mouse input prior to rendering and calls self.render()
        If activated, calls attached objects use function
        :param mouse: mouse input
        :param key: key input
        :return: Activated? (t/f)
        """
        self.gEngine.console_clear(self.window)
        r, g, b = libtcod.white
        if mouse.cx >= self.cx and mouse.cx <= self.cx + 2:
            if mouse.cy >= self.cy and mouse.cy <= self.cy + 2:
                r, g, b = libtcod.green
                t = self.name.capitalize()
                t = chr(libtcod.CHAR_TEEW) + t
                if self.obj:
                    if self.obj.item:
                        t += ' (%d)' % self.obj.item.qty + chr(libtcod.CHAR_TEEE)
                    else:
                        t += chr(libtcod.CHAR_TEEE)
                else:
                    t += chr(libtcod.CHAR_TEEE)
                t, p = get_centered_text(t, 16)
                self.gEngine.console_print(self.owner.window, p, 0, t)
                if mouse.lbutton:
                    r, g, b = libtcod.red
                if mouse.lbutton_pressed:
                    self.use(game)
                if mouse.rbutton_pressed:
                    self.remove_object()


        self.gEngine.console_set_default_foreground(self.window, r, g, b)
        self.gEngine.console_print_frame(self.window, 0, 0, 3, 3, True)
        self.gEngine.console_print(self.window, 0, 0, self.label)

        if self.obj:
            c = color_text(self.obj.char, self.obj.color)
            self.gEngine.console_print(self.window, 1, 1, c)
        else:
            c = color_text('X', libtcod.red)
            self.gEngine.console_print(self.window, 1, 1, c)
Пример #4
0
    def update(self, mouse, key, game):
        """
        Handles keyboard and mouse input prior to rendering and calls self.render()
        If activated, calls attached objects use function
        :param mouse: mouse input
        :param key: key input
        :return: Activated? (t/f)
        """
        self.gEngine.console_clear(self.window)
        r, g, b = libtcod.white
        if mouse.cx >= self.cx and mouse.cx <= self.cx + 2:
            if mouse.cy >= self.cy and mouse.cy <= self.cy + 2:
                r, g, b = libtcod.green
                t = self.name.capitalize()
                t = chr(libtcod.CHAR_TEEW) + t
                if self.obj:
                    if self.obj.item:
                        t += ' (%d)' % self.obj.item.qty + chr(
                            libtcod.CHAR_TEEE)
                    else:
                        t += chr(libtcod.CHAR_TEEE)
                else:
                    t += chr(libtcod.CHAR_TEEE)
                t, p = get_centered_text(t, 16)
                self.gEngine.console_print(self.owner.window, p, 0, t)
                if mouse.lbutton:
                    r, g, b = libtcod.red
                if mouse.lbutton_pressed:
                    self.use(game)
                if mouse.rbutton_pressed:
                    self.remove_object()

        self.gEngine.console_set_default_foreground(self.window, r, g, b)
        self.gEngine.console_print_frame(self.window, 0, 0, 3, 3, True)
        self.gEngine.console_print(self.window, 0, 0, self.label)

        if self.obj:
            c = color_text(self.obj.char, self.obj.color)
            self.gEngine.console_print(self.window, 1, 1, c)
        else:
            c = color_text('X', libtcod.red)
            self.gEngine.console_print(self.window, 1, 1, c)
Пример #5
0
def inventory(con, player, game, width=80, height=43):
    """
    TODO:
        Add in highlighting for Weapons and Equipment consoles
        Add in keyboard arrow selection support
        Add in "drop" mode, drop items from inventory
        Fix Weapons and Equipment keyboard handling
        Fix duplication bug when equipping items (fixed)
        Fix take off weapon confirmation when equipping an item (fixed)
        2 handed code needs work (?)
        keyboard input is sluggish. Might have to update libtcod to fix

    :param con: Destination console (not used atm)
    :param player: The main player object
    :param game: The main game object
    :param width: width of the inventory screen
    :param height: height of the inventory screen
    :return: An item that has been used (potion, scroll, etc..)
    """
    equip_height = 14
    wield_height = 8
    compare_height = height - (equip_height - wield_height)-(wield_height*2)

    r, g, b = libtcod.white
    equip_y = wield_height
    compare_y = equip_height + wield_height

    inventory_window = game.gEngine.console_new(width/2, height)
    game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
    game.gEngine.console_print_frame(inventory_window, 0, 0, width/2, height, True)
    game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)

    equipment_window = game.gEngine.console_new(width/2, equip_height)
    game.gEngine.console_set_default_foreground(equipment_window, r, g, b)
    game.gEngine.console_print_frame(equipment_window, 0, 0, width/2, equip_height, True)
    game.gEngine.console_set_default_background(equipment_window, 0, 0, 0)

    wielded_window = game.gEngine.console_new(width/2, wield_height)
    game.gEngine.console_set_default_foreground(wielded_window, r, g, b)
    game.gEngine.console_print_frame(wielded_window, 0, 0, width/2, wield_height, True)
    game.gEngine.console_set_default_background(wielded_window, 0, 0, 0)

    compare_window = game.gEngine.console_new(width/2, compare_height)
    game.gEngine.console_set_default_foreground(compare_window, r, g, b)
    game.gEngine.console_print_frame(compare_window, 0, 0, width/2, compare_height, True)
    game.gEngine.console_set_default_background(compare_window, 0, 0, 0)

    check_boxes = []
    slots = ['Torso    ',
             'Head     ',
             'Hands    ',
             'Legs     ',
             'Feet     ',
             'Arms     ',
             'Shoulders',
             'Back     ']
    #self.buttons.append(Button(self, self.option_labels[0], self.width//6-5, self.height/2-1, True))
    exit_button = Button(label='Exit', game=game, x_pos=(width/2)-9, y_pos=height-6,
                         window=inventory_window, dest_x=width/2, dest_y=0)
    drop_button = Button(label='Drop', game=game, x_pos=1, y_pos=height-6,
                         window=inventory_window, dest_x=width/2, dest_y=0)
    if len(player.fighter.inventory) == 0:
        inventory_items = ['Inventory is empty.']
    else:
        inventory_items = []
        for x in range(len(player.fighter.inventory)):
            check_boxes.append(CheckBox(x=1, y=x+3))
            i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color)
            if player.fighter.inventory[x].item.check_stackable:
                i += ' (%d)' % player.fighter.inventory[x].item.qty
            inventory_items.append(i)
    i_header = 'Inventory'
    i_header_size = len(i_header)
    i_header_pos = (width/4)-(i_header_size/2)

    w_header = 'Weapons'
    w_header_size = len(w_header)
    w_header_pos = (width/8) - (w_header_size/2)

    e_header = 'Equipment'
    e_header_size = len(e_header)
    e_header_pos = (width/8) - (e_header_size/2)

    c_header = 'Compare/Examine'
    c_header_size = len(c_header)
    c_header_pos = (width/8) - (c_header_size/2)

    return_item = None
    key = libtcod.console_check_for_keypress()
    current_selection = 0
    master_check = CheckBox(1, 30, "Check/Uncheck All")
    while key.vk != libtcod.KEY_ESCAPE:
        game.gEngine.console_flush()
        # get input just after flush
        key = libtcod.console_check_for_keypress(True)
        mouse = libtcod.mouse_get_status()
        exit_input = exit_button.display(mouse)
        drop_input = drop_button.display(mouse)

        game.gEngine.console_blit(inventory_window, 0, 0, width/2, height, 0, (width/2), 0, 1.0, 1.0)
        game.gEngine.console_blit(wielded_window, 0, 0, width/2, height, 0, 0, 0, 1.0, 1.0)
        game.gEngine.console_blit(equipment_window, 0, 0, width/2, height, 0, 0, equip_y, 1.0, 1.0)
        game.gEngine.console_blit(compare_window, 0, 0, width/2, height, 0, 0, compare_y, 1.0, 1.0)

        game.gEngine.console_clear(inventory_window)
        game.gEngine.console_clear(wielded_window)
        game.gEngine.console_clear(equipment_window)
        game.gEngine.console_clear(compare_window)

        # set up draw screen
        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
        game.gEngine.console_print_frame(inventory_window, 0, 0, width/2, height, True)

        game.gEngine.console_set_default_foreground(equipment_window, r, g, b)
        game.gEngine.console_print_frame(equipment_window, 0, 0, width/2, equip_height, True)

        game.gEngine.console_set_default_foreground(wielded_window, r, g, b)
        game.gEngine.console_print_frame(wielded_window, 0, 0, width/2, wield_height, True)

        game.gEngine.console_set_default_foreground(compare_window, r, g, b)
        game.gEngine.console_print_frame(compare_window, 0, 0, width/2, compare_height, True)

        # ========================================================================
        # print inventory
        # ========================================================================
        game.gEngine.console_print(inventory_window, i_header_pos, 0, i_header)
        letter_index = ord('a')
        if len(player.fighter.inventory) > 0:
            y = 1
            for i in range(len(inventory_items)):
                text = '  (' + chr(letter_index) + ') ' + inventory_items[i]
                if current_selection == y - 1:
                    r, g, b = libtcod.color_lerp(player.fighter.inventory[i].color, libtcod.blue, 0.5)
                    game.gEngine.console_set_default_background(inventory_window, r, g, b)
                else:
                    game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
                game.gEngine.console_print_ex(inventory_window, 1, y+2, libtcod.BKGND_SET, libtcod.LEFT, text)
                y += 1
                letter_index += 1
        game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
        game.gEngine.console_print(inventory_window, 1, 31, 'Gold: ' + color_text(str(player.fighter.money), libtcod.gold))

        # ========================================================================
        # print equipped weapons
        # ========================================================================
        game.gEngine.console_print(wielded_window, w_header_pos, 0, w_header)
        index = ord('1')
        if player.fighter.wielded[0] is None:
            text = '(' + chr(index) + ') ' + 'Left Hand : ' + color_text('Empty', libtcod.darker_gray)
        else:
            t = color_text(player.fighter.wielded[0].name.capitalize(), player.fighter.wielded[0].color)
            text = '(' + chr(index) + ') ' + 'Left Hand : ' + t
        index += 1
        game.gEngine.console_print(wielded_window, 1, 2, text)
        if player.fighter.wielded[1] is None:
            text = '(' + chr(index) + ') ' + 'Right Hand: ' + color_text('Empty', libtcod.darker_gray)
        else:
            t = color_text(player.fighter.wielded[1].name.capitalize(), player.fighter.wielded[1].color)
            text = '(' + chr(index) + ') ' + 'Right Hand: ' + t
        index += 1
        game.gEngine.console_print(wielded_window, 1, 3, text)

        item = player.fighter.wielded[0]
        if item:
            damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub)
            text = 'Damage   (total): ' + color_text(damage, libtcod.green)
            game.gEngine.console_print(wielded_window, 1, 4, text)
            accuracy = item.item.equipment.accuracy
            accuracy += game.player.fighter.get_skill(item.item.equipment.damage_type).get_bonus()
            text = 'Accuracy (total): ' + color_text(str(accuracy), libtcod.green)
            game.gEngine.console_print(wielded_window, 1, 5, text)

        # ========================================================================
        # print equipped armor
        # ========================================================================
        game.gEngine.console_print(equipment_window, e_header_pos, 0, e_header)
        i = 0
        game.gEngine.console_set_alignment(equipment_window, libtcod.LEFT)
        armor_bonus = 0
        armor_penalty = 0
        for item in player.fighter.equipment:
            text = '(' + chr(index) + ') ' + slots[i] + ': '
            if item is None:
                text += color_text('Empty', libtcod.darker_gray)
            else:
                text += color_text(item.name.capitalize(), item.color)
                armor_bonus += item.item.equipment.bonus
                armor_penalty += item.item.equipment.penalty
            game.gEngine.console_print(equipment_window, 1, i+2, text)
            i += 1
            index += 1
        c = color_text(str(armor_bonus), libtcod.green)
        text = 'Total Armor Bonus  :  ' + c
        game.gEngine.console_print(equipment_window, 1, i+2, text)
        c = color_text(str(armor_penalty), libtcod.red)
        text = 'Total Armor Penalty: -' + c
        game.gEngine.console_print(equipment_window, 1, i+3, text)
        game.gEngine.console_print(compare_window, c_header_pos, 0, c_header)

        # ========================================================================
        # handle mouse input
        # ========================================================================
        mc = master_check.update(mouse, width)
        master_check.render(inventory_window, game)
        if not mc:
            for box in check_boxes:
                box.update(mouse, width)
                box.render(inventory_window, game)
        else:
            for box in check_boxes:
                box.set_checked(master_check.get_checked())
                box.render(inventory_window, game)

        # Inventory input
        if mouse.cx >= width/2+3 and mouse.cx < width-2:  # inventory screen dims
            if (mouse.cy-3) < len(player.fighter.inventory) and mouse.cy-3 >= 0:
                #if mouse.cy-3 <= len(player.fighter.inventory):
                    item = player.fighter.inventory[mouse.cy-3]
                    current_selection = mouse.cy-3
                    if item.item.equipment:
                        game.gEngine.console_print(compare_window, 1, 2, 'Name    : ' + color_text(item.name.capitalize(), item.color))
                        game.gEngine.console_print(compare_window, 1, 3, 'Type    : ' + item.item.equipment.type.capitalize())
                        if item.item.equipment.type == 'melee':
                            damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub )
                            game.gEngine.console_print(compare_window, 1, 4, 'Damage  : ' + damage)
                            game.gEngine.console_print(compare_window, 1, 5, 'Accuracy: ' + str(item.item.equipment.accuracy))
                            game.gEngine.console_print(compare_window, 1, 7, 'Skill   : ' + item.item.equipment.damage_type)
                        else:
                            game.gEngine.console_print(compare_window, 1, 4, 'Armor   : ' + str(item.item.equipment.bonus))
                            game.gEngine.console_print(compare_window, 1, 5, 'Penalty : ' + str(item.item.equipment.penalty))
                            game.gEngine.console_print(compare_window, 1, 7, 'Location: ' + item.item.equipment.location.capitalize())
                        game.gEngine.console_print(compare_window, 1, 6, 'Value   : ' + str(item.item.value))
                    if item.item.spell:
                        game.gEngine.console_print(compare_window, 1, 2, 'Name  : ' + color_text(item.name.capitalize(), item.color))
                        game.gEngine.console_print(compare_window, 1, 3, 'Type  : ' + item.item.spell.type.capitalize())
                        game.gEngine.console_print(compare_window, 1, 4, 'Power : ' + str(item.item.spell.min) + '-' + str(item.item.spell.max))
                        game.gEngine.console_print(compare_window, 1, 5, 'Range : ' + str(item.item.spell.range))
                        game.gEngine.console_print(compare_window, 1, 6, 'Radius: ' + str(item.item.spell.radius))
                        game.gEngine.console_print(compare_window, 1, 7, 'Value : ' + str(item.item.value))
                    if mouse.lbutton_pressed and item.item.spell:
                        i_n = color_text(item.name.capitalize(), item.color)
                        message = 'Do you want to use %s?' % i_n
                        w = len(message)+2
                        d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window)
                        confirm = d_box.display_box()
                        if confirm == 1:  # make sure if the player uses a scroll or potion, we exit inventory
                            d_box.destroy_box()
                            # Remember to remove consoles in reverse order of creation to avoid OOB errors
                            return_item = item
                            break
                        else:
                            d_box.destroy_box()
                    if mouse.lbutton_pressed and item.item.equipment:
                        i_n = color_text(item.name.capitalize(), item.color)
                        message = 'Do you want to put %s on?' % i_n
                        w = len(message)+2
                        d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window)
                        confirm = d_box.display_box()
                        if confirm == 1:
                            item.item.use(game.player.fighter.inventory, game.player, game)
                            d_box.destroy_box()
                            inventory_items = []
                            check_boxes = []
                            for x in range(len(player.fighter.inventory)):
                                check_boxes.append(CheckBox(x=1, y=x+3))
                                i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color)
                                if player.fighter.inventory[x].item.check_stackable:
                                    i += ' (%d)' % player.fighter.inventory[x].item.qty
                                inventory_items.append(i)
            else:
                current_selection = None
        # game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
        # Wielded
        elif mouse.cx >= 0 and mouse.cx <= ((width/2)-2):  # inventory screen dims
            if (mouse.cy-2) < len(player.fighter.wielded):
                item = player.fighter.wielded[mouse.cy-2]
                if item is not None:
                    if item.item.equipment:
                        game.gEngine.console_print(compare_window, 1, 2, 'Name    : ' + color_text(item.name.capitalize(), item.color))
                        game.gEngine.console_print(compare_window, 1, 3, 'Type    : ' + item.item.equipment.type.capitalize())
                        damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub )
                        game.gEngine.console_print(compare_window, 1, 4, 'Damage  : ' + damage)
                        game.gEngine.console_print(compare_window, 1, 5, 'Accuracy: ' + str(item.item.equipment.accuracy))
                        game.gEngine.console_print(compare_window, 1, 6, 'Value   : ' + str(item.item.value))

        # Equipment
            elif (mouse.cy-2)-equip_y < len(player.fighter.equipment):
                item = player.fighter.equipment[mouse.cy-2-equip_y]
                if item is not None:
                    if item.item.equipment:
                        game.gEngine.console_print(compare_window, 1, 2, 'Name    : ' + color_text(item.name.capitalize(), item.color))
                        game.gEngine.console_print(compare_window, 1, 3, 'Type    : ' + item.item.equipment.type.capitalize())
                        game.gEngine.console_print(compare_window, 1, 4, 'Armor   : ' + str(item.item.equipment.bonus))
                        game.gEngine.console_print(compare_window, 1, 5, 'Penalty : ' + str(item.item.equipment.penalty))
                        game.gEngine.console_print(compare_window, 1, 6, 'Value   : ' + str(item.item.value))
            if mouse.lbutton_pressed and item is not None:
                i_n = color_text(item.name.capitalize(), item.color)
                message = 'Do you want to take %s off?' % i_n
                w = len(message)+2
                d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window)
                confirm = d_box.display_box()
                if confirm == 1:
                    item.item.equipment.un_equip(game.player, item)
                    d_box.destroy_box()
                    inventory_items = []
                    check_boxes = []
                    for x in range(len(player.fighter.inventory)):
                        check_boxes.append(CheckBox(x=1, y=x+3))
                        i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color)
                        if player.fighter.inventory[x].item.check_stackable:
                            i += ' (%d)' % player.fighter.inventory[x].item.qty
                        inventory_items.append(i)
                    i = 0
                    for x in player.fighter.wielded:
                        if x == item:
                            player.fighter.wielded[i] = None
                        i += 1
                    i = 0
                    for x in player.fighter.equipment:
                        if x == item:
                            player.fighter.equipment[i] = None
                        i += 1

        # keyboard input
        # keeps similar feel to old inventory if using the keys
        # keyboard input is sluggish as balls for some reason, need to look into it more
        index = key.c - ord('a')
        if key:
            if index >= 0 and index < len(inventory_items):
                return_item = player.fighter.inventory[index]
                break
            index = key.c - ord('1')
            if index >= 0 and index <= 1:
                return_item = player.fighter.wielded[index]
                break
            elif index >= 2 and index <= 10:
                return_item = player.fighter.equipment[index-2]
                break
            if key.vk == libtcod.KEY_DOWN:
                if current_selection is None:
                    current_selection = 0
                current_selection += 1
                if current_selection > len(inventory_items):
                    current_selection = 0
            if key.vk == libtcod.KEY_UP:
                if current_selection is None:
                    current_selection = 0
                current_selection -= 1
                if current_selection < 0:
                    current_selection = len(inventory_items)
        # ========================================================================
        # handle buttons
        # ========================================================================
        for i in drop_input:
            if i != -1:
                message = 'Drop all selected items?'
                w = len(message)+2
                d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='option', con=inventory_window)
                confirm = d_box.display_box()
                if confirm == 1:
                    d_box.destroy_box()
                    master_check.set_checked(False)
                    items_to_drop = []
                    for box in check_boxes:
                        if box.get_checked():
                            items_to_drop.append(player.fighter.inventory[box.y-3])
                    for item_to_drop in items_to_drop:
                        if item_to_drop:
                            item_to_drop.objects = game.objects
                            item_to_drop.item.drop(player.fighter.inventory, player, False)
                            item_to_drop.send_to_back()
                    check_boxes = []
                    inventory_items = []
                    for x in range(len(player.fighter.inventory)):
                        check_boxes.append(CheckBox(x=1, y=x+3))
                        inventory_items.append(color_text(player.fighter.inventory[x].name.capitalize(),
                                                          player.fighter.inventory[x].color))
                else:
                    d_box.destroy_box()
        # ========================================================================
        # handle exit button
        # ========================================================================

        for i in exit_input:
            if i != -1:
                key.vk = libtcod.KEY_ESCAPE
                break
    # Remember to remove consoles in reverse order of creation to avoid OOB errors
    drop_button.destroy_button()
    exit_button.destroy_button()
    game.gEngine.console_remove_console(compare_window)
    game.gEngine.console_remove_console(wielded_window)
    game.gEngine.console_remove_console(equipment_window)
    game.gEngine.console_remove_console(inventory_window)

    return return_item
Пример #6
0
def shop(con, player, game, container=None, bg=None, header=None, width=80, height=50, splash=False):
    """
    TODO:
        Make sure you cannot buy items if it were to put you over inventory limit - Done
        Attach this to a shopkeeper to control splash and inventory.
        Add a buyback screen to buy back things sold by accident

    :param con: Destination Console (not used atm)
    :param player: The Player Object
    :param game: The main game object
    :param container: The contents of the store
    :param bg: The background image to use
    :param header: The title of the store
    :param width: width of the shop
    :param height: height of the shop
    :param splash: Whether or not to display the splash screen
    :return: Nothing
    """
    if bg:
        dark_bg = game.gEngine.image_load(bg)
        bg = game.gEngine.image_load(bg)
        w, h = game.gEngine.image_get_size(dark_bg)
        for y in range(w):
            for x in range(h):
                col = game.gEngine.image_get_pixel(dark_bg, y, x)
                col = libtcod.color_lerp(col, libtcod.black, 0.9)
                #col = libtcod.color_lerp(col, libtcod.light_azure, 0.2)
                r, g, b = col
                game.gEngine.image_put_pixel(dark_bg, y, x, r, g, b)

    shop_height = 28
    compare_height = height - shop_height
    s_options = []
    s_gold = []
    s_size = 0
    if container:
        for obj in container:
            obj_text = color_text(obj.name.capitalize(), obj.color)
            ob_value = color_text(obj.item.value, libtcod.gold)
            ob_value = '(%s)' % ob_value
            s_gold.append(ob_value)
            opt = '[%s] ' % obj_text
            s_options.append(opt)
            if len(obj.name)+2 > s_size:
                s_size = len(obj.name)+2
    r, g, b = libtcod.white
    compare_y = shop_height

    inventory_window = game.gEngine.console_new(width/2, height)
    game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
    game.gEngine.console_print_frame(inventory_window, 0, 0, width/2, height, True)

    shop_window = game.gEngine.console_new(width/2, shop_height)
    game.gEngine.console_set_default_foreground(shop_window, r, g, b)
    game.gEngine.console_print_frame(shop_window, 0, 0, width/2, shop_height, True)

    compare_window = game.gEngine.console_new(width/2, compare_height)
    game.gEngine.console_set_default_foreground(compare_window, r, g, b)
    game.gEngine.console_print_frame(compare_window, 0, 0, width/2, compare_height, True)

    i_check_boxes = []
    s_check_boxes = []

    exit_button = Button(label='Exit', game=game, x_pos=(width/2)-9, y_pos=height-6,
                         window=inventory_window, dest_x=width/2, dest_y=0)

    sell_button = Button(label='Sell', game=game, x_pos=1, y_pos=height-6,
                         window=inventory_window, dest_x=width/2, dest_y=0)

    buy_button = Button(label='Buy', game=game, x_pos=1, y_pos=compare_height-6,
                        window=compare_window, dest_x=0, dest_y=compare_y)

    if len(player.fighter.inventory) == 0:
        inventory_items = ['Inventory is empty.']
    else:
        inventory_items = []
        for x in range(len(player.fighter.inventory)):
            i_check_boxes.append(CheckBox(x=1, y=x+1))
            i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color)
            if player.fighter.inventory[x].item.check_stackable:
                i += ' (%d)' % player.fighter.inventory[x].item.qty
            inventory_items.append(i)
    for x in range(len(container)):
        s_check_boxes.append(CheckBox(x=1, y=x+1))

    i_header = 'Inventory'
    i_header_size = len(i_header)
    i_header_pos = (width/4)-(i_header_size/2)

    if header:
        s_header = header
    else:
        s_header = 'Shop'
    s_header_size = len(s_header)
    s_header_pos = (width/4) - (s_header_size/2)

    c_header = 'Compare/Examine'
    c_header_size = len(c_header)
    c_header_pos = (width/4) - (c_header_size/2)

    key = libtcod.console_check_for_keypress()
    mouse = libtcod.mouse_get_status()
    current_selection = 0
    s_current_selection = 0
    i_master_check = CheckBox(1, 30, "Check/Uncheck All")
    s_master_check = CheckBox(1, 26, "Check/Uncheck All")
    fade = 1
    if splash:
        splash_screen = game.gEngine.console_new(width, height)
    while key.vk != libtcod.KEY_ESCAPE:
        game.gEngine.console_flush()
        # get input just after flush
        key = libtcod.console_check_for_keypress(True)
        mouse = libtcod.mouse_get_status()
        exit_input = exit_button.display(mouse)
        sell_input = sell_button.display(mouse)
        buy_input = buy_button.display(mouse)

        game.gEngine.console_blit(inventory_window, 0, 0, width/2, height, 0, (width/2), 0, 1.0, 1.0)
        game.gEngine.console_blit(shop_window, 0, 0, width/2, shop_height, 0, 0, 0, 1.0, 1.0)
        game.gEngine.console_blit(compare_window, 0, 0, width/2, compare_height, 0, 0, compare_y, 1.0, 1.0)
        if splash:
            if bg:
                if fade > 0:
                    game.gEngine.console_blit(splash_screen, 0, 0, width, height, 0, 0, 0, fade, fade)
                    game.gEngine.image_blit_2x(bg, splash_screen, 0, 0, 0, 0)
                    fade -= 0.045
                else:
                    fade = 0
        game.gEngine.console_clear(inventory_window)
        game.gEngine.console_clear(shop_window)
        game.gEngine.console_clear(compare_window)

        # set up draw screen
        r, g, b = libtcod.white

        # ========================================================================
        # print inventory
        # ========================================================================
        if bg:
            game.gEngine.image_blit_2x(dark_bg, inventory_window, 0, 0, width, 0)
        game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
        game.gEngine.console_print_frame(inventory_window, 0, 0, width/2, height, False)
        game.gEngine.console_print(inventory_window, i_header_pos, 0, i_header)
        if len(player.fighter.inventory) > 0:
            letter_index = ord('a')
            y = 1
            for i in range(len(inventory_items)):
                text = '  (' + chr(letter_index) + ') ' + inventory_items[i]
                if current_selection == y - 1:
                    r, g, b = libtcod.color_lerp(player.fighter.inventory[i].color, libtcod.blue, 0.5)
                    game.gEngine.console_set_default_background(inventory_window, r, g, b)
                else:
                    game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
                game.gEngine.console_print_ex(inventory_window, 1, y, libtcod.BKGND_SET, libtcod.LEFT, text)
                y += 1
                letter_index += 1
        game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
        game.gEngine.console_print(inventory_window, 1, 31, 'Gold: ' + color_text(str(player.fighter.money), libtcod.gold))
        r, g, b = libtcod.white

        # ========================================================================
        # Print Shop Window
        # ========================================================================
        if bg:
            game.gEngine.image_blit_2x(dark_bg, shop_window, 0, 0)
        game.gEngine.console_set_default_foreground(shop_window, r, g, b)
        game.gEngine.console_print_frame(shop_window, 0, 0, width/2, shop_height, False)
        game.gEngine.console_print(shop_window, s_header_pos, 0, s_header)
        if len(s_options) > 0:
            letter_index = ord('0')
            y = 1
            for i in range(len(s_options)):
                text = '  (' + chr(letter_index) + ') ' + s_options[i]
                if s_current_selection == y -1:
                    rr, gg, bb = libtcod.color_lerp(container[i].color, libtcod.blue, 0.5)
                    game.gEngine.console_set_default_background(shop_window, rr, gg, bb)
                else:
                    game.gEngine.console_set_default_background(shop_window, 0, 0, 0)
                game.gEngine.console_print_ex(shop_window, 1, y, libtcod.BKGND_SET, libtcod.LEFT, text)
                game.gEngine.console_print_ex(shop_window, s_size+10, y, libtcod.BKGND_SET, libtcod.RIGHT, s_gold[i])
                y += 1
                letter_index += 1
            game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
            r, g, b = libtcod.white

        # ========================================================================
        # Print Compare Window
        # ========================================================================
        if bg:
            game.gEngine.image_blit_2x(dark_bg, compare_window, 0, 0, sx=0, sy=compare_y*2)
        game.gEngine.console_set_default_foreground(compare_window, r, g, b)
        game.gEngine.console_print_frame(compare_window, 0, 0, width/2, compare_height, False)
        game.gEngine.console_print(compare_window, c_header_pos, 0, c_header)

        # ========================================================================
        # handle mouse input
        # ========================================================================
        #Render check boxes
        #inventory check boxes
        i_mc = i_master_check.update(mouse, width)
        i_master_check.render(inventory_window, game)
        sell_value = 0
        if not i_mc:
            for box in i_check_boxes:
                box.update(mouse, width)
                box.render(inventory_window, game)
                if box.get_checked():
                    sell_value += player.fighter.inventory[box.y-1].item.value/2
        else:
            for box in i_check_boxes:
                box.set_checked(i_master_check.get_checked())
                box.render(inventory_window, game)
                if box.get_checked():
                    sell_value += player.fighter.inventory[box.y-1].item.value/2
        game.gEngine.console_print(inventory_window, 1, 32, 'Sell Value: ' + color_text(str(sell_value), libtcod.gold))

        #Shop check boxes
        s_mc = s_master_check.update(mouse)
        s_master_check.render(shop_window, game)
        buy_value = 0
        if not s_mc:
            for box in s_check_boxes:
                box.update(mouse)
                box.render(shop_window, game)
                if box.get_checked():
                    buy_value += container[box.y-1].item.value
        else:
            for box in s_check_boxes:
                box.set_checked(s_master_check.get_checked())
                box.render(shop_window, game)
                if box.get_checked():
                    buy_value += container[box.y-1].item.value
        game.gEngine.console_print(shop_window, 1, 25, 'Buy Value: ' + color_text(str(buy_value), libtcod.gold))

        # Inventory input
        if mouse.cx >= width/2+3 and mouse.cx < width-2:  # inventory screen dims
            if (mouse.cy-1) < len(player.fighter.inventory) and mouse.cy-1 >= 0:
                item = player.fighter.inventory[mouse.cy-1]
                current_selection = mouse.cy-1
                game.gEngine.console_set_default_background(compare_window, 0, 0, 0)
                if item.item.equipment:
                    game.gEngine.console_print_ex(compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name    : ' + color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type    : ' + item.item.equipment.type.capitalize())
                    if item.item.equipment.type == 'melee':
                        damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub )
                        game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Damage  : ' + damage)
                        game.gEngine.console_print_ex(compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Accuracy: ' + str(item.item.equipment.accuracy))
                        game.gEngine.console_print_ex(compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Skill   : ' + item.item.equipment.damage_type)
                    else:
                        game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Armor   : ' + str(item.item.equipment.bonus))
                        game.gEngine.console_print_ex(compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Penalty : ' + str(item.item.equipment.penalty))
                        game.gEngine.console_print_ex(compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Location: ' + item.item.equipment.location.capitalize())
                    game.gEngine.console_print_ex(compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Value   : ' + str(item.item.value))
                if item.item.spell:
                    game.gEngine.console_print_ex(compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name  : ' + color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type  : ' + item.item.spell.type.capitalize())
                    game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Power : ' + str(item.item.spell.min) + '-' + str(item.item.spell.max))
                    game.gEngine.console_print_ex(compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Range : ' + str(item.item.spell.range))
                    game.gEngine.console_print_ex(compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Radius: ' + str(item.item.spell.radius))
                    game.gEngine.console_print_ex(compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Value : ' + str(item.item.value))
                if mouse.lbutton_pressed:
                    i_n = color_text(item.name.capitalize(), item.color)
                    price = color_text(item.item.value/2, libtcod.gold)
                    message = 'Sell %s for %s?' % (i_n, price)
                    w = len(message)+2
                    d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window)
                    confirm = d_box.display_box()
                    if confirm == 1:
                        player.fighter.money += item.item.value/2
                        player.fighter.inventory.remove(item)
                        inventory_items = []
                        i_check_boxes = []
                        for x in range(len(player.fighter.inventory)):
                            i_check_boxes.append(CheckBox(x=1, y=x+1))
                            i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color)
                            if player.fighter.inventory[x].item.check_stackable:
                                i += ' (%d)' % player.fighter.inventory[x].item.qty
                            inventory_items.append(i)

        # shop input
        if mouse.cx >= 0 and mouse.cx < width/2-2:  # shop screen dims
            if (mouse.cy-1) < len(container) and mouse.cy-1 >= 0:
                item = container[mouse.cy-1]
                s_current_selection = mouse.cy-1
                game.gEngine.console_set_default_background(compare_window, 0, 0, 0)
                if item.item.equipment:
                    game.gEngine.console_print_ex(compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name    : ' + color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type    : ' + item.item.equipment.type.capitalize())
                    if item.item.equipment.type == 'melee':
                        damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub )
                        game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Damage  : ' + damage)
                        game.gEngine.console_print_ex(compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Accuracy: ' + str(item.item.equipment.accuracy))
                        game.gEngine.console_print_ex(compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Skill   : ' + item.item.equipment.damage_type)
                    else:
                        game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Armor   : ' + str(item.item.equipment.bonus))
                        game.gEngine.console_print_ex(compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Penalty : ' + str(item.item.equipment.penalty))
                        game.gEngine.console_print_ex(compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Location: ' + item.item.equipment.location.capitalize())
                    game.gEngine.console_print_ex(compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Value   : ' + str(item.item.value))

                if item.item.spell:
                    game.gEngine.console_print_ex(compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name  : ' + color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type  : ' + item.item.spell.type.capitalize())
                    game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Power : ' + str(item.item.spell.min) + '-' + str(item.item.spell.max))
                    game.gEngine.console_print_ex(compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Range : ' + str(item.item.spell.range))
                    game.gEngine.console_print_ex(compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Radius: ' + str(item.item.spell.radius))
                    game.gEngine.console_print_ex(compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Value : ' + str(item.item.value))
                    
                if mouse.lbutton_pressed and mouse.cx >= 3:
                    if len(player.fighter.inventory) >= 26:
                        if not item.item.check_stackable():
                            message = 'Not enough inventory space!'
                            w = len(message)+2
                            d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='dialog', con=inventory_window)
                            d_box.display_box()
                        else:
                            pass
                    elif item.item.value > player.fighter.money:
                        message = 'Not enough money!'
                        w = len(message)+2
                        d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='dialog', con=inventory_window)
                        d_box.display_box()
                        s_master_check.set_checked(False)
                        for box in s_check_boxes:
                            box.set_checked(s_master_check.get_checked())
                    else:
                        i_n = color_text(item.name.capitalize(), item.color)
                        price = color_text(item.item.value, libtcod.gold)
                        message = 'Buy %s for %s?' % (i_n, price)
                        w = len(message)+2
                        d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window)
                        confirm = d_box.display_box()
                        if confirm == 1:
                            player.fighter.money -= item.item.value
                            #player.fighter.inventory.append(item)
                            item.item.pick_up(player.fighter.inventory)
                            container.remove(item)
                            inventory_items = []
                            i_check_boxes = []
                            for x in range(len(player.fighter.inventory)):
                                i_check_boxes.append(CheckBox(x=1, y=x+1))
                                i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color)
                                if player.fighter.inventory[x].item.check_stackable:
                                    i += ' (%d)' % player.fighter.inventory[x].item.qty
                                inventory_items.append(i)
                            s_check_boxes = []
                            s_options = []
                            if container:
                                for obj in container:
                                    obj_text = color_text(obj.name.capitalize(), obj.color)
                                    ob_value = color_text(obj.item.value, libtcod.gold)
                                    ob_value = '(%s)' % ob_value
                                    s_gold.append(ob_value)
                                    opt = '[%s] ' % obj_text
                                    s_options.append(opt)
                                    if len(obj.name)+2 > s_size:
                                        s_size = len(obj.name)+2
                            for x in range(len(container)):
                                s_check_boxes.append(CheckBox(x=1, y=x+1))

        # keyboard input
        index = key.c - ord('a')
        if key:
            if key.vk == libtcod.KEY_DOWN:
                if current_selection is None:
                    current_selection = 0
                current_selection += 1
                if current_selection > len(inventory_items):
                    current_selection = 0
            if key.vk == libtcod.KEY_UP:
                if current_selection is None:
                    current_selection = 0
                current_selection -= 1
                if current_selection < 0:
                    current_selection = len(inventory_items)
        # ========================================================================
        # handle buttons
        # ========================================================================

        # ========================================================================
        # Sell Button
        # ========================================================================
        for i in sell_input:
            if i != -1:
                message = 'Sell all selected items?'
                w = len(message)+2
                d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='option', con=inventory_window)
                confirm = d_box.display_box()
                if confirm == 1:
                    d_box.destroy_box()
                    i_master_check.set_checked(False)
                    items_to_sell = []
                    for box in i_check_boxes:
                        if box.get_checked():
                            items_to_sell.append(player.fighter.inventory[box.y-1])
                    for item_to_sell in items_to_sell:
                        if item_to_sell:
                            player.fighter.money += item_to_sell.item.value/2
                            player.fighter.inventory.remove(item_to_sell)
                    i_check_boxes = []
                    inventory_items = []
                    for x in range(len(player.fighter.inventory)):
                        i_check_boxes.append(CheckBox(x=1, y=x+1))
                        inventory_items.append(color_text(player.fighter.inventory[x].name.capitalize(),
                                                          player.fighter.inventory[x].color))
                else:
                    d_box.destroy_box()

        # ========================================================================
        # buy button
        # ========================================================================
        for i in buy_input:
            if i != -1:
                n = 0
                for box in s_check_boxes:
                    if box.get_checked():
                        n += 1
                if len(player.fighter.inventory)+n > 26:
                        message = 'Not enough inventory space!'
                        w = len(message)+2
                        d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='dialog', con=inventory_window)
                        d_box.display_box()
                elif buy_value > player.fighter.money:
                    message = 'Not enough money!'
                    w = len(message)+2
                    d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='dialog', con=inventory_window)
                    d_box.display_box()
                    s_master_check.set_checked(False)
                    for box in s_check_boxes:
                        box.set_checked(s_master_check.get_checked())
                else:
                    message = 'Buy all selected items?'
                    w = len(message)+2
                    d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='option', con=inventory_window)
                    confirm = d_box.display_box()
                    if confirm == 1:
                        #d_box.destroy_box()
                        s_master_check.set_checked(False)
                        items_to_buy = []
                        for box in s_check_boxes:
                            if box.get_checked():
                                items_to_buy.append(container[box.y-1])
                        for item_to_buy in items_to_buy:
                            if item_to_buy:
                                container.remove(item_to_buy)
                                player.fighter.money -= item_to_buy.item.value
                                #player.fighter.inventory.append(item_to_buy)
                                item_to_buy.item.pick_up(player.fighter.inventory)

                        s_check_boxes = []
                        s_options = []
                        if container:
                            for obj in container:
                                obj_text = color_text(obj.name.capitalize(), obj.color)
                                ob_value = color_text(obj.item.value, libtcod.gold)
                                ob_value = '(%s)' % ob_value
                                s_gold.append(ob_value)
                                opt = '[%s] ' % obj_text
                                s_options.append(opt)
                                if len(obj.name)+2 > s_size:
                                    s_size = len(obj.name)+2
                        for x in range(len(container)):
                            s_check_boxes.append(CheckBox(x=1, y=x+1))

                        i_check_boxes = []
                        inventory_items = []
                        for x in range(len(player.fighter.inventory)):
                            i_check_boxes.append(CheckBox(x=1, y=x+1))
                            i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color)
                            if player.fighter.inventory[x].item.check_stackable:
                                i += ' (%d)' % player.fighter.inventory[x].item.qty
                            inventory_items.append(i)
            #else:
                    d_box.destroy_box()

        # ========================================================================
        # handle exit button
        # ========================================================================

        for i in exit_input:
            if i != -1:
                key.vk = libtcod.KEY_ESCAPE
                break
    # Remember to remove consoles in reverse order of creation to avoid OOB errors
    if splash:
        game.gEngine.console_remove_console(splash_screen)
    buy_button.destroy_button()
    sell_button.destroy_button()
    exit_button.destroy_button()
    game.gEngine.console_remove_console(compare_window)
    game.gEngine.console_remove_console(shop_window)
    game.gEngine.console_remove_console(inventory_window)

    return
Пример #7
0
def character_info(con, width, height, game, x=0, y=0):
    skill_window = game.gEngine.console_new(width / 2, height)
    skill_window_y_pos = width / 2
    s_header, s_pos = get_centered_text("Skills", width / 4)

    char_window = game.gEngine.console_new(width / 2, height / 2)
    c_header, c_pos = get_centered_text(
        ("%s's Skills and Abilities" % game.player.name), width / 4)

    skill_desc_window = game.gEngine.console_new(width / 2, height / 2)
    skill_desc_pos = height / 2
    d_header, d_pos = get_centered_text("Description", width / 4)

    exit_button = Button(label='Exit',
                         game=game,
                         x_pos=(width / 2) - 9,
                         y_pos=height - 6,
                         window=skill_window,
                         dest_x=width / 2,
                         dest_y=0)
    current_selection = 0
    key = libtcod.console_check_for_keypress()
    while key.vk != libtcod.KEY_ESCAPE:
        game.gEngine.console_flush()
        # get input just after flush
        key = libtcod.console_check_for_keypress(True)
        mouse = libtcod.mouse_get_status()
        exit_input = exit_button.display(mouse)

        game.gEngine.console_blit(char_window, 0, 0, width / 2, height / 2, 0,
                                  0, 0, 1.0, 1.0)
        game.gEngine.console_blit(skill_window, 0, 0, width / 2, height, 0,
                                  skill_window_y_pos, 0, 1.0, 1.0)
        game.gEngine.console_blit(skill_desc_window, 0, 0, width / 2,
                                  height / 2, 0, 0, skill_desc_pos, 1.0, 1.0)

        game.gEngine.console_clear(char_window)
        game.gEngine.console_clear(skill_desc_window)
        game.gEngine.console_clear(skill_window)

        #Draw Character info
        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(char_window, r, g, b)
        game.gEngine.console_print_frame(char_window, 0, 0, width / 2,
                                         height / 2, True)
        game.gEngine.console_print(char_window, c_pos, 0, c_header)
        game.gEngine.console_print(char_window, 1, 1,
                                   'Name: %s' % game.player.name)
        game.gEngine.console_print(
            char_window, 1, 2, 'Hit Points: %d/%d' %
            (game.player.fighter.hp, game.player.fighter.max_hp))

        game.gEngine.console_print(char_window, 1, 4,
                                   'Level: %d' % game.player.fighter.level)
        game.gEngine.console_print(
            char_window, 1, 5,
            'To Next Level: %d' % game.player.fighter.xp_to_next_level)

        s = color_text(str(game.player.fighter.stats[0]), libtcod.light_gray)
        d = color_text(str(game.player.fighter.stats[1]), libtcod.light_gray)
        i = color_text(str(game.player.fighter.stats[2]), libtcod.light_gray)
        c = color_text(str(game.player.fighter.stats[3]), libtcod.light_gray)
        game.gEngine.console_print(char_window, 1, 7,
                                   'Stats: Str [%s], Dex [%s]' % (s, d))
        game.gEngine.console_print(char_window, 1, 8,
                                   '       Int [%s], Con [%s]' % (i, c))

        bonus = color_text(str(game.player.fighter.armor_bonus), libtcod.green)
        bonus2 = color_text('10 +%d' % game.player.fighter.armor_bonus,
                            libtcod.green)
        game.gEngine.console_print(
            char_window, 1, 10,
            'Bonus to Armor Roll  : [%s](%s) ' % (bonus, bonus2))

        penalty = color_text(str(game.player.fighter.armor_penalty),
                             libtcod.red)
        penalty2 = color_text('1d20 -%d' % game.player.fighter.armor_penalty,
                              libtcod.red)
        game.gEngine.console_print(
            char_window, 1, 11,
            'Penalty to Dodge Roll: [%s](%s)' % (penalty, penalty2))

        speed = color_text(str(game.player.fighter.speed), libtcod.light_gray)
        game.gEngine.console_print(char_window, 1, 13,
                                   'Turn Speed: [%s]' % speed)

        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(skill_window, r, g, b)
        game.gEngine.console_print_frame(skill_window, 0, 0, width / 2, height,
                                         True)
        game.gEngine.console_print(skill_window, s_pos, 0, s_header)
        t, p = get_centered_text(
            'Unspent Skill Points: [%d]' %
            game.player.fighter.unused_skill_points, width / 4)
        game.gEngine.console_print(skill_window, p, 1, t)

        y = 2
        letter_index = ord('a')
        skill_max = 5
        for skill in game.player.fighter.skills:
            s_name = skill.get_name()
            s_bonus = skill.get_bonus()
            s_index = chr(letter_index)
            col = libtcod.white
            if s_bonus == skill_max:
                s_name = color_text(s_name, libtcod.green)
                s_bonus = color_text(str(s_bonus), libtcod.green)
                s_index = color_text(s_index, libtcod.green)
                col = libtcod.green
            elif s_bonus < skill_max and s_bonus > 0:
                s_name = color_text(s_name, libtcod.light_gray)
                s_bonus = color_text(str(s_bonus), libtcod.lighter_gray)
                s_index = color_text(s_index, libtcod.lighter_gray)
                col = libtcod.lighter_gray
            elif s_bonus == 0:
                s_name = color_text(s_name, libtcod.dark_gray)
                s_bonus = color_text(str(s_bonus), libtcod.dark_gray)
                s_index = color_text(s_index, libtcod.dark_gray)
                col = libtcod.dark_gray
            else:
                s_name = color_text(s_name, libtcod.red)
                s_bonus = color_text(str(s_bonus), libtcod.red)
                s_index = color_text(s_index, libtcod.red)
                col = libtcod.red
            text = '(%s) %s: [%s]' % (s_index, s_name, s_bonus)
            game.gEngine.console_print(skill_window, 1, y, text)
            if current_selection == y - 2:
                r, g, b = libtcod.color_lerp(col, libtcod.blue, 0.5)
                game.gEngine.console_set_default_background(
                    skill_window, r, g, b)
            else:
                game.gEngine.console_set_default_background(
                    skill_window, 0, 0, 0)

            game.gEngine.console_print_ex(skill_window, 1, y,
                                          libtcod.BKGND_SET, libtcod.LEFT,
                                          text)
            y += 1
            letter_index += 1
        game.gEngine.console_set_default_background(skill_window, 0, 0, 0)

        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(skill_desc_window, r, g, b)
        game.gEngine.console_print_frame(skill_desc_window, 0, 0, width / 2,
                                         height / 2, True)
        game.gEngine.console_print(skill_desc_window, d_pos, 0, d_header)

        #mouse input
        if mouse.cx >= width / 2 + 3:
            if mouse.cy - 2 < len(
                    game.player.fighter.skills) and mouse.cy >= 0:
                current_selection = mouse.cy - 2
                skill = game.player.fighter.skills[current_selection]
                desc = color_text(skill.get_description(), libtcod.light_gray)
                desc = "Skill Description: %s" % desc
                cat = color_text(skill.get_category(), libtcod.light_gray)
                cat = "Skill Category   : %s" % cat
                bonus = skill.get_bonus()
                name = skill.get_name()
                if bonus == skill_max:
                    bonus = color_text(str(bonus), libtcod.green)
                    name = color_text(name, libtcod.green)
                elif bonus < skill_max and bonus > 0:
                    bonus = color_text(str(bonus), libtcod.lighter_gray)
                    name = color_text(name, libtcod.lighter_gray)
                elif bonus == 0:
                    bonus = color_text(str(bonus), libtcod.dark_gray)
                    name = color_text(name, libtcod.dark_gray)
                else:
                    bonus = color_text(str(bonus), libtcod.red)
                    name = color_text(name, libtcod.red)
                if skill.get_category() == 'Discipline':
                    bonus = 'Increases your (%s) to-hit rolls by [%s].' % (
                        name, bonus)
                elif skill.get_category() == 'Weapon':
                    bonus = 'Increases your (%s) damage by [%s].' % (name,
                                                                     bonus)

                game.gEngine.console_print_rect(skill_desc_window, 1, 1,
                                                width / 2 - 2, 3, desc)
                game.gEngine.console_print(skill_desc_window, 1, 5, cat)
                game.gEngine.console_print_rect(skill_desc_window, 1, 7,
                                                width / 2 - 2, 3, bonus)
                if mouse.lbutton_pressed:
                    game.player.fighter.apply_skill_points(
                        game.player.fighter.skills[current_selection]
                    )  # use unused player skill points

        for i in exit_input:
            if i != -1:
                key.vk = libtcod.KEY_ESCAPE
                break

    exit_button.destroy_button()
    game.gEngine.console_remove_console(skill_desc_window)
    game.gEngine.console_remove_console(char_window)
    game.gEngine.console_remove_console(skill_window)

    return None
Пример #8
0
def inventory(con, player, game, width=80, height=43):
    """
    TODO:
        Add in highlighting for Weapons and Equipment consoles
        Add in keyboard arrow selection support
        Add in "drop" mode, drop items from inventory
        Fix Weapons and Equipment keyboard handling
        Fix duplication bug when equipping items (fixed)
        Fix take off weapon confirmation when equipping an item (fixed)
        2 handed code needs work (?)
        keyboard input is sluggish. Might have to update libtcod to fix

    :param con: Destination console (not used atm)
    :param player: The main player object
    :param game: The main game object
    :param width: width of the inventory screen
    :param height: height of the inventory screen
    :return: An item that has been used (potion, scroll, etc..)
    """
    equip_height = 14
    wield_height = 8
    compare_height = height - (equip_height - wield_height) - (wield_height *
                                                               2)

    r, g, b = libtcod.white
    equip_y = wield_height
    compare_y = equip_height + wield_height

    inventory_window = game.gEngine.console_new(width / 2, height)
    game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
    game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2, height,
                                     True)
    game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)

    equipment_window = game.gEngine.console_new(width / 2, equip_height)
    game.gEngine.console_set_default_foreground(equipment_window, r, g, b)
    game.gEngine.console_print_frame(equipment_window, 0, 0, width / 2,
                                     equip_height, True)
    game.gEngine.console_set_default_background(equipment_window, 0, 0, 0)

    wielded_window = game.gEngine.console_new(width / 2, wield_height)
    game.gEngine.console_set_default_foreground(wielded_window, r, g, b)
    game.gEngine.console_print_frame(wielded_window, 0, 0, width / 2,
                                     wield_height, True)
    game.gEngine.console_set_default_background(wielded_window, 0, 0, 0)

    compare_window = game.gEngine.console_new(width / 2, compare_height)
    game.gEngine.console_set_default_foreground(compare_window, r, g, b)
    game.gEngine.console_print_frame(compare_window, 0, 0, width / 2,
                                     compare_height, True)
    game.gEngine.console_set_default_background(compare_window, 0, 0, 0)

    check_boxes = []
    slots = [
        'Torso    ', 'Head     ', 'Hands    ', 'Legs     ', 'Feet     ',
        'Arms     ', 'Shoulders', 'Back     '
    ]
    #self.buttons.append(Button(self, self.option_labels[0], self.width//6-5, self.height/2-1, True))
    exit_button = Button(label='Exit',
                         game=game,
                         x_pos=(width / 2) - 9,
                         y_pos=height - 6,
                         window=inventory_window,
                         dest_x=width / 2,
                         dest_y=0)
    drop_button = Button(label='Drop',
                         game=game,
                         x_pos=1,
                         y_pos=height - 6,
                         window=inventory_window,
                         dest_x=width / 2,
                         dest_y=0)
    if len(player.fighter.inventory) == 0:
        inventory_items = ['Inventory is empty.']
    else:
        inventory_items = []
        for x in range(len(player.fighter.inventory)):
            check_boxes.append(CheckBox(x=1, y=x + 3))
            i = color_text(player.fighter.inventory[x].name.capitalize(),
                           player.fighter.inventory[x].color)
            if player.fighter.inventory[x].item.check_stackable:
                i += ' (%d)' % player.fighter.inventory[x].item.qty
            inventory_items.append(i)
    i_header = 'Inventory'
    i_header_size = len(i_header)
    i_header_pos = (width / 4) - (i_header_size / 2)

    w_header = 'Weapons'
    w_header_size = len(w_header)
    w_header_pos = (width / 8) - (w_header_size / 2)

    e_header = 'Equipment'
    e_header_size = len(e_header)
    e_header_pos = (width / 8) - (e_header_size / 2)

    c_header = 'Compare/Examine'
    c_header_size = len(c_header)
    c_header_pos = (width / 8) - (c_header_size / 2)

    return_item = None
    key = libtcod.console_check_for_keypress()
    current_selection = 0
    master_check = CheckBox(1, 30, "Check/Uncheck All")
    while key.vk != libtcod.KEY_ESCAPE:
        game.gEngine.console_flush()
        # get input just after flush
        key = libtcod.console_check_for_keypress(True)
        mouse = libtcod.mouse_get_status()
        exit_input = exit_button.display(mouse)
        drop_input = drop_button.display(mouse)

        game.gEngine.console_blit(inventory_window, 0, 0, width / 2, height, 0,
                                  (width / 2), 0, 1.0, 1.0)
        game.gEngine.console_blit(wielded_window, 0, 0, width / 2, height, 0,
                                  0, 0, 1.0, 1.0)
        game.gEngine.console_blit(equipment_window, 0, 0, width / 2, height, 0,
                                  0, equip_y, 1.0, 1.0)
        game.gEngine.console_blit(compare_window, 0, 0, width / 2, height, 0,
                                  0, compare_y, 1.0, 1.0)

        game.gEngine.console_clear(inventory_window)
        game.gEngine.console_clear(wielded_window)
        game.gEngine.console_clear(equipment_window)
        game.gEngine.console_clear(compare_window)

        # set up draw screen
        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
        game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2,
                                         height, True)

        game.gEngine.console_set_default_foreground(equipment_window, r, g, b)
        game.gEngine.console_print_frame(equipment_window, 0, 0, width / 2,
                                         equip_height, True)

        game.gEngine.console_set_default_foreground(wielded_window, r, g, b)
        game.gEngine.console_print_frame(wielded_window, 0, 0, width / 2,
                                         wield_height, True)

        game.gEngine.console_set_default_foreground(compare_window, r, g, b)
        game.gEngine.console_print_frame(compare_window, 0, 0, width / 2,
                                         compare_height, True)

        # ========================================================================
        # print inventory
        # ========================================================================
        game.gEngine.console_print(inventory_window, i_header_pos, 0, i_header)
        letter_index = ord('a')
        if len(player.fighter.inventory) > 0:
            y = 1
            for i in range(len(inventory_items)):
                text = '  (' + chr(letter_index) + ') ' + inventory_items[i]
                if current_selection == y - 1:
                    r, g, b = libtcod.color_lerp(
                        player.fighter.inventory[i].color, libtcod.blue, 0.5)
                    game.gEngine.console_set_default_background(
                        inventory_window, r, g, b)
                else:
                    game.gEngine.console_set_default_background(
                        inventory_window, 0, 0, 0)
                game.gEngine.console_print_ex(inventory_window, 1, y + 2,
                                              libtcod.BKGND_SET, libtcod.LEFT,
                                              text)
                y += 1
                letter_index += 1
        game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
        game.gEngine.console_print(
            inventory_window, 1, 31,
            'Gold: ' + color_text(str(player.fighter.money), libtcod.gold))

        # ========================================================================
        # print equipped weapons
        # ========================================================================
        game.gEngine.console_print(wielded_window, w_header_pos, 0, w_header)
        index = ord('1')
        if player.fighter.wielded[0] is None:
            text = '(' + chr(index) + ') ' + 'Left Hand : ' + color_text(
                'Empty', libtcod.darker_gray)
        else:
            t = color_text(player.fighter.wielded[0].name.capitalize(),
                           player.fighter.wielded[0].color)
            text = '(' + chr(index) + ') ' + 'Left Hand : ' + t
        index += 1
        game.gEngine.console_print(wielded_window, 1, 2, text)
        if player.fighter.wielded[1] is None:
            text = '(' + chr(index) + ') ' + 'Right Hand: ' + color_text(
                'Empty', libtcod.darker_gray)
        else:
            t = color_text(player.fighter.wielded[1].name.capitalize(),
                           player.fighter.wielded[1].color)
            text = '(' + chr(index) + ') ' + 'Right Hand: ' + t
        index += 1
        game.gEngine.console_print(wielded_window, 1, 3, text)

        item = player.fighter.wielded[0]
        if item:
            damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices,
                                   item.item.equipment.damage.nb_faces,
                                   item.item.equipment.damage.addsub)
            text = 'Damage   (total): ' + color_text(damage, libtcod.green)
            game.gEngine.console_print(wielded_window, 1, 4, text)
            accuracy = item.item.equipment.accuracy
            accuracy += game.player.fighter.get_skill(
                item.item.equipment.damage_type).get_bonus()
            text = 'Accuracy (total): ' + color_text(str(accuracy),
                                                     libtcod.green)
            game.gEngine.console_print(wielded_window, 1, 5, text)

        # ========================================================================
        # print equipped armor
        # ========================================================================
        game.gEngine.console_print(equipment_window, e_header_pos, 0, e_header)
        i = 0
        game.gEngine.console_set_alignment(equipment_window, libtcod.LEFT)
        armor_bonus = 0
        armor_penalty = 0
        for item in player.fighter.equipment:
            text = '(' + chr(index) + ') ' + slots[i] + ': '
            if item is None:
                text += color_text('Empty', libtcod.darker_gray)
            else:
                text += color_text(item.name.capitalize(), item.color)
                armor_bonus += item.item.equipment.bonus
                armor_penalty += item.item.equipment.penalty
            game.gEngine.console_print(equipment_window, 1, i + 2, text)
            i += 1
            index += 1
        c = color_text(str(armor_bonus), libtcod.green)
        text = 'Total Armor Bonus  :  ' + c
        game.gEngine.console_print(equipment_window, 1, i + 2, text)
        c = color_text(str(armor_penalty), libtcod.red)
        text = 'Total Armor Penalty: -' + c
        game.gEngine.console_print(equipment_window, 1, i + 3, text)
        game.gEngine.console_print(compare_window, c_header_pos, 0, c_header)

        # ========================================================================
        # handle mouse input
        # ========================================================================
        mc = master_check.update(mouse, width)
        master_check.render(inventory_window, game)
        if not mc:
            for box in check_boxes:
                box.update(mouse, width)
                box.render(inventory_window, game)
        else:
            for box in check_boxes:
                box.set_checked(master_check.get_checked())
                box.render(inventory_window, game)

        # Inventory input
        if mouse.cx >= width / 2 + 3 and mouse.cx < width - 2:  # inventory screen dims
            if (mouse.cy - 3) < len(
                    player.fighter.inventory) and mouse.cy - 3 >= 0:
                #if mouse.cy-3 <= len(player.fighter.inventory):
                item = player.fighter.inventory[mouse.cy - 3]
                current_selection = mouse.cy - 3
                if item.item.equipment:
                    game.gEngine.console_print(
                        compare_window, 1, 2, 'Name    : ' +
                        color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print(
                        compare_window, 1, 3,
                        'Type    : ' + item.item.equipment.type.capitalize())
                    if item.item.equipment.type == 'melee':
                        damage = '%dd%d+%d' % (
                            item.item.equipment.damage.nb_dices,
                            item.item.equipment.damage.nb_faces,
                            item.item.equipment.damage.addsub)
                        game.gEngine.console_print(compare_window, 1, 4,
                                                   'Damage  : ' + damage)
                        game.gEngine.console_print(
                            compare_window, 1, 5,
                            'Accuracy: ' + str(item.item.equipment.accuracy))
                        game.gEngine.console_print(
                            compare_window, 1, 7,
                            'Skill   : ' + item.item.equipment.damage_type)
                    else:
                        game.gEngine.console_print(
                            compare_window, 1, 4,
                            'Armor   : ' + str(item.item.equipment.bonus))
                        game.gEngine.console_print(
                            compare_window, 1, 5,
                            'Penalty : ' + str(item.item.equipment.penalty))
                        game.gEngine.console_print(
                            compare_window, 1, 7, 'Location: ' +
                            item.item.equipment.location.capitalize())
                    game.gEngine.console_print(
                        compare_window, 1, 6,
                        'Value   : ' + str(item.item.value))
                if item.item.spell:
                    game.gEngine.console_print(
                        compare_window, 1, 2, 'Name  : ' +
                        color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print(
                        compare_window, 1, 3,
                        'Type  : ' + item.item.spell.type.capitalize())
                    game.gEngine.console_print(
                        compare_window, 1, 4,
                        'Power : ' + str(item.item.spell.min) + '-' +
                        str(item.item.spell.max))
                    game.gEngine.console_print(
                        compare_window, 1, 5,
                        'Range : ' + str(item.item.spell.range))
                    game.gEngine.console_print(
                        compare_window, 1, 6,
                        'Radius: ' + str(item.item.spell.radius))
                    game.gEngine.console_print(
                        compare_window, 1, 7,
                        'Value : ' + str(item.item.value))
                if mouse.lbutton_pressed and item.item.spell:
                    i_n = color_text(item.name.capitalize(), item.color)
                    message = 'Do you want to use %s?' % i_n
                    w = len(message) + 2
                    d_box = DialogBox(game,
                                      w,
                                      10,
                                      width / 4,
                                      height / 2,
                                      message,
                                      type='option',
                                      con=inventory_window)
                    confirm = d_box.display_box()
                    if confirm == 1:  # make sure if the player uses a scroll or potion, we exit inventory
                        d_box.destroy_box()
                        # Remember to remove consoles in reverse order of creation to avoid OOB errors
                        return_item = item
                        break
                    else:
                        d_box.destroy_box()
                if mouse.lbutton_pressed and item.item.equipment:
                    i_n = color_text(item.name.capitalize(), item.color)
                    message = 'Do you want to put %s on?' % i_n
                    w = len(message) + 2
                    d_box = DialogBox(game,
                                      w,
                                      10,
                                      width / 4,
                                      height / 2,
                                      message,
                                      type='option',
                                      con=inventory_window)
                    confirm = d_box.display_box()
                    if confirm == 1:
                        item.item.use(game.player.fighter.inventory,
                                      game.player, game)
                        d_box.destroy_box()
                        inventory_items = []
                        check_boxes = []
                        for x in range(len(player.fighter.inventory)):
                            check_boxes.append(CheckBox(x=1, y=x + 3))
                            i = color_text(
                                player.fighter.inventory[x].name.capitalize(),
                                player.fighter.inventory[x].color)
                            if player.fighter.inventory[
                                    x].item.check_stackable:
                                i += ' (%d)' % player.fighter.inventory[
                                    x].item.qty
                            inventory_items.append(i)
            else:
                current_selection = None
        # game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
        # Wielded
        elif mouse.cx >= 0 and mouse.cx <= (
            (width / 2) - 2):  # inventory screen dims
            if (mouse.cy - 2) < len(player.fighter.wielded):
                item = player.fighter.wielded[mouse.cy - 2]
                if item is not None:
                    if item.item.equipment:
                        game.gEngine.console_print(
                            compare_window, 1, 2, 'Name    : ' +
                            color_text(item.name.capitalize(), item.color))
                        game.gEngine.console_print(
                            compare_window, 1, 3, 'Type    : ' +
                            item.item.equipment.type.capitalize())
                        damage = '%dd%d+%d' % (
                            item.item.equipment.damage.nb_dices,
                            item.item.equipment.damage.nb_faces,
                            item.item.equipment.damage.addsub)
                        game.gEngine.console_print(compare_window, 1, 4,
                                                   'Damage  : ' + damage)
                        game.gEngine.console_print(
                            compare_window, 1, 5,
                            'Accuracy: ' + str(item.item.equipment.accuracy))
                        game.gEngine.console_print(
                            compare_window, 1, 6,
                            'Value   : ' + str(item.item.value))

        # Equipment
            elif (mouse.cy - 2) - equip_y < len(player.fighter.equipment):
                item = player.fighter.equipment[mouse.cy - 2 - equip_y]
                if item is not None:
                    if item.item.equipment:
                        game.gEngine.console_print(
                            compare_window, 1, 2, 'Name    : ' +
                            color_text(item.name.capitalize(), item.color))
                        game.gEngine.console_print(
                            compare_window, 1, 3, 'Type    : ' +
                            item.item.equipment.type.capitalize())
                        game.gEngine.console_print(
                            compare_window, 1, 4,
                            'Armor   : ' + str(item.item.equipment.bonus))
                        game.gEngine.console_print(
                            compare_window, 1, 5,
                            'Penalty : ' + str(item.item.equipment.penalty))
                        game.gEngine.console_print(
                            compare_window, 1, 6,
                            'Value   : ' + str(item.item.value))
            if mouse.lbutton_pressed and item is not None:
                i_n = color_text(item.name.capitalize(), item.color)
                message = 'Do you want to take %s off?' % i_n
                w = len(message) + 2
                d_box = DialogBox(game,
                                  w,
                                  10,
                                  width / 4,
                                  height / 2,
                                  message,
                                  type='option',
                                  con=inventory_window)
                confirm = d_box.display_box()
                if confirm == 1:
                    item.item.equipment.un_equip(game.player, item)
                    d_box.destroy_box()
                    inventory_items = []
                    check_boxes = []
                    for x in range(len(player.fighter.inventory)):
                        check_boxes.append(CheckBox(x=1, y=x + 3))
                        i = color_text(
                            player.fighter.inventory[x].name.capitalize(),
                            player.fighter.inventory[x].color)
                        if player.fighter.inventory[x].item.check_stackable:
                            i += ' (%d)' % player.fighter.inventory[x].item.qty
                        inventory_items.append(i)
                    i = 0
                    for x in player.fighter.wielded:
                        if x == item:
                            player.fighter.wielded[i] = None
                        i += 1
                    i = 0
                    for x in player.fighter.equipment:
                        if x == item:
                            player.fighter.equipment[i] = None
                        i += 1

        # keyboard input
        # keeps similar feel to old inventory if using the keys
        # keyboard input is sluggish as balls for some reason, need to look into it more
        index = key.c - ord('a')
        if key:
            if index >= 0 and index < len(inventory_items):
                return_item = player.fighter.inventory[index]
                break
            index = key.c - ord('1')
            if index >= 0 and index <= 1:
                return_item = player.fighter.wielded[index]
                break
            elif index >= 2 and index <= 10:
                return_item = player.fighter.equipment[index - 2]
                break
            if key.vk == libtcod.KEY_DOWN:
                if current_selection is None:
                    current_selection = 0
                current_selection += 1
                if current_selection > len(inventory_items):
                    current_selection = 0
            if key.vk == libtcod.KEY_UP:
                if current_selection is None:
                    current_selection = 0
                current_selection -= 1
                if current_selection < 0:
                    current_selection = len(inventory_items)
        # ========================================================================
        # handle buttons
        # ========================================================================
        for i in drop_input:
            if i != -1:
                message = 'Drop all selected items?'
                w = len(message) + 2
                d_box = DialogBox(game,
                                  w,
                                  10,
                                  width / 2 - w / 2,
                                  height / 2 - 5,
                                  message,
                                  type='option',
                                  con=inventory_window)
                confirm = d_box.display_box()
                if confirm == 1:
                    d_box.destroy_box()
                    master_check.set_checked(False)
                    items_to_drop = []
                    for box in check_boxes:
                        if box.get_checked():
                            items_to_drop.append(
                                player.fighter.inventory[box.y - 3])
                    for item_to_drop in items_to_drop:
                        if item_to_drop:
                            item_to_drop.objects = game.objects
                            item_to_drop.item.drop(player.fighter.inventory,
                                                   player, False)
                            item_to_drop.send_to_back()
                    check_boxes = []
                    inventory_items = []
                    for x in range(len(player.fighter.inventory)):
                        check_boxes.append(CheckBox(x=1, y=x + 3))
                        inventory_items.append(
                            color_text(
                                player.fighter.inventory[x].name.capitalize(),
                                player.fighter.inventory[x].color))
                else:
                    d_box.destroy_box()
        # ========================================================================
        # handle exit button
        # ========================================================================

        for i in exit_input:
            if i != -1:
                key.vk = libtcod.KEY_ESCAPE
                break
    # Remember to remove consoles in reverse order of creation to avoid OOB errors
    drop_button.destroy_button()
    exit_button.destroy_button()
    game.gEngine.console_remove_console(compare_window)
    game.gEngine.console_remove_console(wielded_window)
    game.gEngine.console_remove_console(equipment_window)
    game.gEngine.console_remove_console(inventory_window)

    return return_item
Пример #9
0
def shop(con,
         player,
         game,
         container=None,
         bg=None,
         header=None,
         width=80,
         height=50,
         splash=False):
    """
    TODO:
        Make sure you cannot buy items if it were to put you over inventory limit - Done
        Attach this to a shopkeeper to control splash and inventory.
        Add a buyback screen to buy back things sold by accident

    :param con: Destination Console (not used atm)
    :param player: The Player Object
    :param game: The main game object
    :param container: The contents of the store
    :param bg: The background image to use
    :param header: The title of the store
    :param width: width of the shop
    :param height: height of the shop
    :param splash: Whether or not to display the splash screen
    :return: Nothing
    """
    if bg:
        dark_bg = game.gEngine.image_load(bg)
        bg = game.gEngine.image_load(bg)
        w, h = game.gEngine.image_get_size(dark_bg)
        for y in range(w):
            for x in range(h):
                col = game.gEngine.image_get_pixel(dark_bg, y, x)
                col = libtcod.color_lerp(col, libtcod.black, 0.9)
                #col = libtcod.color_lerp(col, libtcod.light_azure, 0.2)
                r, g, b = col
                game.gEngine.image_put_pixel(dark_bg, y, x, r, g, b)

    shop_height = 28
    compare_height = height - shop_height
    s_options = []
    s_gold = []
    s_size = 0
    if container:
        for obj in container:
            obj_text = color_text(obj.name.capitalize(), obj.color)
            ob_value = color_text(obj.item.value, libtcod.gold)
            ob_value = '(%s)' % ob_value
            s_gold.append(ob_value)
            opt = '[%s] ' % obj_text
            s_options.append(opt)
            if len(obj.name) + 2 > s_size:
                s_size = len(obj.name) + 2
    r, g, b = libtcod.white
    compare_y = shop_height

    inventory_window = game.gEngine.console_new(width / 2, height)
    game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
    game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2, height,
                                     True)

    shop_window = game.gEngine.console_new(width / 2, shop_height)
    game.gEngine.console_set_default_foreground(shop_window, r, g, b)
    game.gEngine.console_print_frame(shop_window, 0, 0, width / 2, shop_height,
                                     True)

    compare_window = game.gEngine.console_new(width / 2, compare_height)
    game.gEngine.console_set_default_foreground(compare_window, r, g, b)
    game.gEngine.console_print_frame(compare_window, 0, 0, width / 2,
                                     compare_height, True)

    i_check_boxes = []
    s_check_boxes = []

    exit_button = Button(label='Exit',
                         game=game,
                         x_pos=(width / 2) - 9,
                         y_pos=height - 6,
                         window=inventory_window,
                         dest_x=width / 2,
                         dest_y=0)

    sell_button = Button(label='Sell',
                         game=game,
                         x_pos=1,
                         y_pos=height - 6,
                         window=inventory_window,
                         dest_x=width / 2,
                         dest_y=0)

    buy_button = Button(label='Buy',
                        game=game,
                        x_pos=1,
                        y_pos=compare_height - 6,
                        window=compare_window,
                        dest_x=0,
                        dest_y=compare_y)

    if len(player.fighter.inventory) == 0:
        inventory_items = ['Inventory is empty.']
    else:
        inventory_items = []
        for x in range(len(player.fighter.inventory)):
            i_check_boxes.append(CheckBox(x=1, y=x + 1))
            i = color_text(player.fighter.inventory[x].name.capitalize(),
                           player.fighter.inventory[x].color)
            if player.fighter.inventory[x].item.check_stackable:
                i += ' (%d)' % player.fighter.inventory[x].item.qty
            inventory_items.append(i)
    for x in range(len(container)):
        s_check_boxes.append(CheckBox(x=1, y=x + 1))

    i_header = 'Inventory'
    i_header_size = len(i_header)
    i_header_pos = (width / 4) - (i_header_size / 2)

    if header:
        s_header = header
    else:
        s_header = 'Shop'
    s_header_size = len(s_header)
    s_header_pos = (width / 4) - (s_header_size / 2)

    c_header = 'Compare/Examine'
    c_header_size = len(c_header)
    c_header_pos = (width / 4) - (c_header_size / 2)

    key = libtcod.console_check_for_keypress()
    mouse = libtcod.mouse_get_status()
    current_selection = 0
    s_current_selection = 0
    i_master_check = CheckBox(1, 30, "Check/Uncheck All")
    s_master_check = CheckBox(1, 26, "Check/Uncheck All")
    fade = 1
    if splash:
        splash_screen = game.gEngine.console_new(width, height)
    while key.vk != libtcod.KEY_ESCAPE:
        game.gEngine.console_flush()
        # get input just after flush
        key = libtcod.console_check_for_keypress(True)
        mouse = libtcod.mouse_get_status()
        exit_input = exit_button.display(mouse)
        sell_input = sell_button.display(mouse)
        buy_input = buy_button.display(mouse)

        game.gEngine.console_blit(inventory_window, 0, 0, width / 2, height, 0,
                                  (width / 2), 0, 1.0, 1.0)
        game.gEngine.console_blit(shop_window, 0, 0, width / 2, shop_height, 0,
                                  0, 0, 1.0, 1.0)
        game.gEngine.console_blit(compare_window, 0, 0, width / 2,
                                  compare_height, 0, 0, compare_y, 1.0, 1.0)
        if splash:
            if bg:
                if fade > 0:
                    game.gEngine.console_blit(splash_screen, 0, 0, width,
                                              height, 0, 0, 0, fade, fade)
                    game.gEngine.image_blit_2x(bg, splash_screen, 0, 0, 0, 0)
                    fade -= 0.045
                else:
                    fade = 0
        game.gEngine.console_clear(inventory_window)
        game.gEngine.console_clear(shop_window)
        game.gEngine.console_clear(compare_window)

        # set up draw screen
        r, g, b = libtcod.white

        # ========================================================================
        # print inventory
        # ========================================================================
        if bg:
            game.gEngine.image_blit_2x(dark_bg, inventory_window, 0, 0, width,
                                       0)
        game.gEngine.console_set_default_foreground(inventory_window, r, g, b)
        game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2,
                                         height, False)
        game.gEngine.console_print(inventory_window, i_header_pos, 0, i_header)
        if len(player.fighter.inventory) > 0:
            letter_index = ord('a')
            y = 1
            for i in range(len(inventory_items)):
                text = '  (' + chr(letter_index) + ') ' + inventory_items[i]
                if current_selection == y - 1:
                    r, g, b = libtcod.color_lerp(
                        player.fighter.inventory[i].color, libtcod.blue, 0.5)
                    game.gEngine.console_set_default_background(
                        inventory_window, r, g, b)
                else:
                    game.gEngine.console_set_default_background(
                        inventory_window, 0, 0, 0)
                game.gEngine.console_print_ex(inventory_window, 1, y,
                                              libtcod.BKGND_SET, libtcod.LEFT,
                                              text)
                y += 1
                letter_index += 1
        game.gEngine.console_set_default_background(inventory_window, 0, 0, 0)
        game.gEngine.console_print(
            inventory_window, 1, 31,
            'Gold: ' + color_text(str(player.fighter.money), libtcod.gold))
        r, g, b = libtcod.white

        # ========================================================================
        # Print Shop Window
        # ========================================================================
        if bg:
            game.gEngine.image_blit_2x(dark_bg, shop_window, 0, 0)
        game.gEngine.console_set_default_foreground(shop_window, r, g, b)
        game.gEngine.console_print_frame(shop_window, 0, 0, width / 2,
                                         shop_height, False)
        game.gEngine.console_print(shop_window, s_header_pos, 0, s_header)
        if len(s_options) > 0:
            letter_index = ord('0')
            y = 1
            for i in range(len(s_options)):
                text = '  (' + chr(letter_index) + ') ' + s_options[i]
                if s_current_selection == y - 1:
                    rr, gg, bb = libtcod.color_lerp(container[i].color,
                                                    libtcod.blue, 0.5)
                    game.gEngine.console_set_default_background(
                        shop_window, rr, gg, bb)
                else:
                    game.gEngine.console_set_default_background(
                        shop_window, 0, 0, 0)
                game.gEngine.console_print_ex(shop_window, 1, y,
                                              libtcod.BKGND_SET, libtcod.LEFT,
                                              text)
                game.gEngine.console_print_ex(shop_window, s_size + 10, y,
                                              libtcod.BKGND_SET, libtcod.RIGHT,
                                              s_gold[i])
                y += 1
                letter_index += 1
            game.gEngine.console_set_default_background(
                inventory_window, 0, 0, 0)
            r, g, b = libtcod.white

        # ========================================================================
        # Print Compare Window
        # ========================================================================
        if bg:
            game.gEngine.image_blit_2x(dark_bg,
                                       compare_window,
                                       0,
                                       0,
                                       sx=0,
                                       sy=compare_y * 2)
        game.gEngine.console_set_default_foreground(compare_window, r, g, b)
        game.gEngine.console_print_frame(compare_window, 0, 0, width / 2,
                                         compare_height, False)
        game.gEngine.console_print(compare_window, c_header_pos, 0, c_header)

        # ========================================================================
        # handle mouse input
        # ========================================================================
        #Render check boxes
        #inventory check boxes
        i_mc = i_master_check.update(mouse, width)
        i_master_check.render(inventory_window, game)
        sell_value = 0
        if not i_mc:
            for box in i_check_boxes:
                box.update(mouse, width)
                box.render(inventory_window, game)
                if box.get_checked():
                    sell_value += player.fighter.inventory[box.y -
                                                           1].item.value / 2
        else:
            for box in i_check_boxes:
                box.set_checked(i_master_check.get_checked())
                box.render(inventory_window, game)
                if box.get_checked():
                    sell_value += player.fighter.inventory[box.y -
                                                           1].item.value / 2
        game.gEngine.console_print(
            inventory_window, 1, 32,
            'Sell Value: ' + color_text(str(sell_value), libtcod.gold))

        #Shop check boxes
        s_mc = s_master_check.update(mouse)
        s_master_check.render(shop_window, game)
        buy_value = 0
        if not s_mc:
            for box in s_check_boxes:
                box.update(mouse)
                box.render(shop_window, game)
                if box.get_checked():
                    buy_value += container[box.y - 1].item.value
        else:
            for box in s_check_boxes:
                box.set_checked(s_master_check.get_checked())
                box.render(shop_window, game)
                if box.get_checked():
                    buy_value += container[box.y - 1].item.value
        game.gEngine.console_print(
            shop_window, 1, 25,
            'Buy Value: ' + color_text(str(buy_value), libtcod.gold))

        # Inventory input
        if mouse.cx >= width / 2 + 3 and mouse.cx < width - 2:  # inventory screen dims
            if (mouse.cy - 1) < len(
                    player.fighter.inventory) and mouse.cy - 1 >= 0:
                item = player.fighter.inventory[mouse.cy - 1]
                current_selection = mouse.cy - 1
                game.gEngine.console_set_default_background(
                    compare_window, 0, 0, 0)
                if item.item.equipment:
                    game.gEngine.console_print_ex(
                        compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT,
                        'Name    : ' +
                        color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT,
                        'Type    : ' + item.item.equipment.type.capitalize())
                    if item.item.equipment.type == 'melee':
                        damage = '%dd%d+%d' % (
                            item.item.equipment.damage.nb_dices,
                            item.item.equipment.damage.nb_faces,
                            item.item.equipment.damage.addsub)
                        game.gEngine.console_print_ex(compare_window, 1, 4,
                                                      libtcod.BKGND_SET,
                                                      libtcod.LEFT,
                                                      'Damage  : ' + damage)
                        game.gEngine.console_print_ex(
                            compare_window, 1, 5, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Accuracy: ' + str(item.item.equipment.accuracy))
                        game.gEngine.console_print_ex(
                            compare_window, 1, 7, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Skill   : ' + item.item.equipment.damage_type)
                    else:
                        game.gEngine.console_print_ex(
                            compare_window, 1, 4, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Armor   : ' + str(item.item.equipment.bonus))
                        game.gEngine.console_print_ex(
                            compare_window, 1, 5, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Penalty : ' + str(item.item.equipment.penalty))
                        game.gEngine.console_print_ex(
                            compare_window, 1, 7, libtcod.BKGND_SET,
                            libtcod.LEFT, 'Location: ' +
                            item.item.equipment.location.capitalize())
                    game.gEngine.console_print_ex(
                        compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT,
                        'Value   : ' + str(item.item.value))
                if item.item.spell:
                    game.gEngine.console_print_ex(
                        compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT,
                        'Name  : ' +
                        color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT,
                        'Type  : ' + item.item.spell.type.capitalize())
                    game.gEngine.console_print_ex(
                        compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT,
                        'Power : ' + str(item.item.spell.min) + '-' +
                        str(item.item.spell.max))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT,
                        'Range : ' + str(item.item.spell.range))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT,
                        'Radius: ' + str(item.item.spell.radius))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT,
                        'Value : ' + str(item.item.value))
                if mouse.lbutton_pressed:
                    i_n = color_text(item.name.capitalize(), item.color)
                    price = color_text(item.item.value / 2, libtcod.gold)
                    message = 'Sell %s for %s?' % (i_n, price)
                    w = len(message) + 2
                    d_box = DialogBox(game,
                                      w,
                                      10,
                                      width / 4,
                                      height / 2,
                                      message,
                                      type='option',
                                      con=inventory_window)
                    confirm = d_box.display_box()
                    if confirm == 1:
                        player.fighter.money += item.item.value / 2
                        player.fighter.inventory.remove(item)
                        inventory_items = []
                        i_check_boxes = []
                        for x in range(len(player.fighter.inventory)):
                            i_check_boxes.append(CheckBox(x=1, y=x + 1))
                            i = color_text(
                                player.fighter.inventory[x].name.capitalize(),
                                player.fighter.inventory[x].color)
                            if player.fighter.inventory[
                                    x].item.check_stackable:
                                i += ' (%d)' % player.fighter.inventory[
                                    x].item.qty
                            inventory_items.append(i)

        # shop input
        if mouse.cx >= 0 and mouse.cx < width / 2 - 2:  # shop screen dims
            if (mouse.cy - 1) < len(container) and mouse.cy - 1 >= 0:
                item = container[mouse.cy - 1]
                s_current_selection = mouse.cy - 1
                game.gEngine.console_set_default_background(
                    compare_window, 0, 0, 0)
                if item.item.equipment:
                    game.gEngine.console_print_ex(
                        compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT,
                        'Name    : ' +
                        color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT,
                        'Type    : ' + item.item.equipment.type.capitalize())
                    if item.item.equipment.type == 'melee':
                        damage = '%dd%d+%d' % (
                            item.item.equipment.damage.nb_dices,
                            item.item.equipment.damage.nb_faces,
                            item.item.equipment.damage.addsub)
                        game.gEngine.console_print_ex(compare_window, 1, 4,
                                                      libtcod.BKGND_SET,
                                                      libtcod.LEFT,
                                                      'Damage  : ' + damage)
                        game.gEngine.console_print_ex(
                            compare_window, 1, 5, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Accuracy: ' + str(item.item.equipment.accuracy))
                        game.gEngine.console_print_ex(
                            compare_window, 1, 7, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Skill   : ' + item.item.equipment.damage_type)
                    else:
                        game.gEngine.console_print_ex(
                            compare_window, 1, 4, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Armor   : ' + str(item.item.equipment.bonus))
                        game.gEngine.console_print_ex(
                            compare_window, 1, 5, libtcod.BKGND_SET,
                            libtcod.LEFT,
                            'Penalty : ' + str(item.item.equipment.penalty))
                        game.gEngine.console_print_ex(
                            compare_window, 1, 7, libtcod.BKGND_SET,
                            libtcod.LEFT, 'Location: ' +
                            item.item.equipment.location.capitalize())
                    game.gEngine.console_print_ex(
                        compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT,
                        'Value   : ' + str(item.item.value))

                if item.item.spell:
                    game.gEngine.console_print_ex(
                        compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT,
                        'Name  : ' +
                        color_text(item.name.capitalize(), item.color))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT,
                        'Type  : ' + item.item.spell.type.capitalize())
                    game.gEngine.console_print_ex(
                        compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT,
                        'Power : ' + str(item.item.spell.min) + '-' +
                        str(item.item.spell.max))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT,
                        'Range : ' + str(item.item.spell.range))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT,
                        'Radius: ' + str(item.item.spell.radius))
                    game.gEngine.console_print_ex(
                        compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT,
                        'Value : ' + str(item.item.value))

                if mouse.lbutton_pressed and mouse.cx >= 3:
                    if len(player.fighter.inventory) >= 26:
                        if not item.item.check_stackable():
                            message = 'Not enough inventory space!'
                            w = len(message) + 2
                            d_box = DialogBox(game,
                                              w,
                                              10,
                                              width / 2 - w / 2,
                                              height / 2 - 5,
                                              message,
                                              type='dialog',
                                              con=inventory_window)
                            d_box.display_box()
                        else:
                            pass
                    elif item.item.value > player.fighter.money:
                        message = 'Not enough money!'
                        w = len(message) + 2
                        d_box = DialogBox(game,
                                          w,
                                          10,
                                          width / 2 - w / 2,
                                          height / 2 - 5,
                                          message,
                                          type='dialog',
                                          con=inventory_window)
                        d_box.display_box()
                        s_master_check.set_checked(False)
                        for box in s_check_boxes:
                            box.set_checked(s_master_check.get_checked())
                    else:
                        i_n = color_text(item.name.capitalize(), item.color)
                        price = color_text(item.item.value, libtcod.gold)
                        message = 'Buy %s for %s?' % (i_n, price)
                        w = len(message) + 2
                        d_box = DialogBox(game,
                                          w,
                                          10,
                                          width / 4,
                                          height / 2,
                                          message,
                                          type='option',
                                          con=inventory_window)
                        confirm = d_box.display_box()
                        if confirm == 1:
                            player.fighter.money -= item.item.value
                            #player.fighter.inventory.append(item)
                            item.item.pick_up(player.fighter.inventory)
                            container.remove(item)
                            inventory_items = []
                            i_check_boxes = []
                            for x in range(len(player.fighter.inventory)):
                                i_check_boxes.append(CheckBox(x=1, y=x + 1))
                                i = color_text(
                                    player.fighter.inventory[x].name.
                                    capitalize(),
                                    player.fighter.inventory[x].color)
                                if player.fighter.inventory[
                                        x].item.check_stackable:
                                    i += ' (%d)' % player.fighter.inventory[
                                        x].item.qty
                                inventory_items.append(i)
                            s_check_boxes = []
                            s_options = []
                            if container:
                                for obj in container:
                                    obj_text = color_text(
                                        obj.name.capitalize(), obj.color)
                                    ob_value = color_text(
                                        obj.item.value, libtcod.gold)
                                    ob_value = '(%s)' % ob_value
                                    s_gold.append(ob_value)
                                    opt = '[%s] ' % obj_text
                                    s_options.append(opt)
                                    if len(obj.name) + 2 > s_size:
                                        s_size = len(obj.name) + 2
                            for x in range(len(container)):
                                s_check_boxes.append(CheckBox(x=1, y=x + 1))

        # keyboard input
        index = key.c - ord('a')
        if key:
            if key.vk == libtcod.KEY_DOWN:
                if current_selection is None:
                    current_selection = 0
                current_selection += 1
                if current_selection > len(inventory_items):
                    current_selection = 0
            if key.vk == libtcod.KEY_UP:
                if current_selection is None:
                    current_selection = 0
                current_selection -= 1
                if current_selection < 0:
                    current_selection = len(inventory_items)
        # ========================================================================
        # handle buttons
        # ========================================================================

        # ========================================================================
        # Sell Button
        # ========================================================================
        for i in sell_input:
            if i != -1:
                message = 'Sell all selected items?'
                w = len(message) + 2
                d_box = DialogBox(game,
                                  w,
                                  10,
                                  width / 2 - w / 2,
                                  height / 2 - 5,
                                  message,
                                  type='option',
                                  con=inventory_window)
                confirm = d_box.display_box()
                if confirm == 1:
                    d_box.destroy_box()
                    i_master_check.set_checked(False)
                    items_to_sell = []
                    for box in i_check_boxes:
                        if box.get_checked():
                            items_to_sell.append(
                                player.fighter.inventory[box.y - 1])
                    for item_to_sell in items_to_sell:
                        if item_to_sell:
                            player.fighter.money += item_to_sell.item.value / 2
                            player.fighter.inventory.remove(item_to_sell)
                    i_check_boxes = []
                    inventory_items = []
                    for x in range(len(player.fighter.inventory)):
                        i_check_boxes.append(CheckBox(x=1, y=x + 1))
                        inventory_items.append(
                            color_text(
                                player.fighter.inventory[x].name.capitalize(),
                                player.fighter.inventory[x].color))
                else:
                    d_box.destroy_box()

        # ========================================================================
        # buy button
        # ========================================================================
        for i in buy_input:
            if i != -1:
                n = 0
                for box in s_check_boxes:
                    if box.get_checked():
                        n += 1
                if len(player.fighter.inventory) + n > 26:
                    message = 'Not enough inventory space!'
                    w = len(message) + 2
                    d_box = DialogBox(game,
                                      w,
                                      10,
                                      width / 2 - w / 2,
                                      height / 2 - 5,
                                      message,
                                      type='dialog',
                                      con=inventory_window)
                    d_box.display_box()
                elif buy_value > player.fighter.money:
                    message = 'Not enough money!'
                    w = len(message) + 2
                    d_box = DialogBox(game,
                                      w,
                                      10,
                                      width / 2 - w / 2,
                                      height / 2 - 5,
                                      message,
                                      type='dialog',
                                      con=inventory_window)
                    d_box.display_box()
                    s_master_check.set_checked(False)
                    for box in s_check_boxes:
                        box.set_checked(s_master_check.get_checked())
                else:
                    message = 'Buy all selected items?'
                    w = len(message) + 2
                    d_box = DialogBox(game,
                                      w,
                                      10,
                                      width / 2 - w / 2,
                                      height / 2 - 5,
                                      message,
                                      type='option',
                                      con=inventory_window)
                    confirm = d_box.display_box()
                    if confirm == 1:
                        #d_box.destroy_box()
                        s_master_check.set_checked(False)
                        items_to_buy = []
                        for box in s_check_boxes:
                            if box.get_checked():
                                items_to_buy.append(container[box.y - 1])
                        for item_to_buy in items_to_buy:
                            if item_to_buy:
                                container.remove(item_to_buy)
                                player.fighter.money -= item_to_buy.item.value
                                #player.fighter.inventory.append(item_to_buy)
                                item_to_buy.item.pick_up(
                                    player.fighter.inventory)

                        s_check_boxes = []
                        s_options = []
                        if container:
                            for obj in container:
                                obj_text = color_text(obj.name.capitalize(),
                                                      obj.color)
                                ob_value = color_text(obj.item.value,
                                                      libtcod.gold)
                                ob_value = '(%s)' % ob_value
                                s_gold.append(ob_value)
                                opt = '[%s] ' % obj_text
                                s_options.append(opt)
                                if len(obj.name) + 2 > s_size:
                                    s_size = len(obj.name) + 2
                        for x in range(len(container)):
                            s_check_boxes.append(CheckBox(x=1, y=x + 1))

                        i_check_boxes = []
                        inventory_items = []
                        for x in range(len(player.fighter.inventory)):
                            i_check_boxes.append(CheckBox(x=1, y=x + 1))
                            i = color_text(
                                player.fighter.inventory[x].name.capitalize(),
                                player.fighter.inventory[x].color)
                            if player.fighter.inventory[
                                    x].item.check_stackable:
                                i += ' (%d)' % player.fighter.inventory[
                                    x].item.qty
                            inventory_items.append(i)
            #else:
                    d_box.destroy_box()

        # ========================================================================
        # handle exit button
        # ========================================================================

        for i in exit_input:
            if i != -1:
                key.vk = libtcod.KEY_ESCAPE
                break
    # Remember to remove consoles in reverse order of creation to avoid OOB errors
    if splash:
        game.gEngine.console_remove_console(splash_screen)
    buy_button.destroy_button()
    sell_button.destroy_button()
    exit_button.destroy_button()
    game.gEngine.console_remove_console(compare_window)
    game.gEngine.console_remove_console(shop_window)
    game.gEngine.console_remove_console(inventory_window)

    return
Пример #10
0
def character_info(con, width, height, game, x=0, y=0):
    skill_window = game.gEngine.console_new(width / 2, height)
    skill_window_y_pos = width / 2
    s_header, s_pos = get_centered_text("Skills", width / 4)

    char_window = game.gEngine.console_new(width / 2, height / 2)
    c_header, c_pos = get_centered_text(("%s's Skills and Abilities" % game.player.name), width / 4)

    skill_desc_window = game.gEngine.console_new(width / 2, height / 2)
    skill_desc_pos = height / 2
    d_header, d_pos = get_centered_text("Description", width / 4)

    exit_button = Button(
        label="Exit",
        game=game,
        x_pos=(width / 2) - 9,
        y_pos=height - 6,
        window=skill_window,
        dest_x=width / 2,
        dest_y=0,
    )
    current_selection = 0
    key = libtcod.console_check_for_keypress()
    while key.vk != libtcod.KEY_ESCAPE:
        game.gEngine.console_flush()
        # get input just after flush
        key = libtcod.console_check_for_keypress(True)
        mouse = libtcod.mouse_get_status()
        exit_input = exit_button.display(mouse)

        game.gEngine.console_blit(char_window, 0, 0, width / 2, height / 2, 0, 0, 0, 1.0, 1.0)
        game.gEngine.console_blit(skill_window, 0, 0, width / 2, height, 0, skill_window_y_pos, 0, 1.0, 1.0)
        game.gEngine.console_blit(skill_desc_window, 0, 0, width / 2, height / 2, 0, 0, skill_desc_pos, 1.0, 1.0)

        game.gEngine.console_clear(char_window)
        game.gEngine.console_clear(skill_desc_window)
        game.gEngine.console_clear(skill_window)

        # Draw Character info
        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(char_window, r, g, b)
        game.gEngine.console_print_frame(char_window, 0, 0, width / 2, height / 2, True)
        game.gEngine.console_print(char_window, c_pos, 0, c_header)
        game.gEngine.console_print(char_window, 1, 1, "Name: %s" % game.player.name)
        game.gEngine.console_print(
            char_window, 1, 2, "Hit Points: %d/%d" % (game.player.fighter.hp, game.player.fighter.max_hp)
        )

        game.gEngine.console_print(char_window, 1, 4, "Level: %d" % game.player.fighter.level)
        game.gEngine.console_print(char_window, 1, 5, "To Next Level: %d" % game.player.fighter.xp_to_next_level)

        s = color_text(str(game.player.fighter.stats[0]), libtcod.light_gray)
        d = color_text(str(game.player.fighter.stats[1]), libtcod.light_gray)
        i = color_text(str(game.player.fighter.stats[2]), libtcod.light_gray)
        c = color_text(str(game.player.fighter.stats[3]), libtcod.light_gray)
        game.gEngine.console_print(char_window, 1, 7, "Stats: Str [%s], Dex [%s]" % (s, d))
        game.gEngine.console_print(char_window, 1, 8, "       Int [%s], Con [%s]" % (i, c))

        bonus = color_text(str(game.player.fighter.armor_bonus), libtcod.green)
        bonus2 = color_text("10 +%d" % game.player.fighter.armor_bonus, libtcod.green)
        game.gEngine.console_print(char_window, 1, 10, "Bonus to Armor Roll  : [%s](%s) " % (bonus, bonus2))

        penalty = color_text(str(game.player.fighter.armor_penalty), libtcod.red)
        penalty2 = color_text("1d20 -%d" % game.player.fighter.armor_penalty, libtcod.red)
        game.gEngine.console_print(char_window, 1, 11, "Penalty to Dodge Roll: [%s](%s)" % (penalty, penalty2))

        speed = color_text(str(game.player.fighter.speed), libtcod.light_gray)
        game.gEngine.console_print(char_window, 1, 13, "Turn Speed: [%s]" % speed)

        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(skill_window, r, g, b)
        game.gEngine.console_print_frame(skill_window, 0, 0, width / 2, height, True)
        game.gEngine.console_print(skill_window, s_pos, 0, s_header)
        t, p = get_centered_text("Unspent Skill Points: [%d]" % game.player.fighter.unused_skill_points, width / 4)
        game.gEngine.console_print(skill_window, p, 1, t)

        y = 2
        letter_index = ord("a")
        skill_max = 5
        for skill in game.player.fighter.skills:
            s_name = skill.get_name()
            s_bonus = skill.get_bonus()
            s_index = chr(letter_index)
            col = libtcod.white
            if s_bonus == skill_max:
                s_name = color_text(s_name, libtcod.green)
                s_bonus = color_text(str(s_bonus), libtcod.green)
                s_index = color_text(s_index, libtcod.green)
                col = libtcod.green
            elif s_bonus < skill_max and s_bonus > 0:
                s_name = color_text(s_name, libtcod.light_gray)
                s_bonus = color_text(str(s_bonus), libtcod.lighter_gray)
                s_index = color_text(s_index, libtcod.lighter_gray)
                col = libtcod.lighter_gray
            elif s_bonus == 0:
                s_name = color_text(s_name, libtcod.dark_gray)
                s_bonus = color_text(str(s_bonus), libtcod.dark_gray)
                s_index = color_text(s_index, libtcod.dark_gray)
                col = libtcod.dark_gray
            else:
                s_name = color_text(s_name, libtcod.red)
                s_bonus = color_text(str(s_bonus), libtcod.red)
                s_index = color_text(s_index, libtcod.red)
                col = libtcod.red
            text = "(%s) %s: [%s]" % (s_index, s_name, s_bonus)
            game.gEngine.console_print(skill_window, 1, y, text)
            if current_selection == y - 2:
                r, g, b = libtcod.color_lerp(col, libtcod.blue, 0.5)
                game.gEngine.console_set_default_background(skill_window, r, g, b)
            else:
                game.gEngine.console_set_default_background(skill_window, 0, 0, 0)

            game.gEngine.console_print_ex(skill_window, 1, y, libtcod.BKGND_SET, libtcod.LEFT, text)
            y += 1
            letter_index += 1
        game.gEngine.console_set_default_background(skill_window, 0, 0, 0)

        r, g, b = libtcod.white
        game.gEngine.console_set_default_foreground(skill_desc_window, r, g, b)
        game.gEngine.console_print_frame(skill_desc_window, 0, 0, width / 2, height / 2, True)
        game.gEngine.console_print(skill_desc_window, d_pos, 0, d_header)

        # mouse input
        if mouse.cx >= width / 2 + 3:
            if mouse.cy - 2 < len(game.player.fighter.skills) and mouse.cy >= 0:
                current_selection = mouse.cy - 2
                skill = game.player.fighter.skills[current_selection]
                desc = color_text(skill.get_description(), libtcod.light_gray)
                desc = "Skill Description: %s" % desc
                cat = color_text(skill.get_category(), libtcod.light_gray)
                cat = "Skill Category   : %s" % cat
                bonus = skill.get_bonus()
                name = skill.get_name()
                if bonus == skill_max:
                    bonus = color_text(str(bonus), libtcod.green)
                    name = color_text(name, libtcod.green)
                elif bonus < skill_max and bonus > 0:
                    bonus = color_text(str(bonus), libtcod.lighter_gray)
                    name = color_text(name, libtcod.lighter_gray)
                elif bonus == 0:
                    bonus = color_text(str(bonus), libtcod.dark_gray)
                    name = color_text(name, libtcod.dark_gray)
                else:
                    bonus = color_text(str(bonus), libtcod.red)
                    name = color_text(name, libtcod.red)
                if skill.get_category() == "Discipline":
                    bonus = "Increases your (%s) to-hit rolls by [%s]." % (name, bonus)
                elif skill.get_category() == "Weapon":
                    bonus = "Increases your (%s) damage by [%s]." % (name, bonus)

                game.gEngine.console_print_rect(skill_desc_window, 1, 1, width / 2 - 2, 3, desc)
                game.gEngine.console_print(skill_desc_window, 1, 5, cat)
                game.gEngine.console_print_rect(skill_desc_window, 1, 7, width / 2 - 2, 3, bonus)
                if mouse.lbutton_pressed:
                    game.player.fighter.apply_skill_points(
                        game.player.fighter.skills[current_selection]
                    )  # use unused player skill points

        for i in exit_input:
            if i != -1:
                key.vk = libtcod.KEY_ESCAPE
                break

    exit_button.destroy_button()
    game.gEngine.console_remove_console(skill_desc_window)
    game.gEngine.console_remove_console(char_window)
    game.gEngine.console_remove_console(skill_window)

    return None