我有几个Python函数,我用它来使Pygame的游戏开发更容易.我把它们放在我的Python路径中名为helper.py的文件中,所以我可以从我制作的任何游戏中导入它们.我想,作为一个学习Python扩展的练习,将这个模块转换为C.我的第一个问题是我需要使用Pygame中的函数,我不确定这是否可行.Pygame安装了一些头文件,但它们似乎没有C函数的Python版本.也许我错过了什么.
我怎么解决这个问题?作为一种解决方法,该函数当前接受一个函数参数并调用它,但它不是理想的解决方案.
顺便说一句,使用Windows XP,Python 2.6和Pygame 1.9.1.
/* get the sys.modules dictionary */
PyObject* sysmodules PyImport_GetModuleDict();
PyObject* pygame_module;
if(PyMapping_HasKeyString(sysmodules, "pygame")) {
pygame_module = PyMapping_GetItemString(sysmodules, "pygame");
} else {
PyObject* initresult;
pygame_module = PyImport_ImportModule("pygame");
if(!pygame_module) {
/* insert error handling here! and exit this function */
}
initresult = PyObject_CallMethod(pygame_module, "init", NULL);
if(!initresult) {
/* more error handling &c */
}
Py_DECREF(initresult);
}
/* use PyObject_CallMethod(pygame_module, ...) to your heart's contents */
/* and lastly, when done, don't forget, before you exit, to: */
Py_DECREF(pygame_module);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
427 次 |
| 最近记录: |