我正在做一个大学项目的游戏.我想做一块棋盘,这样球员们就可以移动了.

棋盘应该是这样的,这个是用colored做的,但它不适用于我的目的,因为动作和球员无法执行.

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)

我在他们的documentation中没有发现任何有用的东西.我想知道是否有一种方法可以做到这一点,但与祝福相反.

推荐答案

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

首先,你应该知道他们的回购协议中有各种各样的例子,你可以用它们来了解更多关于这个包的信息.这里是1:https://github.com/jquast/blessed/blob/master/bin/worms.py

所以,在提到这一点之后,我给大家留下一个可能会有所帮助的代码示例.我对此发表了一些 comments ,因为我认为它们会有用.

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


最后一点意见.

Python相关问答推荐

通过仅导入pandas来在for循环中进行多情节

计算相同形状的两个张量的SSE损失

Pandas 在最近的日期合并,考虑到破产

Pandas - groupby字符串字段并按时间范围 Select

如何使用它?

将输入聚合到统一词典中

如何使用scipy的curve_fit与约束,其中拟合的曲线总是在观测值之下?

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

启动带有参数的Python NTFS会导致文件路径混乱

为什么np. exp(1000)给出溢出警告,而np. exp(—100000)没有给出下溢警告?

如何使用Numpy. stracards重新编写滚动和?

为什么if2/if3会提供两种不同的输出?

Polars Group by描述扩展

将CSS链接到HTML文件的问题

如何训练每一个pandaprame行的线性回归并生成斜率

如何使用加速广播主进程张量?

利用SCIPY沿第一轴对数组进行内插

BeatuifulSoup从欧洲志愿者服务中获取数据和解析:一个从EU-Site收集机会的小铲子

如何在Pandas中用迭代器求一个序列的平均值?

PYTHON中的selenium不会打开 chromium URL