def target_tile(actor, max_range=None):
    """
    Return the position of a tile left-clicked in player's FOV
    (optionally in a range), or (None,None) if right-clicked.
    """
    (key, mouse) = poll()
    (ox, oy) = (mouse.cx, mouse.cy)
    using_mouse = False
    using_keyboard = False
    (kx, ky) = renderer.ScreenCoords.fromWorldCoords(actor.camera_position,
                                                     actor.pos)
    pos = None

    while True:
        # Render the screen. This erases the inventory and shows
        # the names of objects under the mouse.
        libtcod.console_flush()
        (key, mouse) = poll()
        renderer.render_all(actor, (kx, ky))
        actor.current_map.fov_needs_recompute = False
        if (mouse.cx != ox or mouse.cy != oy):
            using_mouse = True
            using_keyboard = False
        (key_pressed, direction, shift) = parse_move(key)
        if key_pressed:
            using_keyboard = True
            if using_mouse:
                (ox, oy) = (mouse.cx, mouse.cy)
            using_mouse = False
            if direction:
                kx += direction.x
                ky += direction.y

        if using_mouse:
            (kx, ky) = (mouse.cx, mouse.cy)
        pos = renderer.ScreenCoords.toWorldCoords(actor.camera_position, (kx, ky))
        libtcod.console_set_default_background(renderer._overlay, libtcod.black)
        libtcod.console_clear(renderer._overlay)
        (ux, uy) = renderer.ScreenCoords.fromWorldCoords(actor.camera_position,
                                                         actor.pos)
        libtcod.line_init(ux, uy, kx, ky)

        nx, ny = libtcod.line_step()
        while ((not (nx is None)) and nx >= 0 and ny >= 0 and
               nx < config.MAP_PANEL_WIDTH and
               ny < config.MAP_PANEL_HEIGHT):
            libtcod.console_set_char_background(renderer._overlay, nx, ny, libtcod.sepia, libtcod.BKGND_SET)
            nx, ny = libtcod.line_step()

        if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:
            libtcod.console_clear(renderer._overlay)
            return None

        # Accept the target if the player clicked in FOV
        # and within the range specified.
        if ((mouse.lbutton_pressed or key.vk == libtcod.KEY_ENTER) and
                libtcod.map_is_in_fov(actor.current_map.fov_map, pos.x, pos.y) and
                (max_range is None or actor.distance(pos) <= max_range)):
            libtcod.console_clear(renderer._overlay)
            return pos
def play_game(player):
    """
    Main loop.
    """
    player_action = None

    while not libtcod.console_is_window_closed():
        (key, mouse) = interface.poll()
        player.visible_objects = process_visible_objects(player)
        renderer.render_all(player, (mouse.cx, mouse.cy))
        player.current_map.fov_needs_recompute = False

        libtcod.console_flush()

        check_level_up(player)

        # Erase all objects at their old locations, before they move.
        for object in player.current_map.objects:
            renderer.clear_object(player, object)

        player_action = handle_keys(player, key)
        if player_action == 'exit':
            save_game(player)
            break

        if (player_action != 'didnt-take-turn' and
            (player.game_state == 'playing' or
             player.game_state == 'running')):
            for object in player.current_map.objects:
                if object.ai:
                    object.ai.take_turn(player)
