python - pygame.init() 到底做了什么?

标签 python pygame

我读过/看过的每个 pygame 教程都说过,在考虑做其他事情之前,请将“pygame.init()”挤入代码中。显然,它初始化了 pygame 模块或其他东西,这似乎非常重要。

直到我想从代码中删除“pygame.init()”行,看看会发生什么。你瞧,我的游戏运行得一模一样。

我上网后,无论我走到哪里,我都被告知“pygame.init()”是必要的。但不,显然不是,因为我只是从我的代码中取出它,它运行得很好。

所以,不用说,我很困惑。如果有人解释的话,我将非常感激:

a) pygame.init()函数

b) pygame 程序中是否需要它。 如果是,那为什么我的游戏可以运行 如果不需要,那么什么时候需要?

c)您认为我应该知道的任何其他事情

最佳答案

来自www.pygame.org

pygame.init() initialize all imported pygame modules. No exceptions will be raised if a module fails, but the total number if successful and failed inits will be returned as a tuple. You can always initialize individual modules manually, but pygame.init()initialize all imported pygame modules is a convenient way to get everything started. The init() functions for individual modules will raise exceptions when they fail.

此外,在这里我们可以看到一开始就这样做是安全的:

It is safe to call this init() more than once as repeated calls will have no effect. This is true even if you have pygame.quit() all the modules.

最后,我认为为什么总是在一开始就使用它的主要原因:

This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do.

所以主要结论是,pygame.init() 安全地初始化所有导入的 pygame 模块,无论这些模块是否确实需要初始化;但由于它适用于那些这样做的模块,因此省去了单独手动初始化每个模块的麻烦。

关于python - pygame.init() 到底做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58868486/

相关文章:

python - 使用 Sidekit 进行 UBM 扬声器适配时出现错误

python - pygame.camera "livefeed"在 tkinter 窗口内(在 raspbian 上)

android - 你如何在 Android 的 PyGame 子集中循环播放音乐?

python - Pygame spritecollide 给我可迭代的错误

pygame - 为什么 pyglet 比 pygame 慢?

python - GeoDjango--Python :Can't Display Map

python - 动态 pandas 数据帧生成

python - python 3 的 matplotlib 中的曲线不连续

python - 自动前后移动矩形

python - 如何在 Ubuntu 下捕获原始 IP 数据包?