From 092d91ca44bd8b571e3ae3e00d01a90f723eb8a4 Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 26 Oct 2017 20:11:08 +0200 Subject: Merge branch 'package' of tilde.town:/home/wangofett/programming/asciifarm --- asciifarm/client/display/healthpad.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'asciifarm/client/display/healthpad.py') diff --git a/asciifarm/client/display/healthpad.py b/asciifarm/client/display/healthpad.py index 17baf35..64838d3 100644 --- a/asciifarm/client/display/healthpad.py +++ b/asciifarm/client/display/healthpad.py @@ -1,21 +1,38 @@ import curses - - class HealthPad: - - - def __init__(self, size=(1,1), *args): - self.pad = curses.newpad(size[1], size[0]) - self.size = size + def __init__(self, width=1, char=('@',0), emptyChar=('-',0), colours=False): + self.char = char + self.emptyChar = emptyChar + self.pad = curses.newpad(2, width+1) + self.width = width + self.changed = False + self.lastView = None + self.colours = colours def setHealth(self, health, maxHealth): self.pad.erase() - self.pad.addstr(0,0,"Health: {}/{}".format(health, maxHealth)) + barEnd = round(health/maxHealth * self.width) + self.pad.addstr(0,0,"Health: {}/{}".format(health, maxHealth)[:self.width]) + if self.colours: + self.pad.addstr(1,0, self.char[0]*barEnd, curses.color_pair(self.char[1])) + self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd), curses.color_pair(self.emptyChar[1])) + else: + self.pad.addstr(1,0, self.char[0]*barEnd) + self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd)) + + self.changed = True + + def getHeight(self): + return 2 def update(self, screen, x, y, xmax, ymax): + if not self.changed and (x, y, xmax, ymax) == self.lastView: + return + self.lastView = (x, y, xmax, ymax) + self.changed = False self.pad.noutrefresh( 0, 0, -- cgit