[英]Can I use variables from main.py in other files?
I'm making a text-based adventure game which I plan on being quite long.我正在制作一个基于文本的冒险游戏,我计划很长一段时间。 Naturally, instead of having all the code in
main.py
I decided to split the chapters into different files to be run in if
blocks according to the choices the player makes.自然地,我决定将章节拆分为不同的文件,以便根据玩家所做的选择在
if
块中运行,而不是将所有代码都放在main.py
。 Slight problem, however: all of the user information I'm using to customize the story (name, gender, clothing, etc.) is stored in main.py
.然而,有一个小问题:我用来自定义故事的所有用户信息(姓名、性别、服装等)都存储在
main.py
。 So, if I have another file (let's say option3.py
for example) and I'm writing something like所以,如果我有另一个文件(例如
option3.py
)并且我正在写类似的东西
print(f"{user_name} walked down the street.")
I don't have user_name
stored in option3.py
.我没有将
user_name
存储在option3.py
。 I'd rather not import specific variables into each of the 20+ files every time but I hear that sharing namespace is a bad idea, so I'm wondering if there's a better way to work around this.我不想每次都将特定变量导入 20 多个文件中的每一个,但我听说共享命名空间是一个坏主意,所以我想知道是否有更好的方法来解决这个问题。
Extra: I'd also like to include some sort of item inventory feature but I'm not sure how to go about that, what with this multiple file issue and the fact that the inventory would often change额外:我还想包括某种物品清单功能,但我不知道如何去做,这个多文件问题以及清单经常变化的事实
The best thing is to use classes and import them.最好的办法是使用类并导入它们。 If you created a class named User, and put all the necessary attributes in the class, you could import it.
如果您创建了一个名为 User 的类,并将所有必需的属性放入该类中,您就可以导入它。
For example, create a user.py file:例如,创建一个 user.py 文件:
class User():
def __init__(self, name, gender, clothing):
self.name = name
self.gender = gender
self.clothing = clothing
Then, wherever your main game loop is, you can do from user import User
.然后,无论您的主要游戏循环在哪里,您都可以
from user import User
。
That way, you can create a new user object with user1 = User('SomeName', 'F', 'Skirt')
and access is attributes like this: print(user1.name)
这样,您可以使用
user1 = User('SomeName', 'F', 'Skirt')
创建一个新的用户对象,并且访问是这样的属性: print(user1.name)
You can do something like this if you don't want to pollute the namespace.如果你不想污染命名空间,你可以做这样的事情。
game_variables.py
cat = 12
dog = 24
main.py
import game_variables as gv
print(gv.cat)
print(gv.dog)
But, I think you're missing the point that game states must be shared across all the modules, in that case you need to create global variables.但是,我认为您忽略了游戏状态必须在所有模块之间共享这一点,在这种情况下您需要创建全局变量。 But, this could lead to all sort of inconsistency.
但是,这可能会导致各种不一致。
Instead, it's better to create a class for each module in your design, and passing the object states with getter and setter between multiple files.相反,最好为设计中的每个模块创建一个类,并在多个文件之间使用 getter 和 setter 传递对象状态。
cat.py
class cat:
def __init__(self):
self.cuteness = 100
pass
def get_state(self):
return self.cuteness
def set_state(self, val):
self.cuteness = val
update.py
def update(state):
# some calculation
return state + 1
main.py
import update
import cat
my_cat = cat.cat()
updated_cat_cuteness = update.update(my_cat.get_state())
my_cat.set_state(updated_cat_cuteness)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.