使用.txt文件后,无法使用.txt文件。
我在使用从 .txt 文件读取的文件路径时遇到问题。我创建了一个 .txt 文件,其中包含用于我正在创建的游戏的不同级别的各种信息。
如果我从新创建一个 .txt 文件并将其直接保存到我的游戏根文件夹中,它将可以工作,但如果我编辑 .txt 文件(在 .txt 文件中保持信息的顺序相同),则会收到错误:
FileNotFoundError:没有这样的文件或目录。
发生错误后,我必须删除 .txt 文件并从头开始创建一个新文件。我想知道我是否做错了什么,或者我是否错过了一些 Python 魔法,这些魔法使文件扩展名在从 .txt 文件读取时可用。
下面的代码位于我的 main.py 文件中,用于访问存储在 .txt 文件中的关卡数据并将其放置在 level_data 数组中。我知道 Game_Data_Level_1.txt 文件正在被打开、读取,并且数据正在被放入数组中,因为我可以 print() 数组中保存的各个数据位。
pygame.init()
running = True
size = [Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
level_data = []
my_file = open("Game_Data_Level_1.txt","r") #opens data file
level_data = my_file.readlines()
my_file.close()
game = Game(screen, level_data)
clock = pygame.time.Clock()
然后,level_data 在创建时传递给游戏类
class game():
def __init__(self, screen, level_data):
#Set up game initialisation code here
self.surf = screen
self.bg_img = pg.image.load(level_data[3]).convert()
#self.bg_img = pg.image.load('graphics/space1.bmp').convert()
self.bg_img = pg.transform.scale(self.bg_img, screen.get_size())
。 #self.bg_img = pg.image.load('graphics/space1.bmp').convert()
行是我以前的做法加载图像,但我想创建具有不同背景的不同级别,所有级别均从保存所有单独级别数据的文件中读取。
我已经搜索过答案,但没有看到有人遇到与此相同的问题。
我可以使用从 .txt 文件检索的文件路径并使用它来获取我想要的图像吗?
我是否必须将其转换为 str()
才能将其用作文件扩展名? (我已经尝试过这个,但它仍然不起作用。)
我对 Python 比较陌生,所以我的编码可能不符合标准,所以任何有帮助的建议总是非常感激。如果您需要查看更多代码或更多描述,请询问。
I have a problem using a file path that has been read from a .txt file. I have created a .txt file that will hold various information to be used for different levels to a game I am creating.
If I create a .txt file from new and save it directly into my game root folder it will work but if I edit the .txt file (Keeping the information in the same order in the .txt file) I get a error:
FileNotFoundError: No such file or directory.
After the error I have to delete the .txt file and create a new one from scratch. I am wondering if there is something I am doing wrong or have I missed some Python magic that makes the file extension usable when it has been read from the the .txt file.
The code below is in my main.py file and is used to access the level data stored in the .txt file and place it in the level_data array. I know the Game_Data_Level_1.txt file is being opened, read and the data is being put into the array as I can print() the various bits of data held in the array.
pygame.init()
running = True
size = [Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
level_data = []
my_file = open("Game_Data_Level_1.txt","r") #opens data file
level_data = my_file.readlines()
my_file.close()
game = Game(screen, level_data)
clock = pygame.time.Clock()
The level_data is then passed to the game class when it is created
class game():
def __init__(self, screen, level_data):
#Set up game initialisation code here
self.surf = screen
self.bg_img = pg.image.load(level_data[3]).convert()
#self.bg_img = pg.image.load('graphics/space1.bmp').convert()
self.bg_img = pg.transform.scale(self.bg_img, screen.get_size())
The line #self.bg_img = pg.image.load('graphics/space1.bmp').convert()
is how I used to load the image but I want to create different levels with different backgrounds all read from files that hold all the separate level data.
I have searched for answers but haven't seen anyone having the same problem as this.
Can I use a filepath retrieved from a .txt file and use that to get the image i want?
Do I have to cast it to a str()
before it can be used as a file extension? (I have tried this and it still doesn't work.)
I'm relatively new to Python so my coding is probably not up to standard so any suggestions that will be helpful is always greatly appreciated. If you need to see anymore code or anymore description then just ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论