Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
KikooDX | 5e5f1bc9ef | |
KikooDX | 80cf133bbf | |
KikooDX | d35e62b1d7 | |
KikooDX | b4dc69ced1 | |
KikooDX | 2ab53c8116 | |
KikooDX | 49fc0f6a4f | |
KikooDX | febd7dd1ec | |
KikooDX | f1f664f716 | |
KikooDX | 8ac28a47f4 | |
KikooDX | c6299aaaa0 |
53
locate2.py
53
locate2.py
|
@ -1,34 +1,15 @@
|
|||
class Screen:
|
||||
def _check(self, x, o, name):
|
||||
if not isinstance(x, int):
|
||||
raise TypeError("{} is not an int object".format(name))
|
||||
elif x < 1:
|
||||
raise ValueError("{} is lower than 1".format(name))
|
||||
if o is None:
|
||||
o = 0
|
||||
elif x > o and o:
|
||||
raise ValueError("{} is greater than the height of this object: {} > {}".
|
||||
format(name, x, o))
|
||||
|
||||
def __init__(self, width=21, height=6, patern=" ", copy=None):
|
||||
if isinstance(copy, Screen):
|
||||
self._width = copy._width
|
||||
self._height = copy._height
|
||||
self._mat = copy._mat
|
||||
else:
|
||||
self._check(width, None, "width")
|
||||
self._check(height, None, "height")
|
||||
if not isinstance(patern, str):
|
||||
raise TypeError("patern is not a string")
|
||||
elif len(patern) > 1:
|
||||
raise ValueError("patern is too long (length = {})".format(len(patern)))
|
||||
self._width = width
|
||||
self._height = height
|
||||
self.fill(patern)
|
||||
|
||||
def locate(self, x, y, string):
|
||||
self._check(x, self._width, "x")
|
||||
self._check(y, self._height, "y")
|
||||
string = str(string)
|
||||
i = -1
|
||||
for char in string:
|
||||
|
@ -37,8 +18,6 @@ class Screen:
|
|||
i += 1
|
||||
|
||||
def locate_v(self, x, y, string):
|
||||
self._check(x, self._width, "x")
|
||||
self._check(y, self._height, "y")
|
||||
string = str(string)
|
||||
i = -1
|
||||
for char in string:
|
||||
|
@ -49,23 +28,26 @@ class Screen:
|
|||
def fill(self, patern=" "):
|
||||
self._mat = [[patern[0] for i in range(self._width)] for i in range(self._height)]
|
||||
|
||||
def refresh(self, ask_for_input=True, endl="\n"):
|
||||
def refresh(self, ask_for_input=True, endl="\n", fancy=False, fmargin=4):
|
||||
to_print = str()
|
||||
for line in self._mat:
|
||||
for line in self._mat[:-1] if fancy else self._mat:
|
||||
for cell in line:
|
||||
to_print += cell
|
||||
to_print += "\n"
|
||||
to_print = to_print[:-1]
|
||||
print(to_print)
|
||||
if ask_for_input:
|
||||
if ask_for_input or fancy:
|
||||
line = ""
|
||||
if fancy:
|
||||
for cell in self._mat[-1][:-fmargin]:
|
||||
line += cell
|
||||
return input(line)
|
||||
return input("> ")
|
||||
else:
|
||||
print("", end=endl)
|
||||
return None
|
||||
|
||||
def get_cell(self, x, y):
|
||||
self._check(x, self._width, "x")
|
||||
self._check(y, self._width, "y")
|
||||
return self._mat[y - 1][x - 1]
|
||||
|
||||
def get_dim(self):
|
||||
|
@ -79,21 +61,19 @@ class Screen:
|
|||
return result
|
||||
|
||||
def load(self, string):
|
||||
if type(string) is not str:
|
||||
raise TypeError("string is not a string type")
|
||||
if len(string) != self._width * self._height:
|
||||
raise ValueError("string lenght isn't equal to {}".format(self._width * self._height))
|
||||
i = 0
|
||||
s = 0
|
||||
while i != self._height:
|
||||
self._mat[i] = list(string[s:s + self._width])
|
||||
i += 1
|
||||
s += self._width
|
||||
|
||||
|
||||
get_cell_content = get_cell # For retro-compability
|
||||
|
||||
class Pad(Screen):
|
||||
def refresh(self, x=1, y=1, width=21, height=6, ask_for_input=True, endl="\n"):
|
||||
def refresh(self, x=1, y=1, width=21, height=7, ask_for_input=True,\
|
||||
endl="\n", fancy=True, fmargin=4):
|
||||
ogwidth = width
|
||||
if width > self._width:
|
||||
width = self._width
|
||||
if height > self._height:
|
||||
|
@ -112,13 +92,12 @@ class Pad(Screen):
|
|||
for dy in range(y, height+y):
|
||||
dy = dy
|
||||
for dx in range(x, width+x):
|
||||
print(dx, dy)
|
||||
to_print += self._mat[dy][dx]
|
||||
to_print += "\n"
|
||||
to_print = to_print[:-1]
|
||||
print(to_print)
|
||||
if ask_for_input:
|
||||
return input("> ")
|
||||
print(to_print[:-width], end="")
|
||||
if ask_for_input or fancy:
|
||||
return input(to_print[-width:-1]+ " " * (ogwidth - width - fmargin) if fancy else "> ")
|
||||
else:
|
||||
print("", end=endl)
|
||||
print(to_print[-width:], end=endl)
|
||||
return None
|
||||
|
|
Loading…
Reference in New Issue