1
\$\begingroup\$

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Mast
    Mar 12, 2016 at 22:41
  • \$\begingroup\$ @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. \$\endgroup\$
    – Kactung
    Mar 12, 2016 at 22:53

1 Answer 1

2
\$\begingroup\$

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)))
\$\endgroup\$
4
  • \$\begingroup\$ 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 \$\endgroup\$
    – Kactung
    Mar 13, 2016 at 16:46
  • \$\begingroup\$ @Puyover: I would agree that your way isn't bad, but what doesn't work for mine? \$\endgroup\$
    – zondo
    Mar 13, 2016 at 17:02
  • \$\begingroup\$ 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... \$\endgroup\$
    – Kactung
    Mar 13, 2016 at 17:35
  • \$\begingroup\$ 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. \$\endgroup\$
    – zondo
    Mar 13, 2016 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.