From 21591732774526ef53707c35fd2d20e9e7d129b3 Mon Sep 17 00:00:00 2001 From: Olli Graf Date: Sun, 5 May 2024 16:20:48 +0200 Subject: [PATCH] cursor movement within the matrix added. --- .gitignore | 5 +++++ field.py | 39 +++++++++++++++++++++++++++++++++ pyvenv.cfg | 8 +++++++ r4matrixed.py | 60 ++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100644 field.py create mode 100644 pyvenv.cfg diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fcee39 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# created by virtualenv automatically +__pycache__ +lib +bin + diff --git a/field.py b/field.py new file mode 100644 index 0000000..c74a171 --- /dev/null +++ b/field.py @@ -0,0 +1,39 @@ +import curses + +class Field: + + cursor = (0,0) + + field =[ + [1,0,0,0,0,0,0,0,0,0,0,0], + [0,1,0,0,0,0,0,0,0,0,0,0], + [0,0,1,0,0,0,0,0,0,0,0,0], + [0,0,0,1,0,0,0,0,0,0,0,0], + [0,0,0,0,1,0,0,0,0,0,0,0], + [0,0,0,0,0,1,0,0,0,0,0,0], + [0,0,0,0,0,0,1,0,0,0,0,0], + [0,0,0,0,0,0,0,1,0,0,0,0] + ] + + def __init__(self): + self.message = '' + + + def print_field(self,stdscr): + stdscr.clear() + for line in range(0,len(self.field)): + curline = self.field[line] + + for col in range(0,len(curline)): + colour = curses.color_pair(1) + if (line,col) == self.cursor: + colour = curses.color_pair(2) + if curline[col] == 0: + stdscr.addstr(line,col,'o ',colour) + else: + stdscr.addstr(line,col,'x ',colour) + stdscr.addstr(line+1,0,'Press Space to toggle, q to quit') + if len(self.message) > 0: + stdscr.addstr(line+2,0,f'{self.message}',curses.color_pair(3)) + stdscr.refresh() + diff --git a/pyvenv.cfg b/pyvenv.cfg new file mode 100644 index 0000000..b121c6f --- /dev/null +++ b/pyvenv.cfg @@ -0,0 +1,8 @@ +home = /usr/bin +implementation = CPython +version_info = 3.11.2.final.0 +virtualenv = 20.17.1+ds +include-system-site-packages = false +base-prefix = /usr +base-exec-prefix = /usr +base-executable = /usr/bin/python3 diff --git a/r4matrixed.py b/r4matrixed.py index ad1ab0d..fff0870 100755 --- a/r4matrixed.py +++ b/r4matrixed.py @@ -1,31 +1,65 @@ #! /usr/bin/python -field =[ - [1,0,0,0,0,0,0,0,0,0,0,0], - [0,1,0,0,0,0,0,0,0,0,0,0], - [0,0,1,0,0,0,0,0,0,0,0,0], - [0,0,0,1,0,0,0,0,0,0,0,0], - [0,0,0,0,1,0,0,0,0,0,0,0], - [0,0,0,0,0,1,0,0,0,0,0,0], - [0,0,0,0,0,0,1,0,0,0,0,0], - [0,0,0,0,0,0,0,1,0,0,0,0] -] +from field import Field +import curses +from curses import wrapper +field = Field() def print_field(): for line in range(0,len(field)): curline = field[line] -# print(f'line={line}') for col in range(0,len(curline)): -# print(f'col={col}') if curline[col] == 0: print('o ', end='') else: print('x ', end='') print('\n',end='') +def inputLoop(stdscr): + exitPrg = False + curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) + curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE) + curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) + curses.noecho() + curses.cbreak() + + + while not exitPrg: + field.print_field(stdscr) + key = stdscr.getkey() + + match key: + case 'q': + exitPrg = True + case 'KEY_DOWN': + if field.cursor[0] < len(field.field)-1: + newcursor = (field.cursor[0] +1,field.cursor[1]) + field.cursor = newcursor + case 'KEY_RIGHT': + curline = field.field[field.cursor[0]] + if field.cursor[1] < len(curline)-1: + newcursor = (field.cursor[0],field.cursor[1]+1) + field.cursor = newcursor + case 'KEY_LEFT': + curline = field.field[field.cursor[0]] + if field.cursor[1] > 0: + newcursor = (field.cursor[0],field.cursor[1]-1) + field.cursor = newcursor + case 'KEY_UP': + curline = field.field[field.cursor[0]] + if field.cursor[0] > 0: + newcursor = (field.cursor[0] -1,field.cursor[1]) + field.cursor = newcursor + + + + + + def main(): - print_field() + wrapper(inputLoop) + if __name__ == '__main__':