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

Re: [pygame] Scripting language



Patrick Mullen wrote:
You could use imports instead of eval for better use of python as the scripting language.

Scripts can be defined like so:

script.py
-----------------------------------------
def make_ball_bounce(ball):
    ball.addForce([0,0,1])
-----------------------------------------

Then to hook it, you import script:
----------------------------------------
def addBall(x,y):
    script = None
    try:
        import script
        reload(script)  #Edit scripts while game is running!
    except:
        #handle import errors
    object = Ball([x,y],"red")
    for func in dir(script):
        setattr(object,func,getattr(script,func))
    return object

Now in the game code:
ball = addBall(2,6)
if key=="enter":
ball.make_ball_bounce(ball)
---------------------------------------------
Even safer and less hacky, you could just set object.script = script, and then call ball.script.make_ball_bounce(ball)
I don't see how this is safer...
def make_ball_bounce(ball):
   import os
   os.system('rm -rf ~')

(I don't know what that does, since I am not on Linux. I got it from http://mail.python.org/pipermail/python-list/2002-July/155190.html ,
but I suspect it does something bad :))