Beispiel #1
0
def test_nested_formattingstring(monkeypatch):
    """Test nested __call__ behavior of formatters.FormattingString."""
    from blessed.formatters import FormattingString

    # given, with arg
    pstr = FormattingString(u'a1-', u'n-')
    zstr = FormattingString(u'a2-', u'n-')

    # exercise __call__
    assert pstr('x-', zstr('f-'), 'q-') == 'a1-x-a2-f-n-a1-q-n-'
Beispiel #2
0
def test_formattingstring(monkeypatch):
    """Test simple __call__ behavior of formatters.FormattingString."""
    from blessed.formatters import FormattingString

    # given, with arg
    pstr = FormattingString(u'attr', u'norm')

    # exercise __call__,
    assert pstr._normal == u'norm'
    assert str(pstr) == u'attr'
    assert pstr('text') == u'attrtextnorm'

    # given, with empty attribute
    pstr = FormattingString(u'', u'norm')
    assert pstr('text') == u'text'
Beispiel #3
0
def test_formattingstring(monkeypatch):
    """Test formatters.FormattingString"""
    from blessed.formatters import (FormattingString)

    # given, with arg
    pstr = FormattingString(u'attr', u'norm')

    # excersize __call__,
    assert pstr._normal == u'norm'
    assert str(pstr) == u'attr'
    assert pstr('text') == u'attrtextnorm'

    # given, without arg
    pstr = FormattingString(u'', u'norm')
    assert pstr('text') == u'text'
Beispiel #4
0
def render_all(
        t: Terminal,
        game_map: GameMap,
        entities,
        fov_map,
        fov_recompute: bool
):
    """
    Renders everything. The whole lot.
    """

    # Define tile colors here to save on memory
    # Don't need them defined in each tile instance
    colors = {
         'dark_wall': FormattingString(t.black, t.normal),
         'dark_ground': FormattingString(t.green, t.normal),
         'light_wall': FormattingString(t.yellow, t.normal),
         'light_ground': FormattingString(t.red, t.normal),
    }

    if fov_recompute:
        """
        Only bother redrawing all the tiles if the FOV was recomputed.
        Otherwise they will still be on the screen from last time.
        """
        for y in range(game_map.height):
            for x in range(game_map.width):
                visible = tcod.map_is_in_fov(fov_map, x, y)
                wall = game_map.tiles[x][y].block_sight

                # Optimize this whole thing, it's ridiculous
                if visible:
                    if wall:
                        # TODO: Optimize. Don't need to move to each tile individually.
                        print(colors['light_wall'](t.move(y, x) + '#'))
                    else:
                        print(colors['light_ground'](t.move(y, x) + '.'))
                else:
                    if wall:
                        print(colors['dark_wall'](t.move(y, x) + '#'))
                    else:
                        print(colors['dark_ground'](t.move(y, x) + '.'))

    for ent in entities:
        render_entity(t, ent)
Beispiel #5
0
def test_nested_formattingstring_type_error(monkeypatch):
    """Test formatters.FormattingString raising TypeError."""
    from blessed.formatters import FormattingString

    # given,
    pstr = FormattingString(u'a-', u'n-')
    expected_msg = ("Positional argument #1 is {0} expected any of ".format(
        type(1)))

    # exercise,
    with pytest.raises(TypeError) as err:
        pstr('text', 1, '...')

    # verify,
    assert expected_msg in '{0}'.format(err)
Beispiel #6
0
def test_nested_formattingstring_type_error(monkeypatch):
    """Test formatters.FormattingString raising TypeError."""
    from blessed.formatters import FormattingString

    # given,
    pstr = FormattingString(u'a-', u'n-')
    expected_msgs = (
        (
            "TypeError for FormattingString argument, 291, at position 1: "
            "expected type str, got int"  # py3x
        ),
        ("TypeError for FormattingString argument, 291, at position 1: "
         "expected type basestring, got int"))  # py2

    # exercise,
    with pytest.raises(TypeError) as err:
        pstr('text', 0x123, '...')

    # verify,
    assert str(err.value) in expected_msgs
Beispiel #7
0
def main():
    t = Terminal()

    # Prelude
    logger.info("----------------------------------")
    logger.info("Starting new run.")
    logger.info("Datetime: %s", datetime.datetime.now().isoformat())
    logger.info("Revision: %s", os.getenv('REVISION'))
    logger.info("Terminal colors: %d", t.number_of_colors)
    logger.info("Terminal size: %dx%d", t.width, t.height)
    logger.info("----------------------------------")
    #

    # Create the world
    map_dimensions = (20, 12)

    if t.width < map_dimensions[0] or t.height < map_dimensions[1]:
        logger.fatal("Terminal too small: Must be at least %dx%d in size.",
                     map_dimensions[0], map_dimensions[1])
        sys.exit(1)

    game_map = GameMap(map_dimensions[0], map_dimensions[1])
    game_map.make_map()

    fov_map = initialize_fov(game_map)

    entities = [
        Entity(4,
               4,
               '@',
               'Player',
               FormattingString(t.red, t.normal),
               fighter=Fighter(hp=30, defense=2, power=5)),
        Entity(map_dimensions[0] // 2 + 5, map_dimensions[1] // 2 - 2, '$',
               'Mysterious Object', FormattingString(t.yellow, t.normal)),
        Entity(3,
               3,
               '%',
               'Crate',
               FormattingString(t.blue, t.normal),
               pushable=True),
        Entity(10,
               5,
               'o',
               'Goblin',
               FormattingString(t.green, t.normal),
               fighter=Fighter(hp=16, defense=1, power=3),
               ai=BasicMonster())
    ]

    # Ready the screen for drawing
    print(t.enter_fullscreen())

    with t.hidden_cursor():
        # Handle input immediately
        with t.cbreak():

            # Enter the main game loop
            game_loop(t, game_map, entities, fov_map)

    print(t.exit_fullscreen())