[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Pygame-Fenix - Using generators as game objects





Dan Krol wrote:
The way I would do it is this:

 def begin(self):
    while True:
        for x in range(0,5):
           #state 0 stuff
           yield
        for x in range(0,3):
           #state 1 stuff
           yield
        for x in range(0,7):
           #state 2 stuff
           yield


You could also have conditionals that take input that direct it in
different control paths. The point here being, instead of worrying
about using state variables, you could use your location in the loop
as your state. It can make you think of it a lot more as one script,
instead of having to think about it in terms of frames and states.

Just thought I'd share that; it doesn't look like it would require any
changes to Pygame-Fenix, just a new way of using it.

Dan

Indeed! This is something that people using DIV/Fenix used to do a lot, and one of the reasons why I went with generators rather than just a function of a class that keeps getting called. It can be extremely useful, imagine having an object that you want to have an "intro" of sorts. It can be yielding till it finishes it's intro and then the rest of the code is the main logic.

It's a personal preference that I don't do this, but many have done previously and my library certainly allows for it by design.

Sorry for not really explaining properly why using the frame; method is cool, you've put another notch in for me. Thanks!