新代码是 -
game.py
from globalized import *
"""need to fix events file"""
game = True
while game is True:
print(location[1])
events1()
for (i, t) in enumerate(transitions[location]):
print(i + 1, t[0])
choice = int(input("Enter location: "))
location = transitions[location][choice - 1]
来自globalized.py的
from events import *
start = ('start', 'the starting room')
common_room = ('common room', 'a room for socializing')
storage_bay = ('storage bay', 'a room used for storage')
cross_way = ('cross way', 'A way that parts into multiple rooms')
engine1 = ('engine room 1', 'A room with an engine for powering the ship')
engine2 = ('engine room 2', 'A room with an engine for powering the ship')
bridge = ('The bridge', 'a walk way that tran sends into a mysterious room')
dead_room = ('Mysterious room', 'this room is filled with dead')
elevator = ('elevator', 'a lift that takes you to different places')
sick_bay = ('sick bay', 'A room for storing sick')
storage_bay1 = ('storage bay', 'a room used for storage')
gallery = ('Gallery', 'a room')
bed_room = ('bed room', 'a room used for sleeping')
bridge_2 = ('the bridge', 'used for moving to new areas')
labroatory = ('lab', 'Used to make science')
captain_q_1 = ('captains quarters east', 'where the captain sleeps')
captain_q_2 = ('Captains quarters west', 'where the captain eats')
bath_room = ('bathroom', ' a place for other stuff')
pick_up = ('Pick-up bay', 'a bay for getting goods')
location = start
transitions = {
start: (common_room,),
common_room: (start, storage_bay, cross_way),
storage_bay: common_room,
cross_way: (common_room, engine2, engine1),
engine1: (cross_way, bridge),
engine2: (cross_way, sick_bay),
bridge: (engine1, elevator, dead_room),
dead_room: (bridge,),
sick_bay: (engine2, storage_bay1, elevator),
storage_bay1: (sick_bay,),
elevator: (bridge, sick_bay, gallery),
gallery: (elevator, bed_room, bridge_2),
bed_room: (gallery,),
bridge_2: (gallery, labroatory, captain_q_1),
labroatory: (bridge,),
captain_q_1: (bridge, captain_q_2, bath_room),
bath_room: (captain_q_1,),
captain_q_2: (pick_up, captain_q_1),
pick_up: (captain_q_2,),
}
来自events.py的
from random import randint
from globalized import *
def events1():
if location == engine1:
check = randint(1, 10)
if check > 5:
print("An arrow hits you")
else:
pass
elif location == engine2:
check = randint(1, 10)
if check > 5:
print("An enemy lunges at you")
else:
pass
else:
pass
这是运行此
的错误Traceback (most recent call last):
File "C:/Users/Ayden/PycharmProjects/untitled10/game.py", line 1, in <module>
from globalized import *
File "C:\Users\Ayden\PycharmProjects\untitled10\globalized.py", line 1, in <module>
from events import *
File "C:\Users\Ayden\PycharmProjects\untitled10\events.py", line 4, in <module>
location = start
NameError: name 'start' is not defined
所以继承人解释我想做什么...... 我在game.py文件中获取了几乎所有代码并将其带到全局化文件中,然后仅从事件中获取导入而不创建循环。但是,无论我在哪里放置location = start代码,我都会收到错误,因为它没有定义。我理解为什么好像我把它放在地方的定义之前它没有什么可以设置相等,如果我把它放在事件已经没有房间位置所以它再次出错。我该如何解决这个问题?
答案 0 :(得分:1)
您要导入错误的模块,您应该在游戏模块中导入事件,而不是在事件模块中导入游戏。
换句话说,将此代码添加到游戏文件的开头:
import events
并从事件文件的开头删除它:
from game import *
一旦你这样做,你会遇到更多问题,你应该学习在函数之间传递变量。
编辑:
现在的问题是你在游戏中导入事件,同时在事件中导入游戏。这是在创造一个循环。
我相信你应该从游戏导入中删除&#34;&#34;而是将所需的所有变量传递给events函数。您可以将所有位置变量放在字典中并传递整个字典。
您可以像这样传递您需要的变量和字典:
在游戏中:改变&#34; events.events1()&#34; to&#34; events.events1(location,dictionary_name)&#34;
在事件中:改变&#34; def events1():&#34; to&#34; def events1(location,dictionary_name):&#34;
答案 1 :(得分:0)
问题是循环依赖,正如@Thomas指出: 游戏取决于事件; 事件取决于游戏。
您需要重构此结构。我建议您将全局项目提升到游戏和事件中包含的其他文件,例如文件 game_global.py 。这会使您的两个代码文件如下所示。
game.py:
from game_global import *
import events
while game is True:
print(location[1])
events.events1()
for (i, t) in enumerate(transitions[location]):
print(i + 1, t[0])
choice = int(input("Enter location: "))
location = transitions[location][choice - 1]
events.py:
from random import randint
from game_global import *
def events1():
if location == engine1:
check = randint(1, 10)
if check > 5:
print("An arrow hits you")
else:
pass
elif location == engine2:
check = randint(1, 10)
if check > 5:
print("An enemy lunges at you")
else:
pass
else:
pass
代码在此状态下运行。