游戏终端板

3

我正在做一个大学项目的游戏。我想制作一个棋盘,以便玩家可以移动。

这个棋盘应该长成这样,这个是用颜色制作的,但它不适合我的目的,因为无法实现移动和玩家。

import colored

nb_rows = 20
nb_cols = 20

to_display = ''
for row_id in range(nb_rows):
    for col_id in range(nb_cols):
            if (row_id + col_id) % 2 == 0:
                to_display += colored.bg('yellow') + colored.fg('red')
            else:
                to_display += colored.bg('green') + colored.fg('blue')
                
            to_display += '  ' + colored.attr('reset')
    
    to_display += '\n'
    
print(to_display)

我在他们的文档中没有找到任何有用的东西。我想知道是否可以使用blessed来完成相同的事情。


嗨@Goetia,欢迎来到Stack Overflow!您想要实现什么结果?我无法理解。 - Lucas Vazquez
嗨@LucasVazquez,我想创建一个20×20的棋盘游戏,以便“团队1”可以与“团队2”对战。例如,我必须使用“blessed”来控制玩家的位置或移动。 - Goetia
考虑它像一个棋盘,玩家可以根据他们的角色在多个方向上移动。 - Goetia
1个回答

1

我以前从未使用过 blessed,所以我会给你一个部分解决方案。

首先,你应该知道他们的仓库中有各种示例,可以用来了解更多关于这个包的内容。这是其中之一:https://github.com/jquast/blessed/blob/master/bin/worms.py

因此,在提到这一点之后,我将为您留下一个可能有帮助的代码示例。我在上面加了一些注释,因为我认为它们可能有用。

from functools import partial

from blessed import Terminal

terminal = Terminal()


def create_board(number_of_pair_cells, even_color, odd_color):
    echo = partial(print, end="", flush=True)

    # The height and width values may vary depending on the font size
    # of your terminal.

    # Each value of `cell_height` represents one line.
    cell_height = 1

    # Each value of `cell_width` represents one space.
    # Two more spaces are added by `echo`.
    # In this case, the final computed value is 0 + 2 = 2.
    cell_width = 0

    for i in range(number_of_pair_cells):
        # This generates the intermittent color effect typical of a board.
        if i != 0:
            even_color, odd_color = odd_color, even_color

        # This print the board.
        # I recommend you to replace the `"\n"` and the `" "` with
        # other values to know how this package works.
        # You'll be surprised.
        # Also, I recommend you to replace the `terminal.normal`
        # (that resets the background color) to `terminal.red`,
        # to have more info about the terminal dimensions.
        echo(
            *(
                "\n",
                *(
                    even_color,
                    " " * cell_width,
                    odd_color,
                    " " * cell_width,
                ) * int(number_of_pair_cells / 2),
                terminal.normal,
            ) * cell_height,
        )


# The `on_yellow` value is a reference to a yellow background color.
# This is the same for `on_green`.
# If you want to print a red color over a blue background,
# you need to use `terminal.red` and `terminal.on_blue`.
create_board(20, terminal.on_yellow, terminal.on_green)

result


最后一点评论。 我制作了这个例子来向您展示使用blessed可以制作面板,但您可能会发现更好的方法来做到这一点,更适合您的需要。例如,您可能想要使用print而不是echo,并且您可能希望使用更多的for循环,而不是使用*运算符拆分可迭代对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接