简体   繁体   English

用户做出错误选择后,如何使我的游戏链接重新回到开始

[英]How do i make my game link back to the start once user has made the wrong choice

im currently creating a text based adventure game, and i need to get the game to go back to the beginning once the user has died or chosen the wrong answer. 我目前正在创建一个基于文本的冒险游戏,一旦用户死亡或选择了错误的答案,我就需要让游戏回到开始。 i need it to ask the user if they wish to play again and then loop back to the beginning of the game. 我需要它询问用户是否希望再次玩,然后循环回到游戏开始。 anyone know how to do this, the help would be greatly appreciated. 任何人都知道如何做到这一点,将不胜感激。

this is the code - 这是代码-

#Python Text Adventure Game
#Created By Callum Fawcett
#Created On 20/07/19
#Version 0.1


# Setup
yes_no = ["yes", "no"]
directions = ["left", "right", "forward", "backward"]

# Introduction
name = input("What is your name, adventurer?\n")
print("Greetings, " + name + ". Let us go on a quest!")
print("You find yourself on the edge of a dark Jungle.")
print("Can you find your way through? and find the Hidden treasure at the end?\n")

# Start of game
response = ""
while response not in yes_no:
    response = input("Would you like to step into the Jungle?\nyes/no\n")
    if response == "yes":
        print("You head into the Jungle. You hear crows cawwing in the distance.\n")
    elif response == "no":
        print("You are not ready for this quest. Goodbye, " + name + ".")
        quit()
    else: 
        print("I didn't understand that.\n")

# Second part of game
response = ""
while response not in directions:
    print("To your left, you see a Tiger.")
    print("To your right, there is more Jungle.")
    print("There is a rock wall directly in front of you.")
    print("Behind you is the Jungle exit.\n")
    response = input("What direction do you move?\nleft/right/forward/backward\n")
    if response == "left":
        print("The Tiger eats you. Farewell, " + name + ".")
        quit()
    elif response == "right":
        print("You head deeper into the Jungle.\n")
    elif response == "forward":
        print("You cannot scale the wall.\n")
        response = "" 
    elif response == "backward":
        print("You leave the Jungle. Goodbye, " + name + ".")
        quit()
    else:
        print("I didn't understand that.\n")

# Third Part of game
response = ""
while response not in directions:
    print("To your left, there is a waterfall.")
    print("To your right, you see a run down house.")
    print("Directly in front of you is a dark staircase.")
    print("Behind you is the exit.\n")
    response = input("What direction do you wish to take?\nleft/right/forward/backward\n")
    if response == "left":
        print("You look off the edge of the waterfall, taking the time to think about your choices.\n")
        response = ""
    elif response == "right":
        print("The house is a trap, the doors and windows all shut behind you and youre trapped, Farewell, " + name + ".")
        quit()
    elif response == "forward":
        print("You head down the strairs\n")
    elif response == "backward":
        print("You leave the Jungle. Goodbye, " + name + ".")
        quit()
    else:
        print("I didn't understand that.\n")

# Fourth part of game
response = ""
while response not in directions:
    print("You're now at the bottom of the staircase, it has a come to a fork")
    print("To the right is a well lit path with a sign that reads 'Death to all who continue'.")
    print("To the left is a dark and gloomy path with a sign that reads 'free passage to all those who continue'. ")
    print("Directly in front of you is a a 50ft drop.")
    print("Behind you is the exit.\n")
    response = input("What direction do you wish to take?\nleft/right/forward/backward\n")
    if response == "left":
        print("You are attacked by forrst deamons. Goodebye," + name + ".")
        quit()
    elif response == "right":
        print("You continue through the forrest.\n")
    elif response == "forward":
        print("You cant go down there youll die.\n")
        response = ""
    elif response == "backward":
        print("You leave the Jungle. Goodebye" + name + ".")
        quit()
    else:
        print("I Didn't understand that.\n")

#Fifth part of game
response = ""
while response not in directions:
    print("You have continued through the dark forrest, and found yourself at a cross road.")
    print("To your left is a beaten path with a man lying on the ground.")
    print("To your right is a door.")
    print("Straight ahead is a dark path that is over grown and full of brambles and thorns.")
    print("behind you is the way you came and the way out.\n")
    response = input("What direction do you wish to take?\nleft/right/forward/backward\n")
    if response == "left":
        print("You walk to help the man, but it was a trap and you are attacked and killed by savages. Goodbye, " + name + ".\n")
        quit()
    elif response == "right":
        print("You go through the door, and it takes you to a mystical realm that you do not know, and now youre traped there. Goodbye, " + name + " Have fun.\n")
        quit()
    elif response == "forward":
        print("You continue through the dark Jungle, getting deeper and deeper. Lets hope you're not lost ;)\n")
    elif response == "backward":
        print("You Leave the Jungle. Goodbye" + name + ".")
        quit()
    else:
        print("I Didn't understand that")

