简体   繁体   English

我可以在其他文件中使用 main.py 中的变量吗?

[英]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.

相关问题 如何使用其他 python 文件中的函数,我将其命名为 Features.py 我想在 main.py 的类中使用该函数 - How can i use a function from an other python file i named it as Features.py I want use the function in a class in main.py 如何将fixed_seed从main.py模块转移到其他模块? - How can I transfer a fixed_seed from main.py module to other modules? 如何将数据从 main.qml 传递到 main.py,然后将其发送到其他 qml 文件 - How do you pass data from main.qml to main.py then send it to other qml files 在这些文件上2个python文件和1个main.py传递变量 - 2 python files and 1 main.py passing variables betwoon those files 如何从events.py更改main.py的int和boolean - How can I change int and boolean at main.py from events.py 如何将 cog 导入我的 main.py 文件? - How can I import a cog into my main.py file? 单元测试main.py与其他模块 - unit test main.py with other modules 如何在kivy中使用在另一个.py模块(不是main.py)中定义的变量? - How to use variables in kivy, that are defined in another .py module (not main.py)? fastapi - 从 main.py 导入配置 - fastapi - import config from main.py 如何使 python 下载文件到 main.py 的当前目录? - How can I make python download file to current directory of main.py?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM