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

Re: [pygame] Ridiculously simple way to seperate game-object code



--- Sami Hangaslammi <sami.hangaslammi@xxxxxxxxx>
wrote:

> 
> If there really is need for this sort of thing, let
> me know and I can
> develop it further. :)
> 
> Current version at
> http://codereactor.net/~shang/interpret/
> 

That is very cool. Somehow I assumed that making this
experiment work like normal Python code would be too
difficult a goal, but I guess I'm wrong :)

I'm not ready to do so yet, but I expect to use your
work sometime in the future. It's up to you whether or
not you want to continue developing it; if you don't,
I will try to add any needed parts myself. 

The main feature I'm looking for now is actually not a
built-in Python feature. I want a way for objects to
interrupt each other during execution. In
Megazeux(going back to the source of all my recent
ideas) the scripting language makes this easy by
having named goto commands("labels"). When an object
gets hit with a bullet(for example) its program jumps
to the label ":shot" and resumes execution there.  To
make this feature more powerful, programs can "zap"
and "unzap" labels(comment them out):

:shot
:shot
:shot
* "I'm hurt!"
zap "shot" 1
goto "myloop"
:shot
* "I'm dead!"
die

In this example the object dies after the fourth time
"shot" is called. Otherwise it acknowledges the hit,
comments out the first :shot label it sees and goes
back to what it's doing. This lets you add a lot of
interactivity without the developer overhead of a
continuous loop to monitor variables and events.

Python doesn't have a goto command to abuse, and a
built-in one wouldn't be appropriate for our
interpreter anyway. But I think we don't need to add
goto. Instead, my idea would be to have functions
called by interrupts:

def shot(self):
    self.health-=1
    if self.health>0:
        say("I'm hurt!")
        return
    else:
        say("I'm dead!")
        self.die()

And the bullet would say something like:

if collision:
    interrupt(collision_object, "shot")

This could be done by extending the line-counting
mechanism to be a stack, I think. interrupt() would
translate the function name to a line number, push the
number(or a corresponding iterator) onto the stack,
iterate the top every cycle, and then at the
termination of that code, pop it and resume from the
next block down.

I'm hesitant to try it since I think I'll get
something wrong and spend hours on it and I wanted to
do more graphics stuff tonight. But interrupts are
what would make the interpreter really, really useful.
So if nobody else gets around to it, I will eventually :)

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com