示例#3
0
文件: game.py 项目: mob96/SnakeLike
def main():
    #Initialise some game variables/objects and the libtcod console
    screen_width = 16
    screen_height = 16
    fps_limit = 60

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    libtcod.console_init_root(screen_width, screen_height, 'Snake', False)
    libtcod.sys_set_fps(fps_limit)

    con = libtcod.console_new(screen_width, screen_height)
    key = libtcod.Key()
    mouse = libtcod.Mouse()
    apple = Apple('A', libtcod.Color(200, 20, 20), screen_width, screen_height)
    snake = Snake(int(screen_width / 2), int(screen_height / 2), 'S',
                  libtcod.Color(20, 200, 20), 1, fps_limit, apple,
                  screen_width, screen_height)

    #Main game loop
    while not libtcod.console_is_window_closed():
        #Updates game entities if snake is not dead
        if snake.alive == True:
            snake.update()
            entities = snake.entities.copy()
            entities.append(apple.apple)

        #Handles rendering
        render_all(con, entities, screen_width, screen_height)
        libtcod.console_flush()
        clear_all(con, entities)

        #Handles keyboard input
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)
        input = handle_keys(key)
        move = input.get('move')
        restart = input.get('restart')
        exit = input.get('exit')
        fullscreen = input.get('fullscreen')

        if move:
            snake.set_next_dir(move)

        if restart:
            snake = Snake(int(screen_width / 2), int(screen_height / 2), 'S',
                          libtcod.Color(20, 200, 20), 1, fps_limit, apple,
                          screen_width, screen_height)

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#4
0
def increase_player_skills(player):
    while True:
        options = [
            s.name + ': currently ' +
            str(player.fighter.skills.get(s.name, 0)) + ', costs ' +
            str(s.cost) + ' sp' for s in skill_list
        ]

        # Make sure log.message() displays as we loop
        renderer.render_all(player, None)
        (key, target) = renderer.menu(
            'Choose skill to increase, or x to explain:\n' + '(' +
            str(player.skill_points) + ' skill points available)\n', options,
            INVENTORY_WIDTH)
        if key == ord('x'):
            (c2, i2) = renderer.menu(
                'Choose skill to describe, or any other to cancel.\n\n',
                options, INVENTORY_WIDTH)
            if i2 is not None:
                log.message(skill_list[i2].name + ': ' +
                            skill_list[i2].description)

        if target is None:  # 0 *is* a valid target
            return

        if skill_list[target].cost > player.skill_points:
            log.message(skill_list[target].name.capitalize() + ' costs ' +
                        str(skill_list[target].cost) +
                        ' skill points, you only have ' +
                        str(player.skill_points))
            continue

        value = player.fighter.skills.get(skill_list[target].name, 10)
        if value >= 250:
            log.message(skill_list[target].name.capitalize() +
                        ' is already at its maximum.')
            continue

        player.skill_points -= skill_list[target].cost
        if value < 100:
            value += libtcod.random_get_int(0, 1, 8)
        elif value < 150:
            value += libtcod.random_get_int(0, 1, 4)
        elif value < 200:
            value += libtcod.random_get_int(0, 1, 2)
        elif value < 250:
            value += 1
        player.fighter.skills[skill_list[target].name] = value
        log.message('Increased ' + skill_list[target].name + ' to ' +
                    str(value))
示例#5
0
def _target_tile(player, max_range=None):
    while True:
        libtcodpy.console_flush()
        libtcodpy.sys_check_for_event(libtcodpy.EVENT_KEY_PRESS|libtcodpy.EVENT_MOUSE, ui.key, ui.mouse)
        renderer.render_all(player)

        (x, y) = (ui.mouse.cx, ui.mouse.cy)

        if (ui.mouse.lbutton_pressed and libtcodpy.map_is_in_fov(player.current_map.fov_map, x, y) and
           (max_range is None or player.distance(x, y) <= max_range)):
            return x, y

        if ui.mouse.rbutton_pressed or ui.key.vk == libtcodpy.KEY_ESCAPE:
            return None, None
示例#6
0
def play_game(player):
    """
    Main loop.
    """
    player_action = None

    while not libtcod.console_is_window_closed():
        (key, mouse) = interface.poll()
        player.visible_objects = process_visible_objects(player)
        renderer.render_all(player, (mouse.cx, mouse.cy))
        player.current_map.fov_needs_recompute = False

        libtcod.console_flush()

        # Erase all objects at their old locations, before they move.
        for object in player.current_map.objects:
            renderer.clear_object(player, object)

        player_action = handle_keys(player, key)
        if player_action == 'exit':
            save_game(player)
            break

        # Recompute FOV *here*, not during rendering the way the tutorial did,
        # so that monsters can react to the player's movement!
        # This is a d'oh! sort of issue, because FOV is about gameplay, not
        # just about rendering.
        if player.current_map.fov_needs_recompute:
            libtcod.map_compute_fov(
                player.current_map.fov_map, player.x,
                player.y, config.TORCH_RADIUS, config.FOV_LIGHT_WALLS, config.FOV_ALGO)

        if (player_action != 'didnt-take-turn' and
                (player.game_state == 'playing' or
                 player.game_state == 'running' or
                 player.game_state == 'shooting')):
            for object in player.current_map.objects:
                if object.ai:
                    object.ai.take_turn(player)
                if object.fighter and object.fighter.bleeding > 0:
                    # this will also include the player
                    actions.bleed(object)
            player.turn_count += 1
            if player.fighter.inebriation > 0:
                player.fighter.inebriation -= 1
