# Charging battery animation over time

I have the following method that is working perfectly. Main problem I see is that I think it can be done just in one line, but I don't know how:

def _animate_display(self, display):
self._timer += 1 / bge.logic.getAverageFrameRate()

symbolic_level = self.battery.get_symbolic_level()

if self._timer >= 5.0:
self._timer = symbolic_level / 25
elif (self._timer >= 4.0) and (symbolic_level <= 100):
utils.change_texture(display, 'DisplayMat', 'icons/battery_100.png')
elif (self._timer >= 3.0) and (symbolic_level <= 75):
utils.change_texture(display, 'DisplayMat', 'icons/battery_75.png')
elif (self._timer >= 2.0) and (symbolic_level <= 50):
utils.change_texture(display, 'DisplayMat', 'icons/battery_50.png')
elif (self._timer >= 1.0) and (symbolic_level <= 25):
utils.change_texture(display, 'DisplayMat', 'icons/battery_25.png')
elif (self._timer >= 0.0) and (symbolic_level <= 0):
utils.change_texture(display, 'DisplayMat', 'icons/battery_0.png')


It models a simple animation of a charging battery in steps of 1 second. Something like: [....] --1s--> [=...] --1s--> [==..] --1s--> [===.] --1s--> [====] --1s--> [....] ...

Symbolic level is a variable that keep a value of [0, 25, 50, 75, 100], one of these, so animation loops from that symbolic value so it doesn't start from 0.

• It's Python, so the chance it can be done on one line is quite high. However, is your focus on short or on readable? In both cases the repetition can be lessened, but it's all about intentions. – Mast Mar 12 '16 at 22:41
• @Mast I think the shorter version would be still readable. I just don't find the relationship between the timer condition and the symbolic battery level. – Puyover Mar 12 '16 at 22:53

## 1 Answer

I took it to 6 to make it a little more readable:

def _animate_display(self, display):
self._timer += 1 / bge.logic.getAverageFrameRate()
symbolic_level = self.battery.get_symbolic_level()
if self._timer >= 5.0:
self._timer = symbolic_level / 25
else:
utils.change_texture(display, 'DisplayMat', 'icons/battery_{}.png'.format(next((x,y) for x,y in zip(xrange(100, -1, -25), xrange(4, -1, -1)) if symbolic_level <= x and self._timer >= y)))

• I'm afraid it's not working even after changing the level and timer variables by symbolic_level and self._timer :/ Moreover it seems like black magic for me; I guess my way isn't so bad at all :P – Puyover Mar 13 '16 at 16:46
• @Puyover: I would agree that your way isn't bad, but what doesn't work for mine? – zondo Mar 13 '16 at 17:02
• no errors are displayed but animation is not working. Since it's Python 3 I have replaced xrange with range. Maybe there is the problem... – Puyover Mar 13 '16 at 17:35
• Using range() instead of xrange() shouldn't make a difference. I don't know why it doesn't work. I tested it on my end. – zondo Mar 13 '16 at 17:37