我正在制作一个游戏作为一个大学项目。我想做一个棋盘,让玩家可以移动。
棋盘应该是这样的,这个是用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 代替。
最佳答案
我以前从未使用过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)
只是最后的评论。
我用这个示例来向您展示可以使用 blessed
制作电路板,但您可能会找到更好的方法来完成它,更适合您的需求。例如,您可能想要使用 print
而不是 echo
并且您可能想要更多的 for
循环而不是使用 *
运算符。
关于python - 游戏终端板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71520280/