示例#7
0
def play_game(player):
    """
    Main loop.
    """
    player_action = None

    while not libtcod.console_is_window_closed():
        (key, mouse) = interface.poll()
        player.visible_objects = process_visible_objects(player)
        renderer.render_all(player, (mouse.cx, mouse.cy))
        player.current_map.fov_needs_recompute = False

        libtcod.console_flush()

        # Erase all objects at their old locations, before they move.
        for object in player.current_map.objects:
            renderer.clear_object(player, object)

        player_action = handle_keys(player, key)
        if player_action == 'exit':
            save_game(player)
            break

        # Recompute FOV *here*, not during rendering the way the tutorial did,
        # so that monsters can react to the player's movement!
        # This is a d'oh! sort of issue, because FOV is about gameplay, not
        # just about rendering.
        if player.current_map.fov_needs_recompute:
            libtcod.map_compute_fov(player.current_map.fov_map, player.x,
                                    player.y, config.TORCH_RADIUS,
                                    config.FOV_LIGHT_WALLS, config.FOV_ALGO)

        if (player_action != 'didnt-take-turn' and
            (player.game_state == 'playing' or player.game_state == 'running'
             or player.game_state == 'shooting')):
            for object in player.current_map.objects:
                if object.ai:
                    object.ai.take_turn(player)
                if object.fighter and object.fighter.bleeding > 0:
                    # this will also include the player
                    actions.bleed(object)
            player.turn_count += 1
            if player.fighter.inebriation > 0:
                player.fighter.inebriation -= 1
示例#8
0
文件: vault.py 项目: brewerdave/vault
def play_game(player):
    ui.init()

    while not libtcodpy.console_is_window_closed():
        renderer.render_all(player)
        player.current_map.fov_needs_recompute = False

        libtcodpy.console_flush()

        for entity in player.current_map.map_entities:
            renderer.clear_entity(entity)

        player_action = handle_input(player)
        if player_action == 'exit':
            break

        if player.game_state == 'playing' and player_action != 'didnt-take-turn':
            for entity in player.current_map.map_entities:
                if entity.ai:
                    entity.ai.take_turn(player)
示例#9
0
文件: test.py 项目: mob96/SnakeLike
def main():
    screen_width = 20
    screen_height = 20

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    libtcod.console_init_root(screen_width, screen_height, 'TEST RL', False)

    con = libtcod.console_new(screen_width, screen_height)
    key = libtcod.Key()
    mouse = libtcod.Mouse()

    player = Entity(5, 5, '@', libtcod.red)
    oracle = Entity(2, 2, '0', libtcod.blue)

    entities = [player, oracle]

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        render_all(con, entities, screen_width, screen_height)
        libtcod.console_flush()
        clear_all(con, entities)

        input = handle_keys(key)

        move = input.get('move')
        exit = input.get('exit')
        fullscreen = input.get('fullscreen')

        if move:
            x, y = move
            player.move(x, y)

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#10
0
def increase_player_skills(player):
    while True:
        options = [s.name + ': currently ' + str(player.fighter.skills.get(s.name, 0)) +
                   ', costs ' + str(s.cost) + ' sp'
            for s in skill_list]

        # Make sure log.message() displays as we loop
        renderer.render_all(player, None)
        (key, target) = renderer.menu('Choose skill to increase, or x to explain:\n' +
                                      '(' + str(player.skill_points) + ' skill points available)\n',
                                      options, INVENTORY_WIDTH)
        if key == ord('x'):
            (c2, i2) = renderer.menu('Choose skill to describe, or any other to cancel.\n\n', options, INVENTORY_WIDTH)
            if i2 is not None:
                log.message(skill_list[i2].name + ': ' + skill_list[i2].description)
            
        if target is None:  # 0 *is* a valid target
            return

        if skill_list[target].cost > player.skill_points:
            log.message(skill_list[target].name.capitalize() + ' costs ' + str(skill_list[target].cost) +
                        ' skill points, you only have ' + str(player.skill_points))
            continue

        value = player.fighter.skills.get(skill_list[target].name, 10)
        if value >= 250:
            log.message(skill_list[target].name.capitalize() + ' is already at its maximum.')
            continue

        player.skill_points -= skill_list[target].cost
        if value < 100:
            value += libtcod.random_get_int(0, 1, 8)
        elif value < 150:
            value += libtcod.random_get_int(0, 1, 4)
        elif value < 200:
            value += libtcod.random_get_int(0, 1, 2)
        elif value < 250:
            value += 1
        player.fighter.skills[skill_list[target].name] = value
        log.message('Increased ' + skill_list[target].name + ' to ' + str(value))
