cursor movement within the matrix added.
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# created by virtualenv automatically
|
||||||
|
__pycache__
|
||||||
|
lib
|
||||||
|
bin
|
||||||
|
|
39
field.py
Normal file
39
field.py
Normal file
@@ -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()
|
||||||
|
|
8
pyvenv.cfg
Normal file
8
pyvenv.cfg
Normal file
@@ -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
|
@@ -1,31 +1,65 @@
|
|||||||
#! /usr/bin/python
|
#! /usr/bin/python
|
||||||
|
|
||||||
field =[
|
from field import Field
|
||||||
[1,0,0,0,0,0,0,0,0,0,0,0],
|
import curses
|
||||||
[0,1,0,0,0,0,0,0,0,0,0,0],
|
from curses import wrapper
|
||||||
[0,0,1,0,0,0,0,0,0,0,0,0],
|
field = Field()
|
||||||
[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 print_field():
|
def print_field():
|
||||||
for line in range(0,len(field)):
|
for line in range(0,len(field)):
|
||||||
curline = field[line]
|
curline = field[line]
|
||||||
|
|
||||||
# print(f'line={line}')
|
|
||||||
for col in range(0,len(curline)):
|
for col in range(0,len(curline)):
|
||||||
# print(f'col={col}')
|
|
||||||
if curline[col] == 0:
|
if curline[col] == 0:
|
||||||
print('o ', end='')
|
print('o ', end='')
|
||||||
else:
|
else:
|
||||||
print('x ', end='')
|
print('x ', end='')
|
||||||
print('\n',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():
|
def main():
|
||||||
print_field()
|
wrapper(inputLoop)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user