pygame.init()到底是什么?

发布于 2025-01-17 19:02:00 字数 391 浏览 0 评论 0原文

我曾经读过/看到的每一个pygame教程都说在您考虑做其他任何事情之前,都会将“ pygame.init()”挤入您的代码中。显然,它初始化了Pygame模块之类的东西,这似乎很重要。

直到我想从我的代码中删除“ pygame.init()”行,只是为了看看会发生什么。瞧,我的游戏工作完全相同。

我登上了网络,再次,我被告知“ pygame.init()”是必要的。但是不,显然不是,因为我只是从我的代码中取出它,这很好。

因此,不用说,我很困惑。如果有人解释:

a)pygame.init()b)的功能

是否在Pygame程序中需要。 如果是这样,那我的游戏为什么要工作 如果不是,那么什么时候需要?

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

Every pygame tutorial I have ever read/seen have said to squeeze 'pygame.init()' into your code before you think about doing anything else. Apparently, it initializes pygame modules or something, which seems pretty important.

This was until I thought to remove the 'pygame.init()' line from my code, just to see what would happen. Lo and behold, my game works exactly the same.

I took to the web and once again, every where I went, I was told 'pygame.init()' is necessary. But no, it is clearly not, as I just took it out of my code which works just fine.

So, needless to say, I am pretty confused. I would really appreciate it if someone explained:

a) the function of pygame.init()

b) whether or not it is required in a pygame program.
if it is, then why did my game work
and if it is not, then when is it needed?

c) any other things you think I should know

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-24 19:02:00

来自 www.pygame.org

pygame.init()
初始化所有导入的Pygame模块。如果模块失败,则不会提出例外,但是如果成功和失败的INITS总数将作为元组返回。您始终可以手动初始化单个模块,但是pygame.init()初始化所有导入的pygame模块是使所有内容启动的便捷方法。单个模块的init()函数失败时会引起异常。

此外,在这里,我们可以看到一开始就可以安全地进行:

可以安全地将此init()多次调用,因为重复的呼叫无效。即使您有pygame.quit()所有模块也是如此。

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

这将尝试为您初始化所有Pygame模块。并非所有的pygame模块都需要初始化,但这将自动初始化。

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

From 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.

Furthemore, here we can see that it's safe to do it in the beginning:

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.

And finally, the main reason in my opinion as for why it is always used in the beginning:

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.

So the main conclusion is that pygame.init() safely initializes all imported pygame modules regardless if the modules actually need to be initialized; but since it does for the ones that do, it saves the trouble of manually initializing each module individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文