示例#1
0
 def set_fps(self, change=0, value=0):
     if value:
         if value >= 1:
             tdl.set_fps(int(value))
     elif change:
         fps = tdl.get_fps() + change
         if fps >= 1:
             tdl.set_fps(fps)
         else:
             tdl.set_fps(1)
示例#2
0
def render_all(con, panel, entities, player, game_map, fov_recompute,
               root_console, message_log, screen_width, screen_height,
               bar_width, panel_height, panel_y, mouse_coordinates, colors,
               game_state, animations):
    # draw all the tiles in the game map
    root_console.clear()
    if fov_recompute:
        for x, y in game_map:
            wall = not game_map.transparent[x, y]

            if game_map.fov[x, y]:
                if wall:
                    con.draw_char(x,
                                  y,
                                  ' ',
                                  fg=None,
                                  bg=colors.get('light_wall'))
                else:
                    con.draw_char(x,
                                  y,
                                  '.',
                                  fg=colors.get('light_ground_dot'),
                                  bg=colors.get('light_ground'))
                game_map.explored[x][y] = True
            elif game_map.explored[x][y]:
                if wall:
                    con.draw_char(x,
                                  y,
                                  ' ',
                                  fg=None,
                                  bg=colors.get('dark_wall'))
                else:
                    con.draw_char(x,
                                  y,
                                  '.',
                                  fg=colors.get('black'),
                                  bg=colors.get('dark_ground'))
            else:
                con.draw_char(x, y, 176, fg=(30, 30, 30))

    # draw all entities in the list
    entities_in_render_order = sorted(entities,
                                      key=lambda x: x.render_order.value)

    for entity in entities_in_render_order:
        draw_entity(con, entity, game_map)

    panel.clear(fg=colors.get('white'), bg=colors.get('black'))

    # print messages, one line at a time
    y = 1
    for message in message_log.messages:
        panel.draw_str(message_log.x,
                       y,
                       message.text,
                       bg=None,
                       fg=message.color)
        y += 1

    root_console.blit(con, 0, 0, screen_width, screen_height,
                      (player.x - screen_width / 2),
                      (player.y - screen_height / 2))
    render_bar(panel, 1, 1, bar_width, 'HP', player.fighter.hp,
               player.fighter.max_hp, colors.get('light_red'),
               colors.get('darker_red'), colors.get('black'))

    panel.draw_str(1,
                   3,
                   'Dungeon Level: {0}'.format(game_map.dungeon_level),
                   fg=colors.get('white'),
                   bg=None)

    panel.draw_str(
        1, 0, get_names_under_mouse(mouse_coordinates, entities, game_map))
    panel.draw_str(1, 4, 'FPS: {0}'.format(get_fps()))

    root_console.blit(panel, 0, panel_y, screen_width, panel_height, 0, 0)

    # show menus
    if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY):
        if game_state == GameStates.SHOW_INVENTORY:
            inventory_title = 'Select item to use. ESC to cancel.\n'
        else:
            inventory_title = 'Select item to drop. ESC to cancel.\n'
        inventory_menu(con, root_console, inventory_title, player, 50,
                       screen_width, screen_height)
    elif game_state == GameStates.LEVEL_UP:
        level_up_menu(con, root_console, 'Level up!', player, 40, screen_width,
                      screen_height)
    elif game_state == GameStates.CHAR_SCREEN:
        char_screen(root_console, player, 30, 10, screen_width, screen_height)

    if animations:
        for anim in animations:
            result = anim.animate(game_map, mouse_coordinates)
            if result.get('done'):
                animations.remove(anim)
示例#3
0
def test_fps():
    tdl.set_fps(0)
    tdl.get_fps()
