我刚刚开始学习Python,并正在创建一个简单的文字冒险游戏。在游戏的一部分中,您可以死亡。当你死的时候,我就死定了= True。我还定义了一个名为dead()的函数,并使其仅在dead == True时才运行。问题在于没有调用dead()函数。
无需使用if语句即可调用dead()函数。我也使全球死亡。
我将dead设为True的地方:(在while True循环内,在另一个函数内)
elif action == 'Suicide':
dead = True
del actions[0:3]
print('\n' + name + ', not being able to take the stress, decided to end it all.')
time.sleep(2)
print('-Game Over-')
break
if语句: 如果死==真: dead()
预期结果:在dead = True之后,并且该循环被'break'破坏,我希望由于dead = True而使它调用dead函数。
实际结果:无错误消息,只是跳过if语句。 (我知道这是因为我在if语句后放置了一个空的input())
编辑: 无效功能:
def dead(): # Restart function, when dead is True
while True:
try:
print('\nWould you like to restart the game at your last checkpoint? (Y/N)')
restartAns = input()
if restartAns in ['Y','y']:
print('Restarting at last checkpoint..')
time.sleep(2)
if checkpoint == 0:
gameStart()
elif restartAns in ['N','n']:
print('Game shutting down..')
time.sleep(3)
quit()
except:
print('An error has occurred. Please try again:')
continue
else:
print('Please enter in either Y, or N:')
continue
死者周围的代码= True:
def gameStart(): # Beginning of Game
global checkpoint
global dead
checkpoint = 0
actions = ['Explore Forest', 'Suicide', 'Continue on road']
while True:
try:
print('\nOkay, ' + name + '.' + ' What would you like to do? ' + str(actions))
action = input()
if action not in actions:
print('Please choose one of the options: ' + str(actions))
continue
elif action == 'Explore Forest':
del actions[0:3]
print('\nYou wander into the forest and find an old abandoned mansion.')
time.sleep(2)
print('You walk up to the gates to find that it is closed tight.')
time.sleep(2)
break
elif action == 'Suicide':
dead = True
del actions[0:3]
print('\n' + name + ', not being able to take the stress, decided to end it all.')
time.sleep(2)
print('-Game Over-')
break
elif action == 'Continue on road':
actions.remove('Continue on road')
print('\nYou continue on the road.')
time.sleep(2)
print('After some walking, you realize that going straight will\nbring you nowhere, so you walk back to your car.')
time.sleep(4)
continue
except:
continue