diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-14 13:47:35 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-14 13:47:35 +0200 |
commit | e56c2153add98361ea7909789b195d3a702f85bb (patch) | |
tree | fd7d9691a4b62f44b2c60f5cf62f006a9476124f /tui | |
parent | 822d89c50a70277186f5c845e4341236548c16b7 (diff) |
Moving things around and adding fouth flag option for bot suggestions.
Diffstat (limited to 'tui')
-rw-r--r-- | tui/ansi.py | 46 | ||||
-rw-r--r-- | tui/static.py | 24 | ||||
-rw-r--r-- | tui/tui.py | 40 |
3 files changed, 58 insertions, 52 deletions
diff --git a/tui/ansi.py b/tui/ansi.py new file mode 100644 index 0000000..c25ff6c --- /dev/null +++ b/tui/ansi.py @@ -0,0 +1,46 @@ +""" ansi.py - ansi ohjauskomentoja. värit jne """ + +class Ansi: + """ Ansi - Luokallinen staattisia metodeja ansi komennoille """ + + BLACK = 0 + RED = 1 + GREEN = 2 + YELLOW = 3 + BLUE = 4 + MAGENTA = 5 + CYAN = 6 + WHITE = 7 + GRAY = 8 + BRIGHT_RED = 9 + BRIGHT_GREEN = 0xA + BRIGHT_YELLOW = 0xB + BRIGHT_BLUE = 0xC + BRIGHT_MAGENTA = 0xD + BRIGHT_CYAN = 0xE + BRIGHT_WHITE = 0xF + + @staticmethod + def color(color): + """ asettaa tekstin värin """ + if color in range(16): + print(end=f"\033[{'1;' if color//8 else ''}3{color%8}m") + + + @staticmethod + def bg(color): + """ asettaa tekstin taustan värin""" + if color in range(8): + print(end=f"\033[4{color}m") + + + @staticmethod + def cup(lines): + """ liikuttaa kursoria ylöspäin""" + print(end=f"\033[{lines}F") + + + @staticmethod + def reset(): + """ resetoi tekstin värin ja muut attribuutit perusarvoille """ + print(end="\033[0m") diff --git a/tui/static.py b/tui/static.py index ecd80b9..7bdfa12 100644 --- a/tui/static.py +++ b/tui/static.py @@ -1,5 +1,5 @@ """ tui/static.py - Staattiset määritykset tui:ssa tarvittaville jutuille. """ -from enum import Enum, IntEnum +from enum import Enum from dataclasses import dataclass class Action(Enum): @@ -50,24 +50,6 @@ TileTypes = { 9: TileType( "[@]", [(0xF,1), (0xF,1), (0xF,1)] ), 10: TileType( "[#]", [(0x8,7), (0x8,7), (0x8,7)] ), 11: TileType( "[B]", [(0x8,7), (0x1,7), (0x8,7)] ), - 12: TileType( "[?]", [(0x8,7), (0x3,7), (0x8,7)] ) + 12: TileType( "[ ]", [(0x8,7), (0x3,7), (0x8,7)] ), + 13: TileType( "[?]", [(0x8,7), (0x0,7), (0x8,7)] ) } - -class Colors(IntEnum): - """ ANSI värejä vastaavat lukuarvot """ - BLACK = 0 - RED = 1 - GREEN = 2 - YELLOW = 3 - BLUE = 4 - MAGENTA = 5 - CYAN = 6 - WHITE = 7 - GRAY = 8 - BRIGHT_RED = 9 - BRIGHT_GREEN = 0xA - BRIGHT_YELLOW = 0xB - BRIGHT_BLUE = 0xC - BRIGHT_MAGENTA = 0xD - BRIGHT_CYAN = 0xE - BRIGHT_WHITE = 0xF @@ -2,7 +2,8 @@ # pylint: disable = multiple-imports import termios, fcntl, sys, os from time import sleep -from tui.static import Action, ActionKeys, Colors, TileTypes +from tui.static import Action, ActionKeys, TileTypes +from tui.ansi import Ansi class Tui(): @@ -30,41 +31,19 @@ class Tui(): print() - def set_color(self, color): - """ asettaa tekstin värin """ - if color in range(16): - print(end=f"\033[{'1;' if color//8 else ''}3{color%8}m") - - - def set_bg(self, color): - """ asettaa tekstin taustan värin""" - if color in range(8): - print(end=f"\033[4{color}m") - - - def cursor_up(self, lines): - """ liikuttaa kursoria ylöspäin""" - print(end=f"\033[{lines}F") - - - def reset_color(self): - """ resetoi tekstin värin ja muut attribuutit perusarvoille """ - print(end="\033[0m") - - def draw_tile(self, tile, hilighted): """ "piirtää" yhden ruudun """ for ch, colors in zip(TileTypes[tile].text, TileTypes[tile].colors): color, bg = colors - self.set_color(Colors.BLACK if hilighted else color) - self.set_bg(Colors.CYAN if hilighted else bg) + Ansi.color(Ansi.BLACK if hilighted else color) + Ansi.bg(Ansi.CYAN if hilighted else bg) print(end=ch) - self.reset_color() + Ansi.reset() def draw_matrix(self, matrix, hx, hy): """ "piirtää" ruudukon """ - self.cursor_up(len(matrix[0])) + Ansi.cup(len(matrix[0])) # pylint: disable=consider-using-enumerate for y in range(len(matrix[0])): for x in range(len(matrix)): @@ -113,15 +92,14 @@ class Tui(): """ näyttää laudan, tekstin alla ja jää odottelemaan nappia """ self.draw_matrix(matrix, x, y) print(text) - self.cursor_up(1) + Ansi.cup(1) self.read_action() def game_begin(self, size): """ ruudun alustus ja lähtökoordinaatien määritys """ - for _ in range(size+1): - print() - self.cursor_up(1) + print(end="\n"*(size+1)) + Ansi.cup(1) return size//2, size//2 |