[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Scripting language
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)
Not tested, but some form of this method should work.