示例#4
0
文件: life.py 项目: ddw/python-tdl
def main():
    console = tdl.init(WIDTH, HEIGHT)
    board = LifeBoard(WIDTH, HEIGHT - 1)
    # The R-pentomino
    # board.set_batch(WIDTH // 2 - 2,HEIGHT // 2 - 2,
    #                [' **',
    #                 '** ',
    #                 ' * '])

    # Diehard
    # board.set_batch(WIDTH // 2 - 5,HEIGHT // 2 - 2,
    #                ['      * ',
    #                 '**      ',
    #                 ' *   ***'])

    # Gosper glider gun
    board.set_batch(
        1,
        1,
        [
            "                                    ",
            "                        *           ",
            "                      * *           ",
            "            **      **            **",
            "           *   *    **            **",
            "**        *     *   **              ",
            "**        *   * **    * *           ",
            "          *     *       *           ",
            "           *   *                    ",
            "            **                      ",
        ],
    )

    play = False
    redraw = True
    mouse_drawing = None
    mouse_x = -1
    mouse_y = -1
    while True:
        for event in tdl.event.get():
            if event.type == "QUIT":
                return
            elif event.type == "KEYDOWN":
                if event.key == "SPACE":
                    play = not play
                    redraw = True
                elif event.char.upper() == "S":
                    board.step()
                    redraw = True
                elif event.char.upper() == "C":
                    board.clear()
                    redraw = True
                elif event.char.upper() == "W":
                    board.wrap = not board.wrap
                    redraw = True
            elif event.type == "MOUSEDOWN":
                x, y, = event.cell
                board.toggle(x, y)
                mouse_drawing = event.cell
                redraw = True
            elif event.type == "MOUSEUP":
                mouse_drawing = None
            elif event.type == "MOUSEMOTION":
                if mouse_drawing and mouse_drawing != event.cell:
                    x, y = mouse_drawing = event.cell
                    board.toggle(x, y)
                mouse_x, mouse_y = event.cell
                redraw = True
        if play and mouse_drawing is None:
            board.step()
            redraw = True
        if redraw:
            redraw = False
            console.clear()
            for x, y in board.live_cells:
                console.draw_char(x, y, "*")
            # console.draw_rect(0, -1, None, None, None, bg=(64, 64, 80))
            console.draw_rect(0, -1, None, None, None, bg=(64, 64, 80))
            console.draw_str(
                0,
                -1,
                "Mouse:Toggle Cells, Space:%5s, [S]tep, [C]lear, [W]rap Turn %s"
                % (["Play", "Pause"][play], ["On", "Off"][board.wrap]),
                None,
                None,
            )
            if (mouse_x, mouse_y) in console:
                console.draw_char(mouse_x, mouse_y, None, (0, 0, 0), (255, 255, 255))
        else:
            time.sleep(0.01)
        tdl.flush()
        tdl.set_title("Conway's Game of Life - %i FPS" % tdl.get_fps())
示例#5
0
def main():
    console = tdl.init(WIDTH, HEIGHT)
    board = LifeBoard(WIDTH, HEIGHT - 1)
    # The R-pentomino
    #board.set_batch(WIDTH // 2 - 2,HEIGHT // 2 - 2,
    #                [' **',
    #                 '** ',
    #                 ' * '])

    # Diehard
    #board.set_batch(WIDTH // 2 - 5,HEIGHT // 2 - 2,
    #                ['      * ',
    #                 '**      ',
    #                 ' *   ***'])

    # Gosper glider gun
    board.set_batch(1, 1, [
        '                                    ',
        '                        *           ',
        '                      * *           ',
        '            **      **            **',
        '           *   *    **            **',
        '**        *     *   **              ',
        '**        *   * **    * *           ',
        '          *     *       *           ',
        '           *   *                    ',
        '            **                      '
    ])

    play = False
    redraw = True
    mouse_drawing = None
    mouse_x = -1
    mouse_y = -1
    while True:
        for event in tdl.event.get():
            if event.type == 'QUIT':
                return
            elif event.type == 'KEYDOWN':
                if event.key == 'SPACE':
                    play = not play
                    redraw = True
                elif event.char.upper() == 'S':
                    board.step()
                    redraw = True
                elif event.char.upper() == 'C':
                    board.clear()
                    redraw = True
                elif event.char.upper() == 'W':
                    board.wrap = not board.wrap
                    redraw = True
            elif event.type == 'MOUSEDOWN':
                x, y, = event.cell
                board.toggle(x, y)
                mouse_drawing = event.cell
                redraw = True
            elif event.type == 'MOUSEUP':
                mouse_drawing = None
            elif event.type == 'MOUSEMOTION':
                if (mouse_drawing and mouse_drawing != event.cell):
                    x, y = mouse_drawing = event.cell
                    board.toggle(x, y)
                mouse_x, mouse_y = event.cell
                redraw = True
        if play and mouse_drawing is None:
            board.step()
            redraw = True
        if redraw:
            redraw = False
            console.clear()
            for x, y in board.live_cells:
                console.draw_char(x, y, '*')
            #console.draw_rect(0, -1, None, None, None, bg=(64, 64, 80))
            console.draw_rect(0, -1, None, None, None, bg=(64, 64, 80))
            console.draw_str(
                0, -1,
                "Mouse:Toggle Cells, Space:%5s, [S]tep, [C]lear, [W]rap Turn %s"
                % (['Play', 'Pause'][play], ['On', 'Off'][board.wrap]), None,
                None)
            if (mouse_x, mouse_y) in console:
                console.draw_char(mouse_x, mouse_y, None, (0, 0, 0),
                                  (255, 255, 255))
        else:
            time.sleep(0.01)
        tdl.flush()
        tdl.set_title("Conway's Game of Life - %i FPS" % tdl.get_fps())
示例#6
0
 def get_fps(self):
     return tdl.get_fps()