lundi 7 mai 2018

How to break infinite recursion correctly

I wrote a simple turn-based game.

I have callback which are called every time when game waiting for players actions.

Game also can be animated or be invisible. Animation based on QTimer.

Example in pseudo code:

class Game(QWidget):
    def __init__(self, callback, visualize = True):
        ....
        self.callback = callback
        self.visualize = visualize
        if visualize:
            self.show()

    def PerformTurn(...):
        ....
        if self.visualize:
             self.animationTimer = QTimer()
             self.animationTimer.timeout.connect(animate)
             self.drawingTimer.start(interval)
        else:
           self.callback(...)

    def animate():
        ....
        if animationFinished:
            self.animationTimer.stop()
            self.callback(...)


def myStrategy(...):
    ....
    game.PerformTurn(...)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    game = Game(myStrategy, visualize = True)
    app.exec()

When visualize = True everything works fine. As far as I understand it is because of timer.

When visualize = False I have endless recursion. (myStrategy -> PerformTurn -> myStrategy -> ...)

I have a few ideas how to solve this in dirty way. I am new in python+qt, so my question is how to solve this in good way?

Aucun commentaire:

Enregistrer un commentaire