#python #curses
Вопрос:
Я начал изучать библиотеку проклятий для python. В качестве одного из моих игрушечных проектов я сделал игру об охоте за сокровищами.
В какой-то момент мне нужно было прочитать символ под курсором, чтобы понять, что игрок указал на сокровище под курсором. Я действительно заинтригован, почему у окна.дюйм не возвращает символ, который я только что напечатал.
Вот небольшой пример:
import curses
from curses import wrapper
import numpy as np
def Point(y,x):
return np.array([y,x], dtype=int)
def inch_window(window, position):
ch = window.inch(position[0], position[1]) amp; 0xff
# ch = bytes([ch])
return chr(ch)
def main(stdscr):
curses.initscr()
curses.cbreak()
curses.noecho()
curses.curs_set(0)
# Create a pad containing some string
pad_len = Point(1, 20)
pad = curses.newpad(pad_len[0], pad_len[1])
# initialize pointer to a part of the pad
focus = Point(0,0)
# Define field of the screen where the pad will be drawn
field_top = Point(10,10)
field_size = pad_len.copy() - Point(0,1)
field_bot = field_top field_size - 1
# draw the pad
# None of two options works
# 1) draw a string through the pad
# pad.addstr(0, 0, "ASgfsdfj3ivnDF")
# pd.refresh(*focus,*field_top,*field_bot)
# 2) draw a string directly
stdscr.addstr(*field_top, "ASgfsdfj3ivnDF")
stdscr.refresh()
# read a character from the first cell of the pad
ch = inch_window(stdscr, field_top)
# It should be a letter 'A', but its not. Its always ' ' (space)
stdscr.addstr(50,50, ''{:s}''.format(ch))
stdscr.refresh()
stdscr.getch()
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.curs_set(1)
curses.endwin()
wrapper(main)
Спасибо