【发布时间】:2017-11-05 14:43:58
【问题描述】:
我的文字游戏中有一个nameType()函数,用了两次:
- 创建一个统计类(或其他)以便程序可以保存它们,并且
- 游戏中用于更改某些变量的函数。
这是这篇文章所针对的当前代码(可能会发生变化):
def TitleScreen():
blankHuge()
anar()
print('The Game of Hierarchy and Anarchy\nBy Roach\nText Edition\n▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\nType "new" to start a new game.\nTo load a game, type "load".\nTo exit, type "exit".')
StartGame = input()
if StartGame == 'new':
print('► Crew Member: Hello, fine man. I can see you are on an adventure to the land of Salbury.\nBefore you disembark the boat, please enter your name.')
for filename in file:
os.unlink(filename)
nameType()
global PlayerIG
PlayerIG = Player(CharName)
print('► Crew Member: Thanks for travelling with us, and have a great time,',PlayerIG.name,'.')
saveGame()
MainMenu()
# more stuff that aren't related down here...
这是nameType() 函数:
def nameType():
print('▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬')
CharName = input()
if CharName != '':
if len(CharName) > 20:
print('► The name is too long!')
nameType()
else:
print('► Are you sure this is what you want as a name?\nIf so, type "yes".\nOtherwise, type "no".')
confirmName = input()
if confirmName == 'yes': # If this is true, then jump to next codeline from where the nameType() func was placed...
pause() # Doesn't work
if confirmName == 'no':
nameType()
else:
print('► Invalid Input!')
nameType()
这是应该从原来的位置继续代码的函数,但它不起作用:
def pause():
pause = input('► Press Enter to continue.')
if pause != '':
pause()
有什么方法可以继续我的代码吗?
示例(很可能不起作用):
def TitleScreen():
...
nameType() # Someway, jump to next line after code in nameType() has been executed...
global PlayerIG
PlayerIG = Player(CharName)
...
saveGame()
MainMenu()
def management():
...
opt = input()
if opt = 'name':
nameType() # Someway, jump to next line after code in nameType() has been executed...
PlayerIG.nameWait = 12
PlayerIG.heat = 0
...
注意:我知道这将如何工作,但我不确定它是否会工作。它是:
# outside of nameType()
if confirmName == 'yes':
...
【问题讨论】:
-
您需要在某处存储游戏状态。您将从该商店读取该状态并以该状态开始游戏。
-
你能把你想要/需要做的事情弄清楚吗?
-
@erip 我已经存储了游戏状态。另外,这不是我的问题。我需要做的是找到一种方法,从放置
nameType()函数的位置跳转到下一行代码。 -
我没有回答您的问题,因为您正在尝试解决XY problem。如果你有游戏状态,你可以将它传递给适当的类来处理游戏中的最后一个检查点。
标签: python function class variables