通常は失敗するので、実際のゲームから自分のワールド生成を分割しようとしました。しかし、何らかの理由で、ファイルが空である/そこから得られた変数が空であると主張し続け、後で見ると、実際のプログラムファイルがすべての情報を含むテキストファイルを空にしたり、そうでない場合があります。コードは次のとおりです。
以下は、メイン コードでのファイル処理のみを抜粋したものです。
world_file = open("C:\Users\Ben\Documents\Python Files\PlatformerGame Files\World.txt", "r")
world_file_contents = world_file.read()
world_file.close()
world_file_contents = world_file_contents.split("\n")
WORLD = []
for data in world_file_contents:
usable_data = data.split(":")
WORLD.append(Tile(usable_data[0],usable_data[1]))
そしてタイルクラス:
class Tile():
def __init__(self,location,surface):
self.location = location
self.surface = surface
そしてエラー:
Traceback (most recent call last):
File "C:\Users\Ben\Documents\Python Files\PlatformerGame", line 89, in <module>
Game.__main__()
File "C:\Users\Ben\Documents\Python Files\PlatformerGame", line 42, in __main__
WORLD.append(Tile(usable_data[0],usable_data[1]))
IndexError: list index out of range
明らかな場合は申し訳ありません。また、私はpygameを使用しています。