Reputation: 33
I'm making a game as a university project. I want to make a board so that the players can move.
The board should look like this, this one is made with colored, but it won't work for my purpose since movements and players cannot be implemented.
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)
I didn't find anything helpful in their documentation. I'm wondering if there is a way to do the same but with blessed instead.
Upvotes: 3
Views: 508
Reputation: 1912
I've never used blessed
before, so I'll give you a partial solution.
First of all, you should to know there's various examples in their repo that you can use to learn more about this package. Here is one: https://github.com/jquast/blessed/blob/master/bin/worms.py
So, after mentioning that, I leave you with a code example that might help. I put some comments on it because I think they can be useful.
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)
Just a final comment.
I mad this example to show you that it is possible to make a board using blessed
, but you probably find a better way to do it, more adapted to your needs. For example, you may want to use print
instead of echo
and you may want more for
loops instead of unpacking iterables with the *
operator.
Upvotes: 1