pygame.init() 到底做了什么? [英] What exactly does pygame.init() do?

查看:32
本文介绍了pygame.init() 到底做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过/看过的每个 pygame 教程都说在你考虑做其他事情之前将pygame.init()"压缩到你的代码中.显然,它初始化了 pygame 模块或其他东西,这似乎很重要.

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.

直到我想从我的代码中删除 'pygame.init()' 行,只是为了看看会发生什么.瞧,我的游戏完全一样.

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.

我上网,再一次,无论我走到哪里,我都被告知 'pygame.init()' 是必要的.但不,显然不是,因为我只是从我的代码中取出它,它工作得很好.

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) pygame.init() 的函数

a) the function of pygame.init()

b) 在 pygame 程序中是否需要它.如果是,那为什么我的游戏能正常工作如果不需要,那么什么时候需要?

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) 你认为我应该知道的任何其他事情

c) any other things you think I should know

推荐答案

来自 www.pygame.org

pygame.init()初始化所有导入的 pygame 模块.如果模块失败,则不会引发任何异常,但如果成功和失败,inits 的总数将作为元组返回.您始终可以手动初始化单个模块,但 pygame.init() 初始化所有导入的 pygame 模块是一种开始一切的便捷方式.各个模块的 init() 函数在失败时会引发异常.

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:

多次调用此 init() 是安全的,因为重复调用将不起作用.即使您拥有 pygame.quit() 所有模块也是如此.

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:

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

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 模块,而不管这些模块是否确实需要初始化;但由于它对那些这样做的人有效,因此省去了单独手动初始化每个模块的麻烦.

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.

这篇关于pygame.init() 到底做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