Python 我需要一些帮助我的文字游戏

Python 我需要一些帮助我的文字游戏,python,Python,在我的基于文本的游戏中,如果没有systemexit.exit(),有没有办法让我的代码在“死亡”后重复 我尝试过搜索循环,但什么都没有,我看过其他在线游戏,我也搜索过这个网站,寻找任何可以帮助我的东西 我希望它只是说你死了,然后从一开始就开始说,但系统退出并不能很好地做到这一点。使用循环,准备游戏状态字典并调用游戏功能。 当你死后,退出游戏,请求继续并创建一个“新”游戏状态: def game(starter): # do stuff lr = input(f"Hello

在我的基于文本的游戏中,如果没有
systemexit.exit()
,有没有办法让我的代码在“死亡”后重复


我尝试过搜索循环,但什么都没有,我看过其他在线游戏,我也搜索过这个网站,寻找任何可以帮助我的东西




我希望它只是说你死了,然后从一开始就开始说,但系统退出并不能很好地做到这一点。

使用循环,准备游戏状态字典并调用游戏功能。 当你死后,退出游戏,请求继续并创建一个“新”游戏状态:

def game(starter):
    # do stuff
    lr = input(f"Hello {starter['name']} - nothing to see but go Left or Right? [L,R]: ").lower()
    if lr == "left":
        return  # doing anything is futile, you die anyhow 
    else:
        return # doing anything is futile, you die anyhow 

def main():
        state =  {"name": "someone"} 
        while True:
            game(state)
            if not input("You are dead - Play again? [y,n]").lower() == "y":
                break
            # create new games state dict (just altering the name here)
            state["name"] += "_again"
        print("bye")

main()
输出:

Hello someone - nothing to see but go Left or Right? [L,R]: l
You are dead - Play again? [y,n] y
Hello someone_again - nothing to see but go Left or Right? [L,R]: r
You are dead - Play again? [y,n] y
Hello someone_again_again - nothing to see but go Left or Right? [L,R]: nothing
You are dead - Play again? [y,n] n

bye

通常这样做的方法是在
while
循环中运行游戏代码,因此当玩家死亡时,您可以给他们重新启动或退出的选项。
Hello someone - nothing to see but go Left or Right? [L,R]: l
You are dead - Play again? [y,n] y
Hello someone_again - nothing to see but go Left or Right? [L,R]: r
You are dead - Play again? [y,n] y
Hello someone_again_again - nothing to see but go Left or Right? [L,R]: nothing
You are dead - Play again? [y,n] n

bye