#sixth part of the game.
response = ""
while response not in directions:
    print("You have made it through the thorns and brambles, and have come to a T in the path.")
    print("To your left is a bridge that doesnt look very stable and could break with the slightest bit of weight on it.")
    print("To your right is a dark path with a metal bridge that looks sturdy and safe.")
    print("Directly in front of you is a wall.")
    print("Behind you is the way you came and the way out.\n")
    response = input("What direction do you wish to take?\nleft/right/forward/backward\n")
    if response == "left":
        print("The bridge was actually very safe and you make your way over it, and continue on your quest\n")
    elif response == "right":
        print("The bridge wasnt stable and you have fallen to your death. Goodbye" + name + ".\n")
        quit()
    elif response == "forward":
        print("You cannot scale the wall\n")
        response = ""
    elif response == "backward":
        print("You leave the Jungle. Goodbye" + name + ".")
        quit()
    else:
        print("I Didn't Understand That")

#Final Part of The Game
response = ""
while response not in directions:
    print("You have come to the jungle temple, you make your way in through the door. Once inside you see three doors.")
    print("The door on the left has a weird symbol above it, and what looks like blood coming out from under the door.")
    print("The door on the right has no symbol above it, but does have two large lion statues by the door.")
    print("The door straight in front of you has a crown symbol above it, but has two skelitons sat outside it.")
    print("behind you is the way you came in and the way out.\n")
    response = input("What direction do you wish to take?\nleft/right/forward/backward\n")
    if response == "left":
        print("You open the door and something drags you inside, for you to never be seen again. Goodbye" + name + ".\n")
        quit()
    elif response == "right":
        print("You open the door and go inside, only to find 10 large lions who have not been fed in a very long time, they eat you. Goodbye" + name + ".\n")
        quit()
    elif response == "forward":
        print("You open the door and walk down the strairs infront of you, as you get lower and lower you start to see all the gold and treasure that has been left.")
        print("CONGRATULATIONS " + name + ", you found the Hidden jungle treasure, along with an exit back to the world.")
    elif response == "backward":
        print("You leave the temple and there are re risen soldiers ready to kill you. you die, Goodbye " + name + ".\n")
        quit()
    else:
        print("I Didn't understant that")

put all your code inside a while loop and put continue to jump to the beginning of the loop 将所有代码放入while循环中,并continue跳转到循环的开始

while True:
   ...
   ...
   if response == "some wrong answer": continue
   ...
   ...

A possible solution: move all of your current code to a function, let's call it "gameplay", and encapsulate it in a loop in the main function 一种可能的解决方案:将所有当前代码移至一个函数,我们将其称为“游戏性”,并将其封装在main函数的循环中

def gameplay():

    # Setup
    yes_no = ["yes", "no"]
    ....
    ....
    ....

if __name__ == "__main__":
    while True:
        response = input("start a new game?y/n")
        if response != "y":
            break    
        print("Starting a new game")
        gameplay()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一旦我的代码已经执行,如何让我的代码循环回输入? (角色扮演游戏) - How do I get my code to loop back to an input once it has been executed already? (RPG Game) Python 新手,我的代码有什么问题? 如何让游戏存储金币值,并开始新的迭代直到用户退出? - New to Python, what's wrong with my code? How do I make the game store the gold value, and start a new iteration until user quits? 我如何让我的代码在用户通过 valueerror 异常 go 已经在的同一点开始? - how do i make my code start at the same point the user was already on once they go through a valueerror exception? 如何让我的 python 游戏加载开始屏幕? - How do I make my start screen for my python game to load? pygame,如何使我的游戏中的分数在循环内只添加一次? - pygame, How do I make the score in my game add only once while inside the loop? 首次在livewires模块中编程,如何为可执行游戏创建起始页? - First time programming in livewires module, How do I make a start page for my executable game? 我试图在游戏中进行日光循环,但是一旦转为夜晚,就不会回到白天 - I tried to make a daylight cycle in my game, but once it turns to night it will not go back to day 我做了一个主菜单,但不知道如何让游戏开始 - I made a main menu but don't know how to make the game start 如何让这个开始按钮加载我的游戏? - How Can I Make This Start Button Load My Game? 我如何为开始游戏按钮制作 Function 以禁用开始屏幕并加载我的主游戏? - How Could I Make A Function For The Start Game Button To Disable The Start Screen And Load My Main Game?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM