|
|
|
@ -1,35 +1,40 @@
|
|
|
|
|
# std imports
|
|
|
|
|
from math import floor
|
|
|
|
|
|
|
|
|
|
# local
|
|
|
|
|
from blessed import Terminal
|
|
|
|
|
|
|
|
|
|
class Graphics:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.bottom_left_corner = u"└"
|
|
|
|
|
self.bottom_right_corner = u"┘"
|
|
|
|
|
self.top_left_corner = u"┌"
|
|
|
|
|
self.top_right_corner = u"┐"
|
|
|
|
|
self.border_horizontal = u"─"
|
|
|
|
|
self.border_vertical = u"│"
|
|
|
|
|
|
|
|
|
|
def roundxy(x, y):
|
|
|
|
|
return int(floor(x)), int(floor(y))
|
|
|
|
|
class Window:
|
|
|
|
|
def __init__(self, term, height, width, xpos, ypos, title=None):
|
|
|
|
|
self.term = term
|
|
|
|
|
|
|
|
|
|
self.title = title
|
|
|
|
|
|
|
|
|
|
term = Terminal()
|
|
|
|
|
self.height = height
|
|
|
|
|
self.width = width
|
|
|
|
|
self.xpos = xpos
|
|
|
|
|
self.ypos = ypos
|
|
|
|
|
|
|
|
|
|
x, y, xs, ys = 2, 2, 0.4, 0.3
|
|
|
|
|
with term.cbreak(), term.hidden_cursor():
|
|
|
|
|
# clear the screen
|
|
|
|
|
print(term.home + term.black_on_olivedrab4 + term.clear)
|
|
|
|
|
def draw_borders(self):
|
|
|
|
|
# Print the top
|
|
|
|
|
print(
|
|
|
|
|
self.term.move_xy(self.xpos, self.ypos) +
|
|
|
|
|
u"┌" +
|
|
|
|
|
u"─" * (self.width - 2) +
|
|
|
|
|
u"┐"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# loop every 20ms
|
|
|
|
|
while term.inkey(timeout=0.02) != 'q':
|
|
|
|
|
# erase,
|
|
|
|
|
txt_erase = term.move_xy(*roundxy(x, y)) + ' '
|
|
|
|
|
# Print the middle
|
|
|
|
|
# We exclude the top and bottom rows since we'll draw them with other chars
|
|
|
|
|
for dx in range (1, self.height - 1):
|
|
|
|
|
print(self.term.move_xy(self.xpos + dx, self.ypos) + u'│')
|
|
|
|
|
print(self.term.move_xy(self.xpos + dx, self.ypos + self.width - 1) + u'│')
|
|
|
|
|
|
|
|
|
|
# bounce,
|
|
|
|
|
if x >= (term.width - 1) or x <= 0:
|
|
|
|
|
xs *= -1
|
|
|
|
|
if y >= term.height or y <= 0:
|
|
|
|
|
ys *= -1
|
|
|
|
|
|
|
|
|
|
# move,
|
|
|
|
|
x, y = x + xs, y + ys
|
|
|
|
|
|
|
|
|
|
# draw !
|
|
|
|
|
txt_ball = term.move_xy(*roundxy(x, y)) + '█'
|
|
|
|
|
print(txt_erase + txt_ball, end='', flush=True)
|
|
|
|
|
terminal = Terminal()
|
|
|
|
|
my_window = Window(terminal, 4, 20, 10, 10)
|
|
|
|
|
my_window.draw_borders()
|