示例#11
0
def main():
    ## SETUP ##
    screen_width = 80
    screen_height = 65
    fps_limit = 10

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    libtcod.console_init_root(screen_width, screen_height, 'The Game of Life',
                              False)
    libtcod.sys_set_fps(fps_limit)

    con = libtcod.console_new(screen_width, screen_height)
    key = libtcod.Key()
    mouse = libtcod.Mouse()
    map = GameMap(screen_width, screen_height)

    ## GAME LOOP ##
    while not libtcod.console_is_window_closed():

        ## ENTITY UPDATES AND RENDERING ##
        entities = map.get_entities()
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)
        render_all(con, entities, screen_width, screen_height)
        libtcod.console_flush()
        clear_all(con, entities)
        map.update_tiles()

        ## CALLS TO INPUT HANDLING ##
        input = handle_keys(key)
        exit = input.get('exit')
        fullscreen = input.get('fullscreen')

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#12
0
def main():
    # Importing data from config
    with open('data/config.json') as cfg:
        data=json.load(cfg)
    terminal_width=data['terminal_width']
    terminal_height=data['terminal_height']
    map_width=data['map_width']
    map_height=data['map_height']
    fov_algorithm=data['fov_algorithm']
    fov_light_walls=data['fov_light_walls']
    fov_radius=data['fov_radius']
    # Init root console
    tcod.console_set_custom_font('gfx/fonts/terminal12x16_gs_ro.png', tcod.FONT_TYPE_GREYSCALE | tcod.tcod.FONT_LAYOUT_CP437)
    console_root=tcod.console_init_root(terminal_width, terminal_height, 'Python Game Lol', False, tcod.RENDERER_SDL2, 'C', False)
    console_display=tcod.console.Console(terminal_width, terminal_height, 'C')
    with open('gfx/palette.json') as colours:
        palette=json.load(colours)
    console_display.bg[:]=palette['terminal_green']
    #console_interface=tcod.console.Console(terminal_width, map_height, 'C')
    game_map=GameMap(map_width, map_height)
    spawner=Spawner(map_width, map_height, 0)
            # Testing creatures
    generate_test_area(game_map, spawner)
    player=spawner.entities[0]
    # Then generate map
    fov_recompute=True
    # message log
    game_state=GameStates.TURN_PLAYER
    prev_game_state=game_state
    #targeting_item=None
    # Rendering for the first time
    game_map.recompute_fov(player.x, player.y, fov_radius, fov_light_walls, fov_algorithm)
    render_all(console_root, console_display, spawner.entities, game_map, fov_recompute)
    fov_recompute=False
    tcod.console_flush()
    # Game loop
    while True:
        # Render
        if fov_recompute:
            game_map.recompute_fov(player.x, player.y, fov_radius, fov_light_walls, fov_algorithm)
        render_all(console_root, console_display, spawner.entities, game_map, fov_recompute)
        fov_recompute=False
        tcod.console_flush()
        erase_entities(console_display, spawner.entities, game_map)
        # Processing action
        action=handle_event(game_state)
        move=action.get('move')
        pickup=action.get('pickup')
        take_inventory=action.get('take_inventory')
        fullscreen=action.get('fullscreen')
        exit=action.get('exit')
        # Player's Turn
        results_player=[]
        # Results: Extend if player turn ends and append if doesn't? Tcod tutorial is confusing.
        if game_state==GameStates.TURN_PLAYER:
            if move:
                dx, dy=move
                results_movement=player.handle_move(dx, dy, spawner, game_map.path_map, swappable=True)
                if results_movement:
                    results_player.extend(results_movement)
                    fov_recompute=True
                    game_state=GameStates.TURN_ALLY

            elif pickup: # Should implement a pickup list like POWDER
                for entity in spawner.entities:
                    if entity.item and entity.x==player.x and entity.y==player.y:
                        print('ADD ITEM')   # Add item
                        break
                else:
                    print('GRAB GROUND')    # Message log to grab ground
        
        if take_inventory:
            prev_game_state=game_state
            game_state=GameStates.MENU_INVENTORY
        
        if exit:
            if game_state.value>=10:  # Game states >= 10 are menus: inventory, quipment, etc.
                game_state=prev_game_state
            if game_state.value>=20:  # Game states >= 20 are targetings
                results_player.append({'targeting_cancelled': True})
            # else brings up main menu
        
        if fullscreen:
            tcod.console_set_fullscreen(not tcod.console_is_fullscreen())

        # Player turn messages: handled by an announcer (translation friendly probably)

        # Faction turns (handled by an announcer also)
        if game_state==GameStates.TURN_ALLY:
            for entity in spawner.entities:
                if entity.faction==Factions.ALLY and entity!=player:
                    results_ally=entity.ai.take_turn(spawner, game_map.path_map)
            game_state=GameStates.TURN_ENEMY
        
        if game_state==GameStates.TURN_ENEMY:
            for entity in spawner.entities:
                if entity.faction==Factions.ENEMY:
                    results_enemy=entity.ai.take_turn(spawner, game_map.path_map)
            game_state=GameStates.TURN_NEUTRAL
        
        if game_state==GameStates.TURN_NEUTRAL:
            for entity in spawner.entities:
                if entity.faction==Factions.NEUTRAL:
                    if entity.ai:
                        resutls_neutral=entity.ai.take_turn(spawner, game_map.path_map)
            game_state=GameStates.TURN_PLAYER
示例#13
0
        dest='output_dir',
        help='output directory -- If not specified it will try to use ~/.config/theme-gen')

    # Check if there's a user templates directory
    tpl_dir = path.join(path.expanduser('~'), '.config', 'theme-gen', 'templates')
    if not path.isdir(tpl_dir):
        tpl_dir = path.join(path.dirname(__file__), 'templates')

    parser.add_argument(
        '-t',
        action='store',
        default=tpl_dir,
        dest='tpl_dir',
        help='templates directory -- If not specified it will try to use ./templates')

    parsed = parser.parse_args()

    return parsed


if __name__ == "__main__":
    args = parse_args()
    xresources = xresparser.parse(args.xres_file)

    # Render all templates
    render_all(
        args.tpl_dir,
        args.output_dir,
        xresources
    )
def target_tile(actor, max_range=None):
    """
    Return the position of a tile left-clicked in player's FOV
    (optionally in a range), or (None,None) if right-clicked.
    """
    (key, mouse) = poll()
    (ox, oy) = (mouse.cx, mouse.cy)
    using_mouse = False
    using_keyboard = False
    (kx, ky) = renderer.ScreenCoords.fromWorldCoords(actor.camera_position,
                                                     actor.pos)
    pos = None

    while True:
        # Render the screen. This erases the inventory and shows
        # the names of objects under the mouse.
        libtcod.console_flush()
        (key, mouse) = poll()
        renderer.render_all(actor, (kx, ky))
        actor.current_map.fov_needs_recompute = False
        if (mouse.cx != ox or mouse.cy != oy):
            using_mouse = True
            using_keyboard = False
        (key_pressed, direction, shift) = parse_move(key)
        if key_pressed:
            using_keyboard = True
            if using_mouse:
                (ox, oy) = (mouse.cx, mouse.cy)
            using_mouse = False
            if direction:
                kx += direction.x
                ky += direction.y

        if using_mouse:
            (kx, ky) = (mouse.cx, mouse.cy)
        pos = renderer.ScreenCoords.toWorldCoords(actor.camera_position,
                                                  (kx, ky))
        libtcod.console_set_default_background(renderer._overlay,
                                               libtcod.black)
        libtcod.console_clear(renderer._overlay)
        (ux,
         uy) = renderer.ScreenCoords.fromWorldCoords(actor.camera_position,
                                                     actor.pos)
        libtcod.line_init(ux, uy, kx, ky)

        nx, ny = libtcod.line_step()
        while ((not (nx is None)) and nx >= 0 and ny >= 0
               and nx < config.MAP_PANEL_WIDTH
               and ny < config.MAP_PANEL_HEIGHT):
            libtcod.console_set_char_background(renderer._overlay, nx, ny,
                                                libtcod.sepia,
                                                libtcod.BKGND_SET)
            nx, ny = libtcod.line_step()

        if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:
            libtcod.console_clear(renderer._overlay)
            return None

        # Accept the target if the player clicked in FOV
        # and within the range specified.
        if ((mouse.lbutton_pressed or key.vk == libtcod.KEY_ENTER) and
                libtcod.map_is_in_fov(actor.current_map.fov_map, pos.x, pos.y)
                and (max_range is None or actor.distance(pos) <= max_range)):
            libtcod.console_clear(renderer._overlay)
            return pos