What is the main reason for pygame.init()

huangapple go评论55阅读模式
英文:

What is the main reason for pygame.init()

问题

我正在学习Python中的Pygame,并且在理解pygame.init()函数的目的方面遇到了困难。有人能解释它的功能以及在Pygame中为什么需要使用它吗?我观看的教程称这个函数会“初始化”Pygame,但是导入pygame不已经将所有的模块引入程序中了吗?教程还说pygame.init()将导入所有所需的Pygame模块,但是再次提问,难道import pygame不已经导入了所有的模块吗?

我阅读了Stackoverflow帖子(https://stackoverflow.com/questions/58868486/what-exactly-does-pygame-init-do#:~:text=www.pygame.org-,pygame.,individual%20modules%20manually%2C%20but%20pygame.),但我不明白“pygame.init()初始化所有已导入的pygame模块”这句话的意思。

英文:

I am learning Pygame in Python and am having trouble understanding the purpose of the pygame.init() function. Can someone explain its functionality and why it's necessary to use in Pygame? Tutorials I've watched state that this function "initializes" Pygame, but doesn't importing pygame already bring all of its modules into the program? The tutorials also say that pygame.init() will import all the required Pygame modules, but again, doesn't import pygame already import all the modules?

I've read the Stackoverflow post (https://stackoverflow.com/questions/58868486/what-exactly-does-pygame-init-do#:~:text=www.pygame.org-,pygame.,individual%20modules%20manually%2C%20but%20pygame.) but I don't understand the words "pygame.init() initialize all imported pygame modules".

答案1

得分: 1

(BTW,这与pygame无关) 通常有“加载/导入代码”和“初始化”的区别。例如:

# foo.py

class Foo:
    def __init__(self, value):
        self.value = value
import foo  # 现在加载/导入了代码

my_foo_obj = foo.Foo(123)  # 现在初始化了一个Foo

同样地,pygame.init() 表明它旨在区分加载和初始化。

你可能会问,“为什么要这样区分?”。确实,加载和初始化可以耦合在一起:

# bar.py

class Bar:
    def __init__(self, value):
        # 也许还要进行昂贵的计算
        self.value = value

default_bar = Bar(123)
import bar  # 先加载代码,然后初始化了一个default_bar

print(bar.default_bar.value)

但这样,你只能有一个default_bar,并且失去了控制“昂贵事物”的初始化成本何时发生的能力。例如,也许你想在计算机占用自身初始化你的游戏并似乎“冻结”之前向用户显示加载屏幕/加载进度条。

英文:

(BTW, this is not specific to pygame) Generally there is a difference between "loading/importing code" and "initialization". For example:

# foo.py

class Foo:
    def __init__(self, value):
        self.value = value
import foo  # code is now loaded/imported

my_foo_obj = foo.Foo(123)  # a Foo is now initialized

In the same manner, pygame.init() suggests it's designed to have a distinction between loading & initialization.

You might ask, "why make this distinction?". Indeed, loading and initialization could be coupled together:

# bar.py

class Bar:
    def __init__(self, value):
        # maybe also do expensive computation
        self.value = value

default_bar = Bar(123)
import bar  # code is loaded and then a default_bar is also initailized

print(bar.default_bar.value)

But this way, you can only ever have one default_bar, and you lose control of when the initialization cost of "the expensive thing" is taken. For example, perhaps you want to show a loading screen/loading bar to the user before the computer occupies itself initializing your game and appears to "freeze".

huangapple
  • 本文由 发表于 2023年6月16日 